-
Notifications
You must be signed in to change notification settings - Fork 14
/
example.js
174 lines (130 loc) · 5.65 KB
/
example.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
(function(){
//IE9+ http://youmightnotneedjquery.com/
function ready(fn) {
if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading"){
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
ready(setupPieChart);
function setupPieChart() {
var dimensions = knuthfisheryates2(['walking', 'programming', 'chess', 'eating', 'sleeping']);
var randomProportions = generateRandomProportions(dimensions.length, 0.05);
var proportions = dimensions.map(function(d,i) { return {
label: d,
proportion: randomProportions[i],
collapsed: false,
format: {
label: d.charAt(0).toUpperCase() + d.slice(1) // capitalise first letter
}
}});
var setup = {
canvas: document.getElementById('piechart'),
radius: 0.9,
collapsing: true,
proportions: proportions,
drawSegment: drawSegmentOutlineOnly,
onchange: onPieChartChange
};
var newPie = new DraggablePiechart(setup);
function drawSegmentOutlineOnly(context, piechart, centerX, centerY, radius, startingAngle, arcSize, format, collapsed) {
if (collapsed) { return; }
// Draw segment
context.save();
var endingAngle = startingAngle + arcSize;
context.beginPath();
context.moveTo(centerX, centerY);
context.arc(centerX, centerY, radius, startingAngle, endingAngle, false);
context.closePath();
context.fillStyle = '#f5f5f5';
context.fill();
context.stroke();
context.restore();
// Draw label on top
context.save();
var fontSize = Math.floor(context.canvas.height / 25);
var dx;
var dy;
context.font = fontSize + "pt Helvetica";
const absVal = Math.abs(startingAngle) * 2;
if (absVal > Math.PI) {
dx = 0 - (radius - fontSize);
dy = 5 - fontSize ;
context.translate(centerX, centerY);
context.rotate(Math.PI + startingAngle);
context.textAlign = "left";
console.log(format.label, "left", dx, dy, startingAngle, centerX, centerY);
} else {
dx = radius - fontSize;
dy = fontSize + 5;
context.translate(centerX, centerY);
context.rotate(startingAngle);
context.textAlign = "right";
}
context.fillText(format.label, dx, dy);
context.restore();
}
function onPieChartChange(piechart) {
var table = document.getElementById('proportions-table');
var percentages = piechart.getAllSliceSizePercentages();
var labelsRow = '<tr>';
var propsRow = '<tr>';
for(var i = 0; i < proportions.length; i += 1) {
labelsRow += '<th>' + proportions[i].format.label + '</th>';
var v = '<var>' + percentages[i].toFixed(0) + '%</var>';
var plus = '<div id="plu-' + dimensions[i] + '" class="adjust-button" data-i="' + i + '" data-d="-1">+</div>';
var minus = '<div id="min-' + dimensions[i] + '" class="adjust-button" data-i="' + i + '" data-d="1">−</div>';
propsRow += '<td>' + v + plus + minus + '</td>';
}
labelsRow += '</tr>';
propsRow += '</tr>';
table.innerHTML = labelsRow + propsRow;
var adjust = document.getElementsByClassName("adjust-button");
function adjustClick(e) {
var i = this.getAttribute('data-i');
var d = this.getAttribute('data-d');
piechart.moveAngle(i, (d * 0.1));
}
for (i = 0; i < adjust.length; i++) {
adjust[i].addEventListener('click', adjustClick);
}
}
/*
* Generates n proportions with a minimum percentage gap between them
*/
function generateRandomProportions(n, min) {
// n random numbers 0 - 1
var rnd = Array.apply(null, {length: n}).map(function(){ return Math.random(); });
// sum of numbers
var rndTotal = rnd.reduce(function(a, v) { return a + v; }, 0);
// get proportions, then make sure each propoertion is above min
return validateAndCorrectProportions(rnd.map(function(v) { return v / rndTotal; }), min);
function validateAndCorrectProportions(proportions, min) {
var sortedProportions = proportions.sort(function(a,b){return a - b});
for (var i = 0; i < sortedProportions.length; i += 1) {
if (sortedProportions[i] < min) {
var diff = min - sortedProportions[i];
sortedProportions[i] += diff;
sortedProportions[sortedProportions.length - 1] -= diff;
return validateAndCorrectProportions(sortedProportions, min);
}
}
return sortedProportions;
}
}
/*
* Array sorting algorithm
*/
function knuthfisheryates2(arr) {
var temp, j, i = arr.length;
while (--i) {
j = ~~(Math.random() * (i + 1));
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return arr;
}
}
})();