I'm trying to populate a bar chart from an external JSON in a PHP file. I've trying to use AJAX and getJSON function to do this but still failed to generate a chart.
Here is how my JSON format look like:
[
{
"NAME": "a chart",
"VALUES": "[[439, 233, 233, 495],[292, 360, 262, 173],[222, 95, 27, 27]]"
}
]
Here is the javascript:
$(document).ready(function() {
urlDataJSON = 'data.php';
$.getJSON(urlDataJSON, function (data) {
console.log(data.output);
var dataLabels = ['base', 'key', 'pacing', 'emerging'];
var dataLines = json[0].VALUES;
var chartTitle = json[0].NAME;
$.jqplot('chart2', dataLines, {
legend: {
show: true
},
title: chartTitle,
seriesDefaults: {
renderer: $.jqplot.BarRenderer,
pointLabels: {
show: true,
location: 'e',
edgeTolerance: -15
},
shadowAngle: 135,
renderOptions: {
barDirection: 'horizontal'
}
},
axes: {
xaxis: {
renderer: $.jqplot.CategoryAxisRenderer,
ticks: dataLabels
}
}
});
});
Do you guys know what's wrong with my code above? If anything please let me know.
EDIT: Updated code
var urlDataJSON = 'data.php';
/* var json = [{
"Name": "Poll Results",
"Serie": [[2, 5, 4], [4, 1, 7], [6, 3, 1], [3, 4, 2]]}];*/
$.getJSON(urlDataJSON, function (data) {
console.log(data);
var tick = ['asd','fsdf','asda','fdsf',];
var dataLines = [];
var title = [];
$.each(data, function (entryindex, entry) {
dataLines.push(entry['VALUES']);
title.push(entry['NAME']);
});
var options = {
legend: {
show: true
},
title: 'Poll Results',
seriesDefaults: {
renderer: $.jqplot.BarRenderer,
pointLabels: {
show: true,
location: 'e',
edgeTolerance: -15
},
shadowAngle: 135,
renderOptions: {
barDirection: 'horizontal'
}
},
axes: {
xaxis: {
renderer: $.jqplot.CategoryAxisRenderer,
ticks: tick
}
}
};
var plot = $.jqplot('chart2', [dataLines], options);
});
I've put .each() function to push data into an array. Now, the graph is empty. Only the background is showing.
See Question&Answers more detail:os