score:7

Accepted answer

you need to sort the values before you pass them to labels / data.

example:

let timedata ={
  "2019-08-10t06:27": 12,
  "2019-08-10t06:26": 10,
  "2019-08-10t06:25": 8,
  "2019-08-10t06:24": 2,
  "2019-08-10t06:28": 1
};

let entries = object.entries(timedata);
entries.sort((a,b) => {
  let adate = moment(a[0]);
  let bdate = moment(b[0]);
  return adate.todate() - bdate.todate();
});

let labels = entries.map(e => e[0]);
let data = entries.map(e => e[1]);
console.log(labels);
console.log(data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

see jsfiddle for a working example.

if you want a zero line when there is no data, you need to add the zeroes to the data. example:

let timedata ={
  "2019-08-10t06:27": 12,
  "2019-08-10t06:26": 10,
  "2019-08-10t06:25": 8,
  "2019-08-10t06:24": 2,
  "2019-08-10t06:28": 1
};

let labels = [];
let data = [];

let date = moment('2019-08-10t06:00');
let enddate = moment('2019-08-10t07:00');
do {
  let datestr = date.format("yyyy-mm-ddthh:mm");
  labels.push(datestr);

  if(timedata.hasownproperty(datestr))
    data.push(timedata[datestr]);
  else
    data.push(0);

  date.add(1, 'minute');
} while(date.isbefore(enddate));

console.log(labels);
console.log(data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js">

see jsfiddle with zero line for an example with zero line


Related Query

More Query from same tag