jqplot.pieRenderer.js 37 KB

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