reading-notes

Read: 12 - Docs for the HTML Element & Chart.js

Charts are far better for displaying data visually than tables and have the added benefit that no one is ever going to press-gang them into use as a layout tool. They’re easier to look at and convey data quickly, but they’re not always easy to create.

A great way to get started with charts is with Chart.js, a JavaScript plugin that uses HTML5’s canvas element to draw the graph onto the page. It’s a well documented plugin that makes using all kinds of bar charts, line charts, pie charts and more, incredibly easy.

Chart.js

Setting up <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Chart.js demo</title> <script src='Chart.min.js'></script> </head> <body> </body> </html>

Drawing a line chart

Drawing a pie chart

Drawing a bar chart

The element

<canvas id="tutorial" width="150" height="150"></canvas> At first sight a <canvas> looks like the <img> element, with the only clear difference being that it doesn’t have the src and alt attributes. Indeed, the <canvas> element has only two attributes, width and height. These are both optional and can also be set using DOM properties. When no width and height attributes are specified, the canvas will initially be 300 pixels wide and 150 pixels high. The element can be sized arbitrarily by CSS, but during rendering the image is scaled to fit its layout size: if the CSS sizing doesn’t respect the ratio of the initial canvas, it will appear distorted.

Note: If your renderings seem distorted, try specifying your width and height attributes explicitly in the <canvas> attributes, and not using CSS.

Drawing shapes with canvas

Drawing rectangles

Drawing paths

Lines

Arcs

Applying styles and colors

Colors

Transparency

Line styles

Gradients

Patterns

Shadows

Drawing text

Styling text