开发者

Dynamic jSON into Highcharts Pie Chart

I'm fighting with Highcharts right now trying to get data into it. If I hard-code the options and series, I can get it to work - so it's not my configuration (i.e. scripts, css, etc.)

I lo开发者_如何学编程oked at the following question here on SO as it was related: Ajax JSON in to Highcharts Pie Chart

Unfortunately, it didn't help. Can someone take a look and see what it is that I'm doing wrong?

(This code is directly related to the previously-mentioned SO post)

function renderChart(divId, chartType, chartTitle){
    var options = createOption(divId, chartType, chartTitle);
    var series = createSeries();
    options.series = series;    
    var chart = new Highcharts.Chart(options);
}

function createOption(divId, chartType, chartTitle){
    var options = {
        chart: {
            renderTo: divId,
            defaultSeriesType: 'pie'
        },
        title: {
            text: chartTitle
        },
        plotOptions: {
            pie: {
                allowPointSelect: true,
                cursor: 'pointer',
                dataLabels: {
                    enabled: false
                },
                showInLegend: true
            }
        },
        series: []
        };
        return options; 
}

function createSeries(){
    var series = [];
    series.push('[');
    series.push('{');
    series.push('"type" : "pie",');
    series.push('"name" : "fruits",');
    series.push('"data" : [');
    series.push('[');
    series.push('"Apple",')
    series.push('43.0');
    series.push('],');
    series.push('[');
    series.push('"Pear",')
    series.push('57.0');
    series.push(']');
    series.push(']');
    series.push('}');
    series.push(']');
    return series.join('');
}

Thanks in advance.

Disregard. Easiest way - have the server cook up the series object. See this post: iterate JSON response with jQuery for Highcharts


You are feeding Highcharts a string:

'[{"type" : "pie","name" : "fruits","data" : [["Apple",43.0],["Pear",57.0]]}]'

where it expects an array of configuration object for the series like:

[{
    type : "pie",
    name : "fruits",
    data : [["Apple",43.0],["Pear",57.0]]
}]


You can bind chart with JSON data, directly. You just need to set the json property names as highchart standard. 'Y' for value and 'name' for label.

Your JSON should be as like follow:

[ { name: "Apple", y: 25 }, { name: "Pear", y: 20 } ]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