jqplot.pieRenderer.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. /**
  2. * jqPlot
  3. * Pure JavaScript plotting plugin using jQuery
  4. *
  5. * Version: @VERSION
  6. * Revision: @REVISION
  7. *
  8. * Copyright (c) 2009-2013 Chris Leonello
  9. * jqPlot is currently available for use in all personal or commercial projects
  10. * under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL
  11. * version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can
  12. * choose the license that best suits your project and use it accordingly.
  13. *
  14. * Although not required, the author would appreciate an email letting him
  15. * know of any substantial use of jqPlot. You can reach the author at:
  16. * chris at jqplot dot com or see http://www.jqplot.com/info.php .
  17. *
  18. * If you are feeling kind and generous, consider supporting the project by
  19. * making a donation at: http://www.jqplot.com/donate.php .
  20. *
  21. * sprintf functions contained in jqplot.sprintf.js by Ash Searle:
  22. *
  23. * version 2007.04.27
  24. * author Ash Searle
  25. * http://hexmen.com/blog/2007/03/printf-sprintf/
  26. * http://hexmen.com/js/sprintf.js
  27. * The author (Ash Searle) has placed this code in the public domain:
  28. * "This code is unrestricted: you are free to use it however you like."
  29. *
  30. */
  31. (function($) {
  32. /**
  33. * Class: $.jqplot.PieRenderer
  34. * Plugin renderer to draw a pie chart.
  35. * x values, if present, will be used as slice labels.
  36. * y values give slice size.
  37. *
  38. * To use this renderer, you need to include the
  39. * pie renderer plugin, for example:
  40. *
  41. * > <script type="text/javascript" src="plugins/jqplot.pieRenderer.js"></script>
  42. *
  43. * Properties described here are passed into the $.jqplot function
  44. * as options on the series renderer. For example:
  45. *
  46. * > plot2 = $.jqplot('chart2', [s1, s2], {
  47. * > seriesDefaults: {
  48. * > renderer:$.jqplot.PieRenderer,
  49. * > rendererOptions:{
  50. * > sliceMargin: 2,
  51. * > startAngle: -90
  52. * > }
  53. * > }
  54. * > });
  55. *
  56. * A pie plot will trigger events on the plot target
  57. * according to user interaction. All events return the event object,
  58. * the series index, the point (slice) index, and the point data for
  59. * the appropriate slice.
  60. *
  61. * 'jqplotDataMouseOver' - triggered when user mouseing over a slice.
  62. * 'jqplotDataHighlight' - triggered the first time user mouses over a slice,
  63. * if highlighting is enabled.
  64. * 'jqplotDataUnhighlight' - triggered when a user moves the mouse out of
  65. * a highlighted slice.
  66. * 'jqplotDataClick' - triggered when the user clicks on a slice.
  67. * 'jqplotDataRightClick' - tiggered when the user right clicks on a slice if
  68. * the "captureRightClick" option is set to true on the plot.
  69. */
  70. $.jqplot.PieRenderer = function(){
  71. $.jqplot.LineRenderer.call(this);
  72. };
  73. $.jqplot.PieRenderer.prototype = new $.jqplot.LineRenderer();
  74. $.jqplot.PieRenderer.prototype.constructor = $.jqplot.PieRenderer;
  75. // called with scope of a series
  76. $.jqplot.PieRenderer.prototype.init = function(options, plot) {
  77. // Group: Properties
  78. //
  79. // prop: diameter
  80. // Outer diameter of the pie, auto computed by default
  81. this.diameter = null;
  82. // prop: padding
  83. // padding between the pie and plot edges, legend, etc.
  84. this.padding = 20;
  85. // prop: sliceMargin
  86. // angular spacing between pie slices in degrees.
  87. this.sliceMargin = 0;
  88. // prop: fill
  89. // true or false, whether to fil the slices.
  90. this.fill = true;
  91. // prop: shadowOffset
  92. // offset of the shadow from the slice and offset of
  93. // each succesive stroke of the shadow from the last.
  94. this.shadowOffset = 2;
  95. // prop: shadowAlpha
  96. // transparency of the shadow (0 = transparent, 1 = opaque)
  97. this.shadowAlpha = 0.07;
  98. // prop: shadowDepth
  99. // number of strokes to apply to the shadow,
  100. // each stroke offset shadowOffset from the last.
  101. this.shadowDepth = 5;
  102. // prop: highlightMouseOver
  103. // True to highlight slice when moused over.
  104. // This must be false to enable highlightMouseDown to highlight when clicking on a slice.
  105. this.highlightMouseOver = true;
  106. // prop: highlightMouseDown
  107. // True to highlight when a mouse button is pressed over a slice.
  108. // This will be disabled if highlightMouseOver is true.
  109. this.highlightMouseDown = false;
  110. // prop: highlightColors
  111. // an array of colors to use when highlighting a slice.
  112. this.highlightColors = [];
  113. // prop: dataLabels
  114. // Either 'label', 'value', 'percent' or an array of labels to place on the pie slices.
  115. // Defaults to percentage of each pie slice.
  116. this.dataLabels = 'percent';
  117. // prop: showDataLabels
  118. // true to show data labels on slices.
  119. this.showDataLabels = false;
  120. // prop: dataLabelFormatString
  121. // Format string for data labels. If none, '%s' is used for "label" and for arrays, '%d' for value and '%d%%' for percentage.
  122. this.dataLabelFormatString = null;
  123. // prop: dataLabelThreshold
  124. // Threshhold in percentage (0-100) of pie area, below which no label will be displayed.
  125. // This applies to all label types, not just to percentage labels.
  126. this.dataLabelThreshold = 3;
  127. // prop: dataLabelPositionFactor
  128. // A Multiplier (0-1) of the pie radius which controls position of label on slice.
  129. // Increasing will slide label toward edge of pie, decreasing will slide label toward center of pie.
  130. this.dataLabelPositionFactor = 0.52;
  131. // prop: dataLabelNudge
  132. // Number of pixels to slide the label away from (+) or toward (-) the center of the pie.
  133. this.dataLabelNudge = 2;
  134. // prop: dataLabelCenterOn
  135. // True to center the data label at its position.
  136. // False to set the inside facing edge of the label at its position.
  137. this.dataLabelCenterOn = true;
  138. // prop: startAngle
  139. // Angle to start drawing pie in degrees.
  140. // According to orientation of canvas coordinate system:
  141. // 0 = on the positive x axis
  142. // -90 = on the positive y axis.
  143. // 90 = on the negaive y axis.
  144. // 180 or - 180 = on the negative x axis.
  145. this.startAngle = 0;
  146. this.tickRenderer = $.jqplot.PieTickRenderer;
  147. // Used as check for conditions where pie shouldn't be drawn.
  148. this._drawData = true;
  149. this._type = 'pie';
  150. // if user has passed in highlightMouseDown option and not set highlightMouseOver, disable highlightMouseOver
  151. if (options.highlightMouseDown && options.highlightMouseOver == null) {
  152. options.highlightMouseOver = false;
  153. }
  154. $.extend(true, this, options);
  155. if (this.sliceMargin < 0) {
  156. this.sliceMargin = 0;
  157. }
  158. this._diameter = null;
  159. this._radius = null;
  160. // array of [start,end] angles arrays, one for each slice. In radians.
  161. this._sliceAngles = [];
  162. // index of the currenty highlighted point, if any
  163. this._highlightedPoint = null;
  164. // set highlight colors if none provided
  165. if (this.highlightColors.length == 0) {
  166. for (var i=0; i<this.seriesColors.length; i++){
  167. var rgba = $.jqplot.getColorComponents(this.seriesColors[i]);
  168. var newrgb = [rgba[0], rgba[1], rgba[2]];
  169. var sum = newrgb[0] + newrgb[1] + newrgb[2];
  170. for (var j=0; j<3; j++) {
  171. // when darkening, lowest color component can be is 60.
  172. newrgb[j] = (sum > 570) ? newrgb[j] * 0.8 : newrgb[j] + 0.3 * (255 - newrgb[j]);
  173. newrgb[j] = parseInt(newrgb[j], 10);
  174. }
  175. this.highlightColors.push('rgb('+newrgb[0]+','+newrgb[1]+','+newrgb[2]+')');
  176. }
  177. }
  178. this.highlightColorGenerator = new $.jqplot.ColorGenerator(this.highlightColors);
  179. plot.postParseOptionsHooks.addOnce(postParseOptions);
  180. plot.postInitHooks.addOnce(postInit);
  181. plot.eventListenerHooks.addOnce('jqplotMouseMove', handleMove);
  182. plot.eventListenerHooks.addOnce('jqplotMouseDown', handleMouseDown);
  183. plot.eventListenerHooks.addOnce('jqplotMouseUp', handleMouseUp);
  184. plot.eventListenerHooks.addOnce('jqplotClick', handleClick);
  185. plot.eventListenerHooks.addOnce('jqplotRightClick', handleRightClick);
  186. plot.postDrawHooks.addOnce(postPlotDraw);
  187. };
  188. $.jqplot.PieRenderer.prototype.setGridData = function(plot) {
  189. // set gridData property. This will hold angle in radians of each data point.
  190. var stack = [];
  191. var td = [];
  192. var sa = this.startAngle/180*Math.PI;
  193. var tot = 0;
  194. // don't know if we have any valid data yet, so set plot to not draw.
  195. this._drawData = false;
  196. for (var i=0; i<this.data.length; i++){
  197. if (this.data[i][1] != 0) {
  198. // we have data, O.K. to draw.
  199. this._drawData = true;
  200. }
  201. stack.push(this.data[i][1]);
  202. td.push([this.data[i][0]]);
  203. if (i>0) {
  204. stack[i] += stack[i-1];
  205. }
  206. tot += this.data[i][1];
  207. }
  208. var fact = Math.PI*2/stack[stack.length - 1];
  209. for (var i=0; i<stack.length; i++) {
  210. td[i][1] = stack[i] * fact;
  211. td[i][2] = this.data[i][1]/tot;
  212. }
  213. this.gridData = td;
  214. };
  215. $.jqplot.PieRenderer.prototype.makeGridData = function(data, plot) {
  216. var stack = [];
  217. var td = [];
  218. var tot = 0;
  219. var sa = this.startAngle/180*Math.PI;
  220. // don't know if we have any valid data yet, so set plot to not draw.
  221. this._drawData = false;
  222. for (var i=0; i<data.length; i++){
  223. if (this.data[i][1] != 0) {
  224. // we have data, O.K. to draw.
  225. this._drawData = true;
  226. }
  227. stack.push(data[i][1]);
  228. td.push([data[i][0]]);
  229. if (i>0) {
  230. stack[i] += stack[i-1];
  231. }
  232. tot += data[i][1];
  233. }
  234. var fact = Math.PI*2/stack[stack.length - 1];
  235. for (var i=0; i<stack.length; i++) {
  236. td[i][1] = stack[i] * fact;
  237. td[i][2] = data[i][1]/tot;
  238. }
  239. return td;
  240. };
  241. function calcRadiusAdjustment(ang) {
  242. return Math.sin((ang - (ang-Math.PI) / 8 / Math.PI )/2.0);
  243. }
  244. function calcRPrime(ang1, ang2, sliceMargin, fill, lineWidth) {
  245. var rprime = 0;
  246. var ang = ang2 - ang1;
  247. var absang = Math.abs(ang);
  248. var sm = sliceMargin;
  249. if (fill == false) {
  250. sm += lineWidth;
  251. }
  252. if (sm > 0 && absang > 0.01 && absang < 6.282) {
  253. rprime = parseFloat(sm) / 2.0 / calcRadiusAdjustment(ang);
  254. }
  255. return rprime;
  256. }
  257. $.jqplot.PieRenderer.prototype.drawSlice = function (ctx, ang1, ang2, color, isShadow) {
  258. if (this._drawData) {
  259. var r = this._radius;
  260. var fill = this.fill;
  261. var lineWidth = this.lineWidth;
  262. var sm = this.sliceMargin;
  263. if (this.fill == false) {
  264. sm += this.lineWidth;
  265. }
  266. ctx.save();
  267. ctx.translate(this._center[0], this._center[1]);
  268. var rprime = calcRPrime(ang1, ang2, this.sliceMargin, this.fill, this.lineWidth);
  269. var transx = rprime * Math.cos((ang1 + ang2) / 2.0);
  270. var transy = rprime * Math.sin((ang1 + ang2) / 2.0);
  271. if ((ang2 - ang1) <= Math.PI) {
  272. r -= rprime;
  273. }
  274. else {
  275. r += rprime;
  276. }
  277. ctx.translate(transx, transy);
  278. if (isShadow) {
  279. for (var i=0, l=this.shadowDepth; i<l; i++) {
  280. ctx.save();
  281. ctx.translate(this.shadowOffset*Math.cos(this.shadowAngle/180*Math.PI), this.shadowOffset*Math.sin(this.shadowAngle/180*Math.PI));
  282. doDraw(r);
  283. }
  284. for (var i=0, l=this.shadowDepth; i<l; i++) {
  285. ctx.restore();
  286. }
  287. }
  288. else {
  289. doDraw(r);
  290. }
  291. ctx.restore();
  292. }
  293. function doDraw (rad) {
  294. // Fix for IE and Chrome that can't seem to draw circles correctly.
  295. // ang2 should always be <= 2 pi since that is the way the data is converted.
  296. // 2Pi = 6.2831853, Pi = 3.1415927
  297. if (ang2 > 6.282 + this.startAngle) {
  298. ang2 = 6.282 + this.startAngle;
  299. if (ang1 > ang2) {
  300. ang1 = 6.281 + this.startAngle;
  301. }
  302. }
  303. // Fix for IE, where it can't seem to handle 0 degree angles. Also avoids
  304. // ugly line on unfilled pies.
  305. if (ang1 >= ang2) {
  306. return;
  307. }
  308. ctx.beginPath();
  309. ctx.fillStyle = color;
  310. ctx.strokeStyle = color;
  311. ctx.lineWidth = lineWidth;
  312. ctx.arc(0, 0, rad, ang1, ang2, false);
  313. ctx.lineTo(0,0);
  314. ctx.closePath();
  315. if (fill) {
  316. ctx.fill();
  317. }
  318. else {
  319. ctx.stroke();
  320. }
  321. }
  322. };
  323. // called with scope of series
  324. $.jqplot.PieRenderer.prototype.draw = function (ctx, gd, options, plot) {
  325. var i;
  326. var opts = (options != undefined) ? options : {};
  327. // offset and direction of offset due to legend placement
  328. var offx = 0;
  329. var offy = 0;
  330. var trans = 1;
  331. var colorGenerator = new $.jqplot.ColorGenerator(this.seriesColors);
  332. if (options.legendInfo && options.legendInfo.placement == 'insideGrid') {
  333. var li = options.legendInfo;
  334. switch (li.location) {
  335. case 'nw':
  336. offx = li.width + li.xoffset;
  337. break;
  338. case 'w':
  339. offx = li.width + li.xoffset;
  340. break;
  341. case 'sw':
  342. offx = li.width + li.xoffset;
  343. break;
  344. case 'ne':
  345. offx = li.width + li.xoffset;
  346. trans = -1;
  347. break;
  348. case 'e':
  349. offx = li.width + li.xoffset;
  350. trans = -1;
  351. break;
  352. case 'se':
  353. offx = li.width + li.xoffset;
  354. trans = -1;
  355. break;
  356. case 'n':
  357. offy = li.height + li.yoffset;
  358. break;
  359. case 's':
  360. offy = li.height + li.yoffset;
  361. trans = -1;
  362. break;
  363. default:
  364. break;
  365. }
  366. }
  367. var shadow = (opts.shadow != undefined) ? opts.shadow : this.shadow;
  368. var fill = (opts.fill != undefined) ? opts.fill : this.fill;
  369. var cw = ctx.canvas.width;
  370. var ch = ctx.canvas.height;
  371. var w = cw - offx - 2 * this.padding;
  372. var h = ch - offy - 2 * this.padding;
  373. var mindim = Math.min(w,h);
  374. var d = mindim;
  375. // Fixes issue #272. Thanks hugwijst!
  376. // reset slice angles array.
  377. this._sliceAngles = [];
  378. var sm = this.sliceMargin;
  379. if (this.fill == false) {
  380. sm += this.lineWidth;
  381. }
  382. var rprime;
  383. var maxrprime = 0;
  384. var ang, ang1, ang2, shadowColor;
  385. var sa = this.startAngle / 180 * Math.PI;
  386. // have to pre-draw shadows, so loop throgh here and calculate some values also.
  387. for (var i=0, l=gd.length; i<l; i++) {
  388. ang1 = (i == 0) ? sa : gd[i-1][1] + sa;
  389. ang2 = gd[i][1] + sa;
  390. this._sliceAngles.push([ang1, ang2]);
  391. rprime = calcRPrime(ang1, ang2, this.sliceMargin, this.fill, this.lineWidth);
  392. if (Math.abs(ang2-ang1) > Math.PI) {
  393. maxrprime = Math.max(rprime, maxrprime);
  394. }
  395. }
  396. if (this.diameter != null && this.diameter > 0) {
  397. this._diameter = this.diameter - 2*maxrprime;
  398. }
  399. else {
  400. this._diameter = d - 2*maxrprime;
  401. }
  402. // Need to check for undersized pie. This can happen if
  403. // plot area too small and legend is too big.
  404. if (this._diameter < 6) {
  405. $.jqplot.log('Diameter of pie too small, not rendering.');
  406. return;
  407. }
  408. var r = this._radius = this._diameter/2;
  409. this._center = [(cw - trans * offx)/2 + trans * offx + maxrprime * Math.cos(sa), (ch - trans*offy)/2 + trans * offy + maxrprime * Math.sin(sa)];
  410. if (this.shadow) {
  411. for (var i=0, l=gd.length; i<l; i++) {
  412. shadowColor = 'rgba(0,0,0,'+this.shadowAlpha+')';
  413. this.renderer.drawSlice.call (this, ctx, this._sliceAngles[i][0], this._sliceAngles[i][1], shadowColor, true);
  414. }
  415. }
  416. for (var i=0; i<gd.length; i++) {
  417. this.renderer.drawSlice.call (this, ctx, this._sliceAngles[i][0], this._sliceAngles[i][1], colorGenerator.next(), false);
  418. if (this.showDataLabels && gd[i][2]*100 >= this.dataLabelThreshold) {
  419. var fstr, avgang = (this._sliceAngles[i][0] + this._sliceAngles[i][1])/2, label;
  420. if (this.dataLabels == 'label') {
  421. fstr = this.dataLabelFormatString || '%s';
  422. label = $.jqplot.sprintf(fstr, gd[i][0]);
  423. }
  424. else if (this.dataLabels == 'value') {
  425. fstr = this.dataLabelFormatString || '%d';
  426. label = $.jqplot.sprintf(fstr, this.data[i][1]);
  427. }
  428. else if (this.dataLabels == 'percent') {
  429. fstr = this.dataLabelFormatString || '%d%%';
  430. label = $.jqplot.sprintf(fstr, gd[i][2]*100);
  431. }
  432. else if (this.dataLabels.constructor == Array) {
  433. fstr = this.dataLabelFormatString || '%s';
  434. label = $.jqplot.sprintf(fstr, this.dataLabels[i]);
  435. }
  436. var fact = (this._radius ) * this.dataLabelPositionFactor + this.sliceMargin + this.dataLabelNudge;
  437. var x = this._center[0] + Math.cos(avgang) * fact + this.canvas._offsets.left;
  438. var y = this._center[1] + Math.sin(avgang) * fact + this.canvas._offsets.top;
  439. var labelelem = $('<div class="jqplot-pie-series jqplot-data-label" style="position:absolute;">' + label + '</div>').insertBefore(plot.eventCanvas._elem);
  440. if (this.dataLabelCenterOn) {
  441. x -= labelelem.width()/2;
  442. y -= labelelem.height()/2;
  443. }
  444. else {
  445. x -= labelelem.width() * Math.sin(avgang/2);
  446. y -= labelelem.height()/2;
  447. }
  448. x = Math.round(x);
  449. y = Math.round(y);
  450. labelelem.css({left: x, top: y});
  451. }
  452. }
  453. };
  454. $.jqplot.PieAxisRenderer = function() {
  455. $.jqplot.LinearAxisRenderer.call(this);
  456. };
  457. $.jqplot.PieAxisRenderer.prototype = new $.jqplot.LinearAxisRenderer();
  458. $.jqplot.PieAxisRenderer.prototype.constructor = $.jqplot.PieAxisRenderer;
  459. // There are no traditional axes on a pie chart. We just need to provide
  460. // dummy objects with properties so the plot will render.
  461. // called with scope of axis object.
  462. $.jqplot.PieAxisRenderer.prototype.init = function(options){
  463. //
  464. this.tickRenderer = $.jqplot.PieTickRenderer;
  465. $.extend(true, this, options);
  466. // I don't think I'm going to need _dataBounds here.
  467. // have to go Axis scaling in a way to fit chart onto plot area
  468. // and provide u2p and p2u functionality for mouse cursor, etc.
  469. // for convienence set _dataBounds to 0 and 100 and
  470. // set min/max to 0 and 100.
  471. this._dataBounds = {min:0, max:100};
  472. this.min = 0;
  473. this.max = 100;
  474. this.showTicks = false;
  475. this.ticks = [];
  476. this.showMark = false;
  477. this.show = false;
  478. };
  479. $.jqplot.PieLegendRenderer = function(){
  480. $.jqplot.TableLegendRenderer.call(this);
  481. };
  482. $.jqplot.PieLegendRenderer.prototype = new $.jqplot.TableLegendRenderer();
  483. $.jqplot.PieLegendRenderer.prototype.constructor = $.jqplot.PieLegendRenderer;
  484. /**
  485. * Class: $.jqplot.PieLegendRenderer
  486. * Legend Renderer specific to pie plots. Set by default
  487. * when user creates a pie plot.
  488. */
  489. $.jqplot.PieLegendRenderer.prototype.init = function(options) {
  490. // Group: Properties
  491. //
  492. // prop: numberRows
  493. // Maximum number of rows in the legend. 0 or null for unlimited.
  494. this.numberRows = null;
  495. // prop: numberColumns
  496. // Maximum number of columns in the legend. 0 or null for unlimited.
  497. this.numberColumns = null;
  498. $.extend(true, this, options);
  499. };
  500. // called with context of legend
  501. $.jqplot.PieLegendRenderer.prototype.draw = function() {
  502. var legend = this;
  503. if (this.show) {
  504. var series = this._series;
  505. this._elem = $(document.createElement('table'));
  506. this._elem.addClass('jqplot-table-legend');
  507. var ss = {position:'absolute'};
  508. if (this.background) {
  509. ss['background'] = this.background;
  510. }
  511. if (this.border) {
  512. ss['border'] = this.border;
  513. }
  514. if (this.fontSize) {
  515. ss['fontSize'] = this.fontSize;
  516. }
  517. if (this.fontFamily) {
  518. ss['fontFamily'] = this.fontFamily;
  519. }
  520. if (this.textColor) {
  521. ss['textColor'] = this.textColor;
  522. }
  523. if (this.marginTop != null) {
  524. ss['marginTop'] = this.marginTop;
  525. }
  526. if (this.marginBottom != null) {
  527. ss['marginBottom'] = this.marginBottom;
  528. }
  529. if (this.marginLeft != null) {
  530. ss['marginLeft'] = this.marginLeft;
  531. }
  532. if (this.marginRight != null) {
  533. ss['marginRight'] = this.marginRight;
  534. }
  535. this._elem.css(ss);
  536. // Pie charts legends don't go by number of series, but by number of data points
  537. // in the series. Refactor things here for that.
  538. var pad = false,
  539. reverse = false,
  540. nr,
  541. nc;
  542. var s = series[0];
  543. var colorGenerator = new $.jqplot.ColorGenerator(s.seriesColors);
  544. if (s.show) {
  545. var pd = s.data;
  546. if (this.numberRows) {
  547. nr = this.numberRows;
  548. if (!this.numberColumns){
  549. nc = Math.ceil(pd.length/nr);
  550. }
  551. else{
  552. nc = this.numberColumns;
  553. }
  554. }
  555. else if (this.numberColumns) {
  556. nc = this.numberColumns;
  557. nr = Math.ceil(pd.length/this.numberColumns);
  558. }
  559. else {
  560. nr = pd.length;
  561. nc = 1;
  562. }
  563. var i, j;
  564. var tr, td1, td2;
  565. var lt, rs, color;
  566. var idx = 0;
  567. var div0, div1;
  568. for (i=0; i<nr; i++) {
  569. tr = $(document.createElement('tr'));
  570. tr.addClass('jqplot-table-legend');
  571. if (reverse){
  572. tr.prependTo(this._elem);
  573. }
  574. else{
  575. tr.appendTo(this._elem);
  576. }
  577. for (j=0; j<nc; j++) {
  578. if (idx < pd.length){
  579. lt = this.labels[idx] || pd[idx][0].toString();
  580. color = colorGenerator.next();
  581. if (!reverse){
  582. if (i>0){
  583. pad = true;
  584. }
  585. else{
  586. pad = false;
  587. }
  588. }
  589. else{
  590. if (i == nr -1){
  591. pad = false;
  592. }
  593. else{
  594. pad = true;
  595. }
  596. }
  597. rs = (pad) ? this.rowSpacing : '0';
  598. td1 = $(document.createElement('td'));
  599. td1.addClass('jqplot-table-legend jqplot-table-legend-swatch');
  600. td1.css({textAlign: 'center', paddingTop: rs});
  601. div0 = $(document.createElement('div'));
  602. div0.addClass('jqplot-table-legend-swatch-outline');
  603. div1 = $(document.createElement('div'));
  604. div1.addClass('jqplot-table-legend-swatch');
  605. div1.css({backgroundColor: color, borderColor: color});
  606. td1.append(div0.append(div1));
  607. td2 = $(document.createElement('td'));
  608. td2.addClass('jqplot-table-legend jqplot-table-legend-label');
  609. td2.css('paddingTop', rs);
  610. if (this.escapeHtml){
  611. td2.text(lt);
  612. }
  613. else {
  614. td2.html(lt);
  615. }
  616. if (reverse) {
  617. td2.prependTo(tr);
  618. td1.prependTo(tr);
  619. }
  620. else {
  621. td1.appendTo(tr);
  622. td2.appendTo(tr);
  623. }
  624. pad = true;
  625. }
  626. idx++;
  627. }
  628. }
  629. }
  630. }
  631. return this._elem;
  632. };
  633. $.jqplot.PieRenderer.prototype.handleMove = function(ev, gridpos, datapos, neighbor, plot) {
  634. if (neighbor) {
  635. var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data];
  636. plot.target.trigger('jqplotDataMouseOver', ins);
  637. if (plot.series[ins[0]].highlightMouseOver && !(ins[0] == plot.plugins.pieRenderer.highlightedSeriesIndex && ins[1] == plot.series[ins[0]]._highlightedPoint)) {
  638. plot.target.trigger('jqplotDataHighlight', ins);
  639. highlight (plot, ins[0], ins[1]);
  640. }
  641. }
  642. else if (neighbor == null) {
  643. unhighlight (plot);
  644. }
  645. };
  646. // this.eventCanvas._elem.bind($.jqplot.eventListenerHooks[i][0], {plot:this}, $.jqplot.eventListenerHooks[i][1]);
  647. // setup default renderers for axes and legend so user doesn't have to
  648. // called with scope of plot
  649. function preInit(target, data, options) {
  650. options = options || {};
  651. options.axesDefaults = options.axesDefaults || {};
  652. options.legend = options.legend || {};
  653. options.seriesDefaults = options.seriesDefaults || {};
  654. // only set these if there is a pie series
  655. var setopts = false;
  656. if (options.seriesDefaults.renderer == $.jqplot.PieRenderer) {
  657. setopts = true;
  658. }
  659. else if (options.series) {
  660. for (var i=0; i < options.series.length; i++) {
  661. if (options.series[i].renderer == $.jqplot.PieRenderer) {
  662. setopts = true;
  663. }
  664. }
  665. }
  666. if (setopts) {
  667. options.axesDefaults.renderer = $.jqplot.PieAxisRenderer;
  668. options.legend.renderer = $.jqplot.PieLegendRenderer;
  669. options.legend.preDraw = true;
  670. options.seriesDefaults.pointLabels = {show: false};
  671. }
  672. }
  673. function postInit(target, data, options) {
  674. for (var i=0; i<this.series.length; i++) {
  675. if (this.series[i].renderer.constructor == $.jqplot.PieRenderer) {
  676. // don't allow mouseover and mousedown at same time.
  677. if (this.series[i].highlightMouseOver) {
  678. this.series[i].highlightMouseDown = false;
  679. }
  680. }
  681. }
  682. }
  683. // called with scope of plot
  684. function postParseOptions(options) {
  685. for (var i=0; i<this.series.length; i++) {
  686. this.series[i].seriesColors = this.seriesColors;
  687. this.series[i].colorGenerator = $.jqplot.colorGenerator;
  688. }
  689. }
  690. function highlight (plot, sidx, pidx) {
  691. var s = plot.series[sidx];
  692. var canvas = plot.plugins.pieRenderer.highlightCanvas;
  693. canvas._ctx.clearRect(0,0,canvas._ctx.canvas.width, canvas._ctx.canvas.height);
  694. s._highlightedPoint = pidx;
  695. plot.plugins.pieRenderer.highlightedSeriesIndex = sidx;
  696. s.renderer.drawSlice.call(s, canvas._ctx, s._sliceAngles[pidx][0], s._sliceAngles[pidx][1], s.highlightColorGenerator.get(pidx), false);
  697. }
  698. function unhighlight (plot) {
  699. var canvas = plot.plugins.pieRenderer.highlightCanvas;
  700. canvas._ctx.clearRect(0,0, canvas._ctx.canvas.width, canvas._ctx.canvas.height);
  701. for (var i=0; i<plot.series.length; i++) {
  702. plot.series[i]._highlightedPoint = null;
  703. }
  704. plot.plugins.pieRenderer.highlightedSeriesIndex = null;
  705. plot.target.trigger('jqplotDataUnhighlight');
  706. }
  707. function handleMove(ev, gridpos, datapos, neighbor, plot) {
  708. if (neighbor) {
  709. var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data];
  710. var evt1 = jQuery.Event('jqplotDataMouseOver');
  711. evt1.pageX = ev.pageX;
  712. evt1.pageY = ev.pageY;
  713. plot.target.trigger(evt1, ins);
  714. if (plot.series[ins[0]].highlightMouseOver && !(ins[0] == plot.plugins.pieRenderer.highlightedSeriesIndex && ins[1] == plot.series[ins[0]]._highlightedPoint)) {
  715. var evt = jQuery.Event('jqplotDataHighlight');
  716. evt.which = ev.which;
  717. evt.pageX = ev.pageX;
  718. evt.pageY = ev.pageY;
  719. plot.target.trigger(evt, ins);
  720. highlight (plot, ins[0], ins[1]);
  721. }
  722. }
  723. else if (neighbor == null) {
  724. unhighlight (plot);
  725. }
  726. }
  727. function handleMouseDown(ev, gridpos, datapos, neighbor, plot) {
  728. if (neighbor) {
  729. var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data];
  730. if (plot.series[ins[0]].highlightMouseDown && !(ins[0] == plot.plugins.pieRenderer.highlightedSeriesIndex && ins[1] == plot.series[ins[0]]._highlightedPoint)) {
  731. var evt = jQuery.Event('jqplotDataHighlight');
  732. evt.which = ev.which;
  733. evt.pageX = ev.pageX;
  734. evt.pageY = ev.pageY;
  735. plot.target.trigger(evt, ins);
  736. highlight (plot, ins[0], ins[1]);
  737. }
  738. }
  739. else if (neighbor == null) {
  740. unhighlight (plot);
  741. }
  742. }
  743. function handleMouseUp(ev, gridpos, datapos, neighbor, plot) {
  744. var idx = plot.plugins.pieRenderer.highlightedSeriesIndex;
  745. if (idx != null && plot.series[idx].highlightMouseDown) {
  746. unhighlight(plot);
  747. }
  748. }
  749. function handleClick(ev, gridpos, datapos, neighbor, plot) {
  750. if (neighbor) {
  751. var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data];
  752. var evt = jQuery.Event('jqplotDataClick');
  753. evt.which = ev.which;
  754. evt.pageX = ev.pageX;
  755. evt.pageY = ev.pageY;
  756. plot.target.trigger(evt, ins);
  757. }
  758. }
  759. function handleRightClick(ev, gridpos, datapos, neighbor, plot) {
  760. if (neighbor) {
  761. var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data];
  762. var idx = plot.plugins.pieRenderer.highlightedSeriesIndex;
  763. if (idx != null && plot.series[idx].highlightMouseDown) {
  764. unhighlight(plot);
  765. }
  766. var evt = jQuery.Event('jqplotDataRightClick');
  767. evt.which = ev.which;
  768. evt.pageX = ev.pageX;
  769. evt.pageY = ev.pageY;
  770. plot.target.trigger(evt, ins);
  771. }
  772. }
  773. // called within context of plot
  774. // create a canvas which we can draw on.
  775. // insert it before the eventCanvas, so eventCanvas will still capture events.
  776. function postPlotDraw() {
  777. // Memory Leaks patch
  778. if (this.plugins.pieRenderer && this.plugins.pieRenderer.highlightCanvas) {
  779. this.plugins.pieRenderer.highlightCanvas.resetCanvas();
  780. this.plugins.pieRenderer.highlightCanvas = null;
  781. }
  782. this.plugins.pieRenderer = {highlightedSeriesIndex:null};
  783. this.plugins.pieRenderer.highlightCanvas = new $.jqplot.GenericCanvas();
  784. // do we have any data labels? if so, put highlight canvas before those
  785. var labels = $(this.targetId+' .jqplot-data-label');
  786. if (labels.length) {
  787. $(labels[0]).before(this.plugins.pieRenderer.highlightCanvas.createElement(this._gridPadding, 'jqplot-pieRenderer-highlight-canvas', this._plotDimensions, this));
  788. }
  789. // else put highlight canvas before event canvas.
  790. else {
  791. this.eventCanvas._elem.before(this.plugins.pieRenderer.highlightCanvas.createElement(this._gridPadding, 'jqplot-pieRenderer-highlight-canvas', this._plotDimensions, this));
  792. }
  793. var hctx = this.plugins.pieRenderer.highlightCanvas.setContext();
  794. this.eventCanvas._elem.bind('mouseleave', {plot:this}, function (ev) { unhighlight(ev.data.plot); });
  795. }
  796. $.jqplot.preInitHooks.push(preInit);
  797. $.jqplot.PieTickRenderer = function() {
  798. $.jqplot.AxisTickRenderer.call(this);
  799. };
  800. $.jqplot.PieTickRenderer.prototype = new $.jqplot.AxisTickRenderer();
  801. $.jqplot.PieTickRenderer.prototype.constructor = $.jqplot.PieTickRenderer;
  802. })(jQuery);