Barry_D_Dale
Barry_D_Dale

Reputation: 131

Highcharts Pie Chart data corrupted

I have a Highcharts pie chart which uses an Ajax call to retrieve data in the for of a JSONArray. I need to strip the data of all ("") and make it a Javascript object but I am running into difficulty. JSON.Parse() throws an error because it is saying that the data is already parsed.

How do I get this data in the correct format for a pie chart?

Call

function getData() {
    $.ajax({ 
        url: '/data/pie_chart.jsp', 
        cache: true,
        datatype: 'json',
        error: function () { },
        success: function (data, status, rsp) {
            alert(data);
            setPieData(data);
        }
    });
}

Data

[{"name":"Dual","load":"20"},{"name":"Gas","load":"35"},{"name":"Other_Fossil_Fuels","load":"15"},{"name":"Nuclear","load":"12"},{"name":"Hydro","load":"8"},{"name":"Wind","load":"10"},{"name":"Other_Renewables","load":"10"}]

Upvotes: 0

Views: 76

Answers (1)

Nishith Kant Chaturvedi
Nishith Kant Chaturvedi

Reputation: 4769

your json should return string for name and number of y. however the workaround is to parse string to number as following

 var dataPie =[];
var abc =[{"name":"Dual","load":"20"},{"name":"Gas","load":"35"}, 
{"name":"Other_Fossil_Fuels","load":"15"},
{"name":"Nuclear","load":"12"},{"name":"Hydro","load":"8"},{"name":"Wind","load":"10"},
{"name":"Other_Renewables","load":"10"}];

$.each(abc,function(i,el)
{
  dataPie.push({name :el.name,y: parseFloat(el.load)});

});

See the Working fiddle with your json

Upvotes: 3

Related Questions