Jay Sithiporn
Jay Sithiporn

Reputation: 91

Populate JSON object to highchart bar chart

I am newbie parsing JSON object to highchart and I would like to plot basic bar graph. I have done on the title of graph. The problem is that the series that I would like to show is not showing.(count as series and qpAnswer as xAxis).

Here is my JSON data

[
  {
    qpQuestion: "Is that a dog?",
    qpAnswerId: "1",
    qpAnswer: "Yes",
    count: "0"
  },
  {
    qpQuestion: "Is that a dog?",
    qpAnswerId: "2",
    qpAnswer: "No",
    count: "0"
  },
  {
    qpQuestion: "Is that a dog?",
    qpAnswerId: "3",
    qpAnswer: "ok",
    count: "0"
  }
]

Here is my JS

var url="sections.php?request=graph";
        $.getJSON(url,function(data1){

            var options={
                chart: {
                    renderTo: 'container',
                    type: 'column'
                },
                title: {
                    text: data1[0].qpQuestion
                },
                xAxis:{
                    categories: data1.qpAnswer
                    title: {
                        text: 'Answer'
                    }
                },
                yAxis: {
                    min: 0,
                    title: {
                        text: 'Answer Count'
                    }
                }, 
                series:data1
           };

           var chart = new Highcharts.Chart(options);
       });

Upvotes: 0

Views: 8255

Answers (2)

Ahamed Mustafa M
Ahamed Mustafa M

Reputation: 3139

You can pre-process the data to form like

var answers = ['Yes','No' ,'OK'];
var answer_counts= [
            {name: 'Yes', data : [2,0,0]},
            {name: 'No', data: [0,3,0]},
            {name: 'OK', data: [0,0,1]} ];

Then plot it with

var options={
                chart: {
                    renderTo: 'container',
                    type: 'column'
                },
                title: {
                    text:'QA Answers'
                },
                xAxis:{
                    categories: answers,
                    title: {
                        text: 'Answer'
                    }
                },
                yAxis: {
                    min: 0,
                    title: {
                        text: 'Answer Count'
                    }
                }, 
                series:answer_counts
           };

var chart = new Highcharts.Chart(options);

I have done in the fiddle, http://jsfiddle.net/gwC2V/1/

Let us know if it helps.

Upvotes: 2

Gopal Joshi
Gopal Joshi

Reputation: 2358

Below Example can help you

The JSON file

[
    [1,12],
    [2,5],
    [3,18],
    [4,13],
    [5,7],
    [6,4],
    [7,9],
    [8,10],
    [9,15],
    [10,22]
]

use getJSON() to retrive data from JSON file and Populate to CHART

$(document).ready(function() {

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

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

});

Here is link

Upvotes: -1

Related Questions