iobroker.ebus 3.2.4 → 3.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/.eslintrc.json +34 -34
  2. package/.releaseconfig.json +3 -0
  3. package/LICENSE +20 -20
  4. package/README.md +147 -130
  5. package/admin/index_m.html +419 -419
  6. package/admin/style.css +18 -18
  7. package/admin/words.js +27 -27
  8. package/io-package.json +218 -192
  9. package/lib/support_tools.js +370 -370
  10. package/lib/tools.js +99 -99
  11. package/main.js +1232 -1232
  12. package/package.json +13 -11
  13. package/widgets/ebus/lib/js/flot/jquery.canvaswrapper.js +549 -549
  14. package/widgets/ebus/lib/js/flot/jquery.colorhelpers.js +199 -199
  15. package/widgets/ebus/lib/js/flot/jquery.flot.axislabels.js +212 -212
  16. package/widgets/ebus/lib/js/flot/jquery.flot.browser.js +98 -98
  17. package/widgets/ebus/lib/js/flot/jquery.flot.categories.js +202 -202
  18. package/widgets/ebus/lib/js/flot/jquery.flot.composeImages.js +330 -330
  19. package/widgets/ebus/lib/js/flot/jquery.flot.crosshair.js +202 -202
  20. package/widgets/ebus/lib/js/flot/jquery.flot.drawSeries.js +662 -662
  21. package/widgets/ebus/lib/js/flot/jquery.flot.errorbars.js +375 -375
  22. package/widgets/ebus/lib/js/flot/jquery.flot.fillbetween.js +254 -254
  23. package/widgets/ebus/lib/js/flot/jquery.flot.flatdata.js +47 -47
  24. package/widgets/ebus/lib/js/flot/jquery.flot.hover.js +361 -361
  25. package/widgets/ebus/lib/js/flot/jquery.flot.image.js +249 -249
  26. package/widgets/ebus/lib/js/flot/jquery.flot.js +2953 -2953
  27. package/widgets/ebus/lib/js/flot/jquery.flot.legend.js +437 -437
  28. package/widgets/ebus/lib/js/flot/jquery.flot.logaxis.js +298 -298
  29. package/widgets/ebus/lib/js/flot/jquery.flot.navigate.js +834 -834
  30. package/widgets/ebus/lib/js/flot/jquery.flot.pie.js +794 -794
  31. package/widgets/ebus/lib/js/flot/jquery.flot.resize.js +60 -60
  32. package/widgets/ebus/lib/js/flot/jquery.flot.saturated.js +43 -43
  33. package/widgets/ebus/lib/js/flot/jquery.flot.selection.js +527 -527
  34. package/widgets/ebus/lib/js/flot/jquery.flot.stack.js +220 -220
  35. package/widgets/ebus/lib/js/flot/jquery.flot.symbol.js +98 -98
  36. package/widgets/ebus/lib/js/flot/jquery.flot.threshold.js +143 -143
  37. package/widgets/ebus/lib/js/flot/jquery.flot.time.js +586 -586
  38. package/widgets/ebus/lib/js/flot/jquery.flot.touch.js +320 -320
  39. package/widgets/ebus/lib/js/flot/jquery.flot.touchNavigate.js +360 -360
  40. package/widgets/ebus/lib/js/flot/jquery.flot.uiConstants.js +10 -10
  41. package/widgets/ebus/lib/js/flot/jquery.js +9473 -9473
  42. package/widgets/ebus/lib/js/lib/globalize.culture.en-US.js +33 -33
  43. package/widgets/ebus/lib/js/lib/globalize.js +1601 -1601
  44. package/widgets/ebus/lib/js/lib/jquery.event.drag.js +145 -145
  45. package/widgets/ebus/lib/js/lib/jquery.mousewheel.js +86 -86
  46. package/widgets/ebus.html +2395 -2395
  47. package/readme.txt +0 -297
