jqplot.funnelRenderer.js 39 KB

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