dr200166 2013-07-02 23:02
浏览 102
已采纳

highcharts使用Date.UTC转换json数据

I'm trying to format and load multiple series data via ajax with PHP as per this example:

The following PHP code,

<?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);
?>

produces this valid(JsonLint) ouput:

[
    {
        "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
            ]
        ]
    }
]

First, is this the right format for a Highcharts series, second, how and where do I convert the date to Date.UTC() like the example, JavaScript/PHP? and lastly is the following code even close to what I want.

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

    }

Any help and examples would be greatly appreciated. I've been torturing myself with this for a couple of weeks.

  • 写回答

1条回答 默认 最新

  • douqian9729 2013-07-03 10:09
    关注

    You can achieve that on client side, this way:

     $.each(val, function (key, val) {
       options.series.push([new Date(val[0]).getTime(), val[1]]);
     });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题