[Chartjs]-Line graph with linear timescale in Chart.js

20👍

you need to add a date adapter as provided in the 3.x migration guide

(search in the page for "available adapters")

https://www.chartjs.org/docs/latest/migration/v3-migration.html

here is a working example

const data = {
  labels: [
    new Date(86400000), // Day 1
    new Date(2 * 86400000), // Day 2
    new Date(3 * 86400000), // Day 3
    new Date(4 * 86400000), // Day 4
    new Date(6 * 86400000), // Day 6
    new Date(7 * 86400000), // Day 7
    new Date(13 * 86400000), // Day 13
  ],
  datasets: [
    {
      label: "My First dataset",
      data: [1, 3, 4, 5, 6, 7, 8],
    },
  ],
};

let ctx = document.querySelector("canvas").getContext("2d");

let chart = new Chart(ctx, {
  type: "line",
  data: data,
  
  options: {
    scales: {
      x: {
        type: "time",
      }
    },
  },
});
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/moment@2.27.0"></script>
    <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-moment@0.1.1"></script>
    <title>repl.it</title>
  </head>
  <body>
    <canvas></canvas>
    <script src="new.ts"></script>
  </body>
</html>

Leave a comment