我使用morris.js在图表上随时间绘制一些统计数据。
$(document).ready(function() {
if($('#time-graph').length) {
var week_data = <?php echo($stat_array)?>;
Morris.Line({
element : 'time-graph',
data : week_data,
xkey : 'period',
ykeys : 'temp_avg',
labels : ['temp_avg','temp_avg'],
events : ['2014-06-01 00:00:01', '2014-6-30 23:55:55'],
ymin : -1.0,
ymax : 50.0
});
}
$ stat_array包含一个json字符串,它按照以下方式格式化,在应用程序的早期检索
[{"period":"2014-06-24 18:37:44","temp_avg":"46.845"},{"period":"2014-06-24 18:38:01","temp_avg":"47.28"},{"period":"2014-06-24 18:40:01","temp_avg":"47.185"},{"period":"2014-06-24 18:42:01","temp_avg":"47.4675"},{"period":"2014-06-24 18:44:01","temp_avg":"47.3125"},{"period":"2014-06-24 18:46:01","temp_avg":"48"},{"period":"2014-06-24 18:48:01","temp_avg":"47.2175"},{"period":"2014-06-24 18:50:01","temp_avg":"48"},{"period":"2014-06-24 18:52:01","temp_avg":"48.095"}];
但图表格式不正确,如下所示
如果有人能指出我出错的地方会很棒:D
答案 0 :(得分:1)
实际上,你只是缺少一些东西,首先,删除对象上的;
分号。
其次,我不知道这是否是一个错字,但你错过了$(document).ready({});
的结束。
最后,如果您的数据在某个特定区域混乱,请不要感到惊讶,因为您的数据似乎只有2014-06-24 18:MM:SS
个差异。我只是调整了范围,你会清楚地看到线图。 Sample Output
示例:
<?php $stat_array = '[{"period":"2014-06-24 18:37:44","temp_avg":"46.845"},{"period":"2014-06-24 18:38:01","temp_avg":"47.28"},{"period":"2014-06-24 18:40:01","temp_avg":"47.185"},{"period":"2014-06-24 18:42:01","temp_avg":"47.4675"},{"period":"2014-06-24 18:44:01","temp_avg":"47.3125"},{"period":"2014-06-24 18:46:01","temp_avg":"48"},{"period":"2014-06-24 18:48:01","temp_avg":"47.2175"},{"period":"2014-06-24 18:50:01","temp_avg":"48"},{"period":"2014-06-24 18:52:01","temp_avg":"48.095"}]'; ?>
<div id="time-graph"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="http://cdn.oesmith.co.uk/morris-0.4.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
Morris.Line({
element: 'time-graph',
data: <?php echo $stat_array; ?>,
xkey: 'period',
ykeys: ['temp_avg'],
labels: ['temp_avg'],
events : ['2014-06-24 18:00:00', '2014-6-24 18:59:59'],
ymin : -1.0,
ymax : 50.0
});
});
</script>