JQuery Flot Pie Chart (String to Data)
I am trying to build a pie chart with some 开发者_JS百科data I get serverside build into a string:
"[{ Label: "text", Data: number },{ Label: "text", Data: number }]"
a little like that, but was wondering if there is any way I can parse this string to data that the flot pie chart can use it.
If the data is already in a JSON array and formatted correctly using Label
and Data
, you should be able to just pass the JSON array to flot.
Your code may look something like:
$.plot($("#default"), data,
{
series: {
pie: {
show: true
}
}
});
Where the data variable is the JSON array.
Flot pie charts only accepts array as a default input. JSON is a string format input so it won't work. To solve this you have to either built an array with "label" and "data" columns or split your JSON and form an array from it.
Please check below a dummy example for this:
function dataFormatter() {
var data = [], size = 3, dataInput = 10;
for ( var i = 0; i < size; i++) {
data[i] = {
label : "Series" + (i+1),
data : parseInt(dataInput)
}
dataInput = parseInt(dataInput) * 10;
}
return data;
};
Hope it will solve your problem.
精彩评论