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.
 
 
 
 
 
 

199 line
8.7 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. // class $.jqplot.EnhancedLegendRenderer
  32. // Legend renderer which can specify the number of rows and/or columns in the legend.
  33. $.jqplot.EnhancedLegendRenderer = function(){
  34. $.jqplot.TableLegendRenderer.call(this);
  35. };
  36. $.jqplot.EnhancedLegendRenderer.prototype = new $.jqplot.TableLegendRenderer();
  37. $.jqplot.EnhancedLegendRenderer.prototype.constructor = $.jqplot.EnhancedLegendRenderer;
  38. // called with scope of legend.
  39. $.jqplot.EnhancedLegendRenderer.prototype.init = function(options) {
  40. // prop: numberRows
  41. // Maximum number of rows in the legend. 0 or null for unlimited.
  42. this.numberRows = null;
  43. // prop: numberColumns
  44. // Maximum number of columns in the legend. 0 or null for unlimited.
  45. this.numberColumns = null;
  46. // prop: seriesToggle
  47. // false to not enable series on/off toggling on the legend.
  48. // true or a fadein/fadeout speed (number of milliseconds or 'fast', 'normal', 'slow')
  49. // to enable show/hide of series on click of legend item.
  50. this.seriesToggle = 'normal';
  51. // prop: disableIEFading
  52. // true to toggle series with a show/hide method only and not allow fading in/out.
  53. // This is to overcome poor performance of fade in some versions of IE.
  54. this.disableIEFading = true;
  55. $.extend(true, this, options);
  56. if (this.seriesToggle) {
  57. $.jqplot.postDrawHooks.push(postDraw);
  58. }
  59. };
  60. // called with scope of legend
  61. $.jqplot.EnhancedLegendRenderer.prototype.draw = function() {
  62. var legend = this;
  63. if (this.show) {
  64. var series = this._series;
  65. var s;
  66. var ss = 'position:absolute;';
  67. ss += (this.background) ? 'background:'+this.background+';' : '';
  68. ss += (this.border) ? 'border:'+this.border+';' : '';
  69. ss += (this.fontSize) ? 'font-size:'+this.fontSize+';' : '';
  70. ss += (this.fontFamily) ? 'font-family:'+this.fontFamily+';' : '';
  71. ss += (this.textColor) ? 'color:'+this.textColor+';' : '';
  72. ss += (this.marginTop != null) ? 'margin-top:'+this.marginTop+';' : '';
  73. ss += (this.marginBottom != null) ? 'margin-bottom:'+this.marginBottom+';' : '';
  74. ss += (this.marginLeft != null) ? 'margin-left:'+this.marginLeft+';' : '';
  75. ss += (this.marginRight != null) ? 'margin-right:'+this.marginRight+';' : '';
  76. this._elem = $('<table class="jqplot-table-legend" style="'+ss+'"></table>');
  77. if (this.seriesToggle) {
  78. this._elem.css('z-index', '3');
  79. }
  80. var pad = false,
  81. reverse = false,
  82. nr, nc;
  83. if (this.numberRows) {
  84. nr = this.numberRows;
  85. if (!this.numberColumns){
  86. nc = Math.ceil(series.length/nr);
  87. }
  88. else{
  89. nc = this.numberColumns;
  90. }
  91. }
  92. else if (this.numberColumns) {
  93. nc = this.numberColumns;
  94. nr = Math.ceil(series.length/this.numberColumns);
  95. }
  96. else {
  97. nr = series.length;
  98. nc = 1;
  99. }
  100. var i, j, tr, td1, td2, lt, rs;
  101. var idx = 0;
  102. // check to see if we need to reverse
  103. for (i=series.length-1; i>=0; i--) {
  104. if (series[i]._stack || series[i].renderer.constructor == $.jqplot.BezierCurveRenderer){
  105. reverse = true;
  106. }
  107. }
  108. for (i=0; i<nr; i++) {
  109. if (reverse){
  110. tr = $('<tr class="jqplot-table-legend"></tr>').prependTo(this._elem);
  111. }
  112. else{
  113. tr = $('<tr class="jqplot-table-legend"></tr>').appendTo(this._elem);
  114. }
  115. for (j=0; j<nc; j++) {
  116. if (idx < series.length && series[idx].show && series[idx].showLabel){
  117. s = series[idx];
  118. lt = this.labels[idx] || s.label.toString();
  119. if (lt) {
  120. var color = s.color;
  121. if (!reverse){
  122. if (i>0){
  123. pad = true;
  124. }
  125. else{
  126. pad = false;
  127. }
  128. }
  129. else{
  130. if (i == nr -1){
  131. pad = false;
  132. }
  133. else{
  134. pad = true;
  135. }
  136. }
  137. rs = (pad) ? this.rowSpacing : '0';
  138. td1 = $('<td class="jqplot-table-legend" style="text-align:center;padding-top:'+rs+';">'+
  139. '<div><div class="jqplot-table-legend-swatch" style="background-color:'+color+';border-color:'+color+';"></div>'+
  140. '</div></td>');
  141. td2 = $('<td class="jqplot-table-legend" style="padding-top:'+rs+';"></td>');
  142. if (this.escapeHtml){
  143. td2.text(lt);
  144. }
  145. else {
  146. td2.html(lt);
  147. }
  148. if (reverse) {
  149. if (this.showLabels) {td2.prependTo(tr);}
  150. if (this.showSwatches) {td1.prependTo(tr);}
  151. }
  152. else {
  153. if (this.showSwatches) {td1.appendTo(tr);}
  154. if (this.showLabels) {td2.appendTo(tr);}
  155. }
  156. if (this.seriesToggle) {
  157. var speed;
  158. if (typeof(this.seriesToggle) == 'string' || typeof(this.seriesToggle) == 'number') {
  159. if (!$.jqplot.use_excanvas || !this.disableIEFading) {
  160. speed = this.seriesToggle;
  161. }
  162. }
  163. if (this.showSwatches) {
  164. td1.bind('click', {series:s, speed:speed}, s.toggleDisplay);
  165. td1.addClass('jqplot-seriesToggle');
  166. }
  167. if (this.showLabels) {
  168. td2.bind('click', {series:s, speed:speed}, s.toggleDisplay);
  169. td2.addClass('jqplot-seriesToggle');
  170. }
  171. }
  172. pad = true;
  173. }
  174. }
  175. idx++;
  176. }
  177. }
  178. }
  179. return this._elem;
  180. };
  181. // called with scope of plot.
  182. var postDraw = function () {
  183. if (this.legend.renderer.constructor == $.jqplot.EnhancedLegendRenderer && this.legend.seriesToggle){
  184. var e = this.legend._elem.detach();
  185. this.eventCanvas._elem.after(e);
  186. }
  187. };
  188. })(jQuery);