mint
mint

Reputation: 3433

Highcharts Change Bar Color Based on Value

I'm using Highcharts and was wondering if it was possible to have the top 3 results in a bar chart to have a different color bar then the rest of the chart? I'm populating the chart from a CSV file.

Here is my javascript:

$(document).ready(function() {

        var options = {
            chart: {
                renderTo: 'container',
                defaultSeriesType: 'bar'
            },
            title: {
                text: 'Spiritual Gifts Results'
            },
            colors: [
                '#3BBEE3'
            ],
            xAxis: {
                categories: []
            },

            yAxis: {
                title: {
                    text: 'Service'
                }
            },
            series: []
        };

        var data = document.getElementById("<%= hdn_Data.ClientID %>");
        // Split the lines
        if (data.value != "") {
            var lines = data.value.split('\n');

            // Iterate over the lines and add categories or series
            $.each(lines, function(lineNo, line) {
                var items = line.split(',');
                // header line containes categories
                if (lineNo == 0) {
                    $.each(items, function(itemNo, item) {
                        if (itemNo > 0) options.xAxis.categories.push(item);
                    });
                }
                // the rest of the lines contain data with their name in the first position
                else {
                    var series = {
                        data: []
                    };
                    $.each(items, function(itemNo, item) {
                        if (itemNo == 0) {
                            series.name = item;
                        } else {
                            series.data.push(parseFloat(item));
                        }
                    });

                    options.series.push(series);

                }

            });

            // Create the chart
            var chart1 = new Highcharts.Chart(options);
        }
    });

Here is a sample CSV:

Categories,Administration,Evangelism,Mercy,Shepherding,Leadership,Wisdom,Teaching
Total Points,11,5,4,4,3,2,1

So in this example I'd like for 'Administration', 'Evangelism', and 'Mercy' to have a 'blue bar color' while the 'Shepherding', 'Leadership' etc. have a 'red bar color'.

Is this possible?

Here's fiddle

Upvotes: 12

Views: 34694

Answers (3)

AmirtharajCVijay
AmirtharajCVijay

Reputation: 1108

Here is a Example

data: [
  { y: 34.4, color: 'red'},   // this point is red
  21.8,                        // default blue
  {y: 20.1, color: '#aaff99'}, // this will be greenish
  20                           // default blue
]                             

Upvotes: 17

Jashwant
Jashwant

Reputation: 28995

Based on OP's comment, striked my previous answer.

As you are doing,

series.name = item; 

and

series.data.push(parseFloat(item));

Like wise, you can do,

series.color: '#f6f6f6'

(in your loop you can change color based your conditions )

You can do,

$(document).ready(function() {

    var options = {
        chart: {
            renderTo: 'container',
            defaultSeriesType: 'bar'
        },
        title: {
            text: 'Spiritual Gifts Results'
        },
        colors: [
            '#3BBEE3'
            ],
        xAxis: {
            categories: []
        },

        yAxis: {
            title: {
                text: 'Service'
            }
        },
        series: []
    };

    var data = document.getElementById("hdn_Data");
    // Split the lines
    if (data.value != "") {
        var lines = data.value.split('\n');

        // Iterate over the lines and add categories or series
        $.each(lines, function(lineNo, line) {
            var items = line.split(',');
            // header line containes categories
            if (lineNo == 0) {
                $.each(items, function(itemNo, item) {
                    if (itemNo > 0) options.xAxis.categories.push(item);
                });
            }
            // the rest of the lines contain data with their name in the first position
            else {
                var series = {
                    data: []
                };
                $.each(items, function(itemNo, item) {
                    var data = {};
                    if (itemNo == 0) {
                        series.name = item;
                    } else {
                        data.y = parseFloat(item);
                        if (itemNo <= 3) {
                            data.color = 'Gray';
                        }
                        else {
                            data.color = '#3BBEE3';
                        }
                        series.data.push(data);
                    }
                });
                options.series.push(series);
              }
            });

            // Create the chart
            var chart1 = new Highcharts.Chart(options);
        }
    });

Fiddle

Refer this, you can give data in 3 ways. I've used the third one.

Upvotes: 12

wergeld
wergeld

Reputation: 14442

Yes, In your push execution you should be able to set color for that point. I am not too familiar with push as we use a precompiled data object that we send. But inside of our .NET we set the color (if needed) on a particular point and then send the data object to the chart.

Upvotes: 2

Related Questions