Description


This is an example of JavaScript Chart Range Selector with Zooming.

jQuery Chart: Non-date x-axis range selection

JavaScript Chart Range Selector Example with zooming.

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) {
        return 'Index: ' + itemIndex + ", Value: " + value;
    };

    // prepare jqxChart settings
    var settings = {
        title: "Range selector with regular x-axis",
        description: "In this example the range selector works with (non-date) axis",
        enableAnimations: true,
        showLegend: false,
        animationDuration: 1500,
        enableCrosshairs: true,
        padding: { left: 5, top: 5, right: 20, bottom: 5 },
        colorScheme: 'scheme02',
        source: dataAdapter,
        xAxis:
            {
                minValue: 175,
                maxValue: 550,
                flip: false,
                valuesOnTicks: true,
                rangeSelector: {
                    serieType: 'area',
                    padding: { /*left: 0, right: 0,*/ top: 20, bottom: 0 },
                    // Uncomment the line below to render the selector in a separate container
                    //renderTo: $('#selectorContainer'),
                    backgroundColor: 'white',
                    size: 110,
                    gridLines: {visible: false},
                }
            },
        seriesGroups:
            [
                {
                    type: 'line',
                    toolTipFormatFunction: toolTipCustomFormatFn,
                    valueAxis:
                    {
                        flip: false,
                        title: {text: 'Value<br><br>'}
                    },
                    series: [
                        { dataField: 'Close', lineWidth: 1, lineWidthSelected: 1 }
                    ]
                }

            ]
    };

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

body.html

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

Learn more

More jQuery Chart demos