Ninja
Ninja

Reputation: 183

Create HighCharts-Column type from JSON

I am trying to create a column type highchart using data from json in the following format:

[{"id":7,"dateV":"2015-11-16","count":10},{"id":6,"dateV":"2015-11-15","count":3},{"id":5,"dateV":"2015-11-14","count":15},{"id":4,"dateV":"2015-11-13","count":10},{"id":3,"dateV":"2015-11-12","count":6},{"id":2,"dateV":"2015-11-11","count":8},{"id":1,"dateV":"2015-11-10","count":5}]

X axis should show the date values and the Y axis should show the count. Somehow i am not able to transform this JSON in the format required.

Here is my code.

<script type="text/javascript">
        $('#myButton').click(function() {
            var options = {
                    chart: {
                        renderTo: 'chartContainerDiv',
                        type: 'column'
                    },
                    series: [{}]
                };

                var url = "callToControllerURL";
                $.getJSON(url,  function(data) {
                    console.log(JSON.stringify(data));
                    options.series[0].data = JSON.stringify(data);
                    var chart = new Highcharts.Chart(options);
                });
            });
    </script>

console log shows the JSON in format specified above. I am guessing i have to form the x and y axis values from the options array. How do i do it?

Upvotes: 0

Views: 801

Answers (1)

Nishith Kant Chaturvedi
Nishith Kant Chaturvedi

Reputation: 4769

You need to iterate over the json data and create category data and series by pushing values.

var jsonData = [{"id":7,"dateV":"2015-11-16","count":10},{"id":6,"dateV":"2015-11-15","count":3},{"id":5,"dateV":"2015-11-14","count":15},{"id":4,"dateV":"2015-11-13","count":10},{"id":3,"dateV":"2015-11-12","count":6},{"id":2,"dateV":"2015-11-11","count":8},{"id":1,"dateV":"2015-11-10","count":5}]; 

 var categoryData= [];
 var seriesData= [];
$.each(jsonData, function(i,item){
seriesData.push(jsonData[i].count);
categoryData.push(jsonData[i].dateV);
console.log("seriesData"+JSON.stringify(seriesData));
}); 

See Working demo with your json here

Upvotes: 3

Related Questions