Maria Margarida Brito
Maria Margarida Brito

Reputation: 29

HighCharts load data via Json

I am trying to make a HighCharts chart from my Json Data. Here is my Json Data

[{"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"}]

My problem is when that the graph don't load , what am i doing wrong? Here is my code 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>

Upvotes: 1

Views: 7129

Answers (1)

Paweł Fus
Paweł Fus

Reputation: 45079

There is a couple of issues, see working demo: http://jsfiddle.net/ux74929j/5/

Let me explain:

  • your format is not compatible with Highcharts, and you are trying to use this in a wrong way, parse your data first:

            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); 
    
  • you don't have xAxis in options, but you are trying to assign categories anyway:

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

So, to sum up, your code should looks like this:

       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);
       });

Upvotes: 5

Related Questions