Chartjs-Chartjs + moment() not displaying on django

0👍

Instead of using the bundled version of Chart.js, you can import Moment.js separately through an additional script tag and it should work.

Please take a look at your amended and runnable code.

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>

<div class="btcprices-chart" id="btcprices">
  <canvas id="myChart3"></canvas>
  <script>
    function newDate(days) {
      return moment().add(days, 'd');
    }
    var config = {
      type: 'line',
      data: {
        labels: [newDate(-4), newDate(-3), newDate(-2), newDate(-1), newDate(0)],
        datasets: [{
          label: "My First dataset",
          data: [10, 11, 12, 13, 14],
        }]
      },
      options: {
        scales: {
          xAxes: [{
            type: 'time',
            time: {
              displayFormats: {
                'millisecond': 'MMM DD',
                'second': 'MMM DD',
                'minute': 'MMM DD',
                'hour': 'MMM DD',
                'day': 'MMM DD',
                'week': 'MMM DD',
                'month': 'MMM DD',
                'quarter': 'MMM DD',
                'year': 'MMM DD',
              }
            }
          }],
        },
      }
    };

    var ctx = document.getElementById("myChart3").getContext("2d");
    new Chart(ctx, config);
  </script>
</div>

Leave a comment