-
Notifications
You must be signed in to change notification settings - Fork 0
/
chart.js
36 lines (26 loc) · 895 Bytes
/
chart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// SELECT CHART ELEMENT
const chart = document.querySelector(".chart");
// CREATE CANVAS ELEMMENT
const canvas = document.createElement("canvas");
canvas.width = 50;
canvas.height = 50;
// APPEND CANVAS TO CHART ELEMENT
chart.appendChild(canvas);
// TO DRAW ON CANVAS, WE NEED TO GET CONTEXT OF CANVAS
const ctx = canvas.getContext("2d");
// CHANGE THE LINE WIDTH
ctx.lineWidth = 8;
// CIRCLE RADIUS
const R = 20;
function drawCircle(color, ratio, anticlockwise){
ctx.strokeStyle = color;
ctx.beginPath();
ctx.arc( canvas.width/2, canvas.height/2, R, 0, ratio * 2 * Math.PI, anticlockwise);
ctx.stroke();
}
function updateChart( income, outcome){
ctx.clearRect(0, 0, canvas.width, canvas.height);
let ratio = income / (income+outcome);
drawCircle("#FFFFFF", - ratio, true);
drawCircle("#F0624D", 1 - ratio, false);
}