jquery.flot.pie.js 24 KB

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