33

Is there a bug with Chart.js? Every time I add any of the graphs at Chart.js to my website I get an error, but when I used the graph as stand-alone program it runs smoothly without errors. I am using HTML5.

   <html>
   <head>
      <meta charset="utf-8" />
      <title>Rice Consumption</title>
      <script src='Chart.min.js'></script>
    </head>
    <body>

      <canvas id="rice" width="600" height="400"></canvas>

      <script>
        var riceData = {
        labels : ["January","February","March","April","May","June"],
        datasets :
         [
            {
              fillColor : "rgba(172,194,132,0.4)",
              strokeColor : "#ACC26D",
              pointColor : "#fff",
              pointStrokeColor : "#9DB86D",
              data : [203000,15600,99000,25100,30500,24700]
            }
         ]
        }

          var rice = document.getElementById('rice').getContext('2d');
               new Chart(rice).Line(riceData);
    </script>
    </body>
    </html>

SOLVED: I just decoupled the script from the canvas element (made another file for the script to execute its function).

Updated HTML:

      <html>
      <head>
      <meta charset="utf-8" />
      <title>Rice Consumption</title>
      <script src='Chart.min.js'></script>
      </head>
      <body>
      <canvas id="rice" width="600" height="400"></canvas>
      <script src='Chart.min.js'></script>
      <script src='rice.js'></script>
      </body>
      </html>

New JavaScript file:

var riceData = {
    labels : ["January","February","March","April","May","June"],
    datasets : [
        {
            fillColor : "rgba(172,194,132,0.4)",
            strokeColor : "#ACC26D",
            pointColor : "#fff",
            pointStrokeColor : "#9DB86D",
            data : [203000,15600,99000,25100,30500,24700]
        }
    ]
}

var rice = document.getElementById('rice').getContext('2d');
new Chart(rice).Line(riceData);
7
  • Please add some details as to how exactly you linked ChartJS. Where’s the <script> tag? Commented Apr 22, 2015 at 0:34
  • Thanks! This seems weird, indeed… Commented Apr 22, 2015 at 0:59
  • Is “Chart.min.js” in the same folder as the HTML? Commented Apr 22, 2015 at 1:00
  • include your JS with the correct type. <script type="text/javascript" src="Chart.min.js"></script> What is displayed in your console ? any 404 on your Chart.min.js file? Commented Apr 22, 2015 at 1:13
  • @SamiX the type attribute on <script> tags is optional. It shouldn’t matter. Commented Apr 22, 2015 at 1:21

11 Answers 11

14

I was also getting same error. To fix this I moved the chart script into separate graph.js file.

Still I was getting same error. Which is fixed later when I put tag in following order before end of tag as shown below.

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
<script type="text/javascript" src="jscript/graph.js"></script>
</body>

The page look like this: enter image description here

