@kevinburke/flot 5.0.0

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