-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.htm
564 lines (463 loc) · 20.6 KB
/
index.htm
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
<!DOCTYPE html>
<html>
<head>
<title>3d pie chart</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="opayra thierry vergult candy pie 3d pie chart">
<meta name="viewport" content="width=device-width">
<meta name="mobile-web-app-capable" content="yes">
<script src="https://cdn.babylonjs.com/babylon.js"></script>
<script src="https://cdn.babylonjs.com/gui/babylon.gui.js"></script>
<script src="./candy-pie-babylon.js"></script>
<style>
/* global css variable */
:root {
--main-color: #009FC2;
--opposite-color: white;
}
.container {
display: grid;
grid-gap: 30px;
grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
grid-template-rows: repeat( 1, minmax(200px, 1fr));
font-family: QuicksandBook, Arial, sans-serif;
}
.card > .header {
color: white;
font-weight: bold;
background-color: var( --main-color);
padding: 20px;
}
.card > .header > .note-right {
float: right;
color: var( --main-color);
font-weight: bold;
background-color: white;
padding-left: 8px;
padding-right: 8px;
padding-top: 4px;
padding-bottom: 2px;
margin-top: -4px;
border-radius: 10px;
}
.card .article {
color: var( --main-color);
padding: 20px;
}
.card a {
color: var( --main-color);
font-weight: bold;
text-decoration: inherit;
}
.card .right {
text-align: right;
float: right;
}
pre {
background-color: rgb(255, 247, 229);
padding: 10px;
white-space: pre-line;
overflow: auto;
}
.candy-pie-title {
font-weight: bold;
font-size: large;
margin-top: 20px;
margin-bottom: 20px;
}
.candy-pie-babylon-canvas {
touch-action: none; /* Disables browsers from handling all pan and zoom interactions */
width: 100%;
outline: none; /* remove outline when clicking on canvas */
margin-top: 10px;
}
</style>
</head>
<body>
<div class='container'>
<div class='card' id='intro'>
<div class='header'>candy pie</div>
<div class='article'>
<h3>candy pie: a configurable, interactive 3d pie chart in your browser</h3>
<p>Let's break down that sentence ..</p>
<ul>
<li>candy: don't take it that seriously. Pie charts are not always loved.. One should not use 3d for charts.. Negative times negative equals positive?
<li>pie: yes indeed, a pie chart. See above.
<li>configurable: all options are described below. Curious which other options will prove to be interesting later on.
<li>interactive: the result is not a static image, but a 3d object one can rotate. Or click on a slice for more info via a hover effect.
<li>3d: standing on the shoulders of
<br>
<a href='https://www.babylonjs.com/' target="_blank">
<img src="./babylonidentity.svg" width="50%">
</a>
<li>browser: works in a modern browser, desktop & mobile.
<li>and <a href="https://github.com/thierryVergult/CandyPie" target="_blank">open source</a>.
</ul>
</div>
</div>
<div class='card' id='install'>
<div class='header'>usage</div>
<div class='article'>
<p>Load first the babylon.js code into your page, and add the candy-pie script.</p>
<pre>
<script src="https://cdn.babylonjs.com/babylon.js"></script>
<script src="https://cdn.babylonjs.com/gui/babylon.gui.js"></script>
<script src="https://thierryvergult.github.io/CandyPie/candy-pie-babylon.js"></script>
</pre>
<p>
Define a canvas element in your html page, and call one single javascript function to place the 3d pie chart on that canvas element.
All data and configuration options are defined in one single javascript object.
</p>
<pre>
<canvas id='candy-pie-canvas-id' class='candy-pie-babylon-canvas'></canvas>
let my_pie3d = {
htmlCanvasId: 'candy-pie-canvas-id',
slices: [ ..],
..
};
candyPie.babylon( my_pie3d);
</pre>
<p>Set the width, height or aspect ratio of the canvas element as desired.</p>
<p>some additional css can help</p>
<pre>
.candy-pie-babylon-canvas {
/* Disables all pan and zoom interactions */
touch-action: none;
/* remove outline when clicking on canvas */
outline: none;
}
</pre>
</div>
</div>
<div class='card' id='minimal'>
<div class='header'>minimal</div>
<div class='article'>
<div class='candy-pie-title'>default candy pie, without extra configuration</div>
<p>A pie chart with 3 equal slices. Each slice is specified by
<ul>
<li>a height number, which maps to the relative height of each slice
<li>a color, which can be specified in several formats (see later)
</ul>
</p>
<p>All other options are defaulted, except the id of the canvas where one wants the 3d pie chart to show up.</p>
<div>
<canvas id='candy-pie-id00' class='candy-pie-babylon-canvas'></canvas>
<script>
let pie3d_default = {
htmlCanvasId: 'candy-pie-id00',
slices: [
{ height: 100, color: 'indianred'},
{ height: 100, color: 'steelblue'},
{ height: 100, color: 'olive'}
]
};
candyPie.babylon( pie3d_default);
</script>
<pre>
let pie3d_default = {
htmlCanvasId: 'candy-pie-babylon-canvas-id-default',
slices: [
{ height: 100, color: 'indianred'},
{ height: 100, color: 'steelblue'},
{ height: 100, color: 'olive'}
]
};
// call the 3d pie function with the above settings
candyPie.babylon( pie3d_default);
</pre>
</div>
</div>
</div>
<div class='card' id='1stOptions'>
<div class='header'>some options</div>
<div class='article'>
<p>The same pie chart, but with some extra space between the slices, and a donut hole. And a lightgrey background.
Simply set the following options in the pie3d object:
<ul>
<li><i>spaceBetweenSlices</i>: default false, set to true in case
<li><i>innerRadiusPct</i>: default 0, set to a higher number to have a donut
<li><i>backgroundColor</i>: set the color of the background of the canvas
</ul>
</p>
<div>
<canvas id='candy-pie-id01' class='candy-pie-babylon-canvas'></canvas>
<script>
let pie3d_2 = {
htmlCanvasId: 'candy-pie-id01',
slices: [
{ height: 100, color: 'indianred'},
{ height: 100, color: 'steelblue'},
{ height: 100, color: 'olive'}
],
spaceBetweenSlices: true,
innerRadiusPct: 40,
backgroundColor: 'lightgrey'
};
candyPie.babylon( pie3d_2);
</script>
<pre>
let pie3d_2 = {
htmlCanvasId: 'candy-pie-id01',
slices: [
{ height: 100, color: 'indianred'},
{ height: 100, color: 'steelblue'},
{ height: 100, color: 'olive'}
],
spaceBetweenSlices: true,
innerRadiusPct: 40,
backgroundColor: 'lightgrey'
};
</pre>
</div>
</div>
</div>
<div class='card' id='options2'>
<div class='header'>some more options</div>
<div class='article'>
<p>The same pie chart, but now without vertical rotation, the initial camera angle position is a little lower, so the donut shows more flat:
<ul>
<li><i>allowVerticalRotation</i>: default true, here set to false. So now only horizontal rotation is possible.
<li><i>cameraDegreesY</i>: the camera position is set to 30 degrees, a little lower than the default of 45 degrees. Camera position starts at the ground plane.
</ul>
</p>
<div>
<canvas id='candy-pie-id03' class='candy-pie-babylon-canvas' style="aspect-ratio: 3/2;"></canvas>
<script>
let pie3d_3 = {
htmlCanvasId: 'candy-pie-id03',
slices: [
{ height: 100, color: 'indianred'},
{ height: 100, color: 'steelblue'},
{ height: 100, color: 'olive'}
],
spaceBetweenSlices: true,
innerRadiusPct: 40,
backgroundColor: 'lightgrey',
// 3 more options
cameraDegreesY: 30,
allowVerticalRotation: false
};
candyPie.babylon( pie3d_3);
</script>
<pre>
cameraDegreesY: 30,
allowVerticalRotation: false
</pre>
</div>
</div>
</div>
<div class='card' id='height-widt-arc'>
<div class='header'>variable height and width</div>
<div class='article'>
<p>
Until now, all pie charts had slices with the same size.
Changing the height of the slices changes the relative height.
And setting the arc percentage drives the "width" of each slice.
</p>
<p>The first slice of this example below takes 50%, the other 2 slices take each 25%.</p>
<p>The height increases from 100, to 125, to 150. This influences the relative height of the slices.</p>
<div>
<canvas id='candy-pie-id04' class='candy-pie-babylon-canvas' style="aspect-ratio: 3/2;"></canvas>
<script>
let pie3d_4 = {
htmlCanvasId: 'candy-pie-id04',
slices: [
{ height: 100, 'arcPct': 50, color: 'indianred'},
{ height: 125, 'arcPct': 25, color: 'steelblue'},
{ height: 150, 'arcPct': 25, color: 'olive'}
],
spaceBetweenSlices: true,
innerRadiusPct: 40,
backgroundColor: 'lightgrey',
cameraDegreesY: 30
};
candyPie.babylon( pie3d_4);
</script>
<pre>
slices: [
{ height: 100, arcPct: 50, ..},
{ height: 125, arcPct: 25, ..},
{ height: 150, arcPct: 25, ..}
],
</pre>
</div>
</div>
</div>
<div class='card' id='labels'>
<div class='header'>labels</div>
<div class='article'>
<p>A few options for the labels shown on the slice surface:</p>
<ul>
<li>sliceShowLabel: boolean, default false.
<li>labelFontFactor: default 1. In case a greater font is desired, increase here the font factor.
<li>labelExtraTopMargin: default 0. Increment this figure in case the labels should show up a little lower within each slice.
<li>labelColor: default empty. If so, the scripts uses black or white labels, depending of the color of the slice. If not, this color is used for all slices.
</ul>
</div>
</div>
<div class='card' id='hover-id'>
<div class='header'>hover</div>
<div class='article'>
<p>More info about a slice can be seen when clicking on a slice. That pops up a hover box, which will contain details in line with the configuration below:</p>
<ul>
<li>hoverShowLabel: boolean, default false. Adds the slice label to the hover box, when set.
<li>hoverShowHeight: boolean, default false. Adds the slice's height to the hover box, when set.
<li>hoverShowArcPct: boolean, default false. Adds the slice's arc percentage to the hover box, when set.
</ul>
</div>
</div>
<div class='card' id='all-in'>
<div class='header'>Variable pie chart with labels & hover.</div>
<div class='article'>
<p>Here a pie chart with 5 slices and labels. The slices have a variable height and a variable arc : slice 4 is smaller, applepie is larger than the others.</p>
<pre id='id-pick-color'>
slices: [
{ height: 10, arcPct: 20, label: 'one'},
{ height: 12, arcPct: 20, label: 'two'},
{ height: 14, arcPct: 20, label: 'three'},
{ height: 16, arcPct: 10, label: 'four'},
{ height: 18, arcPct: 30, label: 'apples'}
],
..
sliceShowLabel: true,
labelFontFactor: 1.5,
hoverShowLabel: true,
hoverShowHeight: true,
hoverShowArcPct: true,
</pre>
<p>ps: the background color of the pie chart is dynamically taken from the code snippet above. Can be handy when you want to reuse colors of your site in the pie.</p>
<div>
<canvas id='candy-pie-babylon-canvas-id' class='candy-pie-babylon-canvas' style="aspect-ratio: 16/9"></canvas>
<script>
let pie3d = {
htmlCanvasId: 'candy-pie-babylon-canvas-id',
slices: [
{ height: 10, arcPct: 1/5 * 100, color: 'indianred', label: 'one'},
{ height: 12, arcPct: 1/5 * 100, color: 'steelblue', label: 'two'},
{ height: 14, arcPct: 1/5 * 100, color: 'olive', label: 'three'},
{ height: 16, arcPct: 1/10 * 100, color: 'aliceblue', label: 'four'},
{ height: 18, arcPct: 3/10 * 100, color: '#F5DEB3', label: 'apples'} // color: wheat
],
spaceBetweenSlices: true,
innerRadiusPct: 40,
sliceShowLabel: true,
hoverShowLabel: true,
hoverShowHeight: true,
hoverShowArcPct: true,
labelFontFactor: 1.5,
backgroundColor: window.getComputedStyle( document.getElementById( 'id-pick-color')).backgroundColor,
verticalFactor: 1.6 // increment this number in case you want higher vertical slices
};
candyPie.babylon( pie3d);
</script>
</div>
</div>
</div>
<div class='card' id='vertical'>
<div class='header'>higher slices with a legend</div>
<div class='article'>
<p>This pie chart has also 5 slices, all with same width (arc), but the slices are all made higher than in the previous pie chart.
This is done via the <i>verticalFactor</i> configuration option, which stretches the slices vertically.</p>
<p>Add a legend, with an item for each slice with a label. Clicking on the item brings the slice in front.</p>
<div>
<canvas id='candy-pie-babylon-canvas-id2' class='candy-pie-babylon-canvas' style="width:80%; margin-left: 10%; aspect-ratio: 1/1"></canvas>
<script>
let pie3d2 = {
htmlCanvasId: 'candy-pie-babylon-canvas-id2',
slices: [
{ height: 100, arcPct: 1/5 * 100, color: 'indianred', label: 'one'},
{ height: 80, arcPct: 1/5 * 100, color: 'steelblue', label: 'two'},
{ height: 120, arcPct: 1/5 * 100, color: 'olive', label: 'three'},
{ height: 80, arcPct: 1/5 * 100, color: 'aliceblue', label: 'four'},
{ height: 120, arcPct: 1/5 * 100, color: '#F5DEB3', label: 'apples'} // color: wheat
],
spaceBetweenSlices: true,
innerRadiusPct: 40, // use 0 to get rid of the donut hole
showHeight: true,
sliceShowLabel: true,
labelFontFactor: 1.5,
backgroundColor: 'rgb(155, 102, 102)',
verticalFactor: 4,
addLegend: true
};
candyPie.babylon( pie3d2);
</script>
<pre>
verticalFactor: 4,
addLegend: true
</pre>
</div>
</div>
</div>
<div class='card' id='holidays'>
<div class='header'>holiday pie chart</div>
<div class='article'>
<p>This pie chart has 4 equal slices. Some label-configuration options are used to position & color the labels on the pie.</p>
<p>And the chart does a full rotation in 5 seconds at the beginning, slowing downn</p>
<div>
<canvas id='candy-pie-babylon-canvas-id3' class='candy-pie-babylon-canvas' style="width:75%; margin-left: 12%; aspect-ratio: 4/3"></canvas>
<script>
candyPie.babylon( {
htmlCanvasId: 'candy-pie-babylon-canvas-id3',
slices: [
{ height: 1, color: 'lightblue', label: 'swimming'},
{ height: 1, color: '#f8f6e1', label: 'ice cream'},
{ height: 1, color: 'lightblue', label: 'cocktails'},
{ height: 1, color: '#f8f6e1', label: 'relaxing'}
],
spaceBetweenSlices: true,
innerRadiusPct: 25, // use 0 to get rid of the donut hole
sliceShowLabel: true,
labelFontFactor: 2.5,
labelExtraTopMargin: 20,
labelColor: 'rgb(155, 102, 102)',
backgroundColor: 'rgb(196,238,216)', // green-ish
verticalFactor: 2,
allowVerticalRotation : false,
cameraDegreesY: 30,
secondsPerRotation: 5,
rotationSlowDown: true
});
</script>
<pre>
labelFontFactor: 2.5,
labelExtraTopMargin: 20,
labelColor: 'rgb(155, 102, 102)',
backgroundColor: 'rgb(196,238,216)', // green-ish
verticalFactor: 2,
allowVerticalRotation : false,
cameraDegreesY: 60,
secondsPerRotation: 5,
rotationSlowDown: true
</pre>
</div>
</div>
</div>
<div class='card' id='colors'>
<div class='header'>colors</div>
<div class='article'>
<p>The color of the slices and the canvas background are always set via a string.</p>
<p>Such string can be:</p>
<ul>
<li>a named color, like <span style="font-family:courier, monospace;">red</span>. <a href='https://www.w3schools.com/colors/colors_names.asp' target='_blank' >Some more color names.</a></li>
<li>a hexadecimal value, like <span style="font-family:courier, monospace;">#f8f6e1</span></li>
<li>a rgb function call, like <span style="font-family:courier, monospace;">rgb(196,238,216)</span></li>
<li>a javascript function returning such color string</li>
</ul>
</div>
</div>
<div class='card' id='playground'>
<div class='header'>playground</div>
<div class='article'>
<p>You can try out all candy pie configuration options in this <a href='https://thierryvergult.github.io/CandyPie/playground.htm' target='_blank'>online playground</a>.</p>
<p>This playground allows also to set the number of slices and all data for each slice.</p>
recently added :
<ul>
<li>renderOnce</li>
<li>extraStartDegrees</li>
</ul>
</div>
</div>
</div>
</body>
</html>