Load Json data from URL - Javascript highcharts

Javascript examples for highcharts:Chart Json Data

Description

Load Json data from URL

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
      <script type="text/javascript">
    $(window).load(function(){/*ww  w.  jav  a2 s  . co  m*/
$(function () {
    var seriesOptions = [],
        seriesCounter = 0,
        names = ['MSFT'],
        // create the chart when all data is loaded
        createChart = function () {
            $('#container').highcharts('StockChart', {
                rangeSelector: {
                    selected: 4
                },
                xAxis:{
                    minRange:30 * 24 * 3600 * 1000
                },
                yAxis: {
                    labels: {
                        formatter: function () {
                            return (this.value > 0 ? ' + ' : '') + this.value + '%';
                        }
                    },
                    plotLines: [{
                        value: 0,
                        width: 2,
                        color: 'silver'
                    }]
                },
                plotOptions: {
                    series: {
                        compare: 'percent'
                    }
                },
                tooltip: {
                    pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
                    valueDecimals: 2
                },
                series: seriesOptions
            });
        };
    $.each(names, function (i, name) {
        $.getJSON('https://www.highcharts.com/samples/data/jsonp.php?filename=' + name.toLowerCase() + '-c.json&callback=?',    function (data) {
            seriesOptions[i] = {
                name: name,
                data: [[1346371200000,12],[1348963200000,13],[1354233600000,4],[1377907200000,12],[1380499200000,13],[1385769600000,4],[1406764800000,1],[1409443200000,15],[1412035200000,20],[1414713600000,2],[1417305600000,4]]
            };
            // 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 += 1;
            if (seriesCounter === names.length) {
                createChart();
            }
        });
    });
});
    });

      </script> 
   </head> 
   <body> 
      <script src="https://code.highcharts.com/stock/highstock.js"></script> 
      <script src="https://code.highcharts.com/stock/modules/exporting.js"></script> 
      <div id="container" style="height: 400px; min-width: 310px"></div>  
   </body>
</html>

Related Tutorials