Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

898 wiersze
34 KiB

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