-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
81 lines (62 loc) · 1.81 KB
/
Copy pathscript.js
File metadata and controls
81 lines (62 loc) · 1.81 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var chartBottomY = canvas.height;
var chartBarWidth = 30
var chartBarPadding = 10;
var bars = []; // Aray for current bars
var highest_value = canvas.height;
var scale = function() {
/* Automatically scales height of bars depending on highest value */
return canvas.height/highest_value;
}
function createBar(name, color) {
/* Creates a bar object and pushes it into the bars array */
bars.push({
name: name,
value: 200,
color: color
});
}
function updateBar(name, value) {
/* Updates a bar */
if (value > highest_value) {
highest_value = value;
scale();
}
for (var i in bars) {
var bar = bars[i];
if (bar.name == name) {
bar.value = value;
return true;
}
}
console.error(name + " does not exist! Makse sure to create bar before updating it!");
return false;
}
function animateBars() {
var x = chartBarPadding;
chartBarWidth = (canvas.width/bars.length) - chartBarPadding;
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (var i in bars) {
var name = bars[i].name;
var value = bars[i].value;
var color = bars[i].color;
// Draw names
ctx.fillStyle = "#343838";
ctx.font = "bold 10px Arial";
ctx.fillText(name, x, chartBottomY);
// Draw values
ctx.fillStyle = color;
ctx.font = "bold 10px Arial";
ctx.fillText(value, x, chartBottomY - 10);
ctx.fillStyle = color;
ctx.fillRect(x, chartBottomY - 20, chartBarWidth, -value * scale());
x += chartBarWidth + chartBarPadding;
}
}
function dummy() {
/* Returns a bar object with a new random value */
var randomName = bars[Math.floor(Math.random() * bars.length -1) + 1].name;
var randomValue = Math.floor(Math.random() * 1000) + 1;
return {name: randomName, value: randomValue};
}