Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Charts js not showing

I am trying to use the charts.js to create a simple line graph. When I run the code below no chart appears. What am I doing wrong? I am following this tutorial http://www.chartjs.org/docs/latest/getting-started/

<html>
<head>
    <meta charset="utf-8"/>
    <title>Chart.js demo</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/0.2.0/Chart.min.js" type="text/javascript"></script>

</head>
<body>


    <h1>Chart.js Sample</h1>

    <canvas id="myChart" width="600" height="400"></canvas>
        <script>
        var ctx = document.getElementById('myChart').getContext('2d');
        var chart = new Chart(ctx, {
      // The type of chart we want to create
      type: 'line',

      // The data for our dataset
      data: {
          labels: ["January", "February", "March", "April", "May", "June", "July"],
          datasets: [{
              label: "My First dataset",
              backgroundColor: 'rgb(255, 99, 132)',
              borderColor: 'rgb(255, 99, 132)',
              data: [0, 10, 5, 2, 20, 30, 45],
          }]
      },

      // Configuration options go here
      options: {}
  });
    </script>
</body>
</html>
like image 909
amanda45 Avatar asked Jun 17 '17 20:06

amanda45


People also ask

Can I use chart JS in angular?

Data visualization methods can help group your data in one place and show it in a way that is easy to understand for all, especially in the form of charts. AngularJS provides integration with chart js, which is a popular medium for using visualization tools on your data.

Does chart js use canvas?

Let's get started using Chart. js! First, we need to have a canvas in our page. It's recommended to give the chart its own container for responsiveness.

Is Chart JS free to use?

js is a free, open-source JavaScript library for data visualization, which supports eight chart types: bar, line, area, pie (doughnut), bubble, radar, polar, and scatter.

Does chart JS work offline?

Is it possible to use chart. js for a desktop/mobile application using html that connects through an esp8266 access point or does it need a wifi connection? @marionboynton, CanvasJS is a standalone library that can work offline without any internet connection.


1 Answers

You're using version 0.2.0. Works fine if you use the version in the demo (2.4.0) or the newest version (2.6.0). The CDN link for that is https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js

<html>

<head>
  <meta charset="utf-8" />
  <title>Chart.js demo</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
</head>
<body>

  <h1>Chart.js Sample</h1>

  <canvas id="myChart" width="600" height="400"></canvas>
  <script>
    var ctx = document.getElementById("myChart").getContext("2d");
    var chart = new Chart(ctx, {
      // The type of chart we want to create
      type: "line",
    
      // The data for our dataset
      data: {
        labels: ["January", "February", "March", "April", "May", "June", "July"],
        datasets: [
          {
            label: "My First dataset",
            backgroundColor: "rgb(255, 99, 132)",
            borderColor: "rgb(255, 99, 132)",
            data: [0, 10, 5, 2, 20, 30, 45]
          }
        ]
      },
    
      // Configuration options go here
      options: {
        responsive:false,
        maintainAspectRatio: false
      }
    });
  </script>
</body>
</html>
like image 123
Michael Coker Avatar answered Oct 04 '22 22:10

Michael Coker