Description


This is an example of JavaScript Chart mouse events. You can see how the get the values when the mouseover and mouseout events occur.

jQuery Chart: Chart RangeSelector Events

JavaScript Chart mouse events 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: "tsv",
        datafields: [
            { name: 'Date' },
            { name: 'SPOpen' },
            { name: 'SPHigh' },
            { name: 'SPLow' },
            { name: 'SPClose' },
            { name: 'SPVolume' },
            { name: 'SPAdjClose' }
            ],
        url: '../sampledata/nasdaq_vs_sp500_detailed.txt'
    };

    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];
        var volume = dataItem.SPVolume;

        return '<DIV style="text-align:left"><b>Date: ' +
                categoryValue.getDate() + '-' + months[categoryValue.getMonth()] + '-' + categoryValue.getFullYear() +
                '</b><br />Index value: ' + value +
                '</b><br />Daily volume: ' + volume
        '</DIV>';
    };

    // prepare jqxChart settings
    var settings = {
        title: "S&P 500 Index value and daily volume",
        description: "(June 2017 - November 2018)",
        enableAnimations: true,
        animationDuration: 1500,
        enableCrosshairs: true,
        padding: { left: 20, top: 5, right: 20, bottom: 5 },
        colorScheme: 'scheme17',
        source: dataAdapter,
        xAxis:
            {
                dataField: 'Date',
                type: 'date',
                valuesOnTicks: true,
                minValue: new Date(2017, 1, 1),
                maxValue: new Date(2017, 10, 1),
                labels:
                {
                    formatFunction: function (value) {
                        return value.getDate() + '-' + months[value.getMonth()] + '<br>' + value.getFullYear().toString();
                    }
                },
                gridLines: {visible: false},
                rangeSelector: {
                    // Uncomment the line below to render the selector in a separate container 
                    //renderTo: $('#selectorContainer'),
                    size: 100,
                    padding: { top: 10, bottom: 0 },
                    backgroundColor: 'white',
                    dataField: 'SPClose',
                    baseUnit: 'month',
                    showGridLines: false,
                    formatFunction: function (value) {
                        return months[value.getMonth()] + '\'' + value.getFullYear().toString().substring(2);
                    }
                }
            },
        seriesGroups:
            [
                {
                    type: 'line',
                    linesUnselectMode: 'click',
                    toolTipFormatFunction: toolTipCustomFormatFn,
                    valueAxis:
                    {
                        title: {text: 'S&P 500<br>'},
                        gridLines: {visible: false},
                    },
                    series: [
                        {
                            dataField: 'SPClose',
                            displayText: 'S&P Index Value',
                            lineColor: '#307DD7',
                            lineWidth: 1
                        }
                    ]
                },
                {
                    type: 'area',
                    toolTipFormatFunction: toolTipCustomFormatFn,
                    linesUnselectMode: 'click',
                    valueAxis:
                    {
                        position: 'right',
                        title: {text: '<br>Daily Volume'},
                        gridLines: {visible: false},
                        labels:
                        {
                            formatFunction: function (value) {
                                return value / 1000000 + 'M';
                            }
                        }
                    },
                    series: [
                        {
                            dataField: 'SPVolume',
                            displayText: 'S&P Index Volume',
                            lineWidth: 1,
                            opacity: 0.2
                        }
                    ]
                }
            ]
    };

    function dumpEventInfo(event) {
        var args = event.args;

        if (event.type.indexOf('refresh') != -1) {
            if ($('#chartContainer').jqxChart('getInstance') != args.instance)
                return;
        }

        var text = $('#eventsTextArea').val();
        var date = new Date();
        var line = 'Time: ' + (date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()) + ', event: ' + event.type;
        if (args.elementIndex)
            line += ', element index: ' + args.elementIndex;

        if (args.elementValue) {
            line += ", value: " + args.elementValue;
        }

        if (event.type == 'toggle')
        {
            line += ", series group index: " + settings.seriesGroups.indexOf(args.seriesGroup);
            line += ", visible: " + args.state;
        }

        if (event.type.indexOf('rangeSelection') != -1) {
            line += ", minValue: " + args.minValue.getFullYear() + '-' + (args.minValue.getMonth() + 1) + '-' + args.minValue.getDate();
            line += ", maxValue: " + args.maxValue.getFullYear() + '-' + (args.maxValue.getMonth() + 1) + '-' + args.maxValue.getDate();
        }

        text = line + '\n' + text;
        $('#eventsTextArea').val(text);
    }

    $('#chartContainer').jqxChart(settings)
        .on('click mouseover mouseout toggle rangeSelectionChanging rangeSelectionChanged refreshBegin refreshEnd', function (event) {
            if (event.args)
                dumpEventInfo(event);
        });
});

body.html

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

Learn more

More jQuery Chart demos