PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/jqplot/jqplot.tableLegendRenderer.js

https://github.com/quarkness/piwik
JavaScript | 321 lines | 265 code | 16 blank | 40 comment | 46 complexity | d575774e1970baf6079dfdda0492b602 MD5 | raw file
  1. /**
  2. * jqPlot
  3. * Pure JavaScript plotting plugin using jQuery
  4. *
  5. * Version: @VERSION
  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.TableLegendRenderer
  32. // The default legend renderer for jqPlot.
  33. $.jqplot.TableLegendRenderer = function(){
  34. //
  35. };
  36. $.jqplot.TableLegendRenderer.prototype.init = function(options) {
  37. $.extend(true, this, options);
  38. };
  39. $.jqplot.TableLegendRenderer.prototype.addrow = function (label, color, pad, reverse) {
  40. var rs = (pad) ? this.rowSpacing+'px' : '0px';
  41. var tr;
  42. var td;
  43. var elem;
  44. var div0;
  45. var div1;
  46. elem = document.createElement('tr');
  47. tr = $(elem);
  48. tr.addClass('jqplot-table-legend');
  49. elem = null;
  50. if (reverse){
  51. tr.prependTo(this._elem);
  52. }
  53. else{
  54. tr.appendTo(this._elem);
  55. }
  56. if (this.showSwatches) {
  57. td = $(document.createElement('td'));
  58. td.addClass('jqplot-table-legend');
  59. td.css({textAlign: 'center', paddingTop: rs});
  60. div0 = $(document.createElement('div'));
  61. div1 = $(document.createElement('div'));
  62. div1.addClass('jqplot-table-legend-swatch');
  63. div1.css({backgroundColor: color, borderColor: color});
  64. tr.append(td.append(div0.append(div1)));
  65. // $('<td class="jqplot-table-legend" style="text-align:center;padding-top:'+rs+';">'+
  66. // '<div><div class="jqplot-table-legend-swatch" style="background-color:'+color+';border-color:'+color+';"></div>'+
  67. // '</div></td>').appendTo(tr);
  68. }
  69. if (this.showLabels) {
  70. td = $(document.createElement('td'));
  71. td.addClass('jqplot-table-legend');
  72. td.css('paddingTop', rs);
  73. tr.append(td);
  74. // elem = $('<td class="jqplot-table-legend" style="padding-top:'+rs+';"></td>');
  75. // elem.appendTo(tr);
  76. if (this.escapeHtml) {
  77. td.text(label);
  78. }
  79. else {
  80. td.html(label);
  81. }
  82. }
  83. td = null;
  84. div0 = null;
  85. div1 = null;
  86. tr = null;
  87. elem = null;
  88. };
  89. // called with scope of legend
  90. $.jqplot.TableLegendRenderer.prototype.draw = function() {
  91. if (this._elem) {
  92. this._elem.emptyForce();
  93. this._elem = null;
  94. }
  95. if (this.show) {
  96. var series = this._series;
  97. // make a table. one line label per row.
  98. var elem = document.createElement('table');
  99. this._elem = $(elem);
  100. this._elem.addClass('jqplot-table-legend');
  101. var ss = {position:'absolute'};
  102. if (this.background) {
  103. ss['background'] = this.background;
  104. }
  105. if (this.border) {
  106. ss['border'] = this.border;
  107. }
  108. if (this.fontSize) {
  109. ss['fontSize'] = this.fontSize;
  110. }
  111. if (this.fontFamily) {
  112. ss['fontFamily'] = this.fontFamily;
  113. }
  114. if (this.textColor) {
  115. ss['textColor'] = this.textColor;
  116. }
  117. if (this.marginTop != null) {
  118. ss['marginTop'] = this.marginTop;
  119. }
  120. if (this.marginBottom != null) {
  121. ss['marginBottom'] = this.marginBottom;
  122. }
  123. if (this.marginLeft != null) {
  124. ss['marginLeft'] = this.marginLeft;
  125. }
  126. if (this.marginRight != null) {
  127. ss['marginRight'] = this.marginRight;
  128. }
  129. var pad = false,
  130. reverse = false,
  131. s;
  132. for (var i = 0; i< series.length; i++) {
  133. s = series[i];
  134. if (s._stack || s.renderer.constructor == $.jqplot.BezierCurveRenderer){
  135. reverse = true;
  136. }
  137. if (s.show && s.showLabel) {
  138. var lt = this.labels[i] || s.label.toString();
  139. if (lt) {
  140. var color = s.color;
  141. if (reverse && i < series.length - 1){
  142. pad = true;
  143. }
  144. else if (reverse && i == series.length - 1){
  145. pad = false;
  146. }
  147. this.renderer.addrow.call(this, lt, color, pad, reverse);
  148. pad = true;
  149. }
  150. // let plugins add more rows to legend. Used by trend line plugin.
  151. for (var j=0; j<$.jqplot.addLegendRowHooks.length; j++) {
  152. var item = $.jqplot.addLegendRowHooks[j].call(this, s);
  153. if (item) {
  154. this.renderer.addrow.call(this, item.label, item.color, pad);
  155. pad = true;
  156. }
  157. }
  158. lt = null;
  159. }
  160. }
  161. }
  162. return this._elem;
  163. };
  164. $.jqplot.TableLegendRenderer.prototype.pack = function(offsets) {
  165. if (this.show) {
  166. if (this.placement == 'insideGrid') {
  167. switch (this.location) {
  168. case 'nw':
  169. var a = offsets.left;
  170. var b = offsets.top;
  171. this._elem.css('left', a);
  172. this._elem.css('top', b);
  173. break;
  174. case 'n':
  175. var a = (offsets.left + (this._plotDimensions.width - offsets.right))/2 - this.getWidth()/2;
  176. var b = offsets.top;
  177. this._elem.css('left', a);
  178. this._elem.css('top', b);
  179. break;
  180. case 'ne':
  181. var a = offsets.right;
  182. var b = offsets.top;
  183. this._elem.css({right:a, top:b});
  184. break;
  185. case 'e':
  186. var a = offsets.right;
  187. var b = (offsets.top + (this._plotDimensions.height - offsets.bottom))/2 - this.getHeight()/2;
  188. this._elem.css({right:a, top:b});
  189. break;
  190. case 'se':
  191. var a = offsets.right;
  192. var b = offsets.bottom;
  193. this._elem.css({right:a, bottom:b});
  194. break;
  195. case 's':
  196. var a = (offsets.left + (this._plotDimensions.width - offsets.right))/2 - this.getWidth()/2;
  197. var b = offsets.bottom;
  198. this._elem.css({left:a, bottom:b});
  199. break;
  200. case 'sw':
  201. var a = offsets.left;
  202. var b = offsets.bottom;
  203. this._elem.css({left:a, bottom:b});
  204. break;
  205. case 'w':
  206. var a = offsets.left;
  207. var b = (offsets.top + (this._plotDimensions.height - offsets.bottom))/2 - this.getHeight()/2;
  208. this._elem.css({left:a, top:b});
  209. break;
  210. default: // same as 'se'
  211. var a = offsets.right;
  212. var b = offsets.bottom;
  213. this._elem.css({right:a, bottom:b});
  214. break;
  215. }
  216. }
  217. else if (this.placement == 'outside'){
  218. switch (this.location) {
  219. case 'nw':
  220. var a = this._plotDimensions.width - offsets.left;
  221. var b = offsets.top;
  222. this._elem.css('right', a);
  223. this._elem.css('top', b);
  224. break;
  225. case 'n':
  226. var a = (offsets.left + (this._plotDimensions.width - offsets.right))/2 - this.getWidth()/2;
  227. var b = this._plotDimensions.height - offsets.top;
  228. this._elem.css('left', a);
  229. this._elem.css('bottom', b);
  230. break;
  231. case 'ne':
  232. var a = this._plotDimensions.width - offsets.right;
  233. var b = offsets.top;
  234. this._elem.css({left:a, top:b});
  235. break;
  236. case 'e':
  237. var a = this._plotDimensions.width - offsets.right;
  238. var b = (offsets.top + (this._plotDimensions.height - offsets.bottom))/2 - this.getHeight()/2;
  239. this._elem.css({left:a, top:b});
  240. break;
  241. case 'se':
  242. var a = this._plotDimensions.width - offsets.right;
  243. var b = offsets.bottom;
  244. this._elem.css({left:a, bottom:b});
  245. break;
  246. case 's':
  247. var a = (offsets.left + (this._plotDimensions.width - offsets.right))/2 - this.getWidth()/2;
  248. var b = this._plotDimensions.height - offsets.bottom;
  249. this._elem.css({left:a, top:b});
  250. break;
  251. case 'sw':
  252. var a = this._plotDimensions.width - offsets.left;
  253. var b = offsets.bottom;
  254. this._elem.css({right:a, bottom:b});
  255. break;
  256. case 'w':
  257. var a = this._plotDimensions.width - offsets.left;
  258. var b = (offsets.top + (this._plotDimensions.height - offsets.bottom))/2 - this.getHeight()/2;
  259. this._elem.css({right:a, top:b});
  260. break;
  261. default: // same as 'se'
  262. var a = offsets.right;
  263. var b = offsets.bottom;
  264. this._elem.css({right:a, bottom:b});
  265. break;
  266. }
  267. }
  268. else {
  269. switch (this.location) {
  270. case 'nw':
  271. this._elem.css({left:0, top:offsets.top});
  272. break;
  273. case 'n':
  274. var a = (offsets.left + (this._plotDimensions.width - offsets.right))/2 - this.getWidth()/2;
  275. this._elem.css({left: a, top:offsets.top});
  276. break;
  277. case 'ne':
  278. this._elem.css({right:0, top:offsets.top});
  279. break;
  280. case 'e':
  281. var b = (offsets.top + (this._plotDimensions.height - offsets.bottom))/2 - this.getHeight()/2;
  282. this._elem.css({right:offsets.right, top:b});
  283. break;
  284. case 'se':
  285. this._elem.css({right:offsets.right, bottom:offsets.bottom});
  286. break;
  287. case 's':
  288. var a = (offsets.left + (this._plotDimensions.width - offsets.right))/2 - this.getWidth()/2;
  289. this._elem.css({left: a, bottom:offsets.bottom});
  290. break;
  291. case 'sw':
  292. this._elem.css({left:offsets.left, bottom:offsets.bottom});
  293. break;
  294. case 'w':
  295. var b = (offsets.top + (this._plotDimensions.height - offsets.bottom))/2 - this.getHeight()/2;
  296. this._elem.css({left:offsets.left, top:b});
  297. break;
  298. default: // same as 'se'
  299. this._elem.css({right:offsets.right, bottom:offsets.bottom});
  300. break;
  301. }
  302. }
  303. }
  304. };
  305. })(jQuery);