ChartJS Line Chart

Multiple Line Chart

by Leelenaleee

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.0.0-rc.6/chart.js"></script>
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
</body>

CSS

canvas { background-color : #eee;
}

JavaScript

var options = {
  type: 'bar',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1,
        hoverBackgroundColor: 'green',
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderWidth: 1,
        hoverBackgroundColor: 'red',
      }
    ]
  },
  options: {
    plugins: {
    	legend: {
      	onHover: (evt, item) => {
        	const activeElements = []
        	evt.chart.data.datasets[item.datasetIndex].data.forEach((el, i) => {
          	activeElements.push({
            	datasetIndex: item.datasetIndex,
              index: i,
            })
          })
          chart.setActiveElements(activeElements);
        }
      }
    }
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
const chart = new Chart(ctx, options);