Description


This is an example of Javascript Chart annotations and conditional labels and colors. You can see how to set the type, the values, the color and the offset of the annotation. You can also see how to change the color and the border of the labels above a threshold value.

jQuery Chart: Chart Annotations

JavaScript Chart annotations example.

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, jqxdraw.js, jqxchart.core.js, jqxchart.annotations.js, jqxchart.api.js, jqxdata.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 () {
    // prepare chart data as an array            
    var source =
    {
        datatype: "csv",
        datafields: [
            { name: 'Country' },
            { name: 'GDP' },
            { name: 'DebtPercent' },
            { name: 'Debt' }
        ],
        url: '../sampledata/gdp_dept_2010.txt'
    };

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

    fnLabelsClass = function (value, itemIndex, serie, group) {
        if (value > 100)
            return 'redLabel';

        return 'greenLabel';
    },
    
    fnLabelsBorderColor = function (value, itemIndex, serie, group) {
        if (value > 100)
            return '#FF0000';

        return '#89A54E';
    }

    
    fnFormatLabel = function (value, itemIndex, serie, group) {
        return value;
    }

    // prepare jqxChart settings
    var settings = {
        title: "Country economic comparison",
        description: "Per capita GDP and Debt in 2010",
        showLegend: true,
        enableAnimations: true,
        padding: { left: 5, top: 5, right: 5, bottom: 5 },
        titlePadding: { left: 0, top: 0, right: 0, bottom: 10 },
        source: dataAdapter,
        xAxis:
        {
            dataField: 'Country',
            flip: false,
            gridLines: {visible: false}
        },
        colorScheme: 'scheme01',
        seriesGroups:
            [
                {
                    type: 'column',
                    columnsGapPercent: 50,
                    toolTipFormatSettings: { sufix: ' USD', thousandsSeparator: ','},
                    valueAxis:
                    {
                        title: {
                            text: 'GDP & Debt per Capita (USD)<br>'
                        },
                        gridLines: {visible: false}                                
                    },
                    series: 
                    [
                        { dataField: 'GDP', displayText: 'GDP per Capita' },
                        { dataField: 'Debt', displayText: 'Debt per Capita'}
                    ]
                },
                {
                    type: 'line',
                    valueAxis:
                    {
                        unitInterval: 10,
                        visible: true,
                        position: 'right',
                        title: {text: 'Debt (% of GDP)'},
                        gridLines: {visible: false},
                        labels: {
                            horizontalAlignment: 'left',
                            formatSettings: { sufix: '%' }
                        }
                    },
                    series: 
                    [
                        {
                            linesUnselectMode: 'click',
                            dataField: 'DebtPercent',
                            displayText: 'Debt (% of GDP)',
                            labels:
                            {
                                visible: true,
                                'class': fnLabelsClass,
                                backgroundColor: 'white',
                                padding: {left: 5, right: 5, top: 1, bottom: 1},
                                borderColor: fnLabelsBorderColor,
                                backgroundOpacity: 0.7,
                                borderOpacity: 0.7
                            }
                        }
                    ],
                    bands: [
                        {
                            minValue: 90, maxValue: 90, fillColor: 'red', lineWidth: 2, dashStyle: '2,2'
                        }
                    ],

                    annotations: [
                        {
                            type: 'rect',
                            yValue: 90,
                            xValue: 6,
                            offset: { x: -45, y: -25},
                            width: 90,
                            height: 20,                                    
                            fillColor: '#EFEFEF',
                            lineColor: 'red',
                            text: {
                                value: 'Debt threshold',
                                offset: {
                                    x: 2,
                                    y: 2
                                },
                                'class': 'redLabel',
                               angle: 0
                            }
                        }
                    ],

                }

            ]
    };

    // setup the chart
    $('#chartContainer').jqxChart(settings);
});

body.html

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

Learn more

More jQuery Chart demos