簡體   English   中英

Highcharts 格式化來自 php 腳本/unixtime 的 json 數據

[英]Highcharts format json data from php script / unixtime

我寫了一個帶有目標的 php 腳本來使用 $.getJSON() 函數。#

活-TEMP-data.php創建這樣的數據的JSON文件:數據

這是結果:折線圖

我究竟做錯了什么 ? 代碼如下。

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

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

        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javascript"></script>
        <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>
<script>
$(function () {
    var chart;
    $(document).ready(function() {
        $.getJSON("live-temp-data.php", function(json) {

            Highcharts.chart('container', {
                chart: {
                    type: 'spline'
                },
                title: {
                    text: 'steinhagen-wetter.de'
                },
                subtitle: {
                    text: '2020'
                },
                yAxis: {
                    title: {
                        text: 'Temperature(°C)'
                    }
                },
                time:{
                    timezone: 'Europe/ Berlin'
                },
                xAxis: {
                    type: "datetime",
                    dateTimeLabelFormats: {
                        hour: '%H:%M', 
                    },
                },
                series: [{
                     name: 'Outside Temp °C',
                     data:  json
                }]
            });
        });
    });
});
</script>

    </head>

</html>

1) 打開 Web 控制台,您將看到以下消息:

https://www.highcharts.com/errors/25/

如果您瀏覽此頁面,您將收到錯誤消息:

找不到 Moment.js 庫

使用 global.timezone 選項需要加載 Moment.js 庫。

所以導入那個庫,或者去掉這個選項,這個問題應該就解決了。

2) 您的 HTML 文檔無效,所有內容都在head部分,您應該有一個包含圖形容器的body部分。

3) 您的 JSON 無效: ["[1584713222000, 6.5]","[1584713572000, 6.6]","[1584713989000, 6.7]", ...您不應該在數據項周圍加上引號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM