The*_*ndr 5 javascript arrays json getjson jqplot
我想使用来自服务器端的jqPlot usinge数据来自JSON,如本例中所述:http://www.jqplot.com/tests/data-renderers.php
我的代码几乎与示例相同:
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'}
}
},
});
Run Code Online (Sandbox Code Playgroud)
在服务器端我使用PHP(和Yii).该网页返回一个数组,该数组通过使用CJSON::encode($resultArray);(此Yii函数传递给PHP JSON编码函数)编码为JSON.PHP脚本的结果看起来像这样:
{"2011-04-25 14:46:40":2,"2011-04-26 14:46:40":3,"2011-04-27 14:46:40":5}
Run Code Online (Sandbox Code Playgroud)
客户端的Ajax请求解决了这样的问题(输出console.info();)
Object { 2011-04-25 14:46:40=2, 2011-04-26 14:46:40=3, ...}
Run Code Online (Sandbox Code Playgroud)
也许,jqPlot期望以下格式:
[[["2011-04-25 14:46:40":0],["2011-04-26 14:46:40",3],["2011-04-27 14:46:40",0]]]
Run Code Online (Sandbox Code Playgroud)
我error uncaught exception: [object Object]
总是得到什么是错的?有没有办法将对象转换为典型的数组形式?
谢谢
我有这样的东西。我有 2 个用于值、标签的数组。您应该从数组构造如下字符串。
$size = count($labels);
for ($i = 0; $i < $size; $i++) {
$result = $result . "['" . $labels[$i] . "'," . $values[$i] . "]";
if($i != $size -1 ){
$result = $result . ",";
}
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您没有 2 个数组,只有这个字符串 {"2011-04-25 14:46:40":2,"2011-04-26 14:46:40":3,"2011-04-27 14 :46:40":5} 您可以将 { 替换为 [ , } 替换为 ] ,并将 , 替换为 ],[ 。一个肮脏但快速的解决方案。
在上面的代码之后,您可能需要在两侧附加“[”和“]”并返回值。
| 归档时间: |
|
| 查看次数: |
19281 次 |
| 最近记录: |