I have been trying to create a jQuery Flot pie chart, but I can't get the "data" parameter to work. I have 0..6 values that should total to 100(%), and these values need to be added individually, one label value pair at a time.
All of the examples I have seen assume that the data values are hard wired, and unfortunately that is not the way the real world works. I need some real world examples of how to construct the label - data pairs that Flot uses.
I finally got it working when I realized that the data is supposed to be in jSON object notation. The old light bulb may be dim, but not entirely out. Here's the code:
function updatePieChart() {
if ('#pieChart') {
var total = 0;
var data = new Array();
var verb = "";
var dataIndex = 0;
var count = 0;
for (var dataIndex = 0; dataIndex < 5; dataIndex++) {
var duty = "DUTY_" + (dataIndex + 1);
verb = getData(duty + "_SKILL");
var d = getData(duty + "_PERCENTAGETIMESPENT");
if (isNumeric(d)) {
d = parseInt(d);
if (d > 0) {
total += d;
if (verb == "") verb = duty + dataIndex;
var dataPoint = {
label: verb,
data: d
}
data[count++] = dataPoint;
log(dataPoint.label + ' = ' + dataPoint.data);
}
}
}
if (total != 100) {
if (total < 100) {
var missingDataNum = 100 - total;
var dataPoint = {
label: '**MISSING**',
data: missingDataNum
}
data[count] = dataPoint;
log(dataPoint.label + ' = ' + dataPoint.data);
}
}
if ($.isFunction($.plot)) {
log('Plot version: ' + $.plot.version);
var pie = $.plot($('#pieChart'), data,
{
series: {
pie: {
show: true,
label: {
show: true,
radius: 1,
formatter: function (label, series) {
return '<div style="font-size:11px; text-align:center; padding:2px; color:black;">' + label + '<br />' + Math.round(series.percent) + '%</div>';
},
background: {
opacity: 0.0
}
},
combine: {
color: '#999',
threshold: 0.05
}
}
},
legend: {
show: false,
labelFormatter: function (label, series) {
return '<b>' + label + '</b>';
}
}
});
}
}
}
}
来源:https://stackoverflow.com/questions/13888131/what-is-the-format-of-jquery-flot-data-when-passing-in-the-data-as-variables