jquery.flot.pie.js 23 KB

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