-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathchart.js
More file actions
41 lines (32 loc) · 837 Bytes
/
chart.js
File metadata and controls
41 lines (32 loc) · 837 Bytes
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
37
38
39
40
41
// SELECT CHART ELEMENT
const chartEl = document.querySelector(".chart");
// CREATE CANVAS ELEMENT
const canvas = document.createElement("canvas");
canvas.width = 50;
canvas.height = 50;
chartEl.appendChild(canvas);
// TO DRAW ON CANVAS, WE NEED TO GET CONTEXT OF CANVAS
const ctx = canvas.getContext("2d");
// CHANGE 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 / (outcome + income);
drawCircle("#FFF", -ratio, true);
drawCircle("#F0624D", 1 - ratio, false);
}