This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hexagon-chart.js
executable file
·82 lines (73 loc) · 2.97 KB
/
hexagon-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
var hexagon = {
bg_default_color: '#99ccff',
value_default_color: '#ffff99',
x_offset: 30,
ssin: function(degree) {
return Math.sin(degree * Math.PI / 180);
},
init: function(id, side_length, names, color) {
this.side_length = side_length;
this.hexagon = document.getElementById(id);
this.hexagon.width = this.side_length * 2 * this.ssin(60) + this.x_offset * 2;
this.hexagon.height = this.side_length * 2;
var width = this.hexagon.width;
var height = this.hexagon.height;
var S = this.x_offset;
if (typeof (color) === 'undefined') {
color = this.bg_default_color;
}
hexagoncontext = this.hexagon.getContext('2d');
hexagoncontext.fillStyle = color;
hexagoncontext.strokeStyle = color;
hexagoncontext.beginPath();
hexagoncontext.moveTo(width / 2, 0);
hexagoncontext.lineTo(width - S, height / 4);
hexagoncontext.lineTo(width - S, height * 3 / 4);
hexagoncontext.lineTo(width / 2, height);
hexagoncontext.lineTo(S, height * 3 / 4);
hexagoncontext.lineTo(S, height / 4);
hexagoncontext.lineTo(width / 2, 0);
hexagoncontext.stroke();
hexagoncontext.fill();
hexagoncontext.fillText(names[0], width / 2 + S / 2, 10);
hexagoncontext.fillText(names[1], width - S, height / 4);
hexagoncontext.fillText(names[2], width - S, height * 3 / 4);
hexagoncontext.fillText(names[3], width / 2 + S / 2, height);
hexagoncontext.fillText(names[4], 0, height * 3 / 4);
hexagoncontext.fillText(names[5], 0, height / 4);
},
draw: function(values, color) {
if (values.length < 6) {
return false;
}
for (i in values) {
values[i] = parseFloat(values[i]);
if (values[i] > 1) {
values[i] = 1;
}
if (values[i] < 0) {
values[i] = 0;
}
}
if (typeof (color) === 'undefined') {
color = this.value_default_color;
}
var width = this.hexagon.width;
var L = this.side_length;
var S = this.x_offset;
var V = values;
hexagoncontext = this.hexagon.getContext('2d');
hexagoncontext.fillStyle = color;
hexagoncontext.strokeStyle = color;
hexagoncontext.beginPath();
hexagoncontext.moveTo(width / 2, L * (1 - V[0]));
hexagoncontext.lineTo(this.ssin(60) * L * (1 + V[1]) + S, (1 - V[1] / 2) * L);
hexagoncontext.lineTo(this.ssin(60) * L * (1 + V[2]) + S, (1 + V[2] / 2) * L);
hexagoncontext.lineTo(width / 2, (1 + V[3]) * L);
hexagoncontext.lineTo(this.ssin(60) * L * (1 - V[4]) + S, (1 + V[4] / 2) * L);
hexagoncontext.lineTo(this.ssin(60) * L * (1 - V[5]) + S, (1 - V[5] / 2) * L);
hexagoncontext.lineTo(width / 2, L * (1 - V[0]));
hexagoncontext.stroke();
hexagoncontext.fill();
}
};