简体   繁体   English

Highcharts:如何将数据从 JSON 加载到 xAxis.categories 和 series.data?

[英]Highcharts: How to load data from JSON to xAxis.categories and series.data?

Sorry for the beginner question.对不起初学者的问题。

This is how my JSON looks like:这就是我的 JSON 的样子:

[
   ["junior college", "Primary", "secondary", "polytechnic"],
   ["4", "3", "1", "1"]
]

And this is my HTML,这是我的 HTML,

var chart; // global
var options = {
    chart: {
        renderTo: 'container',
        defaultSeriesType: 'bar'
    },
    title:{
        text: 'No. Schools for different levels'
    },
    xAxis:{
        title:'Education Level',
        categories:[]
    },
    yAxis:{
        title:{
            text:'No. Of Schools'
        }
    },
    series:[{
        name: "No. Schools",
        data: []
    }]
};

$.getJSON('loadData.php', function(JSONresult) {
    yData = options.series[0].data; //Array to store data for y column
    xData = options.xAxis.categories; //Array to store data for x column

    xDataObj = JSONresult[0];
    yDataObj = JSONresult[1];

    for(var key in xDataObj){
        xData.push(xDataObj[key]);
    }

    for(var key in yDataObj){
        yData.push(parseFloat(yDataObj[key]));
    }

I tried to execute the code, firebug shows no error but the chart never shows up.我尝试执行代码,萤火虫没有显示错误,但图表从未出现。 Could you help point out where did I go wrong?你能帮忙指出我 go 哪里错了吗?

I found out the solution.我找到了解决方案。

In my php script, I have to cast integer to the series data,在我的 php 脚本中,我必须将 integer 转换为系列数据,

(int)$variable;

so the JSON out put would be所以 JSON 输出将是

[4, 3, 1, 1]

and the chart will be rendered.并且图表将被渲染。

Thank you ppl:)谢谢大家:)

You need to do something like this to instantiate and render the chart:您需要执行以下操作来实例化和呈现图表:

chart = new Highcharts.Chart(options);

This could go at the end of your getJSON response function.这可能会在您的 getJSON 响应 function 结束时出现 go。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM