我最初將基於unix的整數時間戳傳遞給json_encode以便與dygraphs一起使用,但實際上除了一個問題外,您必須使用xaxis valueFormatter和axisLabelFormatter以及日期/軸上的時間將與數值圖例(圖表右上角)中顯示的日期/時間不一致。如何將json日期反序列化爲dygraph本機數組格式
這裏是我的,除了傳說中的作品是錯誤的,test.php的原始方法:
<?php
$table[] = [1482407341000,92,86];
$table[] = [1482407342000,92,86];
$table[] = [1482407343000,91,85];
$table[] = [1482407344000,92,85];
$table[] = [1482407345000,91,84];
$table[] = [1482407346000,90,83];
$table[] = [1482407347000,91,82];
$table[] = [1482407348000,92,82];
$jsontable = json_encode($table);
echo $jsontable;
?>
<html><head><script type="text/javascript" src="dygraph-combined-dev.js"></script></head>
<body><div id="graph" style="width:100%; height:96%;"></div>
<script type="text/javascript">
g = new Dygraph(document.getElementById("graph"),
<?=$jsontable?>,
{
legend: 'always',
labelsDivStyles: { 'textAlign': 'right' },
labels: ['Date', 'spO2', 'pr'],
axes: {
x: {
valueFormatter: Dygraph.dateString_,
axisLabelFormatter: Dygraph.dateAxisFormatter,
ticker: Dygraph.dateTicker
}
}
});
</script></body></html>
現在,這裏是我的解決方案的嘗試,如果你實際使用的日期對象,然後x軸將調整與圖例(http://jsfiddle.net/b0g9pd1h/),這應該工作,但dygraphs無法加載,所以仍然有我的變量的格式有問題,沒有錯誤信息顯示,所以我不確定爲什麼dygraphs不加載。
與dygraphs你的價值要麼是一個整數/浮動或它將成爲一個字符串/日期,所以與我的JSON.parse函數json_deserialize_helper我將所有字符串作爲日期並試圖返回日期對象,代碼看起來不錯,所以我不確定我做錯了什麼。
test2.php:
<?php
$table[] = ["1482407341000",92,86];
$table[] = ["1482407342000",92,86];
$table[] = ["1482407343000",91,85];
$table[] = ["1482407344000",92,85];
$table[] = ["1482407345000",91,84];
$table[] = ["1482407346000",90,83];
$table[] = ["1482407347000",91,82];
$table[] = ["1482407348000",92,82];
$jsontable = json_encode($table);
echo $jsontable;
?>
<html><head><script type="text/javascript" src="dygraph-combined-dev.js"></script>
<script type="text/javascript">
function json_deserialize_helper(key,value) {
if (typeof value === 'string') {
return new Date(parseInt(value));
}
return value;
}
</script></head>
<body><div id="graph" style="width:100%; height:100%;"></div>
<script type="text/javascript">
var data = <?=$jsontable?>;
var json = JSON.parse(data,json_deserialize_helper);
g = new Dygraph(document.getElementById("graph"),
json,
{
legend: 'always',
labelsDivStyles: { 'textAlign': 'right' },
labels: ['Date', 'spO2', 'pr']
});
</script></body></html>
還,也許這是不是這樣做的最有效的方式,但解析字符串的日期對象似乎是相當有效的對我來說,如果任何人有一個替代的解決方案,請讓我知道。