Description


This is an example of Javascript chart range selector and zooming. The chart data is prepared from a csv file. You can see an example of a complex tool tip custom formatting with the toolTipCustomFormatFn function. You can also see how to limit the duration of the animation with the animationDuration setting. The code also demonstrates how to render the selector in a separate container.

jQuery Chart: Date/time x-axis range selection

JavaScript Chart Range Selector 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, jqxdata.js, jqxdraw.js, jqxchart.core.js, jqxchart.rangeselector.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 the data
    var source =
    {
        datatype: "csv",
        datafields: [
            { name: 'Date' },
            { name: 'Open' },
            { name: 'High' },
            { name: 'Low' },
            { name: 'Close' },
            { name: 'Volume' },
            { name: 'AdjClose' }
            ],
        url: '../sampledata/TSLA_stockprice.csv'
    };

    var dataAdapter = new $.jqx.dataAdapter(source, { async: false, autoBind: true, loadError: function (xhr, status, error) { alert('Error loading "' + source.url + '" : ' + error); } });
    var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

    var toolTipCustomFormatFn = function (value, itemIndex, serie, group, categoryValue, categoryAxis) {
        var dataItem = dataAdapter.records[itemIndex];
        return '<DIV style="text-align:left"><b>Date: ' +
                categoryValue.getDate() + '-' + months[categoryValue.getMonth()] + '-' + categoryValue.getFullYear() +
                '</b><br />Open price: $' + dataItem.Open +
                '</b><br />Close price: $' + dataItem.Close +
                '</b><br />Daily volume: ' + dataItem.Volume +
                 '</DIV>';
    };

    // prepare jqxChart settings
    var settings = {
        title: "Tesla Motors Stock Price",
        description: "(June 2017 - March 2018)",
        enableAnimations: true,
        animationDuration: 1500,
        enableCrosshairs: true,
        padding: { left: 5, top: 5, right: 30, bottom: 5 },
        titlePadding: { left: 30, top: 5, right: 0, bottom: 10 },
        source: dataAdapter,
        xAxis:
        {
            dataField: 'Date',
            minValue: new Date(2012, 0, 1),
            maxValue: new Date(2017, 11, 31),
            type: 'date',
            baseUnit: 'day',
            labels:
            {
                formatFunction: function (value) {
                    return value.getDate() + '-' + months[value.getMonth()] + '\'' + value.getFullYear().toString().substring(2);
                }
            },
            rangeSelector: {
                // Uncomment the line below to render the selector in a separate container 
                //renderTo: $('#selectorContainer'),
                size: 80,
                padding: { /*left: 0, right: 0,*/top: 0, bottom: 0 },
                minValue: new Date(2010, 5, 1),
                backgroundColor: 'white',
                dataField: 'Close',
                baseUnit: 'month',
                gridLines: { visible: false },
                serieType: 'area',
                labels: {
                    formatFunction: function (value) {
                        return months[value.getMonth()] + '\'' + value.getFullYear().toString().substring(2);
                    }
                }
            }
        },
        valueAxis:
        {
            title: { text: 'Price per share [USD]<br><br>' },
            labels: { horizontalAlignment: 'right' }
        },

        colorScheme: 'scheme01',
        seriesGroups:
            [
                {
                    type: 'line',
                    toolTipFormatFunction: toolTipCustomFormatFn,
                    series: [
                        { dataField: 'Close', displayText: 'Close Price', lineWidth: 1, lineWidthSelected: 1 }
                    ]
                }

            ]
    };

    $('#chartContainer').jqxChart(settings).
        on('rangeSelectionChanging', function (event) {
            var args = event.args;
            args.instance.description = args.minValue.getFullYear() + " - " + args.maxValue.getFullYear();
        });
});

body.html

<div>
    <div id="chartContainer" style="width:800px; height:500px;">
    </div>
    <!-- you can optionally render the selecor in this container -->
    <div id="selectorContainer" style="width:500px; height:100px;">
    </div>
</div>

Learn more

More jQuery Chart demos