0

I have a highcharts graph consisting of 3 different plot lines. Y axis is a number format, X axis is a dateTime. The plots are being displayed properly but the labels on X show '00:00:00' and the tooltip shows the 1-st of January 1970.

Here's my code for initialising the chart:

Co2Chart = new Highcharts.Chart({
        chart: {
            renderTo: 'co2-graph',
            type: 'spline',
            animation: Highcharts.svg,
            marginRight: 10,
            events: {
                load: function () {
                    updateData();
                    setInterval(updateData, 5000);
                }
            }
        },
        title: {
            text: ''
        },
        xAxis: {
            type: 'datetime',
            tickPixelInterval: 150,
            labels: {
                formatter: function () {
                    return Highcharts.dateFormat('%H:%M:%S', this.value);
                }
            }
        },
        yAxis: {
            title: {
                text: 'CO2, ppm'
            },
            plotLines: [{
                value: 0,
                width: 1
            },{
                value: 1,
                width: 1
            },{
                value: 2,
                width: 1
            }]
        },
        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: true
        },
        plotOptions: {
            spline: {
                marker: {
                    enabled: false
                }
            }
        },
        exporting: {
            enabled: false
        },
        series: [{
            name: 'Master',
            data: []
        },
        {
            name: 'Slave 1',
            data: []
        },
        {
            name: 'Slave 2',
            data: []
        }]
    });

This one pushes data:

for(var i = 0; i < array.length; i++){
                var time = Date.parse(array[i]["dateTime"]);
                Co2Chart.series[0].addPoint(array[i]["masterCo2"], time);
                Co2Chart.series[1].addPoint(array[i]["slave1Co2"], time);
                Co2Chart.series[2].addPoint(array[i]["slave2Co2"], time);
}

DateTime string that is passed to the Date.parse looks like this 2016-03-28 22:47:49. And in milliseconds – 1459198069000

Any ideas why it's not showing?

graph example

mityakoval
  • 888
  • 3
  • 12
  • 26
  • You're sure "this.value" is what you expect it to be? – Erik Ahlswede Mar 28 '16 at 21:01
  • Don't use the Date constructor (or Date.parse, they are equivalent) to parse strings, it is inconsistent and unreliable. Parsing of a string in the format 2016-03-28 22:47:49 is entirely implementation dependent. – RobG Mar 28 '16 at 21:07
  • @RobG How do I turn a dateTime string into a highcharts acceptable format without parsing it using JS' Date? – mityakoval Mar 28 '16 at 22:05
  • There are various libraries that contain parsers (moment.js is one but it's probably overkill for this) or just write a [*small function*](http://stackoverflow.com/questions/9852224/parse-date-in-ie/9852450#9852450) yourself. A generic parser is pretty simple, see [*String date to javascript date: Parse date*](http://stackoverflow.com/questions/13230360/string-date-to-javascript-date-parse-date). There are also generic parsers and formatters, e.g. [*date-format.js*](https://github.com/barbir/js-date-format/blob/master/js/date-format.js) – RobG Mar 28 '16 at 23:04

1 Answers1

1

The problem is how you use addPoint method:

Co2Chart.series[0].addPoint(array[i]["masterCo2"], time);

Should be:

Co2Chart.series[0].addPoint([time, array[i]["masterCo2"]]);

Or:

Co2Chart.series[0].addPoint({
  y: array[i]["masterCo2"], 
  x: time
});
Paweł Fus
  • 44,795
  • 3
  • 61
  • 77