Read:12 | API Chart.js + Canvas Element
API | Chart.js
Chart.js Article
Chart.js Documentation
-
Chart.js is a JS plugin API Library that uses HTML5’s Canvas element to draw charts -
Download (or grab a CDN link) Chart.js copy Chart.min.jsout of the unzipped folder and into the directory you’re working in - Connect the
Chart.min.jsfile in the HTMl<head>in a<script>tag - Utilize a
<canvas>element - For example, to draw a line chart you would use a mix of vanilla JS and special methods built in to the
<canvas>element -
Here’s an example from the referenced article:

- You’ll also write a JS object literal that will have key-value pairs for the chart labels and datasets (design and data)
- Here is a link to a Chart.js demo
API | Canvas
MDN doc links relating to the use of the <canvas> element
Key Takeaways:
- If
widthandheightaren’t set the default is 300x150 pixels - The script uses
getContext()to get the rendering context of the canvas to drawvar canvas = document.getElementById('tutorial'); var ctx = canvas.getContext('2d'); - The canvas grid uses x-y coordinates
- The two main kinds of methods that dictate any shape drawing will be rectangle or path methods
- The two most important properties for controlling color are
fillStyleandstrokeStyle - There are properties for styling lines and gradients and transparency etc.
- The two methods for rendering text are
fillTextandstrokeText - There are various other methods for styling text (e.g. font)