neuromob
neuromob

Reputation: 70

Highchart dates do not work for some data series

I am encountering a problem with my chart created with HighCharts (StockChart), and more precisely with the dates which do not apply for certain data series and are set by default in 1970. I get the data from ajax request and I create my data series with the Highchart format as below:

data.forEach(element => {
    var d = new Date(Date.parse(element[0]));
    console.log("d : " + d);
    timestampData.push([d, element[1]]);
});
console.log(timestampData);
timestampData = timestampData.sort((a, b) => a[0] - b[0]);

chart.series[0].setData(timestampData, true);

And here is the result for both cases the date format is exactly the same and yet the date applies for one series but not for the other

Here the date works

Here the date is to 1970 but when can see the date result in console is to 2019

This is strange because nothing is done differently for the two series and the conversion to Date format is good for the two series

Upvotes: 0

Views: 67

Answers (1)

Dimitris Papazacharias
Dimitris Papazacharias

Reputation: 691

About the logged dates, it's hard to debug without knowing the value of element iterator, but Date.parse() can have ambiguous results, depending on the format of its argument.

In general, my advice is to use js millisecond timestamps instead of Date objects like so:

data.forEach(element => {
  var d = new Date(Date.parse(element[0]));
  timestampData.push([d.valueOf(), element[1]]);
});

It's more universal and highcharts responds fine to it.

Upvotes: 1

Related Questions