将JSON对象填充到高亮条形图

时间:2013-12-29 05:57:04

标签: javascript json highcharts

我是新手解析JSON对象到高图,我想绘制基本条形图。 我已经完成了图表的标题。问题是我想要显示的系列没有显示。(计为系列,qpAnswer为xAxis)。

这是我的JSON数据

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

这是我的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);
       });

2 个答案:

答案 0 :(得分:2)

您可以预处理数据,如

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

然后用

绘制它
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);

我在小提琴中完成了http://jsfiddle.net/gwC2V/1/

如果有帮助,请告诉我们。

答案 1 :(得分:-1)

以下示例可以帮助您

JSON 文件

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

使用getJSON()从 JSON 文件中检索数据并填充到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);
    });

});

这是link