JSON用于jqPlot [英] JSON for jqPlot
问题描述
我想用jqPlot usinge数据从服务器端的JSON的到来,就像这个例子说明:<一href=\"http://www.jqplot.com/tests/data-renderers.php\">http://www.jqplot.com/tests/data-renderers.php
I would like to use jqPlot usinge data from server side coming in JSON, like described in this example: http://www.jqplot.com/tests/data-renderers.php
我的code是几乎相同的,如例如:
My code is nearly the same like the example:
function myGraph(jsonurl) {
var ajaxDataRenderer = function(url, plot, options) {
var ret = null;
$.ajax({
// have to use synchronous here, else the function
// will return before the data is fetched
async: false,
url: url,
dataType:"json",
success: function(data) {
ret=data;
console.warn(data);
}
});
return ret;
};
var plot1 = $.jqplot('chartdiv', jsonurl, {
title: 'myTitle',
dataRenderer: ajaxDataRenderer,
dataRendererOptions: { unusedOptionalUrl: jsonurl },
series: [{
label: 'myLabel',
neighborThreshold: -1
}],
axes: {
xaxis: {
renderer: $.jqplot.DateAxisRenderer,
// min:'August 1, 2010 16:00:00',
tickInterval: '2 months',
tickOptions:{formatString:'%Y-%m-%d.%H:%M:%S'}
},
yaxis: {
tickOptions:{formatString:'$%.2f'}
}
},
});
在服务器端我使用PHP(和Yii的)。该网页返回一个数组,这是EN coded到JSON使用 CJSON :: EN code($ resultArray);
(这Yii的函数传递到低谷PHP的JSON恩code函数)。
从PHP脚本的结果lookes这样的:
On server side i'm using PHP (and Yii). The webpage returns an array, which is encoded to JSON by using CJSON::encode($resultArray);
(this Yii function passed trough to the PHP JSON encode function).
The Results from the PHP script lookes like that:
{"2011-04-25 14:46:40":2,"2011-04-26 14:46:40":3,"2011-04-27 14:46:40":5}
在客户端的Ajax请求解决这样的事情(输出 console.info();
)
Object { 2011-04-25 14:46:40=2, 2011-04-26 14:46:40=3, ...}
也许,jqPlot期望的格式如下:
Probably, jqPlot expect the following format:
[[["2011-04-25 14:46:40":0],["2011-04-26 14:46:40",3],["2011-04-27 14:46:40",0]]]
所有时间我得到的错误未捕获的异常:[对象的对象]
哪里不对?
有没有办法将对象转换为典型的数组形式?
All the time i get the error uncaught exception: [object Object]
What is wrong?
Is there a way to convert the object for to the typical array form?
感谢您
推荐答案
我有这样的事情。我有2个阵列的值,标签应。你从数组如下构建字符串。
I have something like this . I had 2 arrays for values,labels .You should construct string as below from arrays .
$size = count($labels);
for ($i = 0; $i < $size; $i++) {
$result = $result . "['" . $labels[$i] . "'," . $values[$i] . "]";
if($i != $size -1 ){
$result = $result . ",";
}
}
或者如果你没有2个数组,只是有这个字符串{2011-04-25 14点46分40秒:2,2011-04-26 14点46分40秒:3,2011-04 -27 14时46分40秒:5}您可以替换用{[,}用],并与],[。一个肮脏的,但快速的解决方案。
OR if you dont have 2 arrays and just have this string {"2011-04-25 14:46:40":2,"2011-04-26 14:46:40":3,"2011-04-27 14:46:40":5} you can replace { with [ , } with ] and , with ],[ . A dirty but quick solution .
以上code后,您可能需要追加[和]两边和返回值。
After above code you might need to append '[' and ']' on both sides and return value.
这篇关于JSON用于jqPlot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!