为什么xAxis没有显示日期?

时间:2015-05-05 18:07:43

标签: javascript highcharts

我有一个与HighCharts相关的问题。下面是我的代码,用于在y轴上实现具有整数值的高图,在x轴上实现日期。我从数据库中获取数据的日期和值。

x轴日期和时间未正确显示。在我的数据库中看起来像,2015-04-27 15:26:41.463。

var a = [];
$('#container').highcharts({
    chart: {
        type: 'spline',
        animation: Highcharts.svg, // don't animate in old IE
        marginRight: 10,
        events: {
            load: function () {
                // set up the updating of the chart each second
                var series = this.series[0];
                xAxis[0].setCategories(x);
            }
        }
    },
    title: {
        text: 'Live random data'
    },
    xAxis: {
        categories: a
    },
    yAxis: {
        title: {
            text: 'Value'
        },
        plotLines: [{
            value: 0,
            width: 1,
            color: '#808080'
        }]
    },
    tooltip: {
        formatter: function () {
            return '<b>' + this.series.name + '</b><br/>' + Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' + Highcharts.numberFormat(this.y, 1);
        }
    },
    legend: {
        enabled: false
    },
    exporting: {
        enabled: false
    },
    series: [{
        data: series
    }]
});

Chart

更新

这就是我现在所达到的目标

    .success(function (point) {

    for (i = 0; i < point.length; i++) {
        myDate[i] = point[i].date_time;
        value[i] = point[i].value_db;
    }

    for (j = 0; j < myDate.length; j++) {
        var temp = new Array(myDate[j], value[j]);
        mySeries[j] = temp;
    }
    DrawChart(mySeries, myDate, value);

})

function DrawChart(series, x, y) {
    //Fill chart
    $('#container').highcharts({
        chart: {
            type: 'spline',
            animation: Highcharts.svg, // don't animate in old IE
            marginRight: 10,
            events: {
                load: function () {
                    // set up the updating of the chart each second
                    var series = this.series[0];

                }
            }
        },
        title: {
            text: 'Live random data'
        },
        xAxis: {
            type: 'datetime',
            //tickPixelInterval: 150
        },
        yAxis: {
            title: {
                text: 'Value'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            formatter: function () {
                return '<b>' + this.series.name + '</b><br/>' + Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' + Highcharts.numberFormat(this.y, 2);
            }
        },
        legend: {
            enabled: false
        },
        exporting: {
            enabled: false
        },
        series: [{
            name: 'Random data',
            data: series
        }]
    });
}

Chart2

UPDATE2

Chart3

请帮助。

1 个答案:

答案 0 :(得分:1)

您必须将 myDate[i] = point[i].date_time; 更改为:

myDate[i] = parseInt((point[i].date_time).substring(6, 19));

这样它就会包含日期时间的 UTC 日期格式。

然后你必须像这样使用 xAxis.labels.formatter

xAxis: { 
    labels: { 
        formatter: function() { 
            var date = new Date(this.value); 
            return date.getFullYear() + "-" +
                   date.getMonth() + "-" +
                   date.getDate() + " " +
                   date.getHours() + ":" +
                   date.getMinutes() + ":" +
                   date.getSeconds(); 
        } 
    } 
}

告诉图表如何显示它的xAxis标签。

要在yAxis中显示更多标签,您可以使用:

yAxis: { 
    tickInterval: 100 // if you want the labels to show every 100 values 
}