Highcharts Stock Demo

author(s): Torstein Hønsi

by Ramkumar Pillai

HTML

<div id="container" style="height: 400px; min-width: 600px"></div>

<script src="http://code.highcharts.com/stock/highstock.js"></script>
<script src="http://code.highcharts.com/stock/modules/exporting.js"></script>

Set compare: 
<button class="compare" data-compare="none">None</button>
<button class="compare" data-compare="value">Value</button>
<button class="compare" data-compare="percent">Percent</button>

JavaScript

$(function() {
    var seriesOptions = [],
        yAxisOptions = [],
        seriesCounter = 0,
        names = ['MSFT', 'AAPL', 'GOOG'],
        colors = Highcharts.getOptions().colors;

    $.each(names, function(i, name) {

        $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename='+ name.toLowerCase() +'-c.json&callback=?',    function(data) {

            seriesOptions[i] = {
                name: name,
                data: data
            };

            // As we're loading the data asynchronously, we don't know what order it will arrive. So
            // we keep a counter and create the chart when all the data is loaded.
            seriesCounter++;

            if (seriesCounter == names.length) {
                createChart();
            }
        });
    });



    // create the chart when all data is loaded
    function createChart() {

        $('#container').highcharts('StockChart', {

            rangeSelector: {
                selected: 4
            },

            yAxis: {
                labels: {
                    formatter: function() {
                        return (this.value > 0 ? '+' : '') + this.value;
                    }
                }
            },
            
            plotOptions: {
                series: {
                    compare: 'value'
                }
            },
            
            tooltip: {
                pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y} USD</b> ({point.change})<br/>',
                changeDecimals: 2,
                valueDecimals: 2
            },
            
            series: seriesOptions
        });
    }

    // buttons behaviour
    $('button.compare').click(function () {
        var chart = $('#container').highcharts(),
            compare = $(this).data().compare;
        chart.yAxis[0].setCompare(compare);

    });

});