Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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');
});
like image 502
MacD Avatar asked Nov 08 '13 18:11

MacD


People also ask

How do I change colors in Highcharts?

To change the background color of the chart you have to set in the chart. backgroundColor property. Bar colors in the series color property. Check the example with your chart I posted below.

What is plotOptions in Highcharts?

The plotOptions is a wrapper object for config objects for each series type. The config objects for each series can also be overridden for each series item as given in the series array. Configuration options for the series are given in three levels. Options for all series in a chart are given in the plotOptions.

Is Highcharts responsive?

Since Highcharts 5.0 you can create responsive charts much the same way you work with responsive web pages. A top-level option, responsive, exists in the configuration. One of the most handy options is chart.

Does Highcharts use jQuery?

Highcharts is a jQuery plugin that provides a simple interface for creating great looking charts.


2 Answers

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

};
like image 94
Ethan Wu Avatar answered Sep 18 '22 12:09

Ethan Wu


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
 };
like image 30
AzadMemon Avatar answered Sep 22 '22 12:09

AzadMemon