【问题标题】:HighCharts and PHP json_encode. Data from MySQL. No data on chartHighCharts 和 PHP json_encode。来自 MySQL 的数据。图表上没有数据
【发布时间】:2016-08-19 06:54:32
【问题描述】:

尝试使用来自 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 的输出:

[{"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 ","时间":"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-04-25 08:14:23),但这是我的馈送软件目前正在发送到我的 MySQL 的.什么是完美的?

亲切的问候

【问题讨论】:

    标签: javascript php mysql json highcharts


    【解决方案1】:

    这个问题是由 3 个原因引起的。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-11
      • 1970-01-01
      相关资源
      最近更新 更多