Highcharts没有在日期上升 - 日期被解释为字符串

时间:2016-04-26 20:09:46

标签: javascript php highcharts

我试图创建一个不规则x轴的高图,有点像这样:http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/spline-irregular-time/

我通过PHP从mySQL中提取数据并以JSON格式处理。

我无法让highcharts / JS以日期格式阅读日期 - 似乎是将它们作为字符串拉出来。

这是我的剧本:

$(document).ready(function() {
    var options = {
        chart: {
            type: 'spline',
            marginRight: 130,
            marginBottom: 25
        },
        title: {
            text: 'some title',
            x: -20 //center
        },
        subtitle: {
            text: '',
            x: -20
        },
        xAxis: {
            type: 'datetime',
            dateTimeLabelFormats: { // don't display the dummy year
                month: '%e. %b',
                year: '%b'
            },
            categories: []
        },
        yAxis: {
            title: {
                text: 'L/min'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        plotOptions: {
            spline: {
                marker: {
                    enabled: true
                }
            }
        },

        tooltip: {
            formatter: function() {
                return '<b>'+ this.series.name +': '+
                this.y +'</b><br>'+ this.x;
            }
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'top',
            x: -10,
            y: 100,
            borderWidth: 0
        },
        series: []
    }

        $.getJSON("allreports/report.php", function(json) {

        options.xAxis.categories = json[0]['data'];
        options.series[0] = json[1];
        options.chart.renderTo = 'container';
        chart = new Highcharts.Chart(options);
    });

这是我的PHP页面:

<?php
    $con = mysql_connect("localhost","db-user","db-pw");

    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("db", $con);

    $sth = mysql_query("SELECT v1,v2 FROM table");
    $rows = array();
    $rows['name'] = 'v1';
    while($r = mysql_fetch_array($sth)) {
        $rows['data'][] = $r['v1'];
    }

    $result = array();
    array_push($result,$rows1);
    array_push($result,$rows);


    print json_encode($result, JSON_NUMERIC_CHECK);


    mysql_close($con);
?>

这输出以下内容:

[{"name":"medicinetime","data":["01,01,1970,01,00,00","01,01,1970,01,33,36","01,01,1970,01,33,36","01,01,1970,01,33,36"]},{"name":"units","data":[1,3,2,2]}]

但我的图表如下:

output

对我做错了什么的想法好吗?我需要做些什么才能让PHP / JS以一系列日期时间而不是字符串来接收这个数组?

谢谢!

1 个答案:

答案 0 :(得分:2)

你有两个问题。首先,您需要将日期组件值转换为实际的Javascript日期对象。您正在寻找系列数据的格式是一组X / Y对。使用您的数据看起来像这样:

SELECT id, company FROM table
WHERE id NOT IN (SELECT co_id FROM table WHERE co_id != ' ')
AND co_id = ' ';

尝试这样的事情:

[
  [Date.UTC(1970,1,1,1,0,0),1],
  [Date.UTC(1970,1,1,1,33,36),3],
  [Date.UTC(1970,1,1,1,33,36),2],
  [Date.UTC(1970,1,1,1,33,26),2]
]

有关系列数据有效格式的一些文档在此处:http://www.highcharts.com/docs/chart-concepts/series

其次,你有一些重复的X值,这会令人困惑......你会希望你的时间戳是唯一的,或者很难用它们制作折线图。< / p>