var ctx = document.getElementById("myChart").getContext('2d');
  var myChart = new Chart(ctx, {
      type: 'bar',
      data: {
          labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
          datasets: [{
              label: '# of Votes',
              data: [12, 19, 3, 5, 2, 3],
              backgroundColor: [
                  'rgba(255, 99, 132, 0.2)',
                  'rgba(54, 162, 235, 0.2)',
                  'rgba(255, 206, 86, 0.2)',
                  'rgba(75, 192, 192, 0.2)',
                  'rgba(153, 102, 255, 0.2)',
                  'rgba(255, 159, 64, 0.2)'
              ],
              borderColor: [
                  'rgba(255,99,132,1)',
                  'rgba(54, 162, 235, 1)',
                  'rgba(255, 206, 86, 1)',
                  'rgba(75, 192, 192, 1)',
                  'rgba(153, 102, 255, 1)',
                  'rgba(255, 159, 64, 1)'
              ],
              borderWidth: 1
          }]
      },
      options: {
          scales: {
              yAxes: [{
                  ticks: {
                      beginAtZero:true
                  }
              }]
          }
      }
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
<!DOCTYPE html>

<canvas id="myChart"></canvas>

Sign up to request clarification or add additional context in comments.

Comments

10

here is a working jsfiddle of your code:
new Chart(rice).Line(riceData);
http://jsfiddle.net/mahmalsami/jqcthmyo/
So the problem is definitively coming from your external Chart.min.js inclusion

You may find a 404 on your js get. Please make sure you're linking to the correct js folder. (try accessing your localhost/Chart.min.js to see if you can access to your file)

3 Comments

I have used <script src="cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/…> but my bootstrap template is not working. <script src="build/js/custom.min.js"></script> , this tag seems to be disabled. Could you please suggest me any ideas for enable it?
@FRECEENAFRANCIS, example using your <script> jsfiddle.net/mahmalsami/mxre34b0/6
The fiddle is not working now for some reason
10

I got the same error. To solve the problem, you have to include the chart.js library correctly as follows:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js"></script>

1 Comment

For me it was a versioning problem - This version worked for me in .NET MVC <script src="cdn.jsdelivr.net/npm/[email protected]/dist/…>; the latest 4.4.3 version did not
1

1) I tried Chart.js downloaded from Chartjs.org. But, it's not working.

2) Try to this.

<script type="text/javascript" src="http://www.chartjs.org/assets/Chart.js">
</script>

Working good.

1 Comment

New way of adding it is via the CDN: <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.2/Chart.min.js"></script>
1

If you're seeing this intermittently on Ruby on Rails 5, like I was, the issue was to do with Turbolinks, which I disabled. Works great now!

Comments

1

This error shows you did not install the plugin that helps run the Chart.js library on the HTML page. if that script is not present, this error shows which version you used Chart.js then that version script is pasted in it.

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

It will help you solve the problem. It comes in many versions like

<script src="https://cdn.jsdelivr.net/npm/chart.js/Chart.min.js"></script>

1 Comment

The issue is, chart.min.js is actually trying to "import" stuff, while i'm using plain JS there's not "import" in my case ... so it didn't work. the version of libs/Chart.js/4.2.1/chart.umd.min.js or Chart.min.js works - because this is a compiled suitable JS file that doesn't import - just bundled with everything, so it will work.
0

Have run into this problem two times with Chartjs and solved both by using setTimeout to delay the call of the function that is causing the error.

1 Comment

While this may work, it's not that robust of a solution -- changes in how long various bits of code take could cause this to stop working.
0

I think it's a bit late to answer this question, but the way it worked for me, having the same problem, is to put the CDN after the code to graph, put it before, you can't call an object without declaring it.

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.min.js"></script>
<script>
    // Code to graphic...
    // Check Chart.js Doc (v3.9.1)
    // https://www.chartjs.org/docs/latest/getting-started/
<script>

Comments

0

Give a small timeout before chart creation.

$(document).ready(function(){
setTimeout(function(){
     var ctx = document.getElementById("myChart").getContext('2d');
    var myChart = new Chart(ctx, {
        type: 'pie',
        data: {
            labels: ["M", "T", "W", "T", "F", "S", "S"],
            datasets: [{
                    backgroundColor: [
                        "#2ecc71",
                        "#3498db",
                        "#95a5a6",
                        "#9b59b6",
                        "#f1c40f",
                        "#e74c3c",
                        "#34495e"
                    ],
                    data: [12, 19, 3, 17, 28, 24, 7]
                }]
        }
    });

},100); });

Comments

0

I got the same problem in my vue js application. Solved it by registering the LinearScale component

import { Chart as ChartJS, ArcElement, Tooltip, Legend, LinearScale } from 'chart.js'

ChartJS.register(ArcElement, Tooltip, Legend, LinearScale);

Comments

-3

This is very basic error which should be taken care off at very start of the document. Make sure you properly install chartjs library whether you want it like cdn/npm pacakge,etc. follow the documentation: https://www.chartjs.org/docs/latest/getting-started/installation.html

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.