Description


This is an example of JavaScript chart line series with conditional colors. The data of the chart is loaded from a csv file. The colorFunction is modified to return the desired color. jqxChart will call this function for each data point. Sequential points that have the same color will be grouped automatically in a line segment.

jQuery Chart: Line Series with conditional colors

JavaScript Chart line series with conditional colors.

A live JavaScript/jQuery example of jqxChart, the jQWidgets Chart component, with its complete source code below. It is one of 92 jqxChart demos for jQuery.

jqxChart API used in this demo

Source code

To run it in your own page, load jqx.base.css, a theme and the library files below from npm (npm install jqwidgets-scripts) or the jsDelivr CDN, then add the markup and the script. jQuery is not required: jqxcore.js provides $. Getting started with jqxChart shows the setup step by step.

The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxdata.js, jqxdraw.js, jqxchart.core.js.

It also loads helper scripts of this demo site, which are not part of the library: demos.js (getWidth() and other layout helpers).

script.js

$(document).ready(function () {
    var source =
    {
        datatype: "csv",
        datafields: [
            { name: 'Quarter' },
            { name: 'Change' }
        ],
        url: '../sampledata/us_gdp_2008-2013.csv'
    };

    var dataAdapter = new $.jqx.dataAdapter(
        source,
        {
            async: false,
            autoBind: true,
            loadError: function (xhr, status, error) {
                alert('Error loading "' + source.url + '" : ' + error);
            }
        }
    );

    settings = {
        title: "U.S. GDP Percentage Change",
        borderLineWidth: 1,
        showBorderLine: true,
        enableAnimations: true,
        description: '(source: Bureau of Economic Analysis)',
        showLegend: false,
        padding: { left: 5, top: 5, right: 10, bottom: 5 },
        titlePadding: { left: 0, top: 0, right: 0, bottom: 10 },
        source: dataAdapter,
        categoryAxis:
        {
            //description: 'Year and quarter',
            dataField: 'Quarter',
            unitInterval: 1,
            textRotationAngle: -75,
            formatFunction: function (value, itemIndex, serie, group) {
                return value;
            },
            valuesOnTicks: false
        },
        colorScheme: 'scheme05',
        seriesGroups:
        [
            {
                type: 'line',
                valueAxis:
                {
                    description: 'Percentage Change',
                    formatFunction: function (value) {
                        return value + '%';
                    }
                },
                series:
  [
                        {
                            dataField: 'Change',
                            displayText: 'Change',

                            // Modify this function to return desired colors.
                            // jqxChart will call the function for each data point.
                            // Sequential points that have the same color will be
                            // grouped automatically in a line segment
                            colorFunction: function (value, itemIndex, serie, group) {
                                return (value < 0) ? '#FF0000' : '#00FF00';
                            }
                        }
                    ]
            }

        ]
    };

    $("#chartContainer").jqxChart(settings);
});

body.html

<div id="chartContainer" style="width:800px; height:500px"></div>

Learn more

More jQuery Chart demos