无法让HighCharts从JSON正确运行

时间:2016-11-10 16:55:03

标签: javascript php jquery json highcharts

我首先要说的是我对Javascript完全陌生。我最常使用Java和PHP。

我从示例中复制了大部分代码,并试图修改它以使其运行但没有任何运气。 JSFiddle中的原始示例如下:http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/stock/demo/compare/

赞赏任何意见。

<?php
echo <<< 'EOT'
<html>
<head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
   <script src="https://code.highcharts.com/highstock.js"></script>
   <script src="https://code.highcharts.com/exporting.js"></script>
</head>
<body>
EOT;
require_once('hvst.php'); // Generates the JSON file using json_encode();
echo <<< 'EOT'
function createChart() {

        var fields = ['humidity', 'curtemp'];
        Highcharts.stockChart('container', {

            rangeSelector: {
                selected: 4
            },

            yAxis: {
                labels: {
                    formatter: function () {
                        return (this.value > 0 ? ' + ' : '') + this.value + '%';
                    }
                },
                plotLines: [{
                    value: 0,
                    width: 2,
                    color: 'silver'
                }]
            },
            tooltip: {
                pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
                valueDecimals: 2,
                split: true
            },
            series: seriesOptions
        });

        $.each(fields, function (timestamp, curtemp) {
                $.getJSON('chart1.json', function (humidity) {
                        seriesOptions[timestamp] = {
                                name: curtemp,
                                data: humidity
                        },
                        seriesCounter += 1;

                        if (seriesCounter === fields.length) {
                                createChart();
                        }
                });
        });

};

</script>
<div id="container" style="height: 400px; min-width: 400px"></div>
</body>
</html>
EOT;
?>

编辑:hvst.php

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
//Set JSON header
header("Content-type: text/json");

include 'dbread.php';
//include 'correlate.php';

// Get last 5 days
$days=5;
$resultSet1 = readDB('SELECT timestamp,humidity,curtemp FROM public."WeatherData" WHERE timestamp > NOW()-INTERVAL \''.$days.' days\';');
$fp = fopen('chart1.json', 'w');
fwrite($fp, json_encode($resultSet1,JSON_PRETTY_PRINT|JSON_NUMERIC_CHECK));
fclose($fp);
?>

以下是JSON输出中前几行的示例:

[
    {
        "timestamp": "2016-11-05 10:10:02.427425",
        "humidity": 48,
        "curtemp": 46
    },
    {
        "timestamp": "2016-11-05 10:10:03.83401",
        "humidity": 48,
        "curtemp": 46
    },

0 个答案:

没有答案