简体   繁体   中英

Change Highcharts Series color

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');
});

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
 };

在我看来,你不是在循环数据数组和/或你只有一组数据在data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM