-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathchart.js
84 lines (59 loc) · 2.7 KB
/
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
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
80
81
82
83
84
var Chart = function (canvas) {
var context = canvas.getContext('2d');
var data = [];
this.setData = function (powersByFrequency, curve) {
for (var i = 0; i < END_EDIT_FREQUENCY; i += 1) {
data[i] = [];
}
for (var frequency in powersByFrequency) {
if (frequency < END_EDIT_FREQUENCY) {
for (var i = 0; i < powersByFrequency[frequency].length; ++i) {
data[Math.floor(frequency)].push(powersByFrequency[frequency][i]);
}
}
}
for (var i = 0; i < data.length; i += 1) {
data[i] = averageArray(data[i]) * CHART_SCALE;
}
this.draw(curve);
};
this.draw = function (curve) {
context.fillStyle = CHART_BACKGROUND_COLOR;
context.fillRect(0, 0, canvas.width, canvas.height);
context.fillStyle = CHART_SECONDARY_COLOR;
context.beginPath();
context.moveTo(0, canvas.height);
for (var i = 0; i < data.length; i += 1) {
var x = (i / END_EDIT_FREQUENCY) * canvas.width;
var power = data[i];
context.lineTo(x + 0.5, canvas.height - power + 0.5); //left side of bar (also right side of previous bar)
context.lineTo(x + canvas.width / END_EDIT_FREQUENCY + 0.5, canvas.height - power + 0.5);
}
context.lineTo(canvas.width, canvas.height);
context.closePath();
context.fill();
context.fillStyle = CHART_PRIMARY_COLOR;
context.beginPath();
context.moveTo(0, canvas.height);
for (var i = 0; i < data.length; i += 1) {
var x = (i / END_EDIT_FREQUENCY) * canvas.width;
var scale = (curve.evaluate(CURVE_CANVAS_WIDTH * i / END_EDIT_FREQUENCY) / CURVE_CANVAS_HEIGHT) * CURVE_SCALE;
var power = data[i] * scale;
context.lineTo(x + 0.5, canvas.height - power + 0.5); //left side of bar (also right side of previous bar)
context.lineTo(x + canvas.width / END_EDIT_FREQUENCY + 0.5, canvas.height - power + 0.5);
}
context.lineTo(canvas.width, canvas.height);
context.closePath();
context.fill();
context.beginPath();
context.moveTo(0, canvas.height);
for (var i = 0; i < data.length; i += 1) {
var x = (i / END_EDIT_FREQUENCY) * canvas.width;
var power = data[i];
context.lineTo(x + 0.5, canvas.height - power + 0.5); //left side of bar (also right side of previous bar)
context.lineTo(x + canvas.width / END_EDIT_FREQUENCY + 0.5, canvas.height - power + 0.5);
}
context.strokeStyle = CHART_SECONDARY_COLOR;
context.stroke();
};
};