本文介绍了HighCharts和PHP json_encode。来自MySQL的数据。没有数据在图表上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我的javascript:

 <!DOCTYPE HTML> 
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8>
< title> Highcharts示例< / title>

< script type =text / javascriptsrc =https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js><<< ; /脚本>
< 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 =containerstyle =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 =选择温度,时间从vejr时间>'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的输出:



[{temp:1.6,time:2016-04-25 08:14:23},{temp:2.6,time: 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,时间: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}]



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

从这得到灵感:

另外,我知道我在x轴上的时间戳并不完美,很长(2016- 04-25 08:14:23),但这正是我的饲喂软件目前发送给我的MySQL。什么是完美的?



亲切的问候

解决方案

问题是造成3个原因。


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


Trying to produce HighCharts with data from MySQL. Parsing data from MySQL using PHP and the json_encode function. Problem: No data on chart.

This is my 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>

This is my 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);

?>

This is the output from the json.php:

[{"temp":"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"}]

What I'm trying to do is a chart with time (fx 2016-04-25 08:14:23) on the x-axis, and the value (fx 1.6) on the y-axis.

Got inspiration from this: http://www.highcharts.com/docs/working-with-data/custom-preprocessing#3

And also, I know that my timestamp on the x-axis is not perfect, it is long (2016-04-25 08:14:23), but that is what my feeding-software currently is sending to my MySQL. What would be perfect?

Kind Regards

解决方案

The issue is caused by 3 reasons.

  1. Values should be named like x and y, not custom fields
  2. The y value should be number, not string like you have (use the JSON_NUMERIC_CHECK flag in json_encode)
  3. The x value, should be timestamp (time in miliseconds). In PHP you can use strtotime() function

这篇关于HighCharts和PHP json_encode。来自MySQL的数据。没有数据在图表上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 21:46