Highstock with data.csv starts with 01.01.1970

2019-07-21 05:22发布

问题:

I've started experimenting with Highstock charts to visualize my photovoltaics data. The problem is, that the chart starts with 01-01-1970 00:00:00. It ignores the date and time in my csv-file.

This code is the same as some examples here, but it doesn't work.

My data.csv

2011-08-01 00:00:00,155
2011-08-02 00:00:00,156
2011-08-03 00:00:00,157
2011-08-03 00:06:00,160
2011-08-04 00:00:00,120

My index.html

$(document).ready(function() {

    var c = [];
    var d = [];
    var options = {
            chart: {
                renderTo: 'content',
                defaultSeriesType: 'line'
            },

            xAxis: {
                title: {
                    text: 'Datum'
                },
                type: 'datetime',
                categories: c
            },
            yAxis: {
                title: {
                    text: 'Stromertrag [wH]'
                }
            },
            series: [{
                data: d,
            }]
    };

    var jqxhr = $.get('../data/data.csv', function(data) {
        var lines = data.split('\n');
        $.each(lines, function(lineNo, line) {
            var items = line.split(',');
            c.push(items[0]);
            d.push(parseInt(items[1]));
        })
        var chart = new Highcharts.StockChart(options);

    });
});

回答1:

If you had regularly spaced data then you could make use of the pointStart and pointInterval properties of the data series, see this example from the Highcharts docs:

plotOptions: {
    series: {
        pointStart: Date.UTC(2010, 0, 1),
        pointInterval: 24 * 3600 * 1000 // one day
    }
},

http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/plotoptions/series-pointstart-datetime/

Since it looks like your interval is irregular you will need to convert your time stamp to unix/epoch time (milliseconds) and pass it along with your data.



回答2:

Highcharts use timestamps (time in miliseconds), so you can use pointStart/pointInterval or setting miliseconds as x values in your JSON or use Date.UTC() function to return correct data.

Data should be sorted by x ascending