jqplot.enhancedLegendRenderer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. // class $.jqplot.EnhancedLegendRenderer
  33. // Legend renderer which can specify the number of rows and/or columns in the legend.
  34. $.jqplot.EnhancedLegendRenderer = function(){
  35. $.jqplot.TableLegendRenderer.call(this);
  36. };
  37. $.jqplot.EnhancedLegendRenderer.prototype = new $.jqplot.TableLegendRenderer();
  38. $.jqplot.EnhancedLegendRenderer.prototype.constructor = $.jqplot.EnhancedLegendRenderer;
  39. // called with scope of legend.
  40. $.jqplot.EnhancedLegendRenderer.prototype.init = function(options) {
  41. // prop: numberRows
  42. // Maximum number of rows in the legend. 0 or null for unlimited.
  43. this.numberRows = null;
  44. // prop: numberColumns
  45. // Maximum number of columns in the legend. 0 or null for unlimited.
  46. this.numberColumns = null;
  47. // prop: seriesToggle
  48. // false to not enable series on/off toggling on the legend.
  49. // true or a fadein/fadeout speed (number of milliseconds or 'fast', 'normal', 'slow')
  50. // to enable show/hide of series on click of legend item.
  51. this.seriesToggle = 'normal';
  52. // prop: seriesToggleReplot
  53. // True to replot the chart after toggling series on/off.
  54. // This will set the series show property to false.
  55. // This allows for rescaling or other maniplation of chart.
  56. // Set to an options object (e.g. {resetAxes: true}) for replot options.
  57. this.seriesToggleReplot = false;
  58. // prop: disableIEFading
  59. // true to toggle series with a show/hide method only and not allow fading in/out.
  60. // This is to overcome poor performance of fade in some versions of IE.
  61. this.disableIEFading = true;
  62. $.extend(true, this, options);
  63. if (this.seriesToggle) {
  64. $.jqplot.postDrawHooks.push(postDraw);
  65. }
  66. };
  67. // called with scope of legend
  68. $.jqplot.EnhancedLegendRenderer.prototype.draw = function(offsets, plot) {
  69. var legend = this;
  70. if (this.show) {
  71. var series = this._series;
  72. var s;
  73. var ss = 'position:absolute;';
  74. ss += (this.background) ? 'background:'+this.background+';' : '';
  75. ss += (this.border) ? 'border:'+this.border+';' : '';
  76. ss += (this.fontSize) ? 'font-size:'+this.fontSize+';' : '';
  77. ss += (this.fontFamily) ? 'font-family:'+this.fontFamily+';' : '';
  78. ss += (this.textColor) ? 'color:'+this.textColor+';' : '';
  79. ss += (this.marginTop != null) ? 'margin-top:'+this.marginTop+';' : '';
  80. ss += (this.marginBottom != null) ? 'margin-bottom:'+this.marginBottom+';' : '';
  81. ss += (this.marginLeft != null) ? 'margin-left:'+this.marginLeft+';' : '';
  82. ss += (this.marginRight != null) ? 'margin-right:'+this.marginRight+';' : '';
  83. this._elem = $('<table class="jqplot-table-legend" style="'+ss+'"></table>');
  84. if (this.seriesToggle) {
  85. this._elem.css('z-index', '3');
  86. }
  87. var pad = false,
  88. reverse = false,
  89. nr, nc;
  90. if (this.numberRows) {
  91. nr = this.numberRows;
  92. if (!this.numberColumns){
  93. nc = Math.ceil(series.length/nr);
  94. }
  95. else{
  96. nc = this.numberColumns;
  97. }
  98. }
  99. else if (this.numberColumns) {
  100. nc = this.numberColumns;
  101. nr = Math.ceil(series.length/this.numberColumns);
  102. }
  103. else {
  104. nr = series.length;
  105. nc = 1;
  106. }
  107. var i, j, tr, td1, td2, lt, rs, div, div0, div1;
  108. var idx = 0;
  109. // check to see if we need to reverse
  110. for (i=series.length-1; i>=0; i--) {
  111. if (nc == 1 && series[i]._stack || series[i].renderer.constructor == $.jqplot.BezierCurveRenderer){
  112. reverse = true;
  113. }
  114. }
  115. for (i=0; i<nr; i++) {
  116. tr = $(document.createElement('tr'));
  117. tr.addClass('jqplot-table-legend');
  118. if (reverse){
  119. tr.prependTo(this._elem);
  120. }
  121. else{
  122. tr.appendTo(this._elem);
  123. }
  124. for (j=0; j<nc; j++) {
  125. if (idx < series.length && (series[idx].show || series[idx].showLabel)){
  126. s = series[idx];
  127. lt = this.labels[idx] || s.label.toString();
  128. if (lt) {
  129. var color = s.color;
  130. if (!reverse){
  131. if (i>0){
  132. pad = true;
  133. }
  134. else{
  135. pad = false;
  136. }
  137. }
  138. else{
  139. if (i == nr -1){
  140. pad = false;
  141. }
  142. else{
  143. pad = true;
  144. }
  145. }
  146. rs = (pad) ? this.rowSpacing : '0';
  147. td1 = $(document.createElement('td'));
  148. td1.addClass('jqplot-table-legend jqplot-table-legend-swatch');
  149. td1.css({textAlign: 'center', paddingTop: rs});
  150. div0 = $(document.createElement('div'));
  151. div0.addClass('jqplot-table-legend-swatch-outline');
  152. div1 = $(document.createElement('div'));
  153. div1.addClass('jqplot-table-legend-swatch');
  154. div1.css({backgroundColor: color, borderColor: color});
  155. td1.append(div0.append(div1));
  156. td2 = $(document.createElement('td'));
  157. td2.addClass('jqplot-table-legend jqplot-table-legend-label');
  158. td2.css('paddingTop', rs);
  159. // td1 = $('<td class="jqplot-table-legend" style="text-align:center;padding-top:'+rs+';">'+
  160. // '<div><div class="jqplot-table-legend-swatch" style="background-color:'+color+';border-color:'+color+';"></div>'+
  161. // '</div></td>');
  162. // td2 = $('<td class="jqplot-table-legend" style="padding-top:'+rs+';"></td>');
  163. if (this.escapeHtml){
  164. td2.text(lt);
  165. }
  166. else {
  167. td2.html(lt);
  168. }
  169. if (reverse) {
  170. if (this.showLabels) {td2.prependTo(tr);}
  171. if (this.showSwatches) {td1.prependTo(tr);}
  172. }
  173. else {
  174. if (this.showSwatches) {td1.appendTo(tr);}
  175. if (this.showLabels) {td2.appendTo(tr);}
  176. }
  177. if (this.seriesToggle) {
  178. // add an overlay for clicking series on/off
  179. // div0 = $(document.createElement('div'));
  180. // div0.addClass('jqplot-table-legend-overlay');
  181. // div0.css({position:'relative', left:0, top:0, height:'100%', width:'100%'});
  182. // tr.append(div0);
  183. var speed;
  184. if (typeof(this.seriesToggle) === 'string' || typeof(this.seriesToggle) === 'number') {
  185. if (!$.jqplot.use_excanvas || !this.disableIEFading) {
  186. speed = this.seriesToggle;
  187. }
  188. }
  189. if (this.showSwatches) {
  190. td1.bind('click', {series:s, speed:speed, plot: plot, replot:this.seriesToggleReplot}, handleToggle);
  191. td1.addClass('jqplot-seriesToggle');
  192. }
  193. if (this.showLabels) {
  194. td2.bind('click', {series:s, speed:speed, plot: plot, replot:this.seriesToggleReplot}, handleToggle);
  195. td2.addClass('jqplot-seriesToggle');
  196. }
  197. // for series that are already hidden, add the hidden class
  198. if (!s.show && s.showLabel) {
  199. td1.addClass('jqplot-series-hidden');
  200. td2.addClass('jqplot-series-hidden');
  201. }
  202. }
  203. pad = true;
  204. }
  205. }
  206. idx++;
  207. }
  208. td1 = td2 = div0 = div1 = null;
  209. }
  210. }
  211. return this._elem;
  212. };
  213. var handleToggle = function (ev) {
  214. var d = ev.data,
  215. s = d.series,
  216. replot = d.replot,
  217. plot = d.plot,
  218. speed = d.speed,
  219. sidx = s.index,
  220. showing = false;
  221. if (s.canvas._elem.is(':hidden') || !s.show) {
  222. showing = true;
  223. }
  224. var doLegendToggle = function() {
  225. if (replot) {
  226. var opts = {};
  227. if ($.isPlainObject(replot)) {
  228. $.extend(true, opts, replot);
  229. }
  230. plot.replot(opts);
  231. // if showing, there was no canvas element to fade in, so hide here
  232. // and then do a fade in.
  233. if (showing && speed) {
  234. var s = plot.series[sidx];
  235. if (s.shadowCanvas._elem) {
  236. s.shadowCanvas._elem.hide().fadeIn(speed);
  237. }
  238. s.canvas._elem.hide().fadeIn(speed);
  239. s.canvas._elem.nextAll('.jqplot-point-label.jqplot-series-'+s.index).hide().fadeIn(speed);
  240. }
  241. }
  242. else {
  243. var s = plot.series[sidx];
  244. if (s.canvas._elem.is(':hidden') || !s.show) {
  245. // Not sure if there is a better way to check for showSwatches and showLabels === true.
  246. // Test for "undefined" since default values for both showSwatches and showLables is true.
  247. if (typeof plot.options.legend.showSwatches === 'undefined' || plot.options.legend.showSwatches === true) {
  248. plot.legend._elem.find('td').eq(sidx * 2).addClass('jqplot-series-hidden');
  249. }
  250. if (typeof plot.options.legend.showLabels === 'undefined' || plot.options.legend.showLabels === true) {
  251. plot.legend._elem.find('td').eq((sidx * 2) + 1).addClass('jqplot-series-hidden');
  252. }
  253. }
  254. else {
  255. if (typeof plot.options.legend.showSwatches === 'undefined' || plot.options.legend.showSwatches === true) {
  256. plot.legend._elem.find('td').eq(sidx * 2).removeClass('jqplot-series-hidden');
  257. }
  258. if (typeof plot.options.legend.showLabels === 'undefined' || plot.options.legend.showLabels === true) {
  259. plot.legend._elem.find('td').eq((sidx * 2) + 1).removeClass('jqplot-series-hidden');
  260. }
  261. }
  262. }
  263. };
  264. s.toggleDisplay(ev, doLegendToggle);
  265. };
  266. // called with scope of plot.
  267. var postDraw = function () {
  268. if (this.legend.renderer.constructor == $.jqplot.EnhancedLegendRenderer && this.legend.seriesToggle){
  269. var e = this.legend._elem.detach();
  270. this.eventCanvas._elem.after(e);
  271. }
  272. };
  273. })(jQuery);