Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何反序列化dygraph本机数组格式的json日期_Json_Date_Datetime_Dygraphs_Json Deserialization - Fatal编程技术网

如何反序列化dygraph本机数组格式的json日期

如何反序列化dygraph本机数组格式的json日期,json,date,datetime,dygraphs,json-deserialization,Json,Date,Datetime,Dygraphs,Json Deserialization,我最初是将基于unix的整数时间戳传递给json_encode以用于动态图,这实际上是可行的,除了一个问题,然后您必须使用xaxis valueFormatter和axisLabelFormatter,轴上的日期/时间将不与值图例(图表右上角)中显示的日期/时间对齐 下面是我的原始方法,除了图例错误,test.php: <?php $table[] = [1482407341000,92,86]; $table[] = [1482407342000,92,86];

我最初是将基于unix的整数时间戳传递给json_encode以用于动态图,这实际上是可行的,除了一个问题,然后您必须使用xaxis valueFormatter和axisLabelFormatter,轴上的日期/时间将不与值图例(图表右上角)中显示的日期/时间对齐

下面是我的原始方法,除了图例错误,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>

g=新的动态图(document.getElementById(“图形”),
,
{
传说:“永远”,
labelsDivStyles:{'textAlign':'right'},
标签:[“日期”、“spO2”、“pr”],
轴线:{
x:{
valueFormatter:Dygraph.dateString,
axisLabelFormatter:Dygraph.dateAxisFormatter,
股票代码:Dygraph.dateTicker
}
}
});
现在,我尝试了一个解决方案,如果您实际使用了日期对象,那么xaxis将与图例()对齐,这应该可以工作,但动态图无法加载,因此我的变量的格式肯定仍然存在问题,没有显示错误消息,因此我不确定动态图为什么不加载

对于动态图,您的值要么是整数/浮点值,要么是字符串/日期,因此使用我的JSON.parse函数JSON\u deserialize\u 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>

函数json\u反序列化\u助手(键、值){
如果(值的类型=='string'){
返回新日期(parseInt(value));
}
返回值;
}
var数据=;
var json=json.parse(数据,json\u反序列化\u助手);
g=新的动态图(document.getElementById(“图形”),
json,
{
传说:“永远”,
labelsDivStyles:{'textAlign':'right'},
标签:['Date','spO2','pr']
});

此外,也许这不是最有效的方法,尽管将字符串解析为日期对象对我来说似乎非常有效,但如果有人有其他解决方案,请告诉我。

如果这个答案有助于解决您正在处理的问题,请更新这篇文章,我是这个网站的新手

我成功了,你必须一个接一个地循环和修改元素,下面的解决方案有效,并且X轴和图例的值都正确显示,我可以将labelsUTC设置为true或false,它们都正确显示。键使用的是日期对象,而不是像我之前所做的那样使用日期字符串

<?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:100%;"></div>
<script type="text/javascript">
    var data = <?=$jsontable?>;
    //loop through json table and convert unix timestamp to date object:
    for ( var i in data) {
        data[i][0] = new Date(data[i][0]);
    }
    g = new Dygraph(document.getElementById("graph"),
    data,
    {
        legend: 'always',
        labelsDivStyles: { 'textAlign': 'right' },
        labels: ['Date', 'spO2', 'pr']
    });
</script></body></html>

var数据=;
//循环浏览json表并将unix时间戳转换为日期对象:
用于(数据中的var i){
数据[i][0]=新日期(数据[i][0]);
}
g=新的动态图(document.getElementById(“图形”),
数据,
{
传说:“永远”,
labelsDivStyles:{'textAlign':'right'},
标签:['Date','spO2','pr']
});