score:0

Accepted answer

you should define the xaxis as a time cartesian axis. also you can use property displayformats according to display formats from chart.js documentation. see moment.js for the allowed format strings.

to do so, add the following to your xaxis inside the chart options.

xaxes: [{
  type: 'time',
  time: {
    displayformats: {
      minute: 'dd/mm hh:mm'
    }
  }, 
  ...

please note that chart.js uses moment.js for the functionality of the time axis. therefore you should use the bundled version of chart.js that includes moment.js in a single file.

const worldarray = [{ time: 1583593111036, rank: 665 }, { time: 1583593163490, rank: 665 }, { time: 1583593233697, rank: 665 }, { time: 1583594921644, rank: 666 }, { time: 1583595055581, rank: 665 }, { time: 1583595404573, rank: 665 }, { time: 1583596247042, rank: 665 }, { time: 1583598655437, rank: 666 }, { time: 1583600457443, rank: 667 }, { time: 1583601460665, rank: 668 }, { time: 1583602660570, rank: 665 }, { time: 1583604281776, rank: 668 }, { time: 1583605854175, rank: 669 }, { time: 1583606447275, rank: 668 }, { time: 1583607161055, rank: 667 }, { time: 1583607882093, rank: 666 }, { time: 1583608179707, rank: 667 }, { time: 1583609744777, rank: 668 }, { time: 1583609860652, rank: 669 }, { time: 1583610648775, rank: 670 }, { time: 1583613344874, rank: 671 }, { time: 1583613459587, rank: 672 }];
const worldarraytime = worldarray.map(o => o.time);
const worldarraydata = worldarray.map(o => o.rank);

var worldctx = document.getelementbyid('worldchart').getcontext('2d');
var worldchart = new chart(worldctx, {
  type: 'line',
  data: {
    labels: worldarraytime,
    datasets: [{
      data: worldarraydata,
      label: "world",
      bordercolor: "#3e95ce",
      backgroundcolor: "#3e95ce",
      fill: false
    }]
  },
  options: {
    responsive: true,
    title: {
      display: true,
      text: 'taric ranking'
    },
    scales: {
      xaxes: [{
        type: 'time',
        time: {
          tooltipformat: 'dd/mm hh:mm',
          displayformats: {
            minute: 'dd/mm hh:mm'
          }
        },         
        ticks: {
          maxtickslimit: worldarraydata.length
        }
      }],
      yaxes: [{
        ticks: {
          min: math.floor(math.min.apply(math, worldarraydata) - 2),
          max: math.floor(math.max.apply(math, worldarraydata) + 2)
        }
      }]
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.9.3/chart.bundle.min.js"></script>
<canvas id="worldchart" height="150"></canvas>

score:0

i decided to use chartist.js since it offers an easier implementation of moment.js

if you know how to answer the question, i'm still interested tho.


Related Query

More Query from same tag