-
Notifications
You must be signed in to change notification settings - Fork 45
/
highcharts-3d.src.js
4426 lines (4036 loc) · 162 KB
/
highcharts-3d.src.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license Highcharts JS v6.0.3 (2017-11-14)
*
* 3D features for Highcharts JS
*
* @license: www.highcharts.com/license
*/
'use strict';
(function(factory) {
if (typeof module === 'object' && module.exports) {
module.exports = factory;
} else {
factory(Highcharts);
}
}(function(Highcharts) {
(function(H) {
/**
* (c) 2010-2017 Torstein Honsi
*
* License: www.highcharts.com/license
*/
/**
* Mathematical Functionility
*/
var deg2rad = H.deg2rad,
pick = H.pick;
/**
* Apply 3-D rotation
* Euler Angles (XYZ): cosA = cos(Alfa|Roll), cosB = cos(Beta|Pitch), cosG = cos(Gamma|Yaw)
*
* Composite rotation:
* | cosB * cosG | cosB * sinG | -sinB |
* | sinA * sinB * cosG - cosA * sinG | sinA * sinB * sinG + cosA * cosG | sinA * cosB |
* | cosA * sinB * cosG + sinA * sinG | cosA * sinB * sinG - sinA * cosG | cosA * cosB |
*
* Now, Gamma/Yaw is not used (angle=0), so we assume cosG = 1 and sinG = 0, so we get:
* | cosB | 0 | - sinB |
* | sinA * sinB | cosA | sinA * cosB |
* | cosA * sinB | - sinA | cosA * cosB |
*
* But in browsers, y is reversed, so we get sinA => -sinA. The general result is:
* | cosB | 0 | - sinB | | x | | px |
* | - sinA * sinB | cosA | - sinA * cosB | x | y | = | py |
* | cosA * sinB | sinA | cosA * cosB | | z | | pz |
*/
function rotate3D(x, y, z, angles) {
return {
x: angles.cosB * x - angles.sinB * z,
y: -angles.sinA * angles.sinB * x + angles.cosA * y - angles.cosB * angles.sinA * z,
z: angles.cosA * angles.sinB * x + angles.sinA * y + angles.cosA * angles.cosB * z
};
}
function perspective3D(coordinate, origin, distance) {
var projection = ((distance > 0) && (distance < Number.POSITIVE_INFINITY)) ? distance / (coordinate.z + origin.z + distance) : 1;
return {
x: coordinate.x * projection,
y: coordinate.y * projection
};
}
/**
* Transforms a given array of points according to the angles in chart.options.
* Parameters:
* - points: the array of points
* - chart: the chart
* - insidePlotArea: wether to verifiy the points are inside the plotArea
* Returns:
* - an array of transformed points
*/
H.perspective = function(points, chart, insidePlotArea) {
var options3d = chart.options.chart.options3d,
inverted = insidePlotArea ? chart.inverted : false,
origin = {
x: chart.plotWidth / 2,
y: chart.plotHeight / 2,
z: options3d.depth / 2,
vd: pick(options3d.depth, 1) * pick(options3d.viewDistance, 0)
},
scale = chart.scale3d || 1,
beta = deg2rad * options3d.beta * (inverted ? -1 : 1),
alpha = deg2rad * options3d.alpha * (inverted ? -1 : 1),
angles = {
cosA: Math.cos(alpha),
cosB: Math.cos(-beta),
sinA: Math.sin(alpha),
sinB: Math.sin(-beta)
};
if (!insidePlotArea) {
origin.x += chart.plotLeft;
origin.y += chart.plotTop;
}
// Transform each point
return H.map(points, function(point) {
var rotated = rotate3D(
(inverted ? point.y : point.x) - origin.x,
(inverted ? point.x : point.y) - origin.y,
(point.z || 0) - origin.z,
angles
),
coordinate = perspective3D(rotated, origin, origin.vd); // Apply perspective
// Apply translation
coordinate.x = coordinate.x * scale + origin.x;
coordinate.y = coordinate.y * scale + origin.y;
coordinate.z = rotated.z * scale + origin.z;
return {
x: (inverted ? coordinate.y : coordinate.x),
y: (inverted ? coordinate.x : coordinate.y),
z: coordinate.z
};
});
};
/**
* Calculate a distance from camera to points - made for calculating zIndex of scatter points.
* Parameters:
* - coordinates: The coordinates of the specific point
* - chart: the chart
* Returns:
* - a distance from camera to point
*/
H.pointCameraDistance = function(coordinates, chart) {
var options3d = chart.options.chart.options3d,
cameraPosition = {
x: chart.plotWidth / 2,
y: chart.plotHeight / 2,
z: pick(options3d.depth, 1) * pick(options3d.viewDistance, 0) + options3d.depth
},
distance = Math.sqrt(Math.pow(cameraPosition.x - coordinates.plotX, 2) + Math.pow(cameraPosition.y - coordinates.plotY, 2) + Math.pow(cameraPosition.z - coordinates.plotZ, 2));
return distance;
};
/**
* Calculate area of a 2D polygon using Shoelace algorithm
* http://en.wikipedia.org/wiki/Shoelace_formula
*/
H.shapeArea = function(vertexes) {
var area = 0,
i,
j;
for (i = 0; i < vertexes.length; i++) {
j = (i + 1) % vertexes.length;
area += vertexes[i].x * vertexes[j].y - vertexes[j].x * vertexes[i].y;
}
return area / 2;
};
/**
* Calculate area of a 3D polygon after perspective projection
*/
H.shapeArea3d = function(vertexes, chart, insidePlotArea) {
return H.shapeArea(H.perspective(vertexes, chart, insidePlotArea));
};
}(Highcharts));
(function(H) {
/**
* (c) 2010-2017 Torstein Honsi
*
* License: www.highcharts.com/license
*/
var cos = Math.cos,
PI = Math.PI,
sin = Math.sin;
var animObject = H.animObject,
charts = H.charts,
color = H.color,
defined = H.defined,
deg2rad = H.deg2rad,
each = H.each,
extend = H.extend,
inArray = H.inArray,
map = H.map,
merge = H.merge,
perspective = H.perspective,
pick = H.pick,
SVGElement = H.SVGElement,
SVGRenderer = H.SVGRenderer,
wrap = H.wrap;
/*
EXTENSION TO THE SVG-RENDERER TO ENABLE 3D SHAPES
*/
// HELPER METHODS //
var dFactor = (4 * (Math.sqrt(2) - 1) / 3) / (PI / 2);
/** Method to construct a curved path
* Can 'wrap' around more then 180 degrees
*/
function curveTo(cx, cy, rx, ry, start, end, dx, dy) {
var result = [],
arcAngle = end - start;
if ((end > start) && (end - start > Math.PI / 2 + 0.0001)) {
result = result.concat(curveTo(cx, cy, rx, ry, start, start + (Math.PI / 2), dx, dy));
result = result.concat(curveTo(cx, cy, rx, ry, start + (Math.PI / 2), end, dx, dy));
return result;
}
if ((end < start) && (start - end > Math.PI / 2 + 0.0001)) {
result = result.concat(curveTo(cx, cy, rx, ry, start, start - (Math.PI / 2), dx, dy));
result = result.concat(curveTo(cx, cy, rx, ry, start - (Math.PI / 2), end, dx, dy));
return result;
}
return [
'C',
cx + (rx * Math.cos(start)) - ((rx * dFactor * arcAngle) * Math.sin(start)) + dx,
cy + (ry * Math.sin(start)) + ((ry * dFactor * arcAngle) * Math.cos(start)) + dy,
cx + (rx * Math.cos(end)) + ((rx * dFactor * arcAngle) * Math.sin(end)) + dx,
cy + (ry * Math.sin(end)) - ((ry * dFactor * arcAngle) * Math.cos(end)) + dy,
cx + (rx * Math.cos(end)) + dx,
cy + (ry * Math.sin(end)) + dy
];
}
SVGRenderer.prototype.toLinePath = function(points, closed) {
var result = [];
// Put "L x y" for each point
each(points, function(point) {
result.push('L', point.x, point.y);
});
if (points.length) {
// Set the first element to M
result[0] = 'M';
// If it is a closed line, add Z
if (closed) {
result.push('Z');
}
}
return result;
};
SVGRenderer.prototype.toLineSegments = function(points) {
var result = [];
var m = true;
each(points, function(point) {
result.push(m ? 'M' : 'L', point.x, point.y);
m = !m;
});
return result;
};
/**
* A 3-D Face is defined by it's 3D vertexes, and is only
* visible if it's vertexes are counter-clockwise (Back-face culling).
* It is used as a polyhedron Element
*/
SVGRenderer.prototype.face3d = function(args) {
var renderer = this,
ret = this.createElement('path');
ret.vertexes = [];
ret.insidePlotArea = false;
ret.enabled = true;
wrap(ret, 'attr', function(proceed, hash) {
if (typeof hash === 'object' &&
(defined(hash.enabled) || defined(hash.vertexes) || defined(hash.insidePlotArea))) {
this.enabled = pick(hash.enabled, this.enabled);
this.vertexes = pick(hash.vertexes, this.vertexes);
this.insidePlotArea = pick(hash.insidePlotArea, this.insidePlotArea);
delete hash.enabled;
delete hash.vertexes;
delete hash.insidePlotArea;
var chart = charts[renderer.chartIndex],
vertexes2d = perspective(this.vertexes, chart, this.insidePlotArea),
path = renderer.toLinePath(vertexes2d, true),
area = H.shapeArea(vertexes2d),
visibility = (this.enabled && area > 0) ? 'visible' : 'hidden';
hash.d = path;
hash.visibility = visibility;
}
return proceed.apply(this, [].slice.call(arguments, 1));
});
wrap(ret, 'animate', function(proceed, params) {
if (typeof params === 'object' &&
(defined(params.enabled) || defined(params.vertexes) || defined(params.insidePlotArea))) {
this.enabled = pick(params.enabled, this.enabled);
this.vertexes = pick(params.vertexes, this.vertexes);
this.insidePlotArea = pick(params.insidePlotArea, this.insidePlotArea);
delete params.enabled;
delete params.vertexes;
delete params.insidePlotArea;
var chart = charts[renderer.chartIndex],
vertexes2d = perspective(this.vertexes, chart, this.insidePlotArea),
path = renderer.toLinePath(vertexes2d, true),
area = H.shapeArea(vertexes2d),
visibility = (this.enabled && area > 0) ? 'visible' : 'hidden';
params.d = path;
this.attr('visibility', visibility);
}
return proceed.apply(this, [].slice.call(arguments, 1));
});
return ret.attr(args);
};
/**
* A Polyhedron is a handy way of defining a group of 3-D faces.
* It's only attribute is `faces`, an array of attributes of each one of it's Face3D instances.
*/
SVGRenderer.prototype.polyhedron = function(args) {
var renderer = this,
result = this.g(),
destroy = result.destroy;
result.attr({
'stroke-linejoin': 'round'
});
result.faces = [];
// destroy all children
result.destroy = function() {
for (var i = 0; i < result.faces.length; i++) {
result.faces[i].destroy();
}
return destroy.call(this);
};
wrap(result, 'attr', function(proceed, hash, val, complete, continueAnimation) {
if (typeof hash === 'object' && defined(hash.faces)) {
while (result.faces.length > hash.faces.length) {
result.faces.pop().destroy();
}
while (result.faces.length < hash.faces.length) {
result.faces.push(renderer.face3d().add(result));
}
for (var i = 0; i < hash.faces.length; i++) {
result.faces[i].attr(hash.faces[i], null, complete, continueAnimation);
}
delete hash.faces;
}
return proceed.apply(this, [].slice.call(arguments, 1));
});
wrap(result, 'animate', function(proceed, params, duration, complete) {
if (params && params.faces) {
while (result.faces.length > params.faces.length) {
result.faces.pop().destroy();
}
while (result.faces.length < params.faces.length) {
result.faces.push(renderer.face3d().add(result));
}
for (var i = 0; i < params.faces.length; i++) {
result.faces[i].animate(params.faces[i], duration, complete);
}
delete params.faces;
}
return proceed.apply(this, [].slice.call(arguments, 1));
});
return result.attr(args);
};
// CUBOIDS //
SVGRenderer.prototype.cuboid = function(shapeArgs) {
var result = this.g(),
destroy = result.destroy,
paths = this.cuboidPath(shapeArgs);
result.attr({
'stroke-linejoin': 'round'
});
// create the 3 sides
result.front = this.path(paths[0]).attr({
'class': 'highcharts-3d-front'
}).add(result); // Front, top and side are never overlapping in our case so it is redundant to set zIndex of every element.
result.top = this.path(paths[1]).attr({
'class': 'highcharts-3d-top'
}).add(result);
result.side = this.path(paths[2]).attr({
'class': 'highcharts-3d-side'
}).add(result);
// apply the fill everywhere, the top a bit brighter, the side a bit darker
result.fillSetter = function(fill) {
this.front.attr({
fill: fill
});
this.top.attr({
fill: color(fill).brighten(0.1).get()
});
this.side.attr({
fill: color(fill).brighten(-0.1).get()
});
this.color = fill;
// for animation getter (#6776)
result.fill = fill;
return this;
};
// apply opacaity everywhere
result.opacitySetter = function(opacity) {
this.front.attr({
opacity: opacity
});
this.top.attr({
opacity: opacity
});
this.side.attr({
opacity: opacity
});
return this;
};
result.attr = function(args, val, complete, continueAnimation) {
// Resolve setting attributes by string name
if (typeof args === 'string' && typeof val !== 'undefined') {
var key = args;
args = {};
args[key] = val;
}
if (args.shapeArgs || defined(args.x)) {
var shapeArgs = args.shapeArgs || args;
var paths = this.renderer.cuboidPath(shapeArgs);
this.front.attr({
d: paths[0]
});
this.top.attr({
d: paths[1]
});
this.side.attr({
d: paths[2]
});
} else {
// getter returns value
return SVGElement.prototype.attr.call(
this, args, undefined, complete, continueAnimation
);
}
return this;
};
result.animate = function(args, duration, complete) {
if (defined(args.x) && defined(args.y)) {
var paths = this.renderer.cuboidPath(args);
this.front.animate({
d: paths[0]
}, duration, complete);
this.top.animate({
d: paths[1]
}, duration, complete);
this.side.animate({
d: paths[2]
}, duration, complete);
this.attr({
zIndex: -paths[3] // #4774
});
} else if (args.opacity) {
this.front.animate(args, duration, complete);
this.top.animate(args, duration, complete);
this.side.animate(args, duration, complete);
} else {
SVGElement.prototype.animate.call(this, args, duration, complete);
}
return this;
};
// destroy all children
result.destroy = function() {
this.front.destroy();
this.top.destroy();
this.side.destroy();
return destroy.call(this);
};
// Apply the Z index to the cuboid group
result.attr({
zIndex: -paths[3]
});
return result;
};
/**
* Generates a cuboid
*/
H.SVGRenderer.prototype.cuboidPath = function(shapeArgs) {
var x = shapeArgs.x,
y = shapeArgs.y,
z = shapeArgs.z,
h = shapeArgs.height,
w = shapeArgs.width,
d = shapeArgs.depth,
chart = charts[this.chartIndex],
front,
back,
top,
bottom,
left,
right,
shape,
path1,
path2,
path3,
isFront,
isTop,
isRight,
options3d = chart.options.chart.options3d,
alpha = options3d.alpha,
// Priority for x axis is the biggest,
// because of x direction has biggest influence on zIndex
incrementX = 10000,
// y axis has the smallest priority in case of our charts
// (needs to be set because of stacking)
incrementY = 10,
incrementZ = 100,
zIndex = 0;
// The 8 corners of the cube
var pArr = [{
x: x,
y: y,
z: z
}, {
x: x + w,
y: y,
z: z
}, {
x: x + w,
y: y + h,
z: z
}, {
x: x,
y: y + h,
z: z
}, {
x: x,
y: y + h,
z: z + d
}, {
x: x + w,
y: y + h,
z: z + d
}, {
x: x + w,
y: y,
z: z + d
}, {
x: x,
y: y,
z: z + d
}];
// apply perspective
pArr = perspective(pArr, chart, shapeArgs.insidePlotArea);
// helper method to decide which side is visible
function mapPath(i) {
return pArr[i];
}
/*
* First value - path with specific side
* Second value - added information about side for later calculations.
* Possible second values are 0 for path1, 1 for path2 and -1 for no path choosed.
*/
var pickShape = function(path1, path2) {
var ret = [
[], -1
];
path1 = map(path1, mapPath);
path2 = map(path2, mapPath);
if (H.shapeArea(path1) < 0) {
ret = [path1, 0];
} else if (H.shapeArea(path2) < 0) {
ret = [path2, 1];
}
return ret;
};
// front or back
front = [3, 2, 1, 0];
back = [7, 6, 5, 4];
shape = pickShape(front, back);
path1 = shape[0];
isFront = shape[1];
// top or bottom
top = [1, 6, 7, 0];
bottom = [4, 5, 2, 3];
shape = pickShape(top, bottom);
path2 = shape[0];
isTop = shape[1];
// side
right = [1, 2, 5, 6];
left = [0, 7, 4, 3];
shape = pickShape(right, left);
path3 = shape[0];
isRight = shape[1];
/*
* New block used for calculating zIndex. It is basing on X, Y and Z position of specific columns.
* All zIndexes (for X, Y and Z values) are added to the final zIndex, where every value has different priority.
* The biggest priority is in X and Z directions, the lowest index is for stacked columns (Y direction and the same X and Z positions).
* Big differents between priorities is made because we need to ensure that even for big changes in Y and Z parameters
* all columns will be drawn correctly.
*/
if (isRight === 1) {
zIndex += incrementX * (1000 - x);
} else if (!isRight) {
zIndex += incrementX * x;
}
zIndex += incrementY * (!isTop ||
(alpha >= 0 && alpha <= 180 || alpha < 360 && alpha > 357.5) ? // Numbers checked empirically
chart.plotHeight - y : 10 + y
);
if (isFront === 1) {
zIndex += incrementZ * (z);
} else if (!isFront) {
zIndex += incrementZ * (1000 - z);
}
zIndex = -Math.round(zIndex);
return [
this.toLinePath(path1, true),
this.toLinePath(path2, true),
this.toLinePath(path3, true),
zIndex
]; // #4774
};
// SECTORS //
H.SVGRenderer.prototype.arc3d = function(attribs) {
var wrapper = this.g(),
renderer = wrapper.renderer,
customAttribs = ['x', 'y', 'r', 'innerR', 'start', 'end'];
/**
* Get custom attributes. Don't mutate the original object and return an object with only custom attr.
*/
function suckOutCustom(params) {
var hasCA = false,
ca = {};
params = merge(params); // Don't mutate the original object
for (var key in params) {
if (inArray(key, customAttribs) !== -1) {
ca[key] = params[key];
delete params[key];
hasCA = true;
}
}
return hasCA ? ca : false;
}
attribs = merge(attribs);
attribs.alpha *= deg2rad;
attribs.beta *= deg2rad;
// Create the different sub sections of the shape
wrapper.top = renderer.path();
wrapper.side1 = renderer.path();
wrapper.side2 = renderer.path();
wrapper.inn = renderer.path();
wrapper.out = renderer.path();
/**
* Add all faces
*/
wrapper.onAdd = function() {
var parent = wrapper.parentGroup,
className = wrapper.attr('class');
wrapper.top.add(wrapper);
// These faces are added outside the wrapper group because the z index
// relates to neighbour elements as well
each(['out', 'inn', 'side1', 'side2'], function(face) {
wrapper[face]
.attr({
'class': className + ' highcharts-3d-side'
})
.add(parent);
});
};
// Cascade to faces
each(['addClass', 'removeClass'], function(fn) {
wrapper[fn] = function() {
var args = arguments;
each(['top', 'out', 'inn', 'side1', 'side2'], function(face) {
wrapper[face][fn].apply(wrapper[face], args);
});
};
});
/**
* Compute the transformed paths and set them to the composite shapes
*/
wrapper.setPaths = function(attribs) {
var paths = wrapper.renderer.arc3dPath(attribs),
zIndex = paths.zTop * 100;
wrapper.attribs = attribs;
wrapper.top.attr({
d: paths.top,
zIndex: paths.zTop
});
wrapper.inn.attr({
d: paths.inn,
zIndex: paths.zInn
});
wrapper.out.attr({
d: paths.out,
zIndex: paths.zOut
});
wrapper.side1.attr({
d: paths.side1,
zIndex: paths.zSide1
});
wrapper.side2.attr({
d: paths.side2,
zIndex: paths.zSide2
});
// show all children
wrapper.zIndex = zIndex;
wrapper.attr({
zIndex: zIndex
});
// Set the radial gradient center the first time
if (attribs.center) {
wrapper.top.setRadialReference(attribs.center);
delete attribs.center;
}
};
wrapper.setPaths(attribs);
// Apply the fill to the top and a darker shade to the sides
wrapper.fillSetter = function(value) {
var darker = color(value).brighten(-0.1).get();
this.fill = value;
this.side1.attr({
fill: darker
});
this.side2.attr({
fill: darker
});
this.inn.attr({
fill: darker
});
this.out.attr({
fill: darker
});
this.top.attr({
fill: value
});
return this;
};
// Apply the same value to all. These properties cascade down to the children
// when set to the composite arc3d.
each(['opacity', 'translateX', 'translateY', 'visibility'], function(setter) {
wrapper[setter + 'Setter'] = function(value, key) {
wrapper[key] = value;
each(['out', 'inn', 'side1', 'side2', 'top'], function(el) {
wrapper[el].attr(key, value);
});
};
});
/**
* Override attr to remove shape attributes and use those to set child paths
*/
wrap(wrapper, 'attr', function(proceed, params) {
var ca;
if (typeof params === 'object') {
ca = suckOutCustom(params);
if (ca) {
extend(wrapper.attribs, ca);
wrapper.setPaths(wrapper.attribs);
}
}
return proceed.apply(this, [].slice.call(arguments, 1));
});
/**
* Override the animate function by sucking out custom parameters related to the shapes directly,
* and update the shapes from the animation step.
*/
wrap(wrapper, 'animate', function(proceed, params, animation, complete) {
var ca,
from = this.attribs,
to,
anim;
// Attribute-line properties connected to 3D. These shouldn't have been in the
// attribs collection in the first place.
delete params.center;
delete params.z;
delete params.depth;
delete params.alpha;
delete params.beta;
anim = animObject(pick(animation, this.renderer.globalAnimation));
if (anim.duration) {
ca = suckOutCustom(params);
params.dummy = 1; // Params need to have a property in order for the step to run (#5765)
if (ca) {
to = ca;
anim.step = function(a, fx) {
function interpolate(key) {
return from[key] + (pick(to[key], from[key]) - from[key]) * fx.pos;
}
if (fx.prop === 'dummy') {
fx.elem.setPaths(merge(from, {
x: interpolate('x'),
y: interpolate('y'),
r: interpolate('r'),
innerR: interpolate('innerR'),
start: interpolate('start'),
end: interpolate('end')
}));
}
};
}
animation = anim; // Only when duration (#5572)
}
return proceed.call(this, params, animation, complete);
});
// destroy all children
wrapper.destroy = function() {
this.top.destroy();
this.out.destroy();
this.inn.destroy();
this.side1.destroy();
this.side2.destroy();
SVGElement.prototype.destroy.call(this);
};
// hide all children
wrapper.hide = function() {
this.top.hide();
this.out.hide();
this.inn.hide();
this.side1.hide();
this.side2.hide();
};
wrapper.show = function() {
this.top.show();
this.out.show();
this.inn.show();
this.side1.show();
this.side2.show();
};
return wrapper;
};
/**
* Generate the paths required to draw a 3D arc
*/
SVGRenderer.prototype.arc3dPath = function(shapeArgs) {
var cx = shapeArgs.x, // x coordinate of the center
cy = shapeArgs.y, // y coordinate of the center
start = shapeArgs.start, // start angle
end = shapeArgs.end - 0.00001, // end angle
r = shapeArgs.r, // radius
ir = shapeArgs.innerR, // inner radius
d = shapeArgs.depth, // depth
alpha = shapeArgs.alpha, // alpha rotation of the chart
beta = shapeArgs.beta; // beta rotation of the chart
// Derived Variables
var cs = Math.cos(start), // cosinus of the start angle
ss = Math.sin(start), // sinus of the start angle
ce = Math.cos(end), // cosinus of the end angle
se = Math.sin(end), // sinus of the end angle
rx = r * Math.cos(beta), // x-radius
ry = r * Math.cos(alpha), // y-radius
irx = ir * Math.cos(beta), // x-radius (inner)
iry = ir * Math.cos(alpha), // y-radius (inner)
dx = d * Math.sin(beta), // distance between top and bottom in x
dy = d * Math.sin(alpha); // distance between top and bottom in y
// TOP
var top = ['M', cx + (rx * cs), cy + (ry * ss)];
top = top.concat(curveTo(cx, cy, rx, ry, start, end, 0, 0));
top = top.concat([
'L', cx + (irx * ce), cy + (iry * se)
]);
top = top.concat(curveTo(cx, cy, irx, iry, end, start, 0, 0));
top = top.concat(['Z']);
// OUTSIDE
var b = (beta > 0 ? Math.PI / 2 : 0),
a = (alpha > 0 ? 0 : Math.PI / 2);
var start2 = start > -b ? start : (end > -b ? -b : start),
end2 = end < PI - a ? end : (start < PI - a ? PI - a : end),
midEnd = 2 * PI - a;
// When slice goes over bottom middle, need to add both, left and right outer side.
// Additionally, when we cross right hand edge, create sharp edge. Outer shape/wall:
//
// -------
// / ^ \
// 4) / / \ \ 1)
// / / \ \
// / / \ \
// (c)=> ==== ==== <=(d)
// \ \ / /
// \ \<=(a)/ /
// \ \ / / <=(b)
// 3) \ v / 2)
// -------
//
// (a) - inner side
// (b) - outer side
// (c) - left edge (sharp)
// (d) - right edge (sharp)
// 1..n - rendering order for startAngle = 0, when set to e.g 90, order changes clockwise (1->2, 2->3, n->1) and counterclockwise for negative startAngle
var out = ['M', cx + (rx * cos(start2)), cy + (ry * sin(start2))];
out = out.concat(curveTo(cx, cy, rx, ry, start2, end2, 0, 0));
if (end > midEnd && start < midEnd) { // When shape is wide, it can cross both, (c) and (d) edges, when using startAngle
// Go to outer side
out = out.concat([
'L', cx + (rx * cos(end2)) + dx, cy + (ry * sin(end2)) + dy
]);
// Curve to the right edge of the slice (d)
out = out.concat(curveTo(cx, cy, rx, ry, end2, midEnd, dx, dy));
// Go to the inner side
out = out.concat([
'L', cx + (rx * cos(midEnd)), cy + (ry * sin(midEnd))
]);
// Curve to the true end of the slice
out = out.concat(curveTo(cx, cy, rx, ry, midEnd, end, 0, 0));
// Go to the outer side
out = out.concat([
'L', cx + (rx * cos(end)) + dx, cy + (ry * sin(end)) + dy
]);
// Go back to middle (d)
out = out.concat(curveTo(cx, cy, rx, ry, end, midEnd, dx, dy));
out = out.concat([
'L', cx + (rx * cos(midEnd)), cy + (ry * sin(midEnd))
]);
// Go back to the left edge
out = out.concat(curveTo(cx, cy, rx, ry, midEnd, end2, 0, 0));
} else if (end > PI - a && start < PI - a) { // But shape can cross also only (c) edge:
// Go to outer side
out = out.concat([
'L', cx + (rx * Math.cos(end2)) + dx, cy + (ry * Math.sin(end2)) + dy
]);
// Curve to the true end of the slice
out = out.concat(curveTo(cx, cy, rx, ry, end2, end, dx, dy));
// Go to the inner side
out = out.concat([
'L', cx + (rx * Math.cos(end)), cy + (ry * Math.sin(end))