I have an issue with Flot charts and the data that is coming from AJAX. Basically I created arrays to display the graph but somehow I see the axis label but no data. Also how can I do a stacked or a bar graph?
Edit: Or can anybody tell me how to display data in flot using AJAX with labels on the x-axis. You can see the code here http://jsfiddle.net/DaG5W/284/. Anybody please let me know what I am doing wrong here?
Here is the code I used.
$.getJSON(url, function(output) {
var calendar, count, fieldNames, i, j, values, xAxis;
fieldNames = new Array();
i = 0;
while (i < output.length) {
if (fieldNames.indexOf(output[i].FieldName) < 0) {
fieldNames.push(output[i].FieldName);
}
i++;
}
calendar = new Array();
i = 0;
while (i < output.length) {
if (calendar.indexOf(output[i].Calendar) < 0) {
calendar.push(output[i].Calendar);
}
i++;
}
xAxis = [];
i = 0;
while (i < calendar.length) {
xAxis.push([parseInt(i + 1), calendar[i]]);
i++;
}
data = [];
i = 0;
while (i < fieldNames.length) {
values = [];
count = 0;
j = 0;
while (j < output.length) {
if (fieldNames[i] === output[j].FieldName) {
count++;
values.push([parseInt(count), parseInt(output[j].Totals)]);
}
j++;
}
data.push([
{
label: fieldNames[i].toString(),
data: values,
bars: {
show: true
},
lines: {
show: false
}
}
]);
}
return i++;
});
options = {
xaxis: {
ticks: xAxis
}
};
plot = $.plot($("#div"), [data], options);
}
You're very close to a working solution. All you've done incorrectly is to nest a few too many arrays in 2 places:
data.push([
{
label: fieldNames[i].toString(),
data: values,
bars: {
show: true
},
lines: {
show: false
}
}
]);
No need for the []
around your series object.
plot = $.plot($("#div"), [data], options);
Similarly here, no need for []
around data
. After that, it will graph. See here: http://jsfiddle.net/ryleyb/DaG5W/291/