highcharts使用Date.UTC转换json数据

时间:2013-07-02 23:02:43

标签: php json highcharts

我正在尝试使用PHP格式化并通过ajax加载多个系列数据example:

以下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);
        });

    }

非常感谢任何帮助和示例。我已经用这个折磨了几个星期。

1 个答案:

答案 0 :(得分:3)

你可以通过这种方式在客户端实现这一目标:

 $.each(val, function (key, val) {
   options.series.push([new Date(val[0]).getTime(), val[1]]);
 });