chart.js npm code example

Example 1: install package chart.js

npm install chart.js --save

Example 2: chart.js npm install

npm install chart.js --save

Example 3: chart.js

//in terminal: 
yarn add chart.js // or npm install chart.js if you use npm
//html file: 
<canvas id="my-chart" width="400" height="400"></canvas>
//js file
import Chart from 'chart.js'

const ctx = document.getElementById('my-chart').getContext('2d');
const chart = new Chart(ctx, {
  // The type of chart we want to create
  type: 'bar',

  // 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: {}
  });
};