jquery.flot.pie.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /* Flot plugin for rendering pie charts.
  2. Copyright (c) 2007-2013 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. data: [[1, value]],
  183. color: data[i].color,
  184. label: data[i].label,
  185. angle: value * Math.PI * 2 / total,
  186. percent: value / (total / 100)
  187. });
  188. }
  189. }
  190. if (numCombined > 1) {
  191. newdata.push({
  192. data: [[1, combined]],
  193. color: color,
  194. label: options.series.pie.combine.label,
  195. angle: combined * Math.PI * 2 / total,
  196. percent: combined / (total / 100)
  197. });
  198. }
  199. return newdata;
  200. }
  201. function draw(plot, newCtx) {
  202. if (!target) {
  203. return; // if no series were passed
  204. }
  205. var canvasWidth = plot.getPlaceholder().width(),
  206. canvasHeight = plot.getPlaceholder().height(),
  207. legendWidth = target.children().filter(".legend").children().width() || 0;
  208. ctx = newCtx;
  209. // WARNING: HACK! REWRITE THIS CODE AS SOON AS POSSIBLE!
  210. // When combining smaller slices into an 'other' slice, we need to
  211. // add a new series. Since Flot gives plugins no way to modify the
  212. // list of series, the pie plugin uses a hack where the first call
  213. // to processDatapoints results in a call to setData with the new
  214. // list of series, then subsequent processDatapoints do nothing.
  215. // The plugin-global 'processed' flag is used to control this hack;
  216. // it starts out false, and is set to true after the first call to
  217. // processDatapoints.
  218. // Unfortunately this turns future setData calls into no-ops; they
  219. // call processDatapoints, the flag is true, and nothing happens.
  220. // To fix this we'll set the flag back to false here in draw, when
  221. // all series have been processed, so the next sequence of calls to
  222. // processDatapoints once again starts out with a slice-combine.
  223. // This is really a hack; in 0.9 we need to give plugins a proper
  224. // way to modify series before any processing begins.
  225. processed = false;
  226. // calculate maximum radius and center point
  227. maxRadius = Math.min(canvasWidth, canvasHeight / options.series.pie.tilt) / 2;
  228. centerTop = canvasHeight / 2 + options.series.pie.offset.top;
  229. centerLeft = canvasWidth / 2;
  230. if (options.series.pie.offset.left == "auto") {
  231. if (options.legend.position.match("w")) {
  232. centerLeft += legendWidth / 2;
  233. } else {
  234. centerLeft -= legendWidth / 2;
  235. }
  236. if (centerLeft < maxRadius) {
  237. centerLeft = maxRadius;
  238. } else if (centerLeft > canvasWidth - maxRadius) {
  239. centerLeft = canvasWidth - maxRadius;
  240. }
  241. } else {
  242. centerLeft += options.series.pie.offset.left;
  243. }
  244. var slices = plot.getData(),
  245. attempts = 0;
  246. // Keep shrinking the pie's radius until drawPie returns true,
  247. // indicating that all the labels fit, or we try too many times.
  248. do {
  249. if (attempts > 0) {
  250. maxRadius *= REDRAW_SHRINK;
  251. }
  252. attempts += 1;
  253. clear();
  254. if (options.series.pie.tilt <= 0.8) {
  255. drawShadow();
  256. }
  257. } while (!drawPie() && attempts < REDRAW_ATTEMPTS)
  258. if (attempts >= REDRAW_ATTEMPTS) {
  259. clear();
  260. target.prepend("<div class='error'>Could not draw pie with labels contained inside canvas</div>");
  261. }
  262. if (plot.setSeries && plot.insertLegend) {
  263. plot.setSeries(slices);
  264. plot.insertLegend();
  265. }
  266. // we're actually done at this point, just defining internal functions at this point
  267. function clear() {
  268. ctx.clearRect(0, 0, canvasWidth, canvasHeight);
  269. target.children().filter(".pieLabel, .pieLabelBackground").remove();
  270. }
  271. function drawShadow() {
  272. var shadowLeft = options.series.pie.shadow.left;
  273. var shadowTop = options.series.pie.shadow.top;
  274. var edge = 10;
  275. var alpha = options.series.pie.shadow.alpha;
  276. var radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius;
  277. if (radius >= canvasWidth / 2 - shadowLeft || radius * options.series.pie.tilt >= canvasHeight / 2 - shadowTop || radius <= edge) {
  278. return; // shadow would be outside canvas, so don't draw it
  279. }
  280. ctx.save();
  281. ctx.translate(shadowLeft,shadowTop);
  282. ctx.globalAlpha = alpha;
  283. ctx.fillStyle = "#000";
  284. // center and rotate to starting position
  285. ctx.translate(centerLeft,centerTop);
  286. ctx.scale(1, options.series.pie.tilt);
  287. //radius -= edge;
  288. for (var i = 1; i <= edge; i++) {
  289. ctx.beginPath();
  290. ctx.arc(0, 0, radius, 0, Math.PI * 2, false);
  291. ctx.fill();
  292. radius -= i;
  293. }
  294. ctx.restore();
  295. }
  296. function drawPie() {
  297. var startAngle = Math.PI * options.series.pie.startAngle;
  298. var radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius;
  299. // center and rotate to starting position
  300. ctx.save();
  301. ctx.translate(centerLeft,centerTop);
  302. ctx.scale(1, options.series.pie.tilt);
  303. //ctx.rotate(startAngle); // start at top; -- This doesn't work properly in Opera
  304. // draw slices
  305. ctx.save();
  306. var currentAngle = startAngle;
  307. for (var i = 0; i < slices.length; ++i) {
  308. slices[i].startAngle = currentAngle;
  309. drawSlice(slices[i].angle, slices[i].color, true);
  310. }
  311. ctx.restore();
  312. // draw slice outlines
  313. if (options.series.pie.stroke.width > 0) {
  314. ctx.save();
  315. ctx.lineWidth = options.series.pie.stroke.width;
  316. currentAngle = startAngle;
  317. for (var i = 0; i < slices.length; ++i) {
  318. drawSlice(slices[i].angle, options.series.pie.stroke.color, false);
  319. }
  320. ctx.restore();
  321. }
  322. // draw donut hole
  323. drawDonutHole(ctx);
  324. ctx.restore();
  325. // Draw the labels, returning true if they fit within the plot
  326. if (options.series.pie.label.show) {
  327. return drawLabels();
  328. } else return true;
  329. function drawSlice(angle, color, fill) {
  330. if (angle <= 0 || isNaN(angle)) {
  331. return;
  332. }
  333. if (fill) {
  334. ctx.fillStyle = color;
  335. } else {
  336. ctx.strokeStyle = color;
  337. ctx.lineJoin = "round";
  338. }
  339. ctx.beginPath();
  340. if (Math.abs(angle - Math.PI * 2) > 0.000000001) {
  341. ctx.moveTo(0, 0); // Center of the pie
  342. }
  343. //ctx.arc(0, 0, radius, 0, angle, false); // This doesn't work properly in Opera
  344. ctx.arc(0, 0, radius,currentAngle, currentAngle + angle / 2, false);
  345. ctx.arc(0, 0, radius,currentAngle + angle / 2, currentAngle + angle, false);
  346. ctx.closePath();
  347. //ctx.rotate(angle); // This doesn't work properly in Opera
  348. currentAngle += angle;
  349. if (fill) {
  350. ctx.fill();
  351. } else {
  352. ctx.stroke();
  353. }
  354. }
  355. function drawLabels() {
  356. var currentAngle = startAngle;
  357. var radius = options.series.pie.label.radius > 1 ? options.series.pie.label.radius : maxRadius * options.series.pie.label.radius;
  358. for (var i = 0; i < slices.length; ++i) {
  359. if (slices[i].percent >= options.series.pie.label.threshold * 100) {
  360. if (!drawLabel(slices[i], currentAngle, i)) {
  361. return false;
  362. }
  363. }
  364. currentAngle += slices[i].angle;
  365. }
  366. return true;
  367. function drawLabel(slice, startAngle, index) {
  368. if (slice.data[0][1] == 0) {
  369. return true;
  370. }
  371. // format label text
  372. var lf = options.legend.labelFormatter, text, plf = options.series.pie.label.formatter;
  373. if (lf) {
  374. text = lf(slice.label, slice);
  375. } else {
  376. text = slice.label;
  377. }
  378. if (plf) {
  379. text = plf(text, slice);
  380. }
  381. var halfAngle = ((startAngle + slice.angle) + startAngle) / 2;
  382. var x = centerLeft + Math.round(Math.cos(halfAngle) * radius);
  383. var y = centerTop + Math.round(Math.sin(halfAngle) * radius) * options.series.pie.tilt;
  384. var html = "<span class='pieLabel' id='pieLabel" + index + "' style='position:absolute;top:" + y + "px;left:" + x + "px;'>" + text + "</span>";
  385. target.append(html);
  386. var label = target.children("#pieLabel" + index);
  387. var labelTop = (y - label.height() / 2);
  388. var labelLeft = (x - label.width() / 2);
  389. label.css("top", labelTop);
  390. label.css("left", labelLeft);
  391. // check to make sure that the label is not outside the canvas
  392. if (0 - labelTop > 0 || 0 - labelLeft > 0 || canvasHeight - (labelTop + label.height()) < 0 || canvasWidth - (labelLeft + label.width()) < 0) {
  393. return false;
  394. }
  395. if (options.series.pie.label.background.opacity != 0) {
  396. // put in the transparent background separately to avoid blended labels and label boxes
  397. var c = options.series.pie.label.background.color;
  398. if (c == null) {
  399. c = slice.color;
  400. }
  401. var pos = "top:" + labelTop + "px;left:" + labelLeft + "px;";
  402. $("<div class='pieLabelBackground' style='position:absolute;width:" + label.width() + "px;height:" + label.height() + "px;" + pos + "background-color:" + c + ";'></div>")
  403. .css("opacity", options.series.pie.label.background.opacity)
  404. .insertBefore(label);
  405. }
  406. return true;
  407. } // end individual label function
  408. } // end drawLabels function
  409. } // end drawPie function
  410. } // end draw function
  411. // Placed here because it needs to be accessed from multiple locations
  412. function drawDonutHole(layer) {
  413. if (options.series.pie.innerRadius > 0) {
  414. // subtract the center
  415. layer.save();
  416. var innerRadius = options.series.pie.innerRadius > 1 ? options.series.pie.innerRadius : maxRadius * options.series.pie.innerRadius;
  417. layer.globalCompositeOperation = "destination-out"; // this does not work with excanvas, but it will fall back to using the stroke color
  418. layer.beginPath();
  419. layer.fillStyle = options.series.pie.stroke.color;
  420. layer.arc(0, 0, innerRadius, 0, Math.PI * 2, false);
  421. layer.fill();
  422. layer.closePath();
  423. layer.restore();
  424. // add inner stroke
  425. layer.save();
  426. layer.beginPath();
  427. layer.strokeStyle = options.series.pie.stroke.color;
  428. layer.arc(0, 0, innerRadius, 0, Math.PI * 2, false);
  429. layer.stroke();
  430. layer.closePath();
  431. layer.restore();
  432. // TODO: add extra shadow inside hole (with a mask) if the pie is tilted.
  433. }
  434. }
  435. //-- Additional Interactive related functions --
  436. function isPointInPoly(poly, pt) {
  437. for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
  438. ((poly[i][1] <= pt[1] && pt[1] < poly[j][1]) || (poly[j][1] <= pt[1] && pt[1]< poly[i][1]))
  439. && (pt[0] < (poly[j][0] - poly[i][0]) * (pt[1] - poly[i][1]) / (poly[j][1] - poly[i][1]) + poly[i][0])
  440. && (c = !c);
  441. return c;
  442. }
  443. function findNearbySlice(mouseX, mouseY) {
  444. var slices = plot.getData(),
  445. options = plot.getOptions(),
  446. radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius,
  447. x, y;
  448. for (var i = 0; i < slices.length; ++i) {
  449. var s = slices[i];
  450. if (s.pie.show) {
  451. ctx.save();
  452. ctx.beginPath();
  453. ctx.moveTo(0, 0); // Center of the pie
  454. //ctx.scale(1, options.series.pie.tilt); // this actually seems to break everything when here.
  455. ctx.arc(0, 0, radius, s.startAngle, s.startAngle + s.angle / 2, false);
  456. ctx.arc(0, 0, radius, s.startAngle + s.angle / 2, s.startAngle + s.angle, false);
  457. ctx.closePath();
  458. x = mouseX - centerLeft;
  459. y = mouseY - centerTop;
  460. if (ctx.isPointInPath) {
  461. if (ctx.isPointInPath(mouseX - centerLeft, mouseY - centerTop)) {
  462. ctx.restore();
  463. return {
  464. datapoint: [s.percent, s.data],
  465. dataIndex: 0,
  466. series: s,
  467. seriesIndex: i
  468. };
  469. }
  470. } else {
  471. // excanvas for IE doesn;t support isPointInPath, this is a workaround.
  472. var p1X = radius * Math.cos(s.startAngle),
  473. p1Y = radius * Math.sin(s.startAngle),
  474. p2X = radius * Math.cos(s.startAngle + s.angle / 4),
  475. p2Y = radius * Math.sin(s.startAngle + s.angle / 4),
  476. p3X = radius * Math.cos(s.startAngle + s.angle / 2),
  477. p3Y = radius * Math.sin(s.startAngle + s.angle / 2),
  478. p4X = radius * Math.cos(s.startAngle + s.angle / 1.5),
  479. p4Y = radius * Math.sin(s.startAngle + s.angle / 1.5),
  480. p5X = radius * Math.cos(s.startAngle + s.angle),
  481. p5Y = radius * Math.sin(s.startAngle + s.angle),
  482. arrPoly = [[0, 0], [p1X, p1Y], [p2X, p2Y], [p3X, p3Y], [p4X, p4Y], [p5X, p5Y]],
  483. arrPoint = [x, y];
  484. // TODO: perhaps do some mathmatical trickery here with the Y-coordinate to compensate for pie tilt?
  485. if (isPointInPoly(arrPoly, arrPoint)) {
  486. ctx.restore();
  487. return {
  488. datapoint: [s.percent, s.data],
  489. dataIndex: 0,
  490. series: s,
  491. seriesIndex: i
  492. };
  493. }
  494. }
  495. ctx.restore();
  496. }
  497. }
  498. return null;
  499. }
  500. function onMouseMove(e) {
  501. triggerClickHoverEvent("plothover", e);
  502. }
  503. function onClick(e) {
  504. triggerClickHoverEvent("plotclick", e);
  505. }
  506. // trigger click or hover event (they send the same parameters so we share their code)
  507. function triggerClickHoverEvent(eventname, e) {
  508. var offset = plot.offset();
  509. var canvasX = parseInt(e.pageX - offset.left);
  510. var canvasY = parseInt(e.pageY - offset.top);
  511. var item = findNearbySlice(canvasX, canvasY);
  512. if (options.grid.autoHighlight) {
  513. // clear auto-highlights
  514. for (var i = 0; i < highlights.length; ++i) {
  515. var h = highlights[i];
  516. if (h.auto == eventname && !(item && h.series == item.series)) {
  517. unhighlight(h.series);
  518. }
  519. }
  520. }
  521. // highlight the slice
  522. if (item) {
  523. highlight(item.series, eventname);
  524. }
  525. // trigger any hover bind events
  526. var pos = { pageX: e.pageX, pageY: e.pageY };
  527. target.trigger(eventname, [pos, item]);
  528. }
  529. function highlight(s, auto) {
  530. //if (typeof s == "number") {
  531. // s = series[s];
  532. //}
  533. var i = indexOfHighlight(s);
  534. if (i == -1) {
  535. highlights.push({ series: s, auto: auto });
  536. plot.triggerRedrawOverlay();
  537. } else if (!auto) {
  538. highlights[i].auto = false;
  539. }
  540. }
  541. function unhighlight(s) {
  542. if (s == null) {
  543. highlights = [];
  544. plot.triggerRedrawOverlay();
  545. }
  546. //if (typeof s == "number") {
  547. // s = series[s];
  548. //}
  549. var i = indexOfHighlight(s);
  550. if (i != -1) {
  551. highlights.splice(i, 1);
  552. plot.triggerRedrawOverlay();
  553. }
  554. }
  555. function indexOfHighlight(s) {
  556. for (var i = 0; i < highlights.length; ++i) {
  557. var h = highlights[i];
  558. if (h.series == s)
  559. return i;
  560. }
  561. return -1;
  562. }
  563. function drawOverlay(plot, octx) {
  564. var options = plot.getOptions();
  565. var radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius;
  566. octx.save();
  567. octx.translate(centerLeft, centerTop);
  568. octx.scale(1, options.series.pie.tilt);
  569. for (var i = 0; i < highlights.length; ++i) {
  570. drawHighlight(highlights[i].series);
  571. }
  572. drawDonutHole(octx);
  573. octx.restore();
  574. function drawHighlight(series) {
  575. if (series.angle <= 0 || isNaN(series.angle)) {
  576. return;
  577. }
  578. //octx.fillStyle = parseColor(options.series.pie.highlight.color).scale(null, null, null, options.series.pie.highlight.opacity).toString();
  579. octx.fillStyle = "rgba(255, 255, 255, " + options.series.pie.highlight.opacity + ")"; // this is temporary until we have access to parseColor
  580. octx.beginPath();
  581. if (Math.abs(series.angle - Math.PI * 2) > 0.000000001) {
  582. octx.moveTo(0, 0); // Center of the pie
  583. }
  584. octx.arc(0, 0, radius, series.startAngle, series.startAngle + series.angle / 2, false);
  585. octx.arc(0, 0, radius, series.startAngle + series.angle / 2, series.startAngle + series.angle, false);
  586. octx.closePath();
  587. octx.fill();
  588. }
  589. }
  590. } // end init (plugin body)
  591. // define pie specific options and their default values
  592. var options = {
  593. series: {
  594. pie: {
  595. show: false,
  596. radius: "auto", // actual radius of the visible pie (based on full calculated radius if <=1, or hard pixel value)
  597. innerRadius: 0, /* for donut */
  598. startAngle: 3/2,
  599. tilt: 1,
  600. shadow: {
  601. left: 5, // shadow left offset
  602. top: 15, // shadow top offset
  603. alpha: 0.02 // shadow alpha
  604. },
  605. offset: {
  606. top: 0,
  607. left: "auto"
  608. },
  609. stroke: {
  610. color: "#fff",
  611. width: 1
  612. },
  613. label: {
  614. show: "auto",
  615. formatter: function(label, slice) {
  616. return "<div style='font-size:x-small;text-align:center;padding:2px;color:" + slice.color + ";'>" + label + "<br/>" + Math.round(slice.percent) + "%</div>";
  617. }, // formatter function
  618. radius: 1, // radius at which to place the labels (based on full calculated radius if <=1, or hard pixel value)
  619. background: {
  620. color: null,
  621. opacity: 0
  622. },
  623. threshold: 0 // percentage at which to hide the label (i.e. the slice is too narrow)
  624. },
  625. combine: {
  626. threshold: -1, // percentage at which to combine little slices into one larger slice
  627. color: null, // color to give the new slice (auto-generated if null)
  628. label: "Other" // label to give the new slice
  629. },
  630. highlight: {
  631. //color: "#fff", // will add this functionality once parseColor is available
  632. opacity: 0.5
  633. }
  634. }
  635. }
  636. };
  637. $.plot.plugins.push({
  638. init: init,
  639. options: options,
  640. name: "pie",
  641. version: "1.1"
  642. });
  643. })(jQuery);