最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change Highcharts Series color - Stack Overflow

programmeradmin5浏览0评论

EDIT: So now I have a chart with all my data pushed off to the right, BUT I have labels in different colors for the sets I want to show but no data?? Updated my code

Original post: I have a working highchart here .html . The problem is when I try and change the color of an individual data set, they all change. How could I change these settings given my code? Thanks in advance!

code:

    var options1 = {
    chart: {
        renderTo: 'container1',
        type: 'area'

        },
    xAxis: {
                type: 'datetime'

    },

    series: [{
        name: 'Swell Period',
        color: '#0066FF',
        data: 'newSeriesData',
    },
    {   name: ' Maximum Breaking Wave Height',
        color: '#ffffff',
        data: 'newSeriesData',
    },
    {   name: 'Swell Height',
        color: '#123456',
        data: 'newSeriesData',
    }],
};

var drawChart = function(data, name, color) {



 var newSeriesData = {
    name: name,
    data: data
 };

    // Add the new data to the series array
    options1.series.push(newSeriesData);

    // If you want to remove old series data, you can do that here too

    // Render the chart
    var chart = new Highcharts.Chart(options1);



};
$.getJSON('decode.php', function(data){
    drawChart(data, 'Swell Height');
}); 

$.getJSON('decode2.php', function(data){
    drawChart(data, ' Maximum Breaking Wave Height');
});

$.getJSON('decode3.php', function(data){
    drawChart(data, 'Swell Period');
});

EDIT: So now I have a chart with all my data pushed off to the right, BUT I have labels in different colors for the sets I want to show but no data?? Updated my code

Original post: I have a working highchart here http://opensourcesurf.com/chart.html . The problem is when I try and change the color of an individual data set, they all change. How could I change these settings given my code? Thanks in advance!

code:

    var options1 = {
    chart: {
        renderTo: 'container1',
        type: 'area'

        },
    xAxis: {
                type: 'datetime'

    },

    series: [{
        name: 'Swell Period',
        color: '#0066FF',
        data: 'newSeriesData',
    },
    {   name: ' Maximum Breaking Wave Height',
        color: '#ffffff',
        data: 'newSeriesData',
    },
    {   name: 'Swell Height',
        color: '#123456',
        data: 'newSeriesData',
    }],
};

var drawChart = function(data, name, color) {



 var newSeriesData = {
    name: name,
    data: data
 };

    // Add the new data to the series array
    options1.series.push(newSeriesData);

    // If you want to remove old series data, you can do that here too

    // Render the chart
    var chart = new Highcharts.Chart(options1);



};
$.getJSON('decode.php', function(data){
    drawChart(data, 'Swell Height');
}); 

$.getJSON('decode2.php', function(data){
    drawChart(data, ' Maximum Breaking Wave Height');
});

$.getJSON('decode3.php', function(data){
    drawChart(data, 'Swell Period');
});
Share Improve this question edited Nov 8, 2013 at 19:33 MacD asked Nov 8, 2013 at 18:12 MacDMacD 5863 gold badges9 silver badges28 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 12

Try this:

// 'series' is an array of objects with keys: 
//     - 'name' (string)
//     - 'data' (array)
//     - 'color' (HTML color code)
var newSeriesData = {
    name: name,
    data: data,
    color: color

};

The way to specify a color for a specific series is to define it when you're defining the series. For example:

series: [{
        name: 'John',
        color: '#0066FF',
        dashStyle: 'ShortDash',
        data: [
            [Date.UTC(2010, 0, 1), 29.9],
            [Date.UTC(2010, 2, 1), 71.5],
            [Date.UTC(2010, 3, 1), 106.4]
        ]
    },

So essentially when you're creating your series in your drawchart function, do a check for the name, and appropriately assign a color:

var color;
 if(name=="Swell Height"){
     color="#0066FF";
 }else if(name=="Maximum Breaking Wave Height"){
     color="#0066EE";
 }else if(name=="Swell Period"){
     color="#0066HH";
 }

 var newSeriesData = {
    name: name,
    data: data,
    color: color
 };

It looks to me like you are not looping through the array of data and/or you only have one set of data in data.

发布评论

评论列表(0)

  1. 暂无评论