0

我正在尝试按照此示例通过 ajax 使用 PHP 格式化和加载多个系列数据:

以下PHP代码,

<?php
$connection = pg_connect("host=localhost port=5432 dbname=pccs user=michael                   password=huskies1975") or die(" " . pg_last_error($connection));

$chart1 = pg_query($connection, "SELECT sample_date, a FROM monitor_nutrient WHERE   station_num_id = 201  ORDER BY sample_date ASC LIMIT 5");
$row_a = array();
$row_a['name'] = 'Temperature';
while ($ra = pg_fetch_array($chart1)) {
    $date = str_replace("-",",",$ra['sample_date']);
    $row_a['data'][] = array($date, $ra['a']);
}

$chart1 = pg_query($connection, "SELECT sample_date, b FROM monitor_nutrient WHERE     station_num_id = 201  ORDER BY sample_date ASC LIMIT 5");
$row_b = array();
$row_b['name'] = 'Salinity';
while ($rb = pg_fetch_array($chart1)) {
    $date = str_replace("-",",",$rb['sample_date']);
    $row_b['data'][] = array($date, $rb['b']);
}

$chart1 = pg_query($connection, "SELECT sample_date, c FROM monitor_nutrient WHERE station_num_id = 201  ORDER BY sample_date ASC LIMIT 5");
$row_c = array();
$row_c['name'] = 'Dissolved Oxygen';
while ($rc = pg_fetch_array($chart1)) {
    $date = str_replace("-",",",$rc['sample_date']);
    $row_c['data'][] = array($date, $rc['c']);
}

$result = array();
array_push($result, $row_a);
array_push($result, $row_b);
array_push($result, $row_c);

echo(json_encode($result, JSON_NUMERIC_CHECK));
//print json_encode($result, JSON_NUMERIC_CHECK);

pg_close($connection);
?>

产生这个有效的(JsonLint)输出:

[
    {
        "name": "Temperature",
        "data": [
            [
                "2012,06,12",
                20.38
            ],
            [
                "2012,06,21",
                24.62
            ],
            [
                "2012,07,03",
                25.96
            ],
            [
                "2012,07,20",
                24.92
            ],
            [
                "2012,08,03",
                25.56
            ]
        ]
    },
    {
        "name": "Salinity",
        "data": [
            [
                "2012,06,12",
                31.49
            ],
            [
                "2012,06,21",
                31.47
            ],
            [
                "2012,07,03",
                31.11
            ],
            [
                "2012,07,20",
                30.75
            ],
            [
                "2012,08,03",
                30.94
            ]
        ]
    },
    {
        "name": "Dissolved Oxygen",
        "data": [
            [
                "2012,06,12",
                5.53
            ],
            [
                "2012,06,21",
                7.07
            ],
            [
                "2012,07,03",
                5.3
            ],
            [
                "2012,07,20",
                3.49
            ],
            [
                "2012,08,03",
                6.67
            ]
        ]
    }
]

首先,这是 Highcharts 系列的正确格式,其次,我如何以及在哪里将日期转换为 Date.UTC(),例如 JavaScript/PHP?最后是下面的代码甚至接近我想要的。

function chartParser(data) {

        $.each(data, function (key, value) {
            var series = {name: key, data: []};

            $.each(value, function (key, val) {
                if (key == 'name') {
                    series.name = val;
                }
                else {
                    $.each(val, function (key, val) {
                        options.series.push([val[0], val[1]]);

                    });
                }

            });
            var chart1 = new Highcharts.Chart(options);
        });

    }

任何帮助和示例将不胜感激。几个星期以来,我一直在用这个折磨自己。

4

1 回答 1

3

您可以通过以下方式在客户端实现:

 $.each(val, function (key, val) {
   options.series.push([new Date(val[0]).getTime(), val[1]]);
 });
于 2013-07-03T10:09:45.167 回答