简体   繁体   中英

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:

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

And this is my 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?

I found out the solution.

In my php script, I have to cast integer to the series data,

(int)$variable;

so the JSON out put would be

[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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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