我试图使用flot从我的SQL数据库中绘制数据图表,这是使用php收集的,然后是json编码的。
目前的情况如下:
[{"month":"February","data":482},{"month":"March","data":0}] 然而,要使flot操作数据,则需要以
[X AXIS VALUE, Y AXIS VALUE],[X AXIS VALUE, Y AXIS VALUE],[X AXIS VALUE, Y AXIS VALUE]等等。
我该怎么改变呢?
发布于 2014-02-11 15:57:55
基本过程是将数据转换为数组(或对象,如果您愿意的话),然后取出每个绘图点的数据并将其插入数组中。然后,将该数组转换为字符串。下面是一个例子:
// SAMPLE DATA
$json_string = '[{"month":"February","data":482},{"month":"March","data":0}]';
// DEFAULT
$all_points_array = array();
// JSON DECODE THE STRING AND TURN IT INTO AN ARRAY
$json_array = json_decode($json_string, true);
// LOOP THROUGH THE PLOT POINTS AND ADD EACH SET TO AN ARRAY
foreach ($json_array AS $plot_points) {
$all_points_array[] = '['.$plot_points['month'].','.$plot_points['data'].']';
}
// CONVERT THE ARRAY TO A STRING, SEPARATED BY COMMAS
$all_points = implode(',', $all_points_array);
// PRINT OUT YOUR STRING
echo $all_points;发布于 2014-02-11 15:49:45
使用map返回数组:
var flotData = data.map(function (el) {
return [el.month, el.data];
});
console.log(flotData); // [["February", 482], ["March", 0]]https://stackoverflow.com/questions/21706358
复制相似问题