Home > Back-end >  Highchart epochtime string seen as NaN in html <script>
Highchart epochtime string seen as NaN in html <script>

Time:08-08

If been trying to get the code below working for a while with no success.

If Use the code below the in my html , var x returns as NaN instead of 1659940369.

if I use : var x = (new Date()).getTime(); x returns as the epoch time but I want to be able to get my epoch time from string so I will be able to display historical data later on (data logger)

   //Plot random values for chart
   function plotTemperature(jsonValue) {

     var keys = Object.keys(jsonValue);
     console.log(keys);
     console.log(keys.length);

     for (var i = 0; i < keys.length -1; i  ){
       const key = keys[i 1];
      var x = parseInt(jsonValue[0]);
   //    var x = (new Date()).getTime();
        console.log(x);
        document.getElementById("xvalue").innerHTML = x;
        var y = Number(jsonValue[key]);
    console.log(y);

        if(chartT.series[i].data.length > 1440) {
      chartT.series[i].addPoint([x, y], true, true, true);
        } else {
          chartT.series[i].addPoint([x, y], true, false, true);
        }

      }
    }

Here are a few examples of the json values {"epoch":"1659940369","random1":"0","random2":"95","random3":"7","random4":"21"} {"epoch":"1659940371","random1":"10","random2":"32","random3":"0","random4":"49"} {"epoch":"1659940373","random1":"13","random2":"7","random3":"9","random4":"97"} {"epoch":"1659940375","random1":"20","random2":"90","random3":"32","random4":"16"}

Please let me know if someone needs the full code, I have tried to keep the post as short as possible.

thanks/regards Francois

CodePudding user response:

You need to use timestamp as number in milliseconds:

// convert string timestamp in seconds to number timestamp in milliseconds
const x = jsonValue[i].epoch * 1000;

Live demo: http://jsfiddle.net/BlackLabel/pd7tnawz/

API Reference: https://api.highcharts.com/highcharts/series.line.data

  • Related