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