Populate JSON object to highchart bar chart

只谈情不闲聊 提交于 2019-12-11 19:37:20

问题


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

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/20822588/populate-json-object-to-highchart-bar-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!