HighCharts通过Json加载数据

Mar*_*ito 1 jquery json highcharts

我正在尝试从我的Json Data制作HighCharts图表.这是我的Json数据

[{"ReadData":"99","Time":"07\/09\/2015 00:00:07"},{"ReadData":"101","Time":"07\/09\/2015 00:01:07"},{"ReadData":"113","Time":"07\/09\/2015 00:02:07"},{"ReadData":"115","Time":"07\/09\/2015 00:03:07"},{"ReadData":"96","Time":"07\/09\/2015 00:04:07"},{"ReadData":"103","Time":"07\/09\/2015 00:05:07"}]
Run Code Online (Sandbox Code Playgroud)

我的问题是当图表没有加载时,我做错了什么?这是我的代码HTML.

<html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="http://code.highcharts.com/highcharts.js"></script>
    <script src="http://code.highcharts.com/modules/exporting.js"></script>
    <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
     <script>

        $(document).ready(function() {

    var options = {
        chart: {
            renderTo: 'container',
            type: 'spline'
        },
        series: [{}]
    };

    $.getJSON('data.json', function(data) {
        options.xAxis.categories = json[0]['Time'];
        options.series[0] = json[0]['ReadData'];
        var chart = new Highcharts.Chart(options);
    });

});
    </script>         
    </head>
</html>
Run Code Online (Sandbox Code Playgroud)

Paw*_*Fus 5

有几个问题,请参阅工作演示:http://jsfiddle.net/ux74929j/5/

让我解释:

  • 您的格式与Highcharts不兼容,并且您试图以错误的方式使用它,首先解析您的数据:

            var categories = [],
               points = [];
    
            $.each(JSON, function(i, el) { // Assuming that JSON is data from getJSON()
                categories.push(el.Time);
                points.push(parseFloat(el.ReadData)); // Another issue - data should be number, not string
            });
           options.xAxis.categories = categories;
           options.series[0].data = points;
           var chart = new Highcharts.Chart(options); 
    
    Run Code Online (Sandbox Code Playgroud)
  • 你没有xAxis选项,但你还是试图分配categories:

       var options = {
           chart: {
               renderTo: 'container',
               type: 'spline'
           },
           xAxis: {},
           series: [{}]
       };
    
    Run Code Online (Sandbox Code Playgroud)

总而言之,您的代码应如下所示:

       var options = {
           chart: {
               renderTo: 'container',
               type: 'spline'
           },
           xAxis: {},
           series: [{}]
       };

       $.getJSON('data.json', function (data) {
           var categories = [],
               points = [];

            $.each(data, function(i, el) {
                categories.push(el.Time);
                points.push(parseFloat(el.ReadData));
            });
           options.xAxis.categories = categories;
           options.series[0].data = points;
           var chart = new Highcharts.Chart(options);
       });
Run Code Online (Sandbox Code Playgroud)