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.
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
<canvas id="buyers" width="600" height="400"></canvas>
<script>
var buyers = document.getElementById('buyers').getContext('2d');
new Chart(buyers).Line(buyerData);
</script>
var buyerData = {
labels : ["January","February","March","April","May","June"],
datasets : [
{
fillColor : "rgba(172,194,132,0.4)",
strokeColor : "#ACC26D",
pointColor : "#fff",
Drawing a pie chart
<canvas id="countries" width="600" height="400"></canvas>
Drawing a bar chart
<canvas id="income" width="600" height="400"></canvas>
<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 rectangles
Drawing paths
Lines
Arcs
Colors
Transparency
Line styles
Gradients
Patterns
Shadows
Drawing text
Styling text