@@ -1,794 +1,794 @@
1
- /* Flot plugin for rendering pie charts.
2
-
3
- Copyright (c) 2007-2014 IOLA and Ole Laursen.
4
- Licensed under the MIT license.
5
-
6
- The plugin assumes that each series has a single data value, and that each
7
- value is a positive integer or zero. Negative numbers don't make sense for a
8
- pie chart, and have unpredictable results. The values do NOT need to be
9
- passed in as percentages; the plugin will calculate the total and per-slice
10
- percentages internally.
11
-
12
- * Created by Brian Medendorp
13
-
14
- * Updated with contributions from btburnett3, Anthony Aragues and Xavi Ivars
15
-
16
- The plugin supports these options:
17
-
18
- series: {
19
- pie: {
20
- show: true/false
21
- radius: 0-1 for percentage of fullsize, or a specified pixel length, or 'auto'
22
- innerRadius: 0-1 for percentage of fullsize or a specified pixel length, for creating a donut effect
23
- startAngle: 0-2 factor of PI used for starting angle (in radians) i.e 3/2 starts at the top, 0 and 2 have the same result
24
- tilt: 0-1 for percentage to tilt the pie, where 1 is no tilt, and 0 is completely flat (nothing will show)
25
- offset: {
26
- top: integer value to move the pie up or down
27
- left: integer value to move the pie left or right, or 'auto'
28
- },
29
- stroke: {
30
- color: any hexidecimal color value (other formats may or may not work, so best to stick with something like '#FFF')
31
- width: integer pixel width of the stroke
32
- },
33
- label: {
34
- show: true/false, or 'auto'
35
- formatter: a user-defined function that modifies the text/style of the label text
36
- radius: 0-1 for percentage of fullsize, or a specified pixel length
37
- background: {
38
- color: any hexidecimal color value (other formats may or may not work, so best to stick with something like '#000')
39
- opacity: 0-1
40
- },
41
- threshold: 0-1 for the percentage value at which to hide labels (if they're too small)
42
- },
43
- combine: {
44
- threshold: 0-1 for the percentage value at which to combine slices (if they're too small)
45
- color: any hexidecimal color value (other formats may or may not work, so best to stick with something like '#CCC'), if null, the plugin will automatically use the color of the first slice to be combined
46
- label: any text value of what the combined slice should be labeled
47
- }
48
- highlight: {
49
- opacity: 0-1
50
- }
51
- }
52
- }
53
-
54
- More detail and specific examples can be found in the included HTML file.
55
-
56
- */
57
-
58
- (function($) {
59
- // Maximum redraw attempts when fitting labels within the plot
60
-
61
- var REDRAW_ATTEMPTS = 10;
62
-
63
- // Factor by which to shrink the pie when fitting labels within the plot
64
-
65
- var REDRAW_SHRINK = 0.95;
66
-
67
- function init(plot) {
68
- var canvas = null,
69
- target = null,
70
- options = null,
71
- maxRadius = null,
72
- centerLeft = null,
73
- centerTop = null,
74
- processed = false,
75
- ctx = null;
76
-
77
- // interactive variables
78
-
79
- var highlights = [];
80
-
81
- // add hook to determine if pie plugin in enabled, and then perform necessary operations
82
-
83
- plot.hooks.processOptions.push(function(plot, options) {
84
- if (options.series.pie.show) {
85
- options.grid.show = false;
86
-
87
- // set labels.show
88
-
89
- if (options.series.pie.label.show === "auto") {
90
- if (options.legend.show) {
91
- options.series.pie.label.show = false;
92
- } else {
93
- options.series.pie.label.show = true;
94
- }
95
- }
96
-
97
- // set radius
98
-
99
- if (options.series.pie.radius === "auto") {
100
- if (options.series.pie.label.show) {
101
- options.series.pie.radius = 3 / 4;
102
- } else {
103
- options.series.pie.radius = 1;
104
- }
105
- }
106
-
107
- // ensure sane tilt
108
-
109
- if (options.series.pie.tilt > 1) {
110
- options.series.pie.tilt = 1;
111
- } else if (options.series.pie.tilt < 0) {
112
- options.series.pie.tilt = 0;
113
- }
114
- }
115
- });
116
-
117
- plot.hooks.bindEvents.push(function(plot, eventHolder) {
118
- var options = plot.getOptions();
119
- if (options.series.pie.show) {
120
- if (options.grid.hoverable) {
121
- eventHolder.unbind("mousemove").mousemove(onMouseMove);
122
- eventHolder.bind("mouseleave", onMouseMove);
123
- }
124
- if (options.grid.clickable) {
125
- eventHolder.unbind("click").click(onClick);
126
- }
127
- }
128
- });
129
-
130
- plot.hooks.shutdown.push(function (plot, eventHolder) {
131
- eventHolder.unbind("mousemove", onMouseMove);
132
- eventHolder.unbind("mouseleave", onMouseMove);
133
- eventHolder.unbind("click", onClick);
134
- highlights = [];
135
- });
136
-
137
- plot.hooks.processDatapoints.push(function(plot, series, data, datapoints) {
138
- var options = plot.getOptions();
139
- if (options.series.pie.show) {
140
- processDatapoints(plot, series, data, datapoints);
141
- }
142
- });
143
-
144
- plot.hooks.drawOverlay.push(function(plot, octx) {
145
- var options = plot.getOptions();
146
- if (options.series.pie.show) {
147
- drawOverlay(plot, octx);
148
- }
149
- });
150
-
151
- plot.hooks.draw.push(function(plot, newCtx) {
152
- var options = plot.getOptions();
153
- if (options.series.pie.show) {
154
- draw(plot, newCtx);
155
- }
156
- });
157
-
158
- function processDatapoints(plot, series, datapoints) {
159
- if (!processed) {
160
- processed = true;
161
- canvas = plot.getCanvas();
162
- target = $(canvas).parent();
163
- options = plot.getOptions();
164
- plot.setData(combine(plot.getData()));
165
- }
166
- }
167
-
168
- function combine(data) {
169
- var total = 0,
170
- combined = 0,
171
- numCombined = 0,
172
- color = options.series.pie.combine.color,
173
- newdata = [],
174
- i,
175
- value;
176
-
177
- // Fix up the raw data from Flot, ensuring the data is numeric
178
-
179
- for (i = 0; i < data.length; ++i) {
180
- value = data[i].data;
181
-
182
- // If the data is an array, we'll assume that it's a standard
183
- // Flot x-y pair, and are concerned only with the second value.
184
-
185
- // Note how we use the original array, rather than creating a
186
- // new one; this is more efficient and preserves any extra data
187
- // that the user may have stored in higher indexes.
188
-
189
- if ($.isArray(value) && value.length === 1) {
190
- value = value[0];
191
- }
192
-
193
- if ($.isArray(value)) {
194
- // Equivalent to $.isNumeric() but compatible with jQuery < 1.7
195
- if (!isNaN(parseFloat(value[1])) && isFinite(value[1])) {
196
- value[1] = +value[1];
197
- } else {
198
- value[1] = 0;
199
- }
200
- } else if (!isNaN(parseFloat(value)) && isFinite(value)) {
201
- value = [1, +value];
202
- } else {
203
- value = [1, 0];
204
- }
205
-
206
- data[i].data = [value];
207
- }
208
-
209
- // Sum up all the slices, so we can calculate percentages for each
210
-
211
- for (i = 0; i < data.length; ++i) {
212
- total += data[i].data[0][1];
213
- }
214
-
215
- // Count the number of slices with percentages below the combine
216
- // threshold; if it turns out to be just one, we won't combine.
217
-
218
- for (i = 0; i < data.length; ++i) {
219
- value = data[i].data[0][1];
220
- if (value / total <= options.series.pie.combine.threshold) {
221
- combined += value;
222
- numCombined++;
223
- if (!color) {
224
- color = data[i].color;
225
- }
226
- }
227
- }
228
-
229
- for (i = 0; i < data.length; ++i) {
230
- value = data[i].data[0][1];
231
- if (numCombined < 2 || value / total > options.series.pie.combine.threshold) {
232
- newdata.push(
233
- $.extend(data[i], { /* extend to allow keeping all other original data values
234
- and using them e.g. in labelFormatter. */
235
- data: [[1, value]],
236
- color: data[i].color,
237
- label: data[i].label,
238
- angle: value * Math.PI * 2 / total,
239
- percent: value / (total / 100)
240
- })
241
- );
242
- }
243
- }
244
-
245
- if (numCombined > 1) {
246
- newdata.push({
247
- data: [[1, combined]],
248
- color: color,
249
- label: options.series.pie.combine.label,
250
- angle: combined * Math.PI * 2 / total,
251
- percent: combined / (total / 100)
252
- });
253
- }
254
-
255
- return newdata;
256
- }
257
-
258
- function draw(plot, newCtx) {
259
- if (!target) {
260
- return; // if no series were passed
261
- }
262
-
263
- var canvasWidth = plot.getPlaceholder().width(),
264
- canvasHeight = plot.getPlaceholder().height(),
265
- legendWidth = target.children().filter(".legend").children().width() || 0;
266
-
267
- ctx = newCtx;
268
-
269
- // WARNING: HACK! REWRITE THIS CODE AS SOON AS POSSIBLE!
270
-
271
- // When combining smaller slices into an 'other' slice, we need to
272
- // add a new series. Since Flot gives plugins no way to modify the
273
- // list of series, the pie plugin uses a hack where the first call
274
- // to processDatapoints results in a call to setData with the new
275
- // list of series, then subsequent processDatapoints do nothing.
276
-
277
- // The plugin-global 'processed' flag is used to control this hack;
278
- // it starts out false, and is set to true after the first call to
279
- // processDatapoints.
280
-
281
- // Unfortunately this turns future setData calls into no-ops; they
282
- // call processDatapoints, the flag is true, and nothing happens.
283
-
284
- // To fix this we'll set the flag back to false here in draw, when
285
- // all series have been processed, so the next sequence of calls to
286
- // processDatapoints once again starts out with a slice-combine.
287
- // This is really a hack; in 0.9 we need to give plugins a proper
288
- // way to modify series before any processing begins.
289
-
290
- processed = false;
291
-
292
- // calculate maximum radius and center point
293
- maxRadius = Math.min(canvasWidth, canvasHeight / options.series.pie.tilt) / 2;
294
- centerTop = canvasHeight / 2 + options.series.pie.offset.top;
295
- centerLeft = canvasWidth / 2;
296
-
297
- if (options.series.pie.offset.left === "auto") {
298
- if (options.legend.position.match("w")) {
299
- centerLeft += legendWidth / 2;
300
- } else {
301
- centerLeft -= legendWidth / 2;
302
- }
303
- if (centerLeft < maxRadius) {
304
- centerLeft = maxRadius;
305
- } else if (centerLeft > canvasWidth - maxRadius) {
306
- centerLeft = canvasWidth - maxRadius;
307
- }
308
- } else {
309
- centerLeft += options.series.pie.offset.left;
310
- }
311
-
312
- var slices = plot.getData(),
313
- attempts = 0;
314
-
315
- // Keep shrinking the pie's radius until drawPie returns true,
316
- // indicating that all the labels fit, or we try too many times.
317
- do {
318
- if (attempts > 0) {
319
- maxRadius *= REDRAW_SHRINK;
320
- }
321
- attempts += 1;
322
- clear();
323
- if (options.series.pie.tilt <= 0.8) {
324
- drawShadow();
325
- }
326
- } while (!drawPie() && attempts < REDRAW_ATTEMPTS)
327
-
328
- if (attempts >= REDRAW_ATTEMPTS) {
329
- clear();
330
- target.prepend("<div class='error'>Could not draw pie with labels contained inside canvas</div>");
331
- }
332
-
333
- if (plot.setSeries && plot.insertLegend) {
334
- plot.setSeries(slices);
335
- plot.insertLegend();
336
- }
337
-
338
- // we're actually done at this point, just defining internal functions at this point
339
- function clear() {
340
- ctx.clearRect(0, 0, canvasWidth, canvasHeight);
341
- target.children().filter(".pieLabel, .pieLabelBackground").remove();
342
- }
343
-
344
- function drawShadow() {
345
- var shadowLeft = options.series.pie.shadow.left;
346
- var shadowTop = options.series.pie.shadow.top;
347
- var edge = 10;
348
- var alpha = options.series.pie.shadow.alpha;
349
- var radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius;
350
-
351
- if (radius >= canvasWidth / 2 - shadowLeft || radius * options.series.pie.tilt >= canvasHeight / 2 - shadowTop || radius <= edge) {
352
- return; // shadow would be outside canvas, so don't draw it
353
- }
354
-
355
- ctx.save();
356
- ctx.translate(shadowLeft, shadowTop);
357
- ctx.globalAlpha = alpha;
358
- ctx.fillStyle = "#000";
359
-
360
- // center and rotate to starting position
361
- ctx.translate(centerLeft, centerTop);
362
- ctx.scale(1, options.series.pie.tilt);
363
-
364
- //radius -= edge;
365
- for (var i = 1; i <= edge; i++) {
366
- ctx.beginPath();
367
- ctx.arc(0, 0, radius, 0, Math.PI * 2, false);
368
- ctx.fill();
369
- radius -= i;
370
- }
371
-
372
- ctx.restore();
373
- }
374
-
375
- function drawPie() {
376
- var startAngle = Math.PI * options.series.pie.startAngle;
377
- var radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius;
378
- var i;
379
- // center and rotate to starting position
380
-
381
- ctx.save();
382
- ctx.translate(centerLeft, centerTop);
383
- ctx.scale(1, options.series.pie.tilt);
384
- //ctx.rotate(startAngle); // start at top; -- This doesn't work properly in Opera
385
-
386
- // draw slices
387
- ctx.save();
388
-
389
- var currentAngle = startAngle;
390
- for (i = 0; i < slices.length; ++i) {
391
- slices[i].startAngle = currentAngle;
392
- drawSlice(slices[i].angle, slices[i].color, true);
393
- }
394
-
395
- ctx.restore();
396
-
397
- // draw slice outlines
398
- if (options.series.pie.stroke.width > 0) {
399
- ctx.save();
400
- ctx.lineWidth = options.series.pie.stroke.width;
401
- currentAngle = startAngle;
402
- for (i = 0; i < slices.length; ++i) {
403
- drawSlice(slices[i].angle, options.series.pie.stroke.color, false);
404
- }
405
-
406
- ctx.restore();
407
- }
408
-
409
- // draw donut hole
410
- drawDonutHole(ctx);
411
-
412
- ctx.restore();
413
-
414
- // Draw the labels, returning true if they fit within the plot
415
- if (options.series.pie.label.show) {
416
- return drawLabels();
417
- } else return true;
418
-
419
- function drawSlice(angle, color, fill) {
420
- if (angle <= 0 || isNaN(angle)) {
421
- return;
422
- }
423
-
424
- if (fill) {
425
- ctx.fillStyle = color;
426
- } else {
427
- ctx.strokeStyle = color;
428
- ctx.lineJoin = "round";
429
- }
430
-
431
- ctx.beginPath();
432
- if (Math.abs(angle - Math.PI * 2) > 0.000000001) {
433
- ctx.moveTo(0, 0); // Center of the pie
434
- }
435
-
436
- //ctx.arc(0, 0, radius, 0, angle, false); // This doesn't work properly in Opera
437
- ctx.arc(0, 0, radius, currentAngle, currentAngle + angle / 2, false);
438
- ctx.arc(0, 0, radius, currentAngle + angle / 2, currentAngle + angle, false);
439
- ctx.closePath();
440
- //ctx.rotate(angle); // This doesn't work properly in Opera
441
- currentAngle += angle;
442
-
443
- if (fill) {
444
- ctx.fill();
445
- } else {
446
- ctx.stroke();
447
- }
448
- }
449
-
450
- function drawLabels() {
451
- var currentAngle = startAngle;
452
- var radius = options.series.pie.label.radius > 1 ? options.series.pie.label.radius : maxRadius * options.series.pie.label.radius;
453
-
454
- for (var i = 0; i < slices.length; ++i) {
455
- if (slices[i].percent >= options.series.pie.label.threshold * 100) {
456
- if (!drawLabel(slices[i], currentAngle, i)) {
457
- return false;
458
- }
459
- }
460
- currentAngle += slices[i].angle;
461
- }
462
-
463
- return true;
464
-
465
- function drawLabel(slice, startAngle, index) {
466
- if (slice.data[0][1] === 0) {
467
- return true;
468
- }
469
-
470
- // format label text
471
- var lf = options.legend.labelFormatter, text, plf = options.series.pie.label.formatter;
472
-
473
- if (lf) {
474
- text = lf(slice.label, slice);
475
- } else {
476
- text = slice.label;
477
- }
478
-
479
- if (plf) {
480
- text = plf(text, slice);
481
- }
482
-
483
- var halfAngle = ((startAngle + slice.angle) + startAngle) / 2;
484
- var x = centerLeft + Math.round(Math.cos(halfAngle) * radius);
485
- var y = centerTop + Math.round(Math.sin(halfAngle) * radius) * options.series.pie.tilt;
486
-
487
- var html = "<span class='pieLabel' id='pieLabel" + index + "' style='position:absolute;top:" + y + "px;left:" + x + "px;'>" + text + "</span>";
488
- target.append(html);
489
-
490
- var label = target.children("#pieLabel" + index);
491
- var labelTop = (y - label.height() / 2);
492
- var labelLeft = (x - label.width() / 2);
493
-
494
- label.css("top", labelTop);
495
- label.css("left", labelLeft);
496
-
497
- // check to make sure that the label is not outside the canvas
498
- if (0 - labelTop > 0 || 0 - labelLeft > 0 || canvasHeight - (labelTop + label.height()) < 0 || canvasWidth - (labelLeft + label.width()) < 0) {
499
- return false;
500
- }
501
-
502
- if (options.series.pie.label.background.opacity !== 0) {
503
- // put in the transparent background separately to avoid blended labels and label boxes
504
- var c = options.series.pie.label.background.color;
505
- if (c == null) {
506
- c = slice.color;
507
- }
508
-
509
- var pos = "top:" + labelTop + "px;left:" + labelLeft + "px;";
510
- $("<div class='pieLabelBackground' style='position:absolute;width:" + label.width() + "px;height:" + label.height() + "px;" + pos + "background-color:" + c + ";'></div>")
511
- .css("opacity", options.series.pie.label.background.opacity)
512
- .insertBefore(label);
513
- }
514
-
515
- return true;
516
- } // end individual label function
517
- } // end drawLabels function
518
- } // end drawPie function
519
- } // end draw function
520
-
521
- // Placed here because it needs to be accessed from multiple locations
522
-
523
- function drawDonutHole(layer) {
524
- if (options.series.pie.innerRadius > 0) {
525
- // subtract the center
526
- layer.save();
527
- var innerRadius = options.series.pie.innerRadius > 1 ? options.series.pie.innerRadius : maxRadius * options.series.pie.innerRadius;
528
- layer.globalCompositeOperation = "destination-out"; // this does not work with excanvas, but it will fall back to using the stroke color
529
- layer.beginPath();
530
- layer.fillStyle = options.series.pie.stroke.color;
531
- layer.arc(0, 0, innerRadius, 0, Math.PI * 2, false);
532
- layer.fill();
533
- layer.closePath();
534
- layer.restore();
535
-
536
- // add inner stroke
537
- layer.save();
538
- layer.beginPath();
539
- layer.strokeStyle = options.series.pie.stroke.color;
540
- layer.arc(0, 0, innerRadius, 0, Math.PI * 2, false);
541
- layer.stroke();
542
- layer.closePath();
543
- layer.restore();
544
-
545
- // TODO: add extra shadow inside hole (with a mask) if the pie is tilted.
546
- }
547
- }
548
-
549
- //-- Additional Interactive related functions --
550
-
551
- function isPointInPoly(poly, pt) {
552
- for (var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i) {
553
- ((poly[i][1] <= pt[1] && pt[1] < poly[j][1]) ||
554
- (poly[j][1] <= pt[1] && pt[1] < poly[i][1])) &&
555
- (pt[0] < (poly[j][0] - poly[i][0]) * (pt[1] - poly[i][1]) / (poly[j][1] - poly[i][1]) + poly[i][0]) &&
556
- (c = !c);
557
- }
558
- return c;
559
- }
560
-
561
- function findNearbySlice(mouseX, mouseY) {
562
- var slices = plot.getData(),
563
- options = plot.getOptions(),
564
- radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius,
565
- x, y;
566
-
567
- for (var i = 0; i < slices.length; ++i) {
568
- var s = slices[i];
569
- if (s.pie.show) {
570
- ctx.save();
571
- ctx.beginPath();
572
- ctx.moveTo(0, 0); // Center of the pie
573
- //ctx.scale(1, options.series.pie.tilt); // this actually seems to break everything when here.
574
- ctx.arc(0, 0, radius, s.startAngle, s.startAngle + s.angle / 2, false);
575
- ctx.arc(0, 0, radius, s.startAngle + s.angle / 2, s.startAngle + s.angle, false);
576
- ctx.closePath();
577
- x = mouseX - centerLeft;
578
- y = mouseY - centerTop;
579
-
580
- if (ctx.isPointInPath) {
581
- if (ctx.isPointInPath(mouseX - centerLeft, mouseY - centerTop)) {
582
- ctx.restore();
583
- return {
584
- datapoint: [s.percent, s.data],
585
- dataIndex: 0,
586
- series: s,
587
- seriesIndex: i
588
- };
589
- }
590
- } else {
591
- // excanvas for IE doesn;t support isPointInPath, this is a workaround.
592
- var p1X = radius * Math.cos(s.startAngle),
593
- p1Y = radius * Math.sin(s.startAngle),
594
- p2X = radius * Math.cos(s.startAngle + s.angle / 4),
595
- p2Y = radius * Math.sin(s.startAngle + s.angle / 4),
596
- p3X = radius * Math.cos(s.startAngle + s.angle / 2),
597
- p3Y = radius * Math.sin(s.startAngle + s.angle / 2),
598
- p4X = radius * Math.cos(s.startAngle + s.angle / 1.5),
599
- p4Y = radius * Math.sin(s.startAngle + s.angle / 1.5),
600
- p5X = radius * Math.cos(s.startAngle + s.angle),
601
- p5Y = radius * Math.sin(s.startAngle + s.angle),
602
- arrPoly = [[0, 0], [p1X, p1Y], [p2X, p2Y], [p3X, p3Y], [p4X, p4Y], [p5X, p5Y]],
603
- arrPoint = [x, y];
604
-
605
- // TODO: perhaps do some mathmatical trickery here with the Y-coordinate to compensate for pie tilt?
606
-
607
- if (isPointInPoly(arrPoly, arrPoint)) {
608
- ctx.restore();
609
- return {
610
- datapoint: [s.percent, s.data],
611
- dataIndex: 0,
612
- series: s,
613
- seriesIndex: i
614
- };
615
- }
616
- }
617
-
618
- ctx.restore();
619
- }
620
- }
621
-
622
- return null;
623
- }
624
-
625
- function onMouseMove(e) {
626
- triggerClickHoverEvent("plothover", e);
627
- }
628
-
629
- function onClick(e) {
630
- triggerClickHoverEvent("plotclick", e);
631
- }
632
-
633
- // trigger click or hover event (they send the same parameters so we share their code)
634
-
635
- function triggerClickHoverEvent(eventname, e) {
636
- var offset = plot.offset();
637
- var canvasX = parseInt(e.pageX - offset.left);
638
- var canvasY = parseInt(e.pageY - offset.top);
639
- var item = findNearbySlice(canvasX, canvasY);
640
-
641
- if (options.grid.autoHighlight) {
642
- // clear auto-highlights
643
- for (var i = 0; i < highlights.length; ++i) {
644
- var h = highlights[i];
645
- if (h.auto === eventname && !(item && h.series === item.series)) {
646
- unhighlight(h.series);
647
- }
648
- }
649
- }
650
-
651
- // highlight the slice
652
-
653
- if (item) {
654
- highlight(item.series, eventname);
655
- }
656
-
657
- // trigger any hover bind events
658
-
659
- var pos = { pageX: e.pageX, pageY: e.pageY };
660
- target.trigger(eventname, [pos, item]);
661
- }
662
-
663
- function highlight(s, auto) {
664
- //if (typeof s == "number") {
665
- // s = series[s];
666
- //}
667
-
668
- var i = indexOfHighlight(s);
669
-
670
- if (i === -1) {
671
- highlights.push({ series: s, auto: auto });
672
- plot.triggerRedrawOverlay();
673
- } else if (!auto) {
674
- highlights[i].auto = false;
675
- }
676
- }
677
-
678
- function unhighlight(s) {
679
- if (s == null) {
680
- highlights = [];
681
- plot.triggerRedrawOverlay();
682
- }
683
-
684
- //if (typeof s == "number") {
685
- // s = series[s];
686
- //}
687
-
688
- var i = indexOfHighlight(s);
689
-
690
- if (i !== -1) {
691
- highlights.splice(i, 1);
692
- plot.triggerRedrawOverlay();
693
- }
694
- }
695
-
696
- function indexOfHighlight(s) {
697
- for (var i = 0; i < highlights.length; ++i) {
698
- var h = highlights[i];
699
- if (h.series === s) {
700
- return i;
701
- }
702
- }
703
- return -1;
704
- }
705
-
706
- function drawOverlay(plot, octx) {
707
- var options = plot.getOptions();
708
- var radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius;
709
-
710
- octx.save();
711
- octx.translate(centerLeft, centerTop);
712
- octx.scale(1, options.series.pie.tilt);
713
-
714
- for (var i = 0; i < highlights.length; ++i) {
715
- drawHighlight(highlights[i].series);
716
- }
717
-
718
- drawDonutHole(octx);
719
-
720
- octx.restore();
721
-
722
- function drawHighlight(series) {
723
- if (series.angle <= 0 || isNaN(series.angle)) {
724
- return;
725
- }
726
-
727
- //octx.fillStyle = parseColor(options.series.pie.highlight.color).scale(null, null, null, options.series.pie.highlight.opacity).toString();
728
- octx.fillStyle = "rgba(255, 255, 255, " + options.series.pie.highlight.opacity + ")"; // this is temporary until we have access to parseColor
729
- octx.beginPath();
730
- if (Math.abs(series.angle - Math.PI * 2) > 0.000000001) {
731
- octx.moveTo(0, 0); // Center of the pie
732
- }
733
- octx.arc(0, 0, radius, series.startAngle, series.startAngle + series.angle / 2, false);
734
- octx.arc(0, 0, radius, series.startAngle + series.angle / 2, series.startAngle + series.angle, false);
735
- octx.closePath();
736
- octx.fill();
737
- }
738
- }
739
- } // end init (plugin body)
740
-
741
- // define pie specific options and their default values
742
- var options = {
743
- series: {
744
- pie: {
745
- show: false,
746
- radius: "auto", // actual radius of the visible pie (based on full calculated radius if <=1, or hard pixel value)
747
- innerRadius: 0, /* for donut */
748
- startAngle: 3 / 2,
749
- tilt: 1,
750
- shadow: {
751
- left: 5, // shadow left offset
752
- top: 15, // shadow top offset
753
- alpha: 0.02 // shadow alpha
754
- },
755
- offset: {
756
- top: 0,
757
- left: "auto"
758
- },
759
- stroke: {
760
- color: "#fff",
761
- width: 1
762
- },
763
- label: {
764
- show: "auto",
765
- formatter: function(label, slice) {
766
- return "<div style='font-size:x-small;text-align:center;padding:2px;color:" + slice.color + ";'>" + label + "<br/>" + Math.round(slice.percent) + "%</div>";
767
- }, // formatter function
768
- radius: 1, // radius at which to place the labels (based on full calculated radius if <=1, or hard pixel value)
769
- background: {
770
- color: null,
771
- opacity: 0
772
- },
773
- threshold: 0 // percentage at which to hide the label (i.e. the slice is too narrow)
774
- },
775
- combine: {
776
- threshold: -1, // percentage at which to combine little slices into one larger slice
777
- color: null, // color to give the new slice (auto-generated if null)
778
- label: "Other" // label to give the new slice
779
- },
780
- highlight: {
781
- //color: "#fff", // will add this functionality once parseColor is available
782
- opacity: 0.5
783
- }
784
- }
785
- }
786
- };
787
-
788
- $.plot.plugins.push({
789
- init: init,
790
- options: options,
791
- name: "pie",
792
- version: "1.1"
793
- });
794
- })(jQuery);
1
+ /* Flot plugin for rendering pie charts.
2
+
3
+ Copyright (c) 2007-2014 IOLA and Ole Laursen.
4
+ Licensed under the MIT license.
5
+
6
+ The plugin assumes that each series has a single data value, and that each
7
+ value is a positive integer or zero. Negative numbers don't make sense for a
8
+ pie chart, and have unpredictable results. The values do NOT need to be
9
+ passed in as percentages; the plugin will calculate the total and per-slice
10
+ percentages internally.
11
+
12
+ * Created by Brian Medendorp
13
+
14
+ * Updated with contributions from btburnett3, Anthony Aragues and Xavi Ivars
15
+
16
+ The plugin supports these options:
17
+
18
+ series: {
19
+ pie: {
20
+ show: true/false
21
+ radius: 0-1 for percentage of fullsize, or a specified pixel length, or 'auto'
22
+ innerRadius: 0-1 for percentage of fullsize or a specified pixel length, for creating a donut effect
23
+ startAngle: 0-2 factor of PI used for starting angle (in radians) i.e 3/2 starts at the top, 0 and 2 have the same result
24
+ tilt: 0-1 for percentage to tilt the pie, where 1 is no tilt, and 0 is completely flat (nothing will show)
25
+ offset: {
26
+ top: integer value to move the pie up or down
27
+ left: integer value to move the pie left or right, or 'auto'
28
+ },
29
+ stroke: {
30
+ color: any hexidecimal color value (other formats may or may not work, so best to stick with something like '#FFF')
31
+ width: integer pixel width of the stroke
32
+ },
33
+ label: {
34
+ show: true/false, or 'auto'
35
+ formatter: a user-defined function that modifies the text/style of the label text
36
+ radius: 0-1 for percentage of fullsize, or a specified pixel length
37
+ background: {
38
+ color: any hexidecimal color value (other formats may or may not work, so best to stick with something like '#000')
39
+ opacity: 0-1
40
+ },
41
+ threshold: 0-1 for the percentage value at which to hide labels (if they're too small)
42
+ },
43
+ combine: {
44
+ threshold: 0-1 for the percentage value at which to combine slices (if they're too small)
45
+ color: any hexidecimal color value (other formats may or may not work, so best to stick with something like '#CCC'), if null, the plugin will automatically use the color of the first slice to be combined
46
+ label: any text value of what the combined slice should be labeled
47
+ }
48
+ highlight: {
49
+ opacity: 0-1
50
+ }
51
+ }
52
+ }
53
+
54
+ More detail and specific examples can be found in the included HTML file.
55
+
56
+ */
57
+
58
+ (function($) {
59
+ // Maximum redraw attempts when fitting labels within the plot
60
+
61
+ var REDRAW_ATTEMPTS = 10;
62
+
63
+ // Factor by which to shrink the pie when fitting labels within the plot
64
+
65
+ var REDRAW_SHRINK = 0.95;
66
+
67
+ function init(plot) {
68
+ var canvas = null,
69
+ target = null,
70
+ options = null,
71
+ maxRadius = null,
72
+ centerLeft = null,
73
+ centerTop = null,
74
+ processed = false,
75
+ ctx = null;
76
+
77
+ // interactive variables
78
+
79
+ var highlights = [];
80
+
81
+ // add hook to determine if pie plugin in enabled, and then perform necessary operations
82
+
83
+ plot.hooks.processOptions.push(function(plot, options) {
84
+ if (options.series.pie.show) {
85
+ options.grid.show = false;
86
+
87
+ // set labels.show
88
+
89
+ if (options.series.pie.label.show === "auto") {
90
+ if (options.legend.show) {
91
+ options.series.pie.label.show = false;
92
+ } else {
93
+ options.series.pie.label.show = true;
94
+ }
95
+ }
96
+
97
+ // set radius
98
+
99
+ if (options.series.pie.radius === "auto") {
100
+ if (options.series.pie.label.show) {
101
+ options.series.pie.radius = 3 / 4;
102
+ } else {
103
+ options.series.pie.radius = 1;
104
+ }
105
+ }
106
+
107
+ // ensure sane tilt
108
+
109
+ if (options.series.pie.tilt > 1) {
110
+ options.series.pie.tilt = 1;
111
+ } else if (options.series.pie.tilt < 0) {
112
+ options.series.pie.tilt = 0;
113
+ }
114
+ }
115
+ });
116
+
117
+ plot.hooks.bindEvents.push(function(plot, eventHolder) {
118
+ var options = plot.getOptions();
119
+ if (options.series.pie.show) {
120
+ if (options.grid.hoverable) {
121
+ eventHolder.unbind("mousemove").mousemove(onMouseMove);
122
+ eventHolder.bind("mouseleave", onMouseMove);
123
+ }
124
+ if (options.grid.clickable) {
125
+ eventHolder.unbind("click").click(onClick);
126
+ }
127
+ }
128
+ });
129
+
130
+ plot.hooks.shutdown.push(function (plot, eventHolder) {
131
+ eventHolder.unbind("mousemove", onMouseMove);
132
+ eventHolder.unbind("mouseleave", onMouseMove);
133
+ eventHolder.unbind("click", onClick);
134
+ highlights = [];
135
+ });
136
+
137
+ plot.hooks.processDatapoints.push(function(plot, series, data, datapoints) {
138
+ var options = plot.getOptions();
139
+ if (options.series.pie.show) {
140
+ processDatapoints(plot, series, data, datapoints);
141
+ }
142
+ });
143
+
144
+ plot.hooks.drawOverlay.push(function(plot, octx) {
145
+ var options = plot.getOptions();
146
+ if (options.series.pie.show) {
147
+ drawOverlay(plot, octx);
148
+ }
149
+ });
150
+
151
+ plot.hooks.draw.push(function(plot, newCtx) {
152
+ var options = plot.getOptions();
153
+ if (options.series.pie.show) {
154
+ draw(plot, newCtx);
155
+ }
156
+ });
157
+
158
+ function processDatapoints(plot, series, datapoints) {
159
+ if (!processed) {
160
+ processed = true;
161
+ canvas = plot.getCanvas();
162
+ target = $(canvas).parent();
163
+ options = plot.getOptions();
164
+ plot.setData(combine(plot.getData()));
165
+ }
166
+ }
167
+
168
+ function combine(data) {
169
+ var total = 0,
170
+ combined = 0,
171
+ numCombined = 0,
172
+ color = options.series.pie.combine.color,
173
+ newdata = [],
174
+ i,
175
+ value;
176
+
177
+ // Fix up the raw data from Flot, ensuring the data is numeric
178
+
179
+ for (i = 0; i < data.length; ++i) {
180
+ value = data[i].data;
181
+
182
+ // If the data is an array, we'll assume that it's a standard
183
+ // Flot x-y pair, and are concerned only with the second value.
184
+
185
+ // Note how we use the original array, rather than creating a
186
+ // new one; this is more efficient and preserves any extra data
187
+ // that the user may have stored in higher indexes.
188
+
189
+ if ($.isArray(value) && value.length === 1) {
190
+ value = value[0];
191
+ }
192
+
193
+ if ($.isArray(value)) {
194
+ // Equivalent to $.isNumeric() but compatible with jQuery < 1.7
195
+ if (!isNaN(parseFloat(value[1])) && isFinite(value[1])) {
196
+ value[1] = +value[1];
197
+ } else {
198
+ value[1] = 0;
199
+ }
200
+ } else if (!isNaN(parseFloat(value)) && isFinite(value)) {
201
+ value = [1, +value];
202
+ } else {
203
+ value = [1, 0];
204
+ }
205
+
206
+ data[i].data = [value];
207
+ }
208
+
209
+ // Sum up all the slices, so we can calculate percentages for each
210
+
211
+ for (i = 0; i < data.length; ++i) {
212
+ total += data[i].data[0][1];
213
+ }
214
+
215
+ // Count the number of slices with percentages below the combine
216
+ // threshold; if it turns out to be just one, we won't combine.
217
+
218
+ for (i = 0; i < data.length; ++i) {
219
+ value = data[i].data[0][1];
220
+ if (value / total <= options.series.pie.combine.threshold) {
221
+ combined += value;
222
+ numCombined++;
223
+ if (!color) {
224
+ color = data[i].color;
225
+ }
226
+ }
227
+ }
228
+
229
+ for (i = 0; i < data.length; ++i) {
230
+ value = data[i].data[0][1];
231
+ if (numCombined < 2 || value / total > options.series.pie.combine.threshold) {
232
+ newdata.push(
233
+ $.extend(data[i], { /* extend to allow keeping all other original data values
234
+ and using them e.g. in labelFormatter. */
235
+ data: [[1, value]],
236
+ color: data[i].color,
237
+ label: data[i].label,
238
+ angle: value * Math.PI * 2 / total,
239
+ percent: value / (total / 100)
240
+ })
241
+ );
242
+ }
243
+ }
244
+
245
+ if (numCombined > 1) {
246
+ newdata.push({
247
+ data: [[1, combined]],
248
+ color: color,
249
+ label: options.series.pie.combine.label,
250
+ angle: combined * Math.PI * 2 / total,
251
+ percent: combined / (total / 100)
252
+ });
253
+ }
254
+
255
+ return newdata;
256
+ }
257
+
258
+ function draw(plot, newCtx) {
259
+ if (!target) {
260
+ return; // if no series were passed
261
+ }
262
+
263
+ var canvasWidth = plot.getPlaceholder().width(),
264
+ canvasHeight = plot.getPlaceholder().height(),
265
+ legendWidth = target.children().filter(".legend").children().width() || 0;
266
+
267
+ ctx = newCtx;
268
+
269
+ // WARNING: HACK! REWRITE THIS CODE AS SOON AS POSSIBLE!
270
+
271
+ // When combining smaller slices into an 'other' slice, we need to
272
+ // add a new series. Since Flot gives plugins no way to modify the
273
+ // list of series, the pie plugin uses a hack where the first call
274
+ // to processDatapoints results in a call to setData with the new
275
+ // list of series, then subsequent processDatapoints do nothing.
276
+
277
+ // The plugin-global 'processed' flag is used to control this hack;
278
+ // it starts out false, and is set to true after the first call to
279
+ // processDatapoints.
280
+
281
+ // Unfortunately this turns future setData calls into no-ops; they
282
+ // call processDatapoints, the flag is true, and nothing happens.
283
+
284
+ // To fix this we'll set the flag back to false here in draw, when
285
+ // all series have been processed, so the next sequence of calls to
286
+ // processDatapoints once again starts out with a slice-combine.
287
+ // This is really a hack; in 0.9 we need to give plugins a proper
288
+ // way to modify series before any processing begins.
289
+
290
+ processed = false;
291
+
292
+ // calculate maximum radius and center point
293
+ maxRadius = Math.min(canvasWidth, canvasHeight / options.series.pie.tilt) / 2;
294
+ centerTop = canvasHeight / 2 + options.series.pie.offset.top;
295
+ centerLeft = canvasWidth / 2;
296
+
297
+ if (options.series.pie.offset.left === "auto") {
298
+ if (options.legend.position.match("w")) {
299
+ centerLeft += legendWidth / 2;
300
+ } else {
301
+ centerLeft -= legendWidth / 2;
302
+ }
303
+ if (centerLeft < maxRadius) {
304
+ centerLeft = maxRadius;
305
+ } else if (centerLeft > canvasWidth - maxRadius) {
306
+ centerLeft = canvasWidth - maxRadius;
307
+ }
308
+ } else {
309
+ centerLeft += options.series.pie.offset.left;
310
+ }
311
+
312
+ var slices = plot.getData(),
313
+ attempts = 0;
314
+
315
+ // Keep shrinking the pie's radius until drawPie returns true,
316
+ // indicating that all the labels fit, or we try too many times.
317
+ do {
318
+ if (attempts > 0) {
319
+ maxRadius *= REDRAW_SHRINK;
320
+ }
321
+ attempts += 1;
322
+ clear();
323
+ if (options.series.pie.tilt <= 0.8) {
324
+ drawShadow();
325
+ }
326
+ } while (!drawPie() && attempts < REDRAW_ATTEMPTS)
327
+
328
+ if (attempts >= REDRAW_ATTEMPTS) {
329
+ clear();
330
+ target.prepend("<div class='error'>Could not draw pie with labels contained inside canvas</div>");
331
+ }
332
+
333
+ if (plot.setSeries && plot.insertLegend) {
334
+ plot.setSeries(slices);
335
+ plot.insertLegend();
336
+ }
337
+
338
+ // we're actually done at this point, just defining internal functions at this point
339
+ function clear() {
340
+ ctx.clearRect(0, 0, canvasWidth, canvasHeight);
341
+ target.children().filter(".pieLabel, .pieLabelBackground").remove();
342
+ }
343
+
344
+ function drawShadow() {
345
+ var shadowLeft = options.series.pie.shadow.left;
346
+ var shadowTop = options.series.pie.shadow.top;
347
+ var edge = 10;
348
+ var alpha = options.series.pie.shadow.alpha;
349
+ var radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius;
350
+
351
+ if (radius >= canvasWidth / 2 - shadowLeft || radius * options.series.pie.tilt >= canvasHeight / 2 - shadowTop || radius <= edge) {
352
+ return; // shadow would be outside canvas, so don't draw it
353
+ }
354
+
355
+ ctx.save();
356
+ ctx.translate(shadowLeft, shadowTop);
357
+ ctx.globalAlpha = alpha;
358
+ ctx.fillStyle = "#000";
359
+
360
+ // center and rotate to starting position
361
+ ctx.translate(centerLeft, centerTop);
362
+ ctx.scale(1, options.series.pie.tilt);
363
+
364
+ //radius -= edge;
365
+ for (var i = 1; i <= edge; i++) {
366
+ ctx.beginPath();
367
+ ctx.arc(0, 0, radius, 0, Math.PI * 2, false);
368
+ ctx.fill();
369
+ radius -= i;
370
+ }
371
+
372
+ ctx.restore();
373
+ }
374
+
375
+ function drawPie() {
376
+ var startAngle = Math.PI * options.series.pie.startAngle;
377
+ var radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius;
378
+ var i;
379
+ // center and rotate to starting position
380
+
381
+ ctx.save();
382
+ ctx.translate(centerLeft, centerTop);
383
+ ctx.scale(1, options.series.pie.tilt);
384
+ //ctx.rotate(startAngle); // start at top; -- This doesn't work properly in Opera
385
+
386
+ // draw slices
387
+ ctx.save();
388
+
389
+ var currentAngle = startAngle;
390
+ for (i = 0; i < slices.length; ++i) {
391
+ slices[i].startAngle = currentAngle;
392
+ drawSlice(slices[i].angle, slices[i].color, true);
393
+ }
394
+
395
+ ctx.restore();
396
+
397
+ // draw slice outlines
398
+ if (options.series.pie.stroke.width > 0) {
399
+ ctx.save();
400
+ ctx.lineWidth = options.series.pie.stroke.width;
401
+ currentAngle = startAngle;
402
+ for (i = 0; i < slices.length; ++i) {
403
+ drawSlice(slices[i].angle, options.series.pie.stroke.color, false);
404
+ }
405
+
406
+ ctx.restore();
407
+ }
408
+
409
+ // draw donut hole
410
+ drawDonutHole(ctx);
411
+
412
+ ctx.restore();
413
+
414
+ // Draw the labels, returning true if they fit within the plot
415
+ if (options.series.pie.label.show) {
416
+ return drawLabels();
417
+ } else return true;
418
+
419
+ function drawSlice(angle, color, fill) {
420
+ if (angle <= 0 || isNaN(angle)) {
421
+ return;
422
+ }
423
+
424
+ if (fill) {
425
+ ctx.fillStyle = color;
426
+ } else {
427
+ ctx.strokeStyle = color;
428
+ ctx.lineJoin = "round";
429
+ }
430
+
431
+ ctx.beginPath();
432
+ if (Math.abs(angle - Math.PI * 2) > 0.000000001) {
433
+ ctx.moveTo(0, 0); // Center of the pie
434
+ }
435
+
436
+ //ctx.arc(0, 0, radius, 0, angle, false); // This doesn't work properly in Opera
437
+ ctx.arc(0, 0, radius, currentAngle, currentAngle + angle / 2, false);
438
+ ctx.arc(0, 0, radius, currentAngle + angle / 2, currentAngle + angle, false);
439
+ ctx.closePath();
440
+ //ctx.rotate(angle); // This doesn't work properly in Opera
441
+ currentAngle += angle;
442
+
443
+ if (fill) {
444
+ ctx.fill();
445
+ } else {
446
+ ctx.stroke();
447
+ }
448
+ }
449
+
450
+ function drawLabels() {
451
+ var currentAngle = startAngle;
452
+ var radius = options.series.pie.label.radius > 1 ? options.series.pie.label.radius : maxRadius * options.series.pie.label.radius;
453
+
454
+ for (var i = 0; i < slices.length; ++i) {
455
+ if (slices[i].percent >= options.series.pie.label.threshold * 100) {
456
+ if (!drawLabel(slices[i], currentAngle, i)) {
457
+ return false;
458
+ }
459
+ }
460
+ currentAngle += slices[i].angle;
461
+ }
462
+
463
+ return true;
464
+
465
+ function drawLabel(slice, startAngle, index) {
466
+ if (slice.data[0][1] === 0) {
467
+ return true;
468
+ }
469
+
470
+ // format label text
471
+ var lf = options.legend.labelFormatter, text, plf = options.series.pie.label.formatter;
472
+
473
+ if (lf) {
474
+ text = lf(slice.label, slice);
475
+ } else {
476
+ text = slice.label;
477
+ }
478
+
479
+ if (plf) {
480
+ text = plf(text, slice);
481
+ }
482
+
483
+ var halfAngle = ((startAngle + slice.angle) + startAngle) / 2;
484
+ var x = centerLeft + Math.round(Math.cos(halfAngle) * radius);
485
+ var y = centerTop + Math.round(Math.sin(halfAngle) * radius) * options.series.pie.tilt;
486
+
487
+ var html = "<span class='pieLabel' id='pieLabel" + index + "' style='position:absolute;top:" + y + "px;left:" + x + "px;'>" + text + "</span>";
488
+ target.append(html);
489
+
490
+ var label = target.children("#pieLabel" + index);
491
+ var labelTop = (y - label.height() / 2);
492
+ var labelLeft = (x - label.width() / 2);
493
+
494
+ label.css("top", labelTop);
495
+ label.css("left", labelLeft);
496
+
497
+ // check to make sure that the label is not outside the canvas
498
+ if (0 - labelTop > 0 || 0 - labelLeft > 0 || canvasHeight - (labelTop + label.height()) < 0 || canvasWidth - (labelLeft + label.width()) < 0) {
499
+ return false;
500
+ }
501
+
502
+ if (options.series.pie.label.background.opacity !== 0) {
503
+ // put in the transparent background separately to avoid blended labels and label boxes
504
+ var c = options.series.pie.label.background.color;
505
+ if (c == null) {
506
+ c = slice.color;
507
+ }
508
+
509
+ var pos = "top:" + labelTop + "px;left:" + labelLeft + "px;";
510
+ $("<div class='pieLabelBackground' style='position:absolute;width:" + label.width() + "px;height:" + label.height() + "px;" + pos + "background-color:" + c + ";'></div>")
511
+ .css("opacity", options.series.pie.label.background.opacity)
512
+ .insertBefore(label);
513
+ }
514
+
515
+ return true;
516
+ } // end individual label function
517
+ } // end drawLabels function
518
+ } // end drawPie function
519
+ } // end draw function
520
+
521
+ // Placed here because it needs to be accessed from multiple locations
522
+
523
+ function drawDonutHole(layer) {
524
+ if (options.series.pie.innerRadius > 0) {
525
+ // subtract the center
526
+ layer.save();
527
+ var innerRadius = options.series.pie.innerRadius > 1 ? options.series.pie.innerRadius : maxRadius * options.series.pie.innerRadius;
528
+ layer.globalCompositeOperation = "destination-out"; // this does not work with excanvas, but it will fall back to using the stroke color
529
+ layer.beginPath();
530
+ layer.fillStyle = options.series.pie.stroke.color;
531
+ layer.arc(0, 0, innerRadius, 0, Math.PI * 2, false);
532
+ layer.fill();
533
+ layer.closePath();
534
+ layer.restore();
535
+
536
+ // add inner stroke
537
+ layer.save();
538
+ layer.beginPath();
539
+ layer.strokeStyle = options.series.pie.stroke.color;
540
+ layer.arc(0, 0, innerRadius, 0, Math.PI * 2, false);
541
+ layer.stroke();
542
+ layer.closePath();
543
+ layer.restore();
544
+
545
+ // TODO: add extra shadow inside hole (with a mask) if the pie is tilted.
546
+ }
547
+ }
548
+
549
+ //-- Additional Interactive related functions --
550
+
551
+ function isPointInPoly(poly, pt) {
552
+ for (var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i) {
553
+ ((poly[i][1] <= pt[1] && pt[1] < poly[j][1]) ||
554
+ (poly[j][1] <= pt[1] && pt[1] < poly[i][1])) &&
555
+ (pt[0] < (poly[j][0] - poly[i][0]) * (pt[1] - poly[i][1]) / (poly[j][1] - poly[i][1]) + poly[i][0]) &&
556
+ (c = !c);
557
+ }
558
+ return c;
559
+ }
560
+
561
+ function findNearbySlice(mouseX, mouseY) {
562
+ var slices = plot.getData(),
563
+ options = plot.getOptions(),
564
+ radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius,
565
+ x, y;
566
+
567
+ for (var i = 0; i < slices.length; ++i) {
568
+ var s = slices[i];
569
+ if (s.pie.show) {
570
+ ctx.save();
571
+ ctx.beginPath();
572
+ ctx.moveTo(0, 0); // Center of the pie
573
+ //ctx.scale(1, options.series.pie.tilt); // this actually seems to break everything when here.
574
+ ctx.arc(0, 0, radius, s.startAngle, s.startAngle + s.angle / 2, false);
575
+ ctx.arc(0, 0, radius, s.startAngle + s.angle / 2, s.startAngle + s.angle, false);
576
+ ctx.closePath();
577
+ x = mouseX - centerLeft;
578
+ y = mouseY - centerTop;
579
+
580
+ if (ctx.isPointInPath) {
581
+ if (ctx.isPointInPath(mouseX - centerLeft, mouseY - centerTop)) {
582
+ ctx.restore();
583
+ return {
584
+ datapoint: [s.percent, s.data],
585
+ dataIndex: 0,
586
+ series: s,
587
+ seriesIndex: i
588
+ };
589
+ }
590
+ } else {
591
+ // excanvas for IE doesn;t support isPointInPath, this is a workaround.
592
+ var p1X = radius * Math.cos(s.startAngle),
593
+ p1Y = radius * Math.sin(s.startAngle),
594
+ p2X = radius * Math.cos(s.startAngle + s.angle / 4),
595
+ p2Y = radius * Math.sin(s.startAngle + s.angle / 4),
596
+ p3X = radius * Math.cos(s.startAngle + s.angle / 2),
597
+ p3Y = radius * Math.sin(s.startAngle + s.angle / 2),
598
+ p4X = radius * Math.cos(s.startAngle + s.angle / 1.5),
599
+ p4Y = radius * Math.sin(s.startAngle + s.angle / 1.5),
600
+ p5X = radius * Math.cos(s.startAngle + s.angle),
601
+ p5Y = radius * Math.sin(s.startAngle + s.angle),
602
+ arrPoly = [[0, 0], [p1X, p1Y], [p2X, p2Y], [p3X, p3Y], [p4X, p4Y], [p5X, p5Y]],
603
+ arrPoint = [x, y];
604
+
605
+ // TODO: perhaps do some mathmatical trickery here with the Y-coordinate to compensate for pie tilt?
606
+
607
+ if (isPointInPoly(arrPoly, arrPoint)) {
608
+ ctx.restore();
609
+ return {
610
+ datapoint: [s.percent, s.data],
611
+ dataIndex: 0,
612
+ series: s,
613
+ seriesIndex: i
614
+ };
615
+ }
616
+ }
617
+
618
+ ctx.restore();
619
+ }
620
+ }
621
+
622
+ return null;
623
+ }
624
+
625
+ function onMouseMove(e) {
626
+ triggerClickHoverEvent("plothover", e);
627
+ }
628
+
629
+ function onClick(e) {
630
+ triggerClickHoverEvent("plotclick", e);
631
+ }
632
+
633
+ // trigger click or hover event (they send the same parameters so we share their code)
634
+
635
+ function triggerClickHoverEvent(eventname, e) {
636
+ var offset = plot.offset();
637
+ var canvasX = parseInt(e.pageX - offset.left);
638
+ var canvasY = parseInt(e.pageY - offset.top);
639
+ var item = findNearbySlice(canvasX, canvasY);
640
+
641
+ if (options.grid.autoHighlight) {
642
+ // clear auto-highlights
643
+ for (var i = 0; i < highlights.length; ++i) {
644
+ var h = highlights[i];
645
+ if (h.auto === eventname && !(item && h.series === item.series)) {
646
+ unhighlight(h.series);
647
+ }
648
+ }
649
+ }
650
+
651
+ // highlight the slice
652
+
653
+ if (item) {
654
+ highlight(item.series, eventname);
655
+ }
656
+
657
+ // trigger any hover bind events
658
+
659
+ var pos = { pageX: e.pageX, pageY: e.pageY };
660
+ target.trigger(eventname, [pos, item]);
661
+ }
662
+
663
+ function highlight(s, auto) {
664
+ //if (typeof s == "number") {
665
+ // s = series[s];
666
+ //}
667
+
668
+ var i = indexOfHighlight(s);
669
+
670
+ if (i === -1) {
671
+ highlights.push({ series: s, auto: auto });
672
+ plot.triggerRedrawOverlay();
673
+ } else if (!auto) {
674
+ highlights[i].auto = false;
675
+ }
676
+ }
677
+
678
+ function unhighlight(s) {
679
+ if (s == null) {
680
+ highlights = [];
681
+ plot.triggerRedrawOverlay();
682
+ }
683
+
684
+ //if (typeof s == "number") {
685
+ // s = series[s];
686
+ //}
687
+
688
+ var i = indexOfHighlight(s);
689
+
690
+ if (i !== -1) {
691
+ highlights.splice(i, 1);
692
+ plot.triggerRedrawOverlay();
693
+ }
694
+ }
695
+
696
+ function indexOfHighlight(s) {
697
+ for (var i = 0; i < highlights.length; ++i) {
698
+ var h = highlights[i];
699
+ if (h.series === s) {
700
+ return i;
701
+ }
702
+ }
703
+ return -1;
704
+ }
705
+
706
+ function drawOverlay(plot, octx) {
707
+ var options = plot.getOptions();
708
+ var radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius;
709
+
710
+ octx.save();
711
+ octx.translate(centerLeft, centerTop);
712
+ octx.scale(1, options.series.pie.tilt);
713
+
714
+ for (var i = 0; i < highlights.length; ++i) {
715
+ drawHighlight(highlights[i].series);
716
+ }
717
+
718
+ drawDonutHole(octx);
719
+
720
+ octx.restore();
721
+
722
+ function drawHighlight(series) {
723
+ if (series.angle <= 0 || isNaN(series.angle)) {
724
+ return;
725
+ }
726
+
727
+ //octx.fillStyle = parseColor(options.series.pie.highlight.color).scale(null, null, null, options.series.pie.highlight.opacity).toString();
728
+ octx.fillStyle = "rgba(255, 255, 255, " + options.series.pie.highlight.opacity + ")"; // this is temporary until we have access to parseColor
729
+ octx.beginPath();
730
+ if (Math.abs(series.angle - Math.PI * 2) > 0.000000001) {
731
+ octx.moveTo(0, 0); // Center of the pie
732
+ }
733
+ octx.arc(0, 0, radius, series.startAngle, series.startAngle + series.angle / 2, false);
734
+ octx.arc(0, 0, radius, series.startAngle + series.angle / 2, series.startAngle + series.angle, false);
735
+ octx.closePath();
736
+ octx.fill();
737
+ }
738
+ }
739
+ } // end init (plugin body)
740
+
741
+ // define pie specific options and their default values
742
+ var options = {
743
+ series: {
744
+ pie: {
745
+ show: false,
746
+ radius: "auto", // actual radius of the visible pie (based on full calculated radius if <=1, or hard pixel value)
747
+ innerRadius: 0, /* for donut */
748
+ startAngle: 3 / 2,
749
+ tilt: 1,
750
+ shadow: {
751
+ left: 5, // shadow left offset
752
+ top: 15, // shadow top offset
753
+ alpha: 0.02 // shadow alpha
754
+ },
755
+ offset: {
756
+ top: 0,
757
+ left: "auto"
758
+ },
759
+ stroke: {
760
+ color: "#fff",
761
+ width: 1
762
+ },
763
+ label: {
764
+ show: "auto",
765
+ formatter: function(label, slice) {
766
+ return "<div style='font-size:x-small;text-align:center;padding:2px;color:" + slice.color + ";'>" + label + "<br/>" + Math.round(slice.percent) + "%</div>";
767
+ }, // formatter function
768
+ radius: 1, // radius at which to place the labels (based on full calculated radius if <=1, or hard pixel value)
769
+ background: {
770
+ color: null,
771
+ opacity: 0
772
+ },
773
+ threshold: 0 // percentage at which to hide the label (i.e. the slice is too narrow)
774
+ },
775
+ combine: {
776
+ threshold: -1, // percentage at which to combine little slices into one larger slice
777
+ color: null, // color to give the new slice (auto-generated if null)
778
+ label: "Other" // label to give the new slice
779
+ },
780
+ highlight: {
781
+ //color: "#fff", // will add this functionality once parseColor is available
782
+ opacity: 0.5
783
+ }
784
+ }
785
+ }
786
+ };
787
+
788
+ $.plot.plugins.push({
789
+ init: init,
790
+ options: options,
791
+ name: "pie",
792
+ version: "1.1"
793
+ });
794
+ })(jQuery);