Reputation: 2378
I am trying to render jqplot widget with dynamic JSON and I cannot find how to do it.
The Only examples I saw included reading JSON from file but I want to use JSON as String for example:
{"oranges":"10","apples":"20","bananas":"6"}
any idea?
Upvotes: 1
Views: 11047
Reputation: 1487
$.getJSON(url,"json").success(function(data) {
var line1 = [];
var content = $.parseJSON( data);
$.each(content, function(index, item) {
line1.push([item.date, item.value])
});
var plot1 = $.jqplot("chart1",[line1], { .....
This is a good exemple how to convert json data to array who jqplot can use ...
Upvotes: 0
Reputation: 1851
The data in your question is not a string, it's a javascript object literal.
If you have oranges, apples and bananas you'll probably want to draw a bar chart; you'll need to extract the labels and the values from the object and then plot the chart:
var chart_data = {"oranges":"10", "apples":"20", "bananas":"6"};
var line1 = [];
for (var prop_name in chart_data) {
line1.push([prop_name, chart_data[prop_name]])
}
// line1 should be [["oranges", 10], ["apples", 20], ["bananas", 6]]
// code snippet from http://www.jqplot.com/tests/rotated-tick-labels.php
// please change it as you need
var plot1 = $.jqplot('chart1', [line1], {
title: 'Fruits',
series: [{renderer:$.jqplot.BarRenderer}],
axesDefaults: {
tickRenderer: $.jqplot.CanvasAxisTickRenderer
},
axes: {
xaxis: {
renderer: $.jqplot.CategoryAxisRenderer
}
}
});
Upvotes: 3