You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

938 rivejä
39 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.FunnelRenderer
  33. * Plugin renderer to draw a funnel chart.
  34. * x values, if present, will be used as labels.
  35. * y values give area size.
  36. *
  37. * Funnel charts will draw a single series
  38. * only.
  39. *
  40. * To use this renderer, you need to include the
  41. * funnel renderer plugin, for example:
  42. *
  43. * > <script type="text/javascript" src="plugins/jqplot.funnelRenderer.js"></script>
  44. *
  45. * Properties described here are passed into the $.jqplot function
  46. * as options on the series renderer. For example:
  47. *
  48. * > plot2 = $.jqplot('chart2', [s1, s2], {
  49. * > seriesDefaults: {
  50. * > renderer:$.jqplot.FunnelRenderer,
  51. * > rendererOptions:{
  52. * > sectionMargin: 12,
  53. * > widthRatio: 0.3
  54. * > }
  55. * > }
  56. * > });
  57. *
  58. * IMPORTANT
  59. *
  60. * *The funnel renderer will reorder data in descending order* so the largest value in
  61. * the data set is first and displayed on top of the funnel. Data will then
  62. * be displayed in descending order down the funnel. The area of each funnel
  63. * section will correspond to the value of each data point relative to the sum
  64. * of all values. That is section area is proportional to section value divided by
  65. * sum of all section values.
  66. *
  67. * If your data is not in descending order when passed into the plot, *it will be
  68. * reordered* when stored in the series.data property. A copy of the unordered
  69. * data is kept in the series._unorderedData property.
  70. *
  71. * A funnel plot will trigger events on the plot target
  72. * according to user interaction. All events return the event object,
  73. * the series index, the point (section) index, and the point data for
  74. * the appropriate section. *Note* the point index will referr to the ordered
  75. * data, not the original unordered data.
  76. *
  77. * 'jqplotDataMouseOver' - triggered when mousing over a section.
  78. * 'jqplotDataHighlight' - triggered the first time user mouses over a section,
  79. * if highlighting is enabled.
  80. * 'jqplotDataUnhighlight' - triggered when a user moves the mouse out of
  81. * a highlighted section.
  82. * 'jqplotDataClick' - triggered when the user clicks on a section.
  83. * 'jqplotDataRightClick' - tiggered when the user right clicks on a section if
  84. * the "captureRightClick" option is set to true on the plot.
  85. */
  86. $.jqplot.FunnelRenderer = function(){
  87. $.jqplot.LineRenderer.call(this);
  88. };
  89. $.jqplot.FunnelRenderer.prototype = new $.jqplot.LineRenderer();
  90. $.jqplot.FunnelRenderer.prototype.constructor = $.jqplot.FunnelRenderer;
  91. // called with scope of a series
  92. $.jqplot.FunnelRenderer.prototype.init = function(options, plot) {
  93. // Group: Properties
  94. //
  95. // prop: padding
  96. // padding between the funnel and plot edges, legend, etc.
  97. this.padding = {top: 20, right: 20, bottom: 20, left: 20};
  98. // prop: sectionMargin
  99. // spacing between funnel sections in pixels.
  100. this.sectionMargin = 6;
  101. // prop: fill
  102. // true or false, wether to fill the areas.
  103. this.fill = true;
  104. // prop: shadowOffset
  105. // offset of the shadow from the area and offset of
  106. // each succesive stroke of the shadow from the last.
  107. this.shadowOffset = 2;
  108. // prop: shadowAlpha
  109. // transparency of the shadow (0 = transparent, 1 = opaque)
  110. this.shadowAlpha = 0.07;
  111. // prop: shadowDepth
  112. // number of strokes to apply to the shadow,
  113. // each stroke offset shadowOffset from the last.
  114. this.shadowDepth = 5;
  115. // prop: highlightMouseOver
  116. // True to highlight area when moused over.
  117. // This must be false to enable highlightMouseDown to highlight when clicking on a area.
  118. this.highlightMouseOver = true;
  119. // prop: highlightMouseDown
  120. // True to highlight when a mouse button is pressed over a area.
  121. // This will be disabled if highlightMouseOver is true.
  122. this.highlightMouseDown = false;
  123. // prop: highlightColors
  124. // array of colors to use when highlighting an area.
  125. this.highlightColors = [];
  126. // prop: widthRatio
  127. // The ratio of the width of the top of the funnel to the bottom.
  128. // a ratio of 0 will make an upside down pyramid.
  129. this.widthRatio = 0.2;
  130. // prop: lineWidth
  131. // width of line if areas are stroked and not filled.
  132. this.lineWidth = 2;
  133. // prop: dataLabels
  134. // Either 'label', 'value', 'percent' or an array of labels to place on the pie slices.
  135. // Defaults to percentage of each pie slice.
  136. this.dataLabels = 'percent';
  137. // prop: showDataLabels
  138. // true to show data labels on slices.
  139. this.showDataLabels = false;
  140. // prop: dataLabelFormatString
  141. // Format string for data labels. If none, '%s' is used for "label" and for arrays, '%d' for value and '%d%%' for percentage.
  142. this.dataLabelFormatString = null;
  143. // prop: dataLabelThreshold
  144. // Threshhold in percentage (0 - 100) of pie area, below which no label will be displayed.
  145. // This applies to all label types, not just to percentage labels.
  146. this.dataLabelThreshold = 3;
  147. this._type = 'funnel';
  148. this.tickRenderer = $.jqplot.FunnelTickRenderer;
  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. // index of the currenty highlighted point, if any
  155. this._highlightedPoint = null;
  156. // lengths of bases, or horizontal sides of areas of trapezoid.
  157. this._bases = [];
  158. // total area
  159. this._atot;
  160. // areas of segments.
  161. this._areas = [];
  162. // vertical lengths of segments.
  163. this._lengths = [];
  164. // angle of the funnel to vertical.
  165. this._angle;
  166. this._dataIndices = [];
  167. // sort data
  168. this._unorderedData = $.extend(true, [], this.data);
  169. var idxs = $.extend(true, [], this.data);
  170. for (var i=0; i<idxs.length; i++) {
  171. idxs[i].push(i);
  172. }
  173. this.data.sort( function (a, b) { return b[1] - a[1]; } );
  174. idxs.sort( function (a, b) { return b[1] - a[1]; });
  175. for (var i=0; i<idxs.length; i++) {
  176. this._dataIndices.push(idxs[i][2]);
  177. }
  178. // set highlight colors if none provided
  179. if (this.highlightColors.length == 0) {
  180. for (var i=0; i<this.seriesColors.length; i++){
  181. var rgba = $.jqplot.getColorComponents(this.seriesColors[i]);
  182. var newrgb = [rgba[0], rgba[1], rgba[2]];
  183. var sum = newrgb[0] + newrgb[1] + newrgb[2];
  184. for (var j=0; j<3; j++) {
  185. // when darkening, lowest color component can be is 60.
  186. newrgb[j] = (sum > 570) ? newrgb[j] * 0.8 : newrgb[j] + 0.4 * (255 - newrgb[j]);
  187. newrgb[j] = parseInt(newrgb[j], 10);
  188. }
  189. this.highlightColors.push('rgb('+newrgb[0]+','+newrgb[1]+','+newrgb[2]+')');
  190. }
  191. }
  192. plot.postParseOptionsHooks.addOnce(postParseOptions);
  193. plot.postInitHooks.addOnce(postInit);
  194. plot.eventListenerHooks.addOnce('jqplotMouseMove', handleMove);
  195. plot.eventListenerHooks.addOnce('jqplotMouseDown', handleMouseDown);
  196. plot.eventListenerHooks.addOnce('jqplotMouseUp', handleMouseUp);
  197. plot.eventListenerHooks.addOnce('jqplotClick', handleClick);
  198. plot.eventListenerHooks.addOnce('jqplotRightClick', handleRightClick);
  199. plot.postDrawHooks.addOnce(postPlotDraw);
  200. };
  201. // gridData will be of form [label, percentage of total]
  202. $.jqplot.FunnelRenderer.prototype.setGridData = function(plot) {
  203. // set gridData property. This will hold angle in radians of each data point.
  204. var sum = 0;
  205. var td = [];
  206. for (var i=0; i<this.data.length; i++){
  207. sum += this.data[i][1];
  208. td.push([this.data[i][0], this.data[i][1]]);
  209. }
  210. // normalize y values, so areas are proportional.
  211. for (var i=0; i<td.length; i++) {
  212. td[i][1] = td[i][1]/sum;
  213. }
  214. this._bases = new Array(td.length + 1);
  215. this._lengths = new Array(td.length);
  216. this.gridData = td;
  217. };
  218. $.jqplot.FunnelRenderer.prototype.makeGridData = function(data, plot) {
  219. // set gridData property. This will hold angle in radians of each data point.
  220. var sum = 0;
  221. var td = [];
  222. for (var i=0; i<this.data.length; i++){
  223. sum += this.data[i][1];
  224. td.push([this.data[i][0], this.data[i][1]]);
  225. }
  226. // normalize y values, so areas are proportional.
  227. for (var i=0; i<td.length; i++) {
  228. td[i][1] = td[i][1]/sum;
  229. }
  230. this._bases = new Array(td.length + 1);
  231. this._lengths = new Array(td.length);
  232. return td;
  233. };
  234. $.jqplot.FunnelRenderer.prototype.drawSection = function (ctx, vertices, color, isShadow) {
  235. var fill = this.fill;
  236. var lineWidth = this.lineWidth;
  237. ctx.save();
  238. if (isShadow) {
  239. for (var i=0; i<this.shadowDepth; i++) {
  240. ctx.save();
  241. ctx.translate(this.shadowOffset*Math.cos(this.shadowAngle/180*Math.PI), this.shadowOffset*Math.sin(this.shadowAngle/180*Math.PI));
  242. doDraw();
  243. }
  244. }
  245. else {
  246. doDraw();
  247. }
  248. function doDraw () {
  249. ctx.beginPath();
  250. ctx.fillStyle = color;
  251. ctx.strokeStyle = color;
  252. ctx.lineWidth = lineWidth;
  253. ctx.moveTo(vertices[0][0], vertices[0][1]);
  254. for (var i=1; i<4; i++) {
  255. ctx.lineTo(vertices[i][0], vertices[i][1]);
  256. }
  257. ctx.closePath();
  258. if (fill) {
  259. ctx.fill();
  260. }
  261. else {
  262. ctx.stroke();
  263. }
  264. }
  265. if (isShadow) {
  266. for (var i=0; i<this.shadowDepth; i++) {
  267. ctx.restore();
  268. }
  269. }
  270. ctx.restore();
  271. };
  272. // called with scope of series
  273. $.jqplot.FunnelRenderer.prototype.draw = function (ctx, gd, options, plot) {
  274. var i;
  275. var opts = (options != undefined) ? options : {};
  276. // offset and direction of offset due to legend placement
  277. var offx = 0;
  278. var offy = 0;
  279. var trans = 1;
  280. this._areas = [];
  281. // var colorGenerator = new this.colorGenerator(this.seriesColors);
  282. if (options.legendInfo && options.legendInfo.placement == 'insideGrid') {
  283. var li = options.legendInfo;
  284. switch (li.location) {
  285. case 'nw':
  286. offx = li.width + li.xoffset;
  287. break;
  288. case 'w':
  289. offx = li.width + li.xoffset;
  290. break;
  291. case 'sw':
  292. offx = li.width + li.xoffset;
  293. break;
  294. case 'ne':
  295. offx = li.width + li.xoffset;
  296. trans = -1;
  297. break;
  298. case 'e':
  299. offx = li.width + li.xoffset;
  300. trans = -1;
  301. break;
  302. case 'se':
  303. offx = li.width + li.xoffset;
  304. trans = -1;
  305. break;
  306. case 'n':
  307. offy = li.height + li.yoffset;
  308. break;
  309. case 's':
  310. offy = li.height + li.yoffset;
  311. trans = -1;
  312. break;
  313. default:
  314. break;
  315. }
  316. }
  317. var loff = (trans==1) ? this.padding.left + offx : this.padding.left;
  318. var toff = (trans==1) ? this.padding.top + offy : this.padding.top;
  319. var roff = (trans==-1) ? this.padding.right + offx : this.padding.right;
  320. var boff = (trans==-1) ? this.padding.bottom + offy : this.padding.bottom;
  321. var shadow = (opts.shadow != undefined) ? opts.shadow : this.shadow;
  322. var showLine = (opts.showLine != undefined) ? opts.showLine : this.showLine;
  323. var fill = (opts.fill != undefined) ? opts.fill : this.fill;
  324. var cw = ctx.canvas.width;
  325. var ch = ctx.canvas.height;
  326. this._bases[0] = cw - loff - roff;
  327. var ltot = this._length = ch - toff - boff;
  328. var hend = this._bases[0]*this.widthRatio;
  329. this._atot = ltot/2 * (this._bases[0] + this._bases[0]*this.widthRatio);
  330. this._angle = Math.atan((this._bases[0] - hend)/2/ltot);
  331. for (i=0; i<gd.length; i++) {
  332. this._areas.push(gd[i][1] * this._atot);
  333. }
  334. var guess, err, count, lsum=0;
  335. var tolerance = 0.0001;
  336. for (i=0; i<this._areas.length; i++) {
  337. guess = this._areas[i]/this._bases[i];
  338. err = 999999;
  339. this._lengths[i] = guess;
  340. count = 0;
  341. while (err > this._lengths[i]*tolerance && count < 100) {
  342. this._lengths[i] = this._areas[i]/(this._bases[i] - this._lengths[i] * Math.tan(this._angle));
  343. err = Math.abs(this._lengths[i] - guess);
  344. this._bases[i+1] = this._bases[i] - (2*this._lengths[i]*Math.tan(this._angle));
  345. guess = this._lengths[i];
  346. count++;
  347. }
  348. lsum += this._lengths[i];
  349. }
  350. // figure out vertices of each section
  351. this._vertices = new Array(gd.length);
  352. // these are 4 coners of entire trapezoid
  353. var p0 = [loff, toff],
  354. p1 = [loff+this._bases[0], toff],
  355. p2 = [loff + (this._bases[0] - this._bases[this._bases.length-1])/2, toff + this._length],
  356. p3 = [p2[0] + this._bases[this._bases.length-1], p2[1]];
  357. // equations of right and left sides, returns x, y values given height of section (y value)
  358. function findleft (l) {
  359. var m = (p0[1] - p2[1])/(p0[0] - p2[0]);
  360. var b = p0[1] - m*p0[0];
  361. var y = l + p0[1];
  362. return [(y - b)/m, y];
  363. }
  364. function findright (l) {
  365. var m = (p1[1] - p3[1])/(p1[0] - p3[0]);
  366. var b = p1[1] - m*p1[0];
  367. var y = l + p1[1];
  368. return [(y - b)/m, y];
  369. }
  370. var x = offx, y = offy;
  371. var h=0, adj=0;
  372. for (i=0; i<gd.length; i++) {
  373. this._vertices[i] = new Array();
  374. var v = this._vertices[i];
  375. var sm = this.sectionMargin;
  376. if (i == 0) {
  377. adj = 0;
  378. }
  379. if (i == 1) {
  380. adj = sm/3;
  381. }
  382. else if (i > 0 && i < gd.length-1) {
  383. adj = sm/2;
  384. }
  385. else if (i == gd.length -1) {
  386. adj = 2*sm/3;
  387. }
  388. v.push(findleft(h+adj));
  389. v.push(findright(h+adj));
  390. h += this._lengths[i];
  391. if (i == 0) {
  392. adj = -2*sm/3;
  393. }
  394. else if (i > 0 && i < gd.length-1) {
  395. adj = -sm/2;
  396. }
  397. else if (i == gd.length - 1) {
  398. adj = 0;
  399. }
  400. v.push(findright(h+adj));
  401. v.push(findleft(h+adj));
  402. }
  403. if (this.shadow) {
  404. var shadowColor = 'rgba(0,0,0,'+this.shadowAlpha+')';
  405. for (var i=0; i<gd.length; i++) {
  406. this.renderer.drawSection.call (this, ctx, this._vertices[i], shadowColor, true);
  407. }
  408. }
  409. for (var i=0; i<gd.length; i++) {
  410. var v = this._vertices[i];
  411. this.renderer.drawSection.call (this, ctx, v, this.seriesColors[i]);
  412. if (this.showDataLabels && gd[i][1]*100 >= this.dataLabelThreshold) {
  413. var fstr, label;
  414. if (this.dataLabels == 'label') {
  415. fstr = this.dataLabelFormatString || '%s';
  416. label = $.jqplot.sprintf(fstr, gd[i][0]);
  417. }
  418. else if (this.dataLabels == 'value') {
  419. fstr = this.dataLabelFormatString || '%d';
  420. label = $.jqplot.sprintf(fstr, this.data[i][1]);
  421. }
  422. else if (this.dataLabels == 'percent') {
  423. fstr = this.dataLabelFormatString || '%d%%';
  424. label = $.jqplot.sprintf(fstr, gd[i][1]*100);
  425. }
  426. else if (this.dataLabels.constructor == Array) {
  427. fstr = this.dataLabelFormatString || '%s';
  428. label = $.jqplot.sprintf(fstr, this.dataLabels[this._dataIndices[i]]);
  429. }
  430. var fact = (this._radius ) * this.dataLabelPositionFactor + this.sliceMargin + this.dataLabelNudge;
  431. var x = (v[0][0] + v[1][0])/2 + this.canvas._offsets.left;
  432. var y = (v[1][1] + v[2][1])/2 + this.canvas._offsets.top;
  433. var labelelem = $('<span class="jqplot-funnel-series jqplot-data-label" style="position:absolute;">' + label + '</span>').insertBefore(plot.eventCanvas._elem);
  434. x -= labelelem.width()/2;
  435. y -= labelelem.height()/2;
  436. x = Math.round(x);
  437. y = Math.round(y);
  438. labelelem.css({left: x, top: y});
  439. }
  440. }
  441. };
  442. $.jqplot.FunnelAxisRenderer = function() {
  443. $.jqplot.LinearAxisRenderer.call(this);
  444. };
  445. $.jqplot.FunnelAxisRenderer.prototype = new $.jqplot.LinearAxisRenderer();
  446. $.jqplot.FunnelAxisRenderer.prototype.constructor = $.jqplot.FunnelAxisRenderer;
  447. // There are no traditional axes on a funnel chart. We just need to provide
  448. // dummy objects with properties so the plot will render.
  449. // called with scope of axis object.
  450. $.jqplot.FunnelAxisRenderer.prototype.init = function(options){
  451. //
  452. this.tickRenderer = $.jqplot.FunnelTickRenderer;
  453. $.extend(true, this, options);
  454. // I don't think I'm going to need _dataBounds here.
  455. // have to go Axis scaling in a way to fit chart onto plot area
  456. // and provide u2p and p2u functionality for mouse cursor, etc.
  457. // for convienence set _dataBounds to 0 and 100 and
  458. // set min/max to 0 and 100.
  459. this._dataBounds = {min:0, max:100};
  460. this.min = 0;
  461. this.max = 100;
  462. this.showTicks = false;
  463. this.ticks = [];
  464. this.showMark = false;
  465. this.show = false;
  466. };
  467. /**
  468. * Class: $.jqplot.FunnelLegendRenderer
  469. * Legend Renderer specific to funnel plots. Set by default
  470. * when the user creates a funnel plot.
  471. */
  472. $.jqplot.FunnelLegendRenderer = function(){
  473. $.jqplot.TableLegendRenderer.call(this);
  474. };
  475. $.jqplot.FunnelLegendRenderer.prototype = new $.jqplot.TableLegendRenderer();
  476. $.jqplot.FunnelLegendRenderer.prototype.constructor = $.jqplot.FunnelLegendRenderer;
  477. $.jqplot.FunnelLegendRenderer.prototype.init = function(options) {
  478. // Group: Properties
  479. //
  480. // prop: numberRows
  481. // Maximum number of rows in the legend. 0 or null for unlimited.
  482. this.numberRows = null;
  483. // prop: numberColumns
  484. // Maximum number of columns in the legend. 0 or null for unlimited.
  485. this.numberColumns = null;
  486. $.extend(true, this, options);
  487. };
  488. // called with context of legend
  489. $.jqplot.FunnelLegendRenderer.prototype.draw = function() {
  490. var legend = this;
  491. if (this.show) {
  492. var series = this._series;
  493. var ss = 'position:absolute;';
  494. ss += (this.background) ? 'background:'+this.background+';' : '';
  495. ss += (this.border) ? 'border:'+this.border+';' : '';
  496. ss += (this.fontSize) ? 'font-size:'+this.fontSize+';' : '';
  497. ss += (this.fontFamily) ? 'font-family:'+this.fontFamily+';' : '';
  498. ss += (this.textColor) ? 'color:'+this.textColor+';' : '';
  499. ss += (this.marginTop != null) ? 'margin-top:'+this.marginTop+';' : '';
  500. ss += (this.marginBottom != null) ? 'margin-bottom:'+this.marginBottom+';' : '';
  501. ss += (this.marginLeft != null) ? 'margin-left:'+this.marginLeft+';' : '';
  502. ss += (this.marginRight != null) ? 'margin-right:'+this.marginRight+';' : '';
  503. this._elem = $('<table class="jqplot-table-legend" style="'+ss+'"></table>');
  504. // Funnel charts legends don't go by number of series, but by number of data points
  505. // in the series. Refactor things here for that.
  506. var pad = false,
  507. reverse = false,
  508. nr, nc;
  509. var s = series[0];
  510. var colorGenerator = new $.jqplot.ColorGenerator(s.seriesColors);
  511. if (s.show) {
  512. var pd = s.data;
  513. if (this.numberRows) {
  514. nr = this.numberRows;
  515. if (!this.numberColumns){
  516. nc = Math.ceil(pd.length/nr);
  517. }
  518. else{
  519. nc = this.numberColumns;
  520. }
  521. }
  522. else if (this.numberColumns) {
  523. nc = this.numberColumns;
  524. nr = Math.ceil(pd.length/this.numberColumns);
  525. }
  526. else {
  527. nr = pd.length;
  528. nc = 1;
  529. }
  530. var i, j, tr, td1, td2, lt, rs, color;
  531. var idx = 0;
  532. for (i=0; i<nr; i++) {
  533. if (reverse){
  534. tr = $('<tr class="jqplot-table-legend"></tr>').prependTo(this._elem);
  535. }
  536. else{
  537. tr = $('<tr class="jqplot-table-legend"></tr>').appendTo(this._elem);
  538. }
  539. for (j=0; j<nc; j++) {
  540. if (idx < pd.length){
  541. lt = this.labels[idx] || pd[idx][0].toString();
  542. color = colorGenerator.next();
  543. if (!reverse){
  544. if (i>0){
  545. pad = true;
  546. }
  547. else{
  548. pad = false;
  549. }
  550. }
  551. else{
  552. if (i == nr -1){
  553. pad = false;
  554. }
  555. else{
  556. pad = true;
  557. }
  558. }
  559. rs = (pad) ? this.rowSpacing : '0';
  560. td1 = $('<td class="jqplot-table-legend" style="text-align:center;padding-top:'+rs+';">'+
  561. '<div><div class="jqplot-table-legend-swatch" style="border-color:'+color+';"></div>'+
  562. '</div></td>');
  563. td2 = $('<td class="jqplot-table-legend" style="padding-top:'+rs+';"></td>');
  564. if (this.escapeHtml){
  565. td2.text(lt);
  566. }
  567. else {
  568. td2.html(lt);
  569. }
  570. if (reverse) {
  571. td2.prependTo(tr);
  572. td1.prependTo(tr);
  573. }
  574. else {
  575. td1.appendTo(tr);
  576. td2.appendTo(tr);
  577. }
  578. pad = true;
  579. }
  580. idx++;
  581. }
  582. }
  583. }
  584. }
  585. return this._elem;
  586. };
  587. // $.jqplot.FunnelLegendRenderer.prototype.pack = function(offsets) {
  588. // if (this.show) {
  589. // // fake a grid for positioning
  590. // var grid = {_top:offsets.top, _left:offsets.left, _right:offsets.right, _bottom:this._plotDimensions.height - offsets.bottom};
  591. // if (this.placement == 'insideGrid') {
  592. // switch (this.location) {
  593. // case 'nw':
  594. // var a = grid._left + this.xoffset;
  595. // var b = grid._top + this.yoffset;
  596. // this._elem.css('left', a);
  597. // this._elem.css('top', b);
  598. // break;
  599. // case 'n':
  600. // var a = (offsets.left + (this._plotDimensions.width - offsets.right))/2 - this.getWidth()/2;
  601. // var b = grid._top + this.yoffset;
  602. // this._elem.css('left', a);
  603. // this._elem.css('top', b);
  604. // break;
  605. // case 'ne':
  606. // var a = offsets.right + this.xoffset;
  607. // var b = grid._top + this.yoffset;
  608. // this._elem.css({right:a, top:b});
  609. // break;
  610. // case 'e':
  611. // var a = offsets.right + this.xoffset;
  612. // var b = (offsets.top + (this._plotDimensions.height - offsets.bottom))/2 - this.getHeight()/2;
  613. // this._elem.css({right:a, top:b});
  614. // break;
  615. // case 'se':
  616. // var a = offsets.right + this.xoffset;
  617. // var b = offsets.bottom + this.yoffset;
  618. // this._elem.css({right:a, bottom:b});
  619. // break;
  620. // case 's':
  621. // var a = (offsets.left + (this._plotDimensions.width - offsets.right))/2 - this.getWidth()/2;
  622. // var b = offsets.bottom + this.yoffset;
  623. // this._elem.css({left:a, bottom:b});
  624. // break;
  625. // case 'sw':
  626. // var a = grid._left + this.xoffset;
  627. // var b = offsets.bottom + this.yoffset;
  628. // this._elem.css({left:a, bottom:b});
  629. // break;
  630. // case 'w':
  631. // var a = grid._left + this.xoffset;
  632. // var b = (offsets.top + (this._plotDimensions.height - offsets.bottom))/2 - this.getHeight()/2;
  633. // this._elem.css({left:a, top:b});
  634. // break;
  635. // default: // same as 'se'
  636. // var a = grid._right - this.xoffset;
  637. // var b = grid._bottom + this.yoffset;
  638. // this._elem.css({right:a, bottom:b});
  639. // break;
  640. // }
  641. //
  642. // }
  643. // else {
  644. // switch (this.location) {
  645. // case 'nw':
  646. // var a = this._plotDimensions.width - grid._left + this.xoffset;
  647. // var b = grid._top + this.yoffset;
  648. // this._elem.css('right', a);
  649. // this._elem.css('top', b);
  650. // break;
  651. // case 'n':
  652. // var a = (offsets.left + (this._plotDimensions.width - offsets.right))/2 - this.getWidth()/2;
  653. // var b = this._plotDimensions.height - grid._top + this.yoffset;
  654. // this._elem.css('left', a);
  655. // this._elem.css('bottom', b);
  656. // break;
  657. // case 'ne':
  658. // var a = this._plotDimensions.width - offsets.right + this.xoffset;
  659. // var b = grid._top + this.yoffset;
  660. // this._elem.css({left:a, top:b});
  661. // break;
  662. // case 'e':
  663. // var a = this._plotDimensions.width - offsets.right + this.xoffset;
  664. // var b = (offsets.top + (this._plotDimensions.height - offsets.bottom))/2 - this.getHeight()/2;
  665. // this._elem.css({left:a, top:b});
  666. // break;
  667. // case 'se':
  668. // var a = this._plotDimensions.width - offsets.right + this.xoffset;
  669. // var b = offsets.bottom + this.yoffset;
  670. // this._elem.css({left:a, bottom:b});
  671. // break;
  672. // case 's':
  673. // var a = (offsets.left + (this._plotDimensions.width - offsets.right))/2 - this.getWidth()/2;
  674. // var b = this._plotDimensions.height - offsets.bottom + this.yoffset;
  675. // this._elem.css({left:a, top:b});
  676. // break;
  677. // case 'sw':
  678. // var a = this._plotDimensions.width - grid._left + this.xoffset;
  679. // var b = offsets.bottom + this.yoffset;
  680. // this._elem.css({right:a, bottom:b});
  681. // break;
  682. // case 'w':
  683. // var a = this._plotDimensions.width - grid._left + this.xoffset;
  684. // var b = (offsets.top + (this._plotDimensions.height - offsets.bottom))/2 - this.getHeight()/2;
  685. // this._elem.css({right:a, top:b});
  686. // break;
  687. // default: // same as 'se'
  688. // var a = grid._right - this.xoffset;
  689. // var b = grid._bottom + this.yoffset;
  690. // this._elem.css({right:a, bottom:b});
  691. // break;
  692. // }
  693. // }
  694. // }
  695. // };
  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 funnel series
  704. var setopts = false;
  705. if (options.seriesDefaults.renderer == $.jqplot.FunnelRenderer) {
  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.FunnelRenderer) {
  711. setopts = true;
  712. }
  713. }
  714. }
  715. if (setopts) {
  716. options.axesDefaults.renderer = $.jqplot.FunnelAxisRenderer;
  717. options.legend.renderer = $.jqplot.FunnelLegendRenderer;
  718. options.legend.preDraw = true;
  719. options.sortData = false;
  720. options.seriesDefaults.pointLabels = {show: false};
  721. }
  722. }
  723. function postInit(target, data, options) {
  724. // if multiple series, add a reference to the previous one so that
  725. // funnel rings can nest.
  726. for (var i=0; i<this.series.length; i++) {
  727. if (this.series[i].renderer.constructor == $.jqplot.FunnelRenderer) {
  728. // don't allow mouseover and mousedown at same time.
  729. if (this.series[i].highlightMouseOver) {
  730. this.series[i].highlightMouseDown = false;
  731. }
  732. }
  733. }
  734. this.target.bind('mouseout', {plot:this}, function (ev) { unhighlight(ev.data.plot); });
  735. }
  736. // called with scope of plot
  737. function postParseOptions(options) {
  738. for (var i=0; i<this.series.length; i++) {
  739. this.series[i].seriesColors = this.seriesColors;
  740. this.series[i].colorGenerator = this.colorGenerator;
  741. }
  742. }
  743. function highlight (plot, sidx, pidx) {
  744. var s = plot.series[sidx];
  745. var canvas = plot.plugins.funnelRenderer.highlightCanvas;
  746. canvas._ctx.clearRect(0,0,canvas._ctx.canvas.width, canvas._ctx.canvas.height);
  747. s._highlightedPoint = pidx;
  748. plot.plugins.funnelRenderer.highlightedSeriesIndex = sidx;
  749. s.renderer.drawSection.call(s, canvas._ctx, s._vertices[pidx], s.highlightColors[pidx], false);
  750. }
  751. function unhighlight (plot) {
  752. var canvas = plot.plugins.funnelRenderer.highlightCanvas;
  753. canvas._ctx.clearRect(0,0, canvas._ctx.canvas.width, canvas._ctx.canvas.height);
  754. for (var i=0; i<plot.series.length; i++) {
  755. plot.series[i]._highlightedPoint = null;
  756. }
  757. plot.plugins.funnelRenderer.highlightedSeriesIndex = null;
  758. plot.target.trigger('jqplotDataUnhighlight');
  759. }
  760. function handleMove(ev, gridpos, datapos, neighbor, plot) {
  761. if (neighbor) {
  762. var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data];
  763. var evt1 = jQuery.Event('jqplotDataMouseOver');
  764. evt1.pageX = ev.pageX;
  765. evt1.pageY = ev.pageY;
  766. plot.target.trigger(evt1, ins);
  767. if (plot.series[ins[0]].highlightMouseOver && !(ins[0] == plot.plugins.funnelRenderer.highlightedSeriesIndex && ins[1] == plot.series[ins[0]]._highlightedPoint)) {
  768. var evt = jQuery.Event('jqplotDataHighlight');
  769. evt.pageX = ev.pageX;
  770. evt.pageY = ev.pageY;
  771. plot.target.trigger(evt, ins);
  772. highlight (plot, ins[0], ins[1]);
  773. }
  774. }
  775. else if (neighbor == null) {
  776. unhighlight (plot);
  777. }
  778. }
  779. function handleMouseDown(ev, gridpos, datapos, neighbor, plot) {
  780. if (neighbor) {
  781. var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data];
  782. if (plot.series[ins[0]].highlightMouseDown && !(ins[0] == plot.plugins.funnelRenderer.highlightedSeriesIndex && ins[1] == plot.series[ins[0]]._highlightedPoint)) {
  783. var evt = jQuery.Event('jqplotDataHighlight');
  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.funnelRenderer.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.pageX = ev.pageX;
  805. evt.pageY = ev.pageY;
  806. plot.target.trigger(evt, ins);
  807. }
  808. }
  809. function handleRightClick(ev, gridpos, datapos, neighbor, plot) {
  810. if (neighbor) {
  811. var ins = [neighbor.seriesIndex, neighbor.pointIndex, neighbor.data];
  812. var idx = plot.plugins.funnelRenderer.highlightedSeriesIndex;
  813. if (idx != null && plot.series[idx].highlightMouseDown) {
  814. unhighlight(plot);
  815. }
  816. var evt = jQuery.Event('jqplotDataRightClick');
  817. evt.pageX = ev.pageX;
  818. evt.pageY = ev.pageY;
  819. plot.target.trigger(evt, ins);
  820. }
  821. }
  822. // called within context of plot
  823. // create a canvas which we can draw on.
  824. // insert it before the eventCanvas, so eventCanvas will still capture events.
  825. function postPlotDraw() {
  826. // Memory Leaks patch
  827. if (this.plugins.funnelRenderer && this.plugins.funnelRenderer.highlightCanvas) {
  828. this.plugins.funnelRenderer.highlightCanvas.resetCanvas();
  829. this.plugins.funnelRenderer.highlightCanvas = null;
  830. }
  831. this.plugins.funnelRenderer = {};
  832. this.plugins.funnelRenderer.highlightCanvas = new $.jqplot.GenericCanvas();
  833. // do we have any data labels? if so, put highlight canvas before those
  834. var labels = $(this.targetId+' .jqplot-data-label');
  835. if (labels.length) {
  836. $(labels[0]).before(this.plugins.funnelRenderer.highlightCanvas.createElement(this._gridPadding, 'jqplot-funnelRenderer-highlight-canvas', this._plotDimensions, this));
  837. }
  838. // else put highlight canvas before event canvas.
  839. else {
  840. this.eventCanvas._elem.before(this.plugins.funnelRenderer.highlightCanvas.createElement(this._gridPadding, 'jqplot-funnelRenderer-highlight-canvas', this._plotDimensions, this));
  841. }
  842. var hctx = this.plugins.funnelRenderer.highlightCanvas.setContext();
  843. }
  844. $.jqplot.preInitHooks.push(preInit);
  845. $.jqplot.FunnelTickRenderer = function() {
  846. $.jqplot.AxisTickRenderer.call(this);
  847. };
  848. $.jqplot.FunnelTickRenderer.prototype = new $.jqplot.AxisTickRenderer();
  849. $.jqplot.FunnelTickRenderer.prototype.constructor = $.jqplot.FunnelTickRenderer;
  850. })(jQuery);