Description


This is an example of Javascript Chart OHLC series. The Open High Low Close series is a type of chart used in finance to display the movements of the price over time.

jQuery Chart: OHLC Chart

JavaScript Chart OHLC series 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' },
            { name: 'NQOpen' },
            { name: 'NQHigh' },
            { name: 'NQLow' },
            { name: 'NQClose' },
            { name: 'NQVolume' },
            { name: 'NQAdjClose' }
            ],
        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 = serie == 0 ? dataItem.SPVolume : dataItem.NQVolume;

        return '<DIV style="text-align:left"><b>Date: ' +
                categoryValue.getDate() + '-' + months[categoryValue.getMonth()] + '-' + categoryValue.getFullYear() +
                '</b><br />Open price: $' + value.open +
                '</b><br />Close price: $' + value.close +
                '</b><br />Low price: $' + value.low +
                '</b><br />High price: $' + value.high +
                '</b><br />Daily volume: ' + volume
        '</DIV>';
    };

    // prepare jqxChart settings
    var settings = {
        title: "NASDAQ and S&P 500 - OHLC Example",
        description: "(June 2017 - November 2018)",
        enableAnimations: true,
        animationDuration: 1500,
        enableCrosshairs: true,
        padding: { left: 5, top: 5, right: 5, bottom: 5 },
        source: dataAdapter,
        colorScheme: 'scheme01',
        xAxis:
        {
            dataField: 'Date',
            labels:
            {
                formatFunction: function (value) {
                    return value.getDate() + '-' + months[value.getMonth()] + '<br>' + value.getFullYear().toString();
                }
            },
            type: 'date',
            valuesOnTicks: true,
            minValue: new Date(2017, 8, 20),
            maxValue: new Date(2017, 10, 1),
            rangeSelector: {
                // Uncomment the line below to render the selector in a separate container 
                renderTo: $('#selectorContainer'),
                // size: 120,
                serieType: 'area',
                padding: { left: 25, right: 10, top: 10, bottom: 10 },
                backgroundColor: 'white',
                dataField: 'SPClose',
                baseUnit: 'month',
                gridLines: { visible: false },
                labels:
                {
                    formatFunction: function (value) {
                        return months[value.getMonth()] + '\'' + value.getFullYear().toString().substring(2);
                    }
                }
            }
        },
        seriesGroups:
            [
                {
                    type: 'ohlc',
                    //skipOverlappingPoints: false,
                    toolTipFormatFunction: toolTipCustomFormatFn,
                    valueAxis:
                    {
                        description: 'S&P 500<br>'
                    },
                    series: [
                        {
                            dataFieldClose: 'SPClose',
                            displayTextClose: 'S&P Close price',
                            dataFieldOpen: 'SPOpen',
                            displayTextOpen: 'S&P Open price',
                            dataFieldHigh: 'SPHigh',
                            displayTextHigh: 'S&P High price',
                            dataFieldLow: 'SPLow',
                            displayTextLow: 'S&P Low price',
                            displayText: 'S&P 500',
                            lineWidth: 1
                        }
                    ]
                },
                {
                    type: 'line',
                    toolTipFormatFunction: toolTipCustomFormatFn,
                    valueAxis:
                    {
                        position: 'right',
                        title: { text: '<br>NASDAQ' },
                        gridLines: { visible: false }
                    },
                    series: [
                        {
                            dataField: 'NQClose',
                            dataFieldClose: 'NQClose',
                            displayTextClose: 'Nasdaq Close price',
                            dataFieldOpen: 'NQOpen',
                            displayTextOpen: 'Nasdaq Open price',
                            dataFieldHigh: 'NQHigh',
                            displayTextHigh: 'Nasdaq High price',
                            dataFieldLow: 'NQLow',
                            displayTextLow: 'Nasdaq Low price',
                            displayText: 'NASDAQ',
                            lineWidth: 1
                        }
                    ]
                }
            ]
    };

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

body.html

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

Learn more

More jQuery Chart demos