The first thing we need to do is download Chart.js. Copy the Chart.min.js out of the unzipped folder and into the directory you’ll be working in. Then create a new html page and import the script:
<!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>
<canvas id="buyers" width="600" height="400"></canvas>
<script>
var buyers = document.getElementById('buyers').getContext('2d');
new Chart(buyers).Line(buyerData);
</script>
var buyers
var buyerData = {
labels : ["first","second","third","fourth","Fifth","Sixth"],
datasets : [
{
fillColor : "rgba(172,194,132,0.4)",
strokeColor : "#ACC26D",
pointColor : "#fff",
pointStrokeColor : "#9DB86D",
data : [203,156,99,251,305,247]
}
]
}
<canvas id="countries" width="600" height="400"></canvas>
var countries= document.getElementById("countries").getContext("2d");
new Chart(countries).Pie(pieData, pieOptions);
var pieData = [
{
value: 20,
color:"#878BB6"
},
{
value : 40,
color : "#4ACAB4"
},
{
value : 10,
color : "#FF8153"
},
{
value : 30,
color : "#FFEA88"
}
];
var pieOptions = {
segmentShowStroke : false,
animateScale : true
}
Finally, let’s add a bar chart to our page. the syntax for the bar chart is very similar to the line chart we’ve already added.
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.
<canvas>
only supports two primitive shapes: rectangles and paths (lists of points connected by lines). All other shapes must be created by combining one or more paths. Luckily, we have an assortment of path drawing functions which make it possible to compose very complex shapes.
First let’s look at the rectangle. There are three functions that draw rectangles on the canvas:
fillRect(x, y, width, height)
strokeRect(x, y, width, height)
3.Clears the specified rectangular area, making it fully transparent.
clearRect(x, y, width, height)
function draw() {
var canvas = document.getElementById('canvas');
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
ctx.fillRect(25, 25, 100, 100);
ctx.clearRect(45, 45, 60, 60);
ctx.strokeRect(50, 50, 50, 50);
}
}
Now let’s look at paths. A path is a list of points, connected by segments of lines that can be of different shapes, curved or not, of different width and of different color. A path, or even a subpath, can be closed.
To make shapes using paths, we take some extra steps:
Here are the functions used to perform these steps:
beginPath()
:Creates a new path. Once created, future drawing commands are directed into the path and used to build the path up.
Path methods
:Methods to set different paths for objects.
closePath()
:Adds a straight line to the path, going to the start of the current sub-path.
stroke()
:Draws the shape by stroking its outline.
fill()
:Draws a solid shape by filling the path’s content area.
to apply a color to a shape, there are two important properties to use : fillStyle and strokeStyle.
fillStyle = color
:Sets the style used when filling shapes.strokeStyle = color
:Sets the style for shapes’ outlines.fillText(text, x, y [, maxWidth])
:Fills a given text at the given (x,y) position. Optionally with a maximum width to draw.strokeText(text, x, y [, maxWidth])
:Strokes a given text at the given (x,y) position. Optionally with a maximum width to draw.The text is filled using the current fillStyle.
function draw() {
var ctx = document.getElementById('canvas').getContext('2d');
ctx.font = '48px serif';
ctx.fillText('Hello world', 10, 50);
}
The text is filled using the current strokeStyle.
function draw() {
var ctx = document.getElementById('canvas').getContext('2d');
ctx.font = '48px serif';
ctx.strokeText('Hello world', 10, 50);
}
font property let you adjust the way the text gets displayed on the canvas:
font = value
:The current text style being used when drawing text. This string uses the same syntax as the CSS font property. The default font is 10px sans-serif.textAlign = value
:Text alignment setting. Possible values: start, end, left, right or center. The default value is start.textBaseline = value
:Baseline alignment setting. Possible values: top, hanging, middle, alphabetic, ideographic, bottom. The default value is alphabetic.direction = value
:Directionality. Possible values: ltr, rtl, inherit. The default value is inherit.