jquery.flot.pie.js 22 KB

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