2016-04-25 51 views
0

尝试用MySQL中的数据生成HighCharts。使用PHP和json_encode函数从MySQL解析数据。问题:图表中没有数据。HighCharts和PHP json_encode。来自MySQL的数据。图表上没有数据

这是我的javascript:

<!DOCTYPE HTML> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>Highcharts Example</title> 

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
<style type="text/css"> 

</style> 
<script type="text/javascript"> 

$(document).ready(function() { 

    var options = { 
     chart: { 
      renderTo: 'container', 
      type: 'spline' 
     }, 
     series: [{}] 
    }; 

    $.getJSON('json.php', function(data) { 
     options.series[0].data = data; 
     var chart = new Highcharts.Chart(options); 
    }); 

}); 

</script> 

<script src="https://code.highcharts.com/highcharts.js"></script> 
<script src="https://code.highcharts.com/modules/exporting.js"></script> 

</head> 
<body> 

<div id="container" style="max-width: 500px; height: 400px; margin: 0 auto"></div> 

</body> 
</html> 

这是我的json.php:

<?php 

$host = "localhost:3306"; 
$user = "removed"; 
$pass = "removed"; 
$dbname = "removed"; 

$connection = mysqli_connect($host, $user, $pass, $dbname); 

$query = "SELECT temp, time from vejr where time > '2016-04-25 06:14:23'"; 

$result = mysqli_query($connection, $query); 

$emparray = array(); 
    while($row =mysqli_fetch_assoc($result)) 
    { 
     $emparray[] = $row; 
    } 
     echo json_encode($emparray); 

?> 

这是从json.php输出:

[{ “温度”: “1.6”,“time”:“2016-04-25 08:14:23”},{“temp”:“2.6”,“time”:“2016-04-25 09:14:23”},{ “temp”:“3.6”,“time”:“2016-04-25 10:14:23”},{“temp”:“4.6”,“time”:“2016-04-25 11:14:23 “},{”temp“:”5.6“,”time“:”2016-04-25 12:14:23“},{”temp“:”6.6“,”time“:”2016 -04-25 13:14:23“},{”temp“:”7.6“,”time“:”2016-04-25 14:14:23“},{”temp“:”8.6“,”time “:”2016-04-25 15:14:23“}]

我想要做的是在x轴上的时间图(fx 2016-04-25 08:14:23) ,以及y轴上的值(fx 1.6)。

从这个

得到了灵感:http://www.highcharts.com/docs/working-with-data/custom-preprocessing#3

还有,我知道我在x轴的时间戳是不完美的,它是长(2016年4月25日8点十四分23秒),但是这是我的饲养软件目前发送给我的MySQL。什么是完美的?

亲切的问候

回答

1

该问题是由3个原因造成的。

  1. 价值观应该被命名为像x和y,而不是自定义字段
  2. y值应该是数字,而不是字符串像你这样(使用JSON_NUMERIC_CHECK标志json_encode)
  3. 的x值,应该是时间戳(以毫秒为单位的时间)。在PHP中,你可以使用strtotime()函数