Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chartjs displays numbers not time

I'm trying to place time scale on the X axis. I have tried to write a simpler and reproducible code below. I can get it to run fine by removing the options block but it will display the time as just superficially-meaningless numbers to the viewer. How do I display time on the X axis? Am I missing some external source?

Note1: javascript file is a separate file, with the canvas/element "myChart" residing in the html file.


HTML:

<script src="https://www.chartjs.org/dist/2.8.0/Chart.min.js"></script>
<canvas  id="myChart" style="position: relative; width: 100%; max-height: 500px; max-width: 1000px;" ></canvas>

ERROR: (myScript.js is the file in question)

Chart.min.js:7 Uncaught Error: This method is not implemented: either no adapter can be found or an incomplete integration was provided.
    at ri.oi (Chart.min.js:7)
    at i.update (Chart.min.js:7)
    at Chart.min.js:7
    at Object.each (Chart.min.js:7)
    at Object.update (Chart.min.js:7)
    at ni.updateLayout (Chart.min.js:7)
    at ni.update (Chart.min.js:7)
    at ni.construct (Chart.min.js:7)
    at new ni (Chart.min.js:7)
    at myScript.js:191
oi @ Chart.min.js:7
update @ Chart.min.js:7
(anonymous) @ Chart.min.js:7
each @ Chart.min.js:7
update @ Chart.min.js:7
updateLayout @ Chart.min.js:7
update @ Chart.min.js:7
construct @ Chart.min.js:7
ni @ Chart.min.js:7
(anonymous) @ myScript.js:191

Javascript:

pt1 = new Date("2019-03-15T23:36:35.316Z");
pt2 = new Date("2019-03-17T23:32:11.114Z");

var ctx = document.getElementById('myChart').getContext('2d');
var myBubbleChart =new Chart(ctx, {
type: 'bubble',
data: {
  datasets: [
    {
      label: ["pt1"],
      data: [{
        x: pt1,
        y: 1,
        r: 10
      }]
    }, {
      label: ["pt2"],
      data: [{
        x: pt2,
        y: 1,
        r: 10
      }]
    }
  ]
},
options: {
    scales: {
        xAxes: [{
            type: 'time',
            distribution: 'series'
        }]
    }
}

});
like image 341
Kevin Preston Avatar asked Mar 16 '19 01:03

Kevin Preston


People also ask

What is utils in Chartjs?

The Utils file contains multiple helper functions that the chart. js sample pages use to generate charts.

Does Chartjs use canvas?

The last thing we need to prepare before we can start visualizing our data is to define an area in our HTML where we want to draw the graph. For Chart. js you do this by adding a canvas element, and setting width and height to define the proportions of your graph.

How do you hide a legend in Chartjs?

To remove legend on charts with Chart. js v2 and JavaScript, we can set the options. legend to false . const chart1 = new Chart(canvas, { type: "pie", data: data, options: { legend: { display: false, }, tooltips: { enabled: false, }, }, });


2 Answers

I had this same issue and attempted Kevin's solution, but it did not work. I found that it was necessary to place the moment script before the chartjs script and regardless of the versions I used, that solved the problem for me.

like image 150
Daniel Albus Avatar answered Sep 29 '22 01:09

Daniel Albus


You have to use the bundle version of Chartjs that already include Moment.js

    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/Chart.bundle.js"></script>
like image 33
Ygalbel Avatar answered Sep 29 '22 02:09

Ygalbel