DV Unit-3 PPT
DV Unit-3 PPT
• Example:
<canvas id=“myCanvas” width=“200” height=“100”>
</canvas>
• Example:
<canvas id=“myCanvas” width=“200” height=“100”
style=“ 1px solid black;” >
</canvas>
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(300, 150);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.lineTo(20, 100);
ctx.lineTo(70, 100);
ctx.stroke();
Basic Elements: Rectangle
Draw a filled rectangle: ctx.fillRect(x, y, width, height);
Tip: Use the fillStyle property to set a color, gradient, or pattern used to fill the
drawing.
ctx.fillText(text, x, y);
• The strokeText() method draws text (with no fill) on the canvas. The default color
of the text is black.
Tip: Use the font property to specify font and font size, and use the strokeStyle
property to render the text in another color/gradient.
context.srokeText(text, x, y);
Scaling
• The scale() method scales the current drawing, bigger or smaller.
• Note: If you scale a drawing, all future drawings will also be scaled.
• The positioning will also be scaled. If you scale(2,2); drawings will
be positioned twice as far from the left and top of the canvas as
you specify.
ctx.scale(scalewidth, scaleheight);
ScaleWidth and ScaleHeight (1=100%, 0.5=50%, 2=200%, etc.)
• Very simple and intuitive API – you can get started in minutes.
• Comes with Beautiful and Elegant looking themes.
• High performance.
• Works on all modern devices.
• CanvasJS is Standalone – does not depend on any other library.
• Fanatic Support from developers.
HTML5 Canvas Basics
• Download canvasjs.min.js from https://canvasjs.com/fdm/chart
• Extract the downloaded file and put the content in the same folder
as the html file.
Adding CanvasJS to Your Web Pages
• Include the canvasjs.min.js file inside the tag of your web page.
<head>
<script type="text/javascript" src = "canvasjs.min.js">
</script>
</head>
Column Chart with CanvasJS
• Column charts are rectangular bars with lengths proportional to
the values that they represent. A column chart is useful to compare
dataPoints in one or more dataSeries.
});
chart.render();
Column Chart with CanvasJS
var chart = new CanvasJS.Chart("chartContainer",{
title:{
text: “Title of Column Chart" //**Change the title here
},
});
chart.render();
Column Chart with CanvasJS
var chart = new CanvasJS.Chart("chartContainer",{
title:{
text: “Title of Column Chart" //**Change the title here
},
Other Code here
});
chart.render();
Column Chart with CanvasJS
var chart = new CanvasJS.Chart("chartContainer",{
title:{
text: “Title of Column Chart" //**Change the title here
},
data: [ …]
});
chart.render();
Column Chart with CanvasJS
var chart = new CanvasJS.Chart("chartContainer",{
title:{
text: “Title of Column Chart" //**Change the title here
},
data: [
]
});
chart.render();
Column Chart with CanvasJS
var chart = new CanvasJS.Chart("chartContainer",{
title:{
text: “Title of Column Chart" //**Change the title here
},
data: [
{
}
]
});
chart.render();
Column Chart with CanvasJS
var chart = new CanvasJS.Chart("chartContainer",{
title:{
text: “Title of Column Chart" //**Change the title here
},
data: [
{ type: "column",
dataPoints: [..]
}
]
});
chart.render();
Column Chart with CanvasJS
var chart = new CanvasJS.Chart("chartContainer",{
title:{
text: “Title of Column Chart" //**Change the title here
},
data: [
{ type: "column",
dataPoints: [
]
}
]
});
chart.render();
Column Chart with CanvasJS
dataPoints: [
{ x: 10, y: 71 },
{ x: 20, y: 55},
{ x: 30, y: 50 },
{ x: 40, y: 65 },
{ x: 50, y: 95 },
{ x: 60, y: 68 },
{ x: 70, y: 28 },
{ x: 80, y: 34 },
{ x: 90, y: 14}
]
Column chart Using CanvasJS
Putting all together…
… In a web page
A Simple Column Char t
<!DOCTYPE HTML>
<html>
<head>
<script type=“text/javascript” src=“canvasjs.min.js”>
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer",
{
title:{
text:“Simple Column Chart"
},
Charts using Canvas: A Simple Column Chart
data: [
{
type: "column", //change type to bar, line, area, pie, etc
dataPoints: [
{ x: 10, y: 71 },
{ x: 20, y: 55},
{ x: 30, y: 50 },
{ x: 40, y: 65 },
{ x: 50, y: 95 },
{ x: 60, y: 68 },
{ x: 70, y: 28 },
{ x: 80, y: 34 },
{ x: 90, y: 14} ] }
] });
Charts using Canvas: A Simple Column Chart
chart.render();
}
</script>
<script type="text/javascript" src="canvasjs.min.js"></script></head>
<body>
<div id="chartContainer" style="height: 300px; width: 100%;">
</div>
</body>
</html>
Output
Other charts: bar, line, area, pie
Just by changing type in the previous code, e.g.
type: "column“ //change type to bar, line, area, pie etc
We can draw different types of charts
Other charts: bar, line, area, pie
Just by changing type in the previous code, e.g.
type: "column“ //change type to bar, line, area, pie etc
We can draw different types of charts
Outline
Customizing HTML5 Canvas Chart
• Labels
• Titles
• Animation
• Theme and multiple series data
Column Chart Customization
• You can set data and all the attributes/properties of a
chart using chart option object.
𝑦 − 𝑦0 𝑦1 − 𝑦0
=
𝑥 − 𝑥0 𝑥1 − 𝑥0
𝑥 − 𝑥0
𝑦 = 𝑦0 + (𝑦1 −𝑦0 ) ∗
𝑥1 − 𝑥0
Linear Interpolation
𝑥−𝑥0
• is that it resolves to a value between 0
𝑥1 −𝑥0
and 1 depending on how far x is between x0
and x1.
𝑥 − 𝑥0
𝑦 = 𝑦0 + (𝑦1 −𝑦0 ) ∗
𝑥1 − 𝑥0
Linear Interpolation in Animation
𝐿𝑖𝑛𝑒𝑎𝑟 𝑖𝑛𝑡𝑒𝑟𝑝𝑜𝑙𝑎𝑡𝑖𝑜𝑛𝑠 𝑎𝑟𝑒 𝑢𝑠𝑒𝑓𝑢𝑙 𝑓𝑜𝑟 𝑐𝑜𝑛𝑛𝑒𝑐𝑡𝑖𝑛𝑔 𝑝𝑜𝑖𝑛𝑡𝑠 𝑤𝑖𝑡ℎ 𝑙𝑖𝑛𝑒 𝑠𝑒𝑔𝑚𝑒𝑛𝑡𝑠,
𝑏𝑢𝑡 𝑡ℎ𝑎𝑡′𝑠 𝑛𝑜𝑡 𝑎𝑙𝑙 𝑦𝑜𝑢 𝑢𝑠𝑒 𝑡ℎ𝑒𝑚 𝑓𝑜𝑟.
𝑥 − 𝑥0
𝑦 = 𝑦0 + (𝑦1 −𝑦0 ) ∗
𝑥1 − 𝑥0
• They are also very useful for moving/blending.
Linear Interpolation in Animation
• If you want a linear animated move between two values based on a time
value that varies between 0 and 1, you can express it with the following
equation:
requestAnimationFrame(draw);
setInterval(logic, 1000/60);
Method: setInterval()
The setInterval() method calls a function at specified intervals
(in milliseconds).
setInterval(function, milliseconds);
setInterval(logic,1000/60);
Animating Circle
Linear Interpolation
• If the two known points are given by the
coordinates (x0, y0) and (x1, y1), the linear
interpolant is the straight line between these
points.
𝑦 − 𝑦0 𝑦1 − 𝑦0
=
𝑥 − 𝑥0 𝑥1 − 𝑥0
𝑥 − 𝑥0
𝑦 = 𝑦0 + (𝑦1 −𝑦0 ) ∗
𝑥1 − 𝑥0
Linear Interpolation
𝑥−𝑥0
• is that it resolves to a value between 0
𝑥1 −𝑥0
and 1 depending on how far x is between x0
and x1.
𝑥 − 𝑥0
𝑦 = 𝑦0 + (𝑦1 −𝑦0 ) ∗
𝑥1 − 𝑥0
Linear Interpolation in Animation
𝐿𝑖𝑛𝑒𝑎𝑟 𝑖𝑛𝑡𝑒𝑟𝑝𝑜𝑙𝑎𝑡𝑖𝑜𝑛𝑠 𝑎𝑟𝑒 𝑢𝑠𝑒𝑓𝑢𝑙 𝑓𝑜𝑟 𝑐𝑜𝑛𝑛𝑒𝑐𝑡𝑖𝑛𝑔 𝑝𝑜𝑖𝑛𝑡𝑠 𝑤𝑖𝑡ℎ 𝑙𝑖𝑛𝑒 𝑠𝑒𝑔𝑚𝑒𝑛𝑡𝑠,
𝑏𝑢𝑡 𝑡ℎ𝑎𝑡′𝑠 𝑛𝑜𝑡 𝑎𝑙𝑙 𝑦𝑜𝑢 𝑢𝑠𝑒 𝑡ℎ𝑒𝑚 𝑓𝑜𝑟.
𝑥 − 𝑥0
𝑦 = 𝑦0 + (𝑦1 −𝑦0 ) ∗
𝑥1 − 𝑥0
• They are also very useful for moving/blending.
Linear Interpolation in Animation
• If you want a linear animated move between two values based on a time
value that varies between 0 and 1, you can express it with the following
equation:
requestAnimationFrame(draw);
setInterval(logic, 1000/60);
Method: setInterval()
The setInterval() method calls a function at specified intervals
(in milliseconds).
setInterval(function, milliseconds);
setInterval(logic,1000/60);
Animating Circle
Outline
• Google Chart API Basics
• A Basic Bar Chart
Google Charts Basics
• Google Charts is a pure JavaScript based charting
library to enhance web application by adding
interactive charting capability.
• Google charts provides variety of charts such as :
Bar Chart
Pie Chart
Line Chart
Spline Chart
Area Chart
• In order to implement Google Charts we need
knowledge of JavaScript.
Use/ Include: Google Charts
• To use Google Charts Google charts provides Content Delivery Network
(CDN)
• Using CDN
• Include the google charts JavaScript file in the HTML page using
following script.
• Syntax:
<head>
<script src = “https://www.gstatic.com/charts/loader,js”/>
</head>
Loading Using: Google Charts
• Once you have included the loader in your webpage, you can use it to load
the desired library packages by calling a load function.
Syntax:
google.charts.load('current', { packages: [ 'corechart‘ ] } );
• But after you load the library packages, you must wait for them to finish
being loaded before proceeding to use them.
• The way to wait is to set up a callback by calling a setOnLoadCallback
function.
Using: Google Charts
• Steps:
• 1. Include Google charts :
<script src = https://www.gstatic.com/charts/loader.js>
• 2. Load charts :
google.charts.load('current', { packages: [ 'corechart‘ ] } );
• 3. use setCallback:
google.charts.setOnLoadCallback(drawChart);
A Basic Bar Chart
• A bar chart or bar graph is chart or graph that
represent categorical data with rectangular bars.
• The bars can be plotted vertically or horizontally.
• A vertical bar chart is called a column chart.