-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdraggable-piechart.js
668 lines (521 loc) · 21.6 KB
/
draggable-piechart.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/**
* Created by james on 23/02/2017.
*/
(function () {
var extend = function (out) {
out = out || {};
for (var i = 1; i < arguments.length; i++) {
if (!arguments[i])
continue;
for (var key in arguments[i]) {
if (arguments[i].hasOwnProperty(key))
out[key] = arguments[i][key];
}
}
return out;
};
var DraggablePiechart = function (setup) {
var piechart = this;
setup = extend({}, this.defaults, setup);
this.canvas = setup.canvas;
this.context = setup.canvas.getContext("2d");
if (!this.context) {
console.log('Error: DraggablePiechart needs an html5 canvas.');
return;
}
if (setup.proportions) {
this.data = generateDataFromProportions(setup.proportions);
} else if (setup.data) {
this.data = setup.data;
}
this.draggedPie = null;
this.hoveredIndex = -1;
this.radius = setup.radius;
this.collapsing = setup.collapsing;
this.minAngle = setup.minAngle;
this.drawSegment = setup.drawSegment;
this.drawNode = setup.drawNode;
this.onchange = setup.onchange;
// Bind appropriate events
this.canvas.addEventListener('touchstart', function (e) {
touchStart(e);
e.preventDefault();
});
this.canvas.addEventListener('touchmove', function (e) {
touchMove(e);
e.preventDefault();
});
document.addEventListener('touchend', function (e) {
touchEnd(e);
});
this.canvas.addEventListener('mousedown', touchStart);
this.canvas.addEventListener('mousemove', touchMove);
document.addEventListener('mouseup', touchEnd);
this.draw();
function touchStart(event) {
piechart.draggedPie = piechart.getTarget(getMouseLocation(event));
if (piechart.draggedPie) {
piechart.hoveredIndex = piechart.draggedPie.index;
}
}
function touchEnd() {
if (piechart.draggedPie) {
piechart.draggedPie = null;
piechart.draw();
}
}
function touchMove(event) {
var dragLocation = getMouseLocation(event);
if (!piechart.draggedPie) {
var hoveredTarget = piechart.getTarget(dragLocation);
if (hoveredTarget) {
piechart.hoveredIndex = hoveredTarget.index;
piechart.draw();
} else if (piechart.hoveredIndex != -1) {
piechart.hoveredIndex = -1;
piechart.draw();
}
return;
}
var draggedPie = piechart.draggedPie;
var dx = dragLocation.x - draggedPie.centerX;
var dy = dragLocation.y - draggedPie.centerY;
// Get angle of grabbed target from centre of pie
var newAngle = Math.atan2(dy, dx) - draggedPie.angleOffset;
piechart.shiftSelectedAngle(newAngle);
piechart.draw();
}
function getMouseLocation(evt) {
var rect = piechart.canvas.getBoundingClientRect();
if (evt.clientX) {
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
}
} else {
return {
x: evt.targetTouches[0].clientX - rect.left,
y: evt.targetTouches[0].clientY - rect.top
}
}
}
/*
* Generates angle data from proportions (array of objects with proportion, format
*/
function generateDataFromProportions(proportions) {
// sum of proportions
var total = proportions.reduce(function (a, v) {
return a + v.proportion;
}, 0);
// begin at 0
var currentAngle = 0;
// use the proportions to reconstruct angles
return proportions.map(function (v, i) {
var arcSize = TAU * v.proportion / total;
var data = {
angle: currentAngle,
format: v.format,
collapsed: arcSize <= 0
};
currentAngle = normaliseAngle(currentAngle + arcSize);
return data;
});
}
};
/*
* Move angle specified by index: i, by amount: angle in rads
*/
DraggablePiechart.prototype.moveAngle = function (i, amount) {
if (this.data[i].collapsed && amount < 0) {
this.setCollapsed(i, false);
return;
}
var geometry = this.getGeometry();
this.draggedPie = {
index: i,
angleOffset: 0,
centerX: geometry.centerX,
centerY: geometry.centerY,
startingAngles: this.data.map(function (v) {
return v.angle;
}),
collapsed: this.data.map(function (v) {
return v.collapsed;
}),
angleDragDistance: 0
};
this.shiftSelectedAngle(this.data[i].angle + amount);
this.draggedPie = null;
this.draw();
};
/*
* Gets percentage of indexed slice
*/
DraggablePiechart.prototype.getSliceSizePercentage = function (index) {
var visibleSegments = this.getVisibleSegments();
for (var i = 0; i < visibleSegments.length; i += 1) {
if (visibleSegments[i].index == index) {
return 100 * visibleSegments[i].arcSize / TAU;
}
}
return 0;
};
/*
* Gets all percentages for each slice
*/
DraggablePiechart.prototype.getAllSliceSizePercentages = function () {
var visibleSegments = this.getVisibleSegments();
var percentages = [];
for (var i = 0; i < this.data.length; i += 1) {
if (this.data[i].collapsed) {
percentages[i] = 0;
} else {
for (var j = 0; j < visibleSegments.length; j += 1) {
if (visibleSegments[j].index == i) {
percentages[i] = 100 * visibleSegments[j].arcSize / TAU;
}
}
}
}
return percentages;
};
/*
* Gets the geometry of the pie chart in the canvas
*/
DraggablePiechart.prototype.getGeometry = function () {
var centerX = Math.floor(this.canvas.width / 2);
var centerY = Math.floor(this.canvas.height / 2);
return {
centerX: centerX,
centerY: centerY,
radius: Math.min(centerX, centerY) * this.radius
}
};
/*
* Returns a segment to drag if given a close enough location
*/
DraggablePiechart.prototype.getTarget = function (targetLocation) {
var geometry = this.getGeometry();
var startingAngles = [];
var collapsed = [];
var closest = {
index: -1,
distance: 9999999,
angle: null
};
for (var i = 0; i < this.data.length; i += 1) {
startingAngles.push(this.data[i].angle);
collapsed.push(this.data[i].collapsed);
if (this.data[i].collapsed) {
continue;
}
var dx = targetLocation.x - geometry.centerX;
var dy = targetLocation.y - geometry.centerY;
var trueGrabbedAngle = Math.atan2(dy, dx);
var distance = Math.abs(smallestSignedAngleBetween(trueGrabbedAngle, this.data[i].angle));
if (distance < closest.distance) {
closest.index = i;
closest.distance = distance;
closest.angle = trueGrabbedAngle;
}
}
if (closest.distance < 0.1) {
return {
index: closest.index,
angleOffset: smallestSignedAngleBetween(closest.angle, startingAngles[closest.index]),
centerX: geometry.centerX,
centerY: geometry.centerY,
startingAngles: startingAngles,
collapsed: collapsed,
angleDragDistance: 0
}
} else {
return null;
}
};
/*
* Sets segments collapsed or uncollapsed
*/
DraggablePiechart.prototype.setCollapsed = function (index, collapsed) {
// Flag to set position of previously collapsed to new location
var setNewPos = this.data[index].collapsed && !collapsed;
this.data[index].collapsed = collapsed;
var visibleSegments = this.getVisibleSegments();
// Shift other segments along to make space if necessary
for (var i = 0; i < visibleSegments.length; i += 1) {
// Start at this segment
if (visibleSegments[i].index == index) {
//Set new position
if (setNewPos) {
var nextSegment = visibleSegments[mod(i + 1, visibleSegments.length)];
this.data[index].angle = nextSegment.angle - this.minAngle;
}
for (var j = 0; j < (visibleSegments.length - 1); j += 1) {
var currentSegment = visibleSegments[mod(1 + i - j, visibleSegments.length)];
var nextAlongSegment = visibleSegments[mod(i - j, visibleSegments.length)];
var angleBetween = Math.abs(smallestSignedAngleBetween(this.data[currentSegment.index].angle, this.data[nextAlongSegment.index].angle));
if (angleBetween < this.minAngle) {
this.data[nextAlongSegment.index].angle = normaliseAngle(this.data[currentSegment.index].angle - this.minAngle);
}
}
break;
}
}
this.draw();
};
/*
* Returns visible segments
*/
DraggablePiechart.prototype.getVisibleSegments = function () {
var piechart = this;
// Collect data for visible segments
var visibleSegments = [];
for (var i = 0; i < piechart.data.length; i += 1) {
if (!piechart.data[i].collapsed) {
var startingAngle = piechart.data[i].angle;
// Get arcSize
var foundNextAngle = false;
for (var j = 1; j < piechart.data.length; j += 1) {
var nextAngleIndex = (i + j) % piechart.data.length;
if (!piechart.data[nextAngleIndex].collapsed) {
var arcSize = piechart.data[nextAngleIndex].angle - startingAngle;
if (arcSize <= 0) {
arcSize += TAU;
}
visibleSegments.push({
arcSize: arcSize,
angle: startingAngle,
format: piechart.data[i].format,
index: i
});
foundNextAngle = true;
break;
}
}
// Only one segment
if (!foundNextAngle) {
visibleSegments.push({
arcSize: TAU,
angle: startingAngle,
format: piechart.data[i].format,
index: i
});
break;
}
}
}
return visibleSegments;
};
/*
* Returns invisible segments
*/
DraggablePiechart.prototype.getInvisibleSegments = function () {
var piechart = this;
// Collect data for visible segments
var invisibleSegments = [];
for (var i = 0; i < piechart.data.length; i += 1) {
if (piechart.data[i].collapsed) {
invisibleSegments.push({
index: i,
format: piechart.data[i].format
})
}
}
return invisibleSegments;
};
/*
* Draws the piechart
*/
DraggablePiechart.prototype.draw = function () {
var piechart = this;
var context = piechart.context;
var canvas = piechart.canvas;
context.clearRect(0, 0, canvas.width, canvas.height);
var geometry = this.getGeometry();
var visibleSegments = this.getVisibleSegments();
// Flags to get arc sizes and index of largest arc, for drawing order
var largestArcSize = 0;
var indexLargestArcSize = -1;
// Get the largeset arcsize
for (var i = 0; i < visibleSegments.length; i += 1) {
if (visibleSegments[i].arcSize > largestArcSize) {
largestArcSize = visibleSegments[i].arcSize;
indexLargestArcSize = i;
}
}
// Need to draw in correct order
for (i = 0; i < visibleSegments.length; i += 1) {
// Start with one *after* largest
var index = mod(i + indexLargestArcSize + 1, visibleSegments.length);
piechart.drawSegment(context, piechart, geometry.centerX, geometry.centerY, geometry.radius, visibleSegments[index].angle, visibleSegments[index].arcSize, visibleSegments[index].format, false);
}
// Now draw invisible segments
var invisibleSegments = this.getInvisibleSegments();
for (i = 0; i < invisibleSegments.length; i += 1) {
piechart.drawSegment(context, piechart, geometry.centerX, geometry.centerY, geometry.radius, 0, 0, invisibleSegments[i].format, true);
}
// Finally draw drag nodes on top (order not important)
for (i = 0; i < visibleSegments.length; i += 1) {
var location = polarToCartesian(visibleSegments[i].angle, geometry.radius);
piechart.drawNode(context, piechart, location.x, location.y, geometry.centerX, geometry.centerY, i == piechart.hoveredIndex);
}
piechart.onchange(piechart);
};
/*
* *INTERNAL USE ONLY*
* Moves the selected angle to a new angle
*/
DraggablePiechart.prototype.shiftSelectedAngle = function (newAngle) {
var piechart = this;
if (!piechart.draggedPie) {
return;
}
var draggedPie = piechart.draggedPie;
// Get starting angle of the target
var startingAngle = draggedPie.startingAngles[draggedPie.index];
// Get previous angle of the target
var previousAngle = piechart.data[draggedPie.index].angle;
// Get diff from grabbed target start (as -pi to +pi)
var angleDragDistance = smallestSignedAngleBetween(newAngle, startingAngle);
// Get previous diff
var previousDragDistance = draggedPie.angleDragDistance;
// Determines whether we go clockwise or anticlockwise
var rotationDirection = previousDragDistance > 0 ? 1 : -1;
// Reverse the direction if we have done over 180 in either direction
var sameDirection = previousDragDistance > 0 == angleDragDistance > 0;
var greaterThanHalf = Math.abs(previousDragDistance - angleDragDistance) > Math.PI;
if (greaterThanHalf && !sameDirection) {
// Reverse the angle
angleDragDistance = (TAU - Math.abs(angleDragDistance)) * rotationDirection;
} else {
rotationDirection = angleDragDistance > 0 ? 1 : -1;
}
draggedPie.angleDragDistance = angleDragDistance;
// Set the new angle:
piechart.data[draggedPie.index].angle = normaliseAngle(startingAngle + angleDragDistance);
// Reset Collapse
piechart.data[draggedPie.index].collapsed = draggedPie.collapsed[draggedPie.index];
// Search other angles
var shifting = true;
var collapsed = false;
var minAngle = piechart.minAngle;
var numberOfAnglesShifted = 0;
for (var i = 1; i < piechart.data.length; i += 1) {
// Index to test each slice in order
var index = mod(parseInt(draggedPie.index) + (i * rotationDirection), piechart.data.length);
// Get angle from target start to this angle
var startingAngleToNonDragged = smallestSignedAngleBetween(draggedPie.startingAngles[index], startingAngle);
// If angle is in the wrong direction then it should actually be OVER 180
if (startingAngleToNonDragged * rotationDirection < 0) {
startingAngleToNonDragged = ((startingAngleToNonDragged * rotationDirection) + TAU) * rotationDirection;
}
if (piechart.collapsing) {
// *Collapsing behaviour* when smallest angle encountered
// Reset collapse
piechart.data[index].collapsed = draggedPie.collapsed[index];
var checkForSnap = !collapsed && !piechart.data[index].collapsed;
// Snap node to collapse, and prevent going any further
if (checkForSnap && startingAngleToNonDragged > 0 && angleDragDistance > (startingAngleToNonDragged - minAngle)) {
piechart.data[draggedPie.index].angle = piechart.data[index].angle;
piechart.data[draggedPie.index].collapsed = true;
collapsed = true;
} else if (checkForSnap && startingAngleToNonDragged < 0 && angleDragDistance < (startingAngleToNonDragged + minAngle)) {
piechart.data[draggedPie.index].angle = piechart.data[index].angle;
piechart.data[index].collapsed = true;
collapsed = true;
} else {
piechart.data[index].angle = draggedPie.startingAngles[index];
}
} else {
// *Shifting behaviour* when smallest angle encountered
// Shift all other angles along
var shift = (numberOfAnglesShifted + 1) * minAngle;
if (shifting && startingAngleToNonDragged > 0 && angleDragDistance > (startingAngleToNonDragged - shift)) {
piechart.data[index].angle = normaliseAngle(draggedPie.startingAngles[index] + (angleDragDistance - startingAngleToNonDragged) + shift);
numberOfAnglesShifted += 1;
} else if (shifting && startingAngleToNonDragged < 0 && angleDragDistance < (startingAngleToNonDragged + shift)) {
piechart.data[index].angle = normaliseAngle(draggedPie.startingAngles[index] - (startingAngleToNonDragged - angleDragDistance) - shift);
numberOfAnglesShifted += 1;
} else {
shifting = false;
piechart.data[index].angle = draggedPie.startingAngles[index];
}
}
//console.log(JSON.stringify(piechart.data));
}
};
DraggablePiechart.prototype.defaults = {
onchange: function (piechart) {
},
radius: 0.9,
data: [
{angle: -2, format: {color: "#2665da", label: 'Walking'}, collapsed: false},
{angle: -1, format: {color: "#6dd020", label: 'Programming'}, collapsed: false},
{angle: 0, format: {color: "#f9df18", label: 'Chess'}, collapsed: false},
{angle: 1, format: {color: "#d42a00", label: 'Eating'}, collapsed: false},
{angle: 2, format: {color: "#e96400", label: 'Sleeping'}, collapsed: false}],
collapsing: false,
minAngle: 0.1,
drawSegment: function (context, piechart, centerX, centerY, radius, startingAngle, arcSize, format, collapsed) {
if (collapsed) {
return;
}
// Draw coloured 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 = format.color;
context.fill();
context.restore();
// Draw label on top
context.save();
context.translate(centerX, centerY);
context.rotate(startingAngle);
var fontSize = Math.floor(context.canvas.height / 25);
var dx = radius - fontSize;
var dy = centerY / 10;
context.textAlign = "right";
context.font = fontSize + "pt Helvetica";
context.fillText(format.label, dx, dy);
context.restore();
},
drawNode: function (context, piechart, x, y, centerX, centerY, hover) {
context.save();
context.translate(centerX, centerY);
context.fillStyle = '#DDDDDD';
var rad = hover ? 7 : 5;
context.beginPath();
context.arc(x, y, rad, 0, TAU, true);
context.fill();
context.stroke();
context.restore();
}
};
window.DraggablePiechart = DraggablePiechart;
/*
* Utilities + Constants
*/
var TAU = Math.PI * 2;
function degreesToRadians(degrees) {
return (degrees * Math.PI) / 180;
}
function smallestSignedAngleBetween(target, source) {
return Math.atan2(Math.sin(target - source), Math.cos(target - source));
}
function mod(n, m) {
return ((n % m) + m) % m;
}
function normaliseAngle(angle) {
return mod(angle + Math.PI, TAU) - Math.PI;
}
function polarToCartesian(angle, radius) {
return {
x: radius * Math.cos(angle),
y: radius * Math.sin(angle)
}
}
})();