variable-pie.src.js
18 KB
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
/**
* @license Highcharts JS v6.2.0 (2018-10-17)
*
* Variable Pie module for Highcharts
*
* (c) 2010-2017 Grzegorz Blachliński
*
* License: www.highcharts.com/license
*/
'use strict';
(function (factory) {
if (typeof module === 'object' && module.exports) {
module.exports = factory;
} else if (typeof define === 'function' && define.amd) {
define(function () {
return factory;
});
} else {
factory(Highcharts);
}
}(function (Highcharts) {
(function (H) {
/**
*
* Variable Pie module for Highcharts
*
* (c) 2010-2017 Grzegorz Blachliński
*
* License: www.highcharts.com/license
*/
var pick = H.pick,
each = H.each,
grep = H.grep,
arrayMin = H.arrayMin,
arrayMax = H.arrayMax,
seriesType = H.seriesType,
pieProto = H.seriesTypes.pie.prototype;
/**
* The variablepie series type.
*
* @constructor seriesTypes.variablepie
* @augments seriesTypes.pie
*/
seriesType('variablepie', 'pie',
/**
* A variable pie series is a two dimensional series type, where each point
* renders an Y and Z value. Each point is drawn as a pie slice where the
* size (arc) of the slice relates to the Y value and the radius of pie
* slice relates to the Z value. Requires `highcharts-more.js`.
*
* @extends plotOptions.pie
* @product highcharts
* @sample {highcharts} highcharts/demo/variable-radius-pie/
* Variable-radius pie chart
* @since 6.0.0
* @optionparent plotOptions.variablepie
*/
{
/**
* The minimum size of the points' radius related to chart's `plotArea`.
* If a number is set, it applies in pixels.
*
* @sample {highcharts}
* highcharts/variable-radius-pie/min-max-point-size/
* Example of minPointSize and maxPointSize
* @sample {highcharts}
* highcharts/variable-radius-pie/min-point-size-100/
* minPointSize set to 100
* @type {String|Number}
* @since 6.0.0
* @product highcharts
*/
minPointSize: '10%',
/**
* The maximum size of the points' radius related to chart's `plotArea`.
* If a number is set, it applies in pixels.
*
* @sample {highcharts}
* highcharts/variable-radius-pie/min-max-point-size/
* Example of minPointSize and maxPointSize
* @type {String|Number}
* @since 6.0.0
* @product highcharts
*/
maxPointSize: '100%',
/**
* The minimum possible z value for the point's radius calculation.
* If the point's Z value is smaller than zMin, the slice will be drawn
* according to the zMin value.
*
* @sample {highcharts}
* highcharts/variable-radius-pie/zmin-5/
* zMin set to 5, smaller z values are treated as 5
* @sample {highcharts}
* highcharts/variable-radius-pie/zmin-zmax/
* Series limited by both zMin and zMax
* @type {Number}
* @since 6.0.0
* @product highcharts
*/
zMin: undefined,
/**
* The maximum possible z value for the point's radius calculation. If
* the point's Z value is bigger than zMax, the slice will be drawn
* according to the zMax value
*
* @sample {highcharts}
* highcharts/variable-radius-pie/zmin-zmax/
* Series limited by both zMin and zMax
* @type {Number}
* @since 6.0.0
* @product highcharts
*/
zMax: undefined,
/**
* Whether the pie slice's value should be represented by the area
* or the radius of the slice. Can be either `area` or `radius`. The
* default, `area`, corresponds best to the human perception of the size
* of each pie slice.
*
* @sample {highcharts}
* highcharts/variable-radius-pie/sizeby/
* Difference between area and radius sizeBy
* @type {String}
* @validvalue ["area", "radius"]
* @since 6.0.0
* @product highcharts
*/
sizeBy: 'area',
tooltip: {
pointFormat: '<span style="color:{point.color}">\u25CF</span> {series.name}<br/>Value: {point.y}<br/>Size: {point.z}<br/>'
}
}, {
pointArrayMap: ['y', 'z'],
parallelArrays: ['x', 'y', 'z'],
/*
* It is needed to null series.center on chart redraw. Probably good
* idea will be to add this option in directly in pie series.
*/
redraw: function () {
this.center = null;
pieProto.redraw.call(this, arguments);
},
/*
* For arrayMin and arrayMax calculations array shouldn't have
* null/undefined/string values.
* In this case it is needed to check if points Z value is a Number.
*/
zValEval: function (zVal) {
if (typeof zVal === 'number' && !isNaN(zVal)) {
return true;
}
return null;
},
/*
* Before standard translate method for pie chart it is needed to
* calculate min/max radius of each pie slice based on its Z value.
*/
calculateExtremes: function () {
var series = this,
chart = series.chart,
plotWidth = chart.plotWidth,
plotHeight = chart.plotHeight,
seriesOptions = series.options,
slicingRoom = 2 * (seriesOptions.slicedOffset || 0),
zMin,
zMax,
zData = series.zData,
smallestSize = Math.min(plotWidth, plotHeight) - slicingRoom,
extremes = {}, // Min and max size of pie slice.
// In pie charts size of a pie is changed to make space for
// dataLabels, then series.center is changing.
positions = series.center || series.getCenter();
each(['minPointSize', 'maxPointSize'], function (prop) {
var length = seriesOptions[prop],
isPercent = /%$/.test(length);
length = parseInt(length, 10);
extremes[prop] = isPercent ?
smallestSize * length / 100 :
length * 2; // Because it should be radius, not diameter.
});
series.minPxSize = positions[3] + extremes.minPointSize;
series.maxPxSize = Math.max(
Math.min(positions[2], extremes.maxPointSize),
positions[3] + extremes.minPointSize
);
if (zData.length) {
zMin = pick(
seriesOptions.zMin,
arrayMin(grep(zData, series.zValEval))
);
zMax = pick(
seriesOptions.zMax,
arrayMax(grep(zData, series.zValEval))
);
this.getRadii(zMin, zMax, series.minPxSize, series.maxPxSize);
}
},
/*
* Finding radius of series points based on their Z value and min/max Z
* value for all series
* zMin - min threshold for Z value. If point's Z value is smaller that
* zMin, point will have the smallest possible radius.
* zMax - max threshold for Z value. If point's Z value is bigger that
* zMax, point will have the biggest possible radius.
* minSize - minimal pixel size possible for radius
* maxSize - minimal pixel size possible for radius
*/
getRadii: function (zMin, zMax, minSize, maxSize) {
var i = 0,
pos,
zData = this.zData,
len = zData.length,
radii = [],
options = this.options,
sizeByArea = options.sizeBy !== 'radius',
zRange = zMax - zMin,
value,
radius;
// Calculate radius for all pie slice's based on their Z values
for (i; i < len; i++) {
// if zData[i] is null/undefined/string we need to take zMin for
// smallest radius.
value = this.zValEval(zData[i]) ? zData[i] : zMin;
if (value <= zMin) {
radius = minSize / 2;
} else if (value >= zMax) {
radius = maxSize / 2;
} else {
// Relative size, a number between 0 and 1
pos = zRange > 0 ? (value - zMin) / zRange : 0.5;
if (sizeByArea) {
pos = Math.sqrt(pos);
}
radius = Math.ceil(minSize + pos * (maxSize - minSize)) / 2;
}
radii.push(radius);
}
this.radii = radii;
},
/**
* Extend tranlate by updating radius for each pie slice instead of
* using one global radius.
*/
translate: function (positions) {
this.generatePoints();
var series = this,
cumulative = 0,
precision = 1000, // issue #172
options = series.options,
slicedOffset = options.slicedOffset,
connectorOffset = slicedOffset + (options.borderWidth || 0),
finalConnectorOffset,
start,
end,
angle,
startAngle = options.startAngle || 0,
startAngleRad = Math.PI / 180 * (startAngle - 90),
endAngleRad = Math.PI / 180 * (pick(
options.endAngle,
startAngle + 360) - 90),
circ = endAngleRad - startAngleRad, // 2 * Math.PI,
points = series.points,
// the x component of the radius vector for a given point
radiusX,
radiusY,
labelDistance = options.dataLabels.distance,
ignoreHiddenPoint = options.ignoreHiddenPoint,
i,
len = points.length,
point,
pointRadii,
pointRadiusX,
pointRadiusY;
series.startAngleRad = startAngleRad;
series.endAngleRad = endAngleRad;
// Use calculateExtremes to get series.radii array.
series.calculateExtremes();
// Get positions - either an integer or a percentage string must be
// given. If positions are passed as a parameter, we're in a
// recursive loop for adjusting space for data labels.
if (!positions) {
series.center = positions = series.getCenter();
}
// Utility for getting the x value from a given y, used for
// anticollision logic in data labels. Added point for using
// specific points' label distance.
series.getX = function (y, left, point) {
var radii = point.series.radii[point.index];
angle = Math.asin(
Math.max( // #7663
Math.min(
(y - positions[1]) /
(radii + point.labelDistance),
1
),
-1
)
);
return positions[0] +
(left ? -1 : 1) *
(Math.cos(angle) * (radii +
point.labelDistance));
};
// Calculate the geometry for each point
for (i = 0; i < len; i++) {
point = points[i];
pointRadii = series.radii[i];
// Used for distance calculation for specific point.
point.labelDistance = pick(
point.options.dataLabels &&
point.options.dataLabels.distance,
labelDistance
);
// Saved for later dataLabels distance calculation.
series.maxLabelDistance = Math.max(
series.maxLabelDistance || 0,
point.labelDistance
);
// set start and end angle
start = startAngleRad + (cumulative * circ);
if (!ignoreHiddenPoint || point.visible) {
cumulative += point.percentage / 100;
}
end = startAngleRad + (cumulative * circ);
// set the shape
point.shapeType = 'arc';
point.shapeArgs = {
x: positions[0],
y: positions[1],
r: pointRadii,
innerR: positions[3] / 2,
start: Math.round(start * precision) / precision,
end: Math.round(end * precision) / precision
};
// The angle must stay within -90 and 270 (#2645)
angle = (end + start) / 2;
if (angle > 1.5 * Math.PI) {
angle -= 2 * Math.PI;
} else if (angle < -Math.PI / 2) {
angle += 2 * Math.PI;
}
// Center for the sliced out slice
point.slicedTranslation = {
translateX: Math.round(Math.cos(angle) * slicedOffset),
translateY: Math.round(Math.sin(angle) * slicedOffset)
};
// set the anchor point for tooltips
radiusX = Math.cos(angle) * positions[2] / 2;
radiusY = Math.sin(angle) * positions[2] / 2;
pointRadiusX = Math.cos(angle) * pointRadii;
pointRadiusY = Math.sin(angle) * pointRadii;
point.tooltipPos = [
positions[0] + radiusX * 0.7,
positions[1] + radiusY * 0.7
];
point.half = angle < -Math.PI / 2 || angle > Math.PI / 2 ?
1 :
0;
point.angle = angle;
// Set the anchor point for data labels. Use point.labelDistance
// instead of labelDistance // #1174
// finalConnectorOffset - not override connectorOffset value.
finalConnectorOffset = Math.min(
connectorOffset,
point.labelDistance / 5
); // #1678
point.labelPos = [
positions[0] + pointRadiusX +
// first break of connector
Math.cos(angle) * point.labelDistance,
positions[1] + pointRadiusY +
Math.sin(angle) * point.labelDistance, // a/a
positions[0] + pointRadiusX +
// second break, right outside pie
Math.cos(angle) * finalConnectorOffset,
positions[1] + pointRadiusY +
Math.sin(angle) * finalConnectorOffset, // a/a
positions[0] + pointRadiusX, // landing point for connector
positions[1] + pointRadiusY, // a/a
point.labelDistance < 0 ? // alignment
'center' :
point.half ? 'right' : 'left', // alignment
angle // center angle
];
}
}
}
);
/**
* A `variablepie` series. If the [type](#series.variablepie.type) option is not
* specified, it is inherited from [chart.type](#chart.type).
*
* @type {Object}
* @extends series,plotOptions.variablepie
* @excluding dataParser,dataURL,stack,xAxis,yAxis
* @product highcharts
* @apioption series.variablepie
*/
/**
* An array of data points for the series. For the `variablepie` series type,
* points can be given in the following ways:
*
* 1. An array of arrays with 2 values. In this case, the numerical values
* will be interpreted as `y, z` options. Example:
*
* ```js
* data: [
* [40, 75],
* [50, 50],
* [60, 40]
* ]
* ```
*
* 2. An array of objects with named values. The following snippet shows only a
* few settings, see the complete options set below. If the total number of data
* points exceeds the series'
* [turboThreshold](#series.variablepie.turboThreshold), this option is not
* available.
*
* ```js
* data: [{
* y: 1,
* z: 4,
* name: "Point2",
* color: "#00FF00"
* }, {
* y: 7,
* z: 10,
* name: "Point1",
* color: "#FF00FF"
* }]
* ```
*
* @type {Array<Object|Number>}
* @extends series.pie.data
* @excluding marker,x
* @sample {highcharts} highcharts/chart/reflow-true/
* Numerical values
* @sample {highcharts} highcharts/series/data-array-of-arrays/
* Arrays of numeric x and y
* @sample {highcharts} highcharts/series/data-array-of-arrays-datetime/
* Arrays of datetime x and y
* @sample {highcharts} highcharts/series/data-array-of-name-value/
* Arrays of point.name and y
* @sample {highcharts} highcharts/series/data-array-of-objects/
* Config objects
* @product highcharts
* @apioption series.variablepie.data
*/
}(Highcharts));
return (function () {
}());
}));