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.
 
 
 
 
 
 

207 lines
8.1 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.CanvasAxisLabelRenderer
  33. * Renderer to draw axis labels with a canvas element to support advanced
  34. * featrues such as rotated text. This renderer uses a separate rendering engine
  35. * to draw the text on the canvas. Two modes of rendering the text are available.
  36. * If the browser has native font support for canvas fonts (currently Mozila 3.5
  37. * and Safari 4), you can enable text rendering with the canvas fillText method.
  38. * You do so by setting the "enableFontSupport" option to true.
  39. *
  40. * Browsers lacking native font support will have the text drawn on the canvas
  41. * using the Hershey font metrics. Even if the "enableFontSupport" option is true
  42. * non-supporting browsers will still render with the Hershey font.
  43. *
  44. */
  45. $.jqplot.CanvasAxisLabelRenderer = function(options) {
  46. // Group: Properties
  47. // prop: angle
  48. // angle of text, measured clockwise from x axis.
  49. this.angle = 0;
  50. // name of the axis associated with this tick
  51. this.axis;
  52. // prop: show
  53. // wether or not to show the tick (mark and label).
  54. this.show = true;
  55. // prop: showLabel
  56. // wether or not to show the label.
  57. this.showLabel = true;
  58. // prop: label
  59. // label for the axis.
  60. this.label = '';
  61. // prop: fontFamily
  62. // CSS spec for the font-family css attribute.
  63. // Applies only to browsers supporting native font rendering in the
  64. // canvas tag. Currently Mozilla 3.5 and Safari 4.
  65. this.fontFamily = '"Trebuchet MS", Arial, Helvetica, sans-serif';
  66. // prop: fontSize
  67. // CSS spec for font size.
  68. this.fontSize = '11pt';
  69. // prop: fontWeight
  70. // CSS spec for fontWeight: normal, bold, bolder, lighter or a number 100 - 900
  71. this.fontWeight = 'normal';
  72. // prop: fontStretch
  73. // Multiplier to condense or expand font width.
  74. // Applies only to browsers which don't support canvas native font rendering.
  75. this.fontStretch = 1.0;
  76. // prop: textColor
  77. // css spec for the color attribute.
  78. this.textColor = '#666666';
  79. // prop: enableFontSupport
  80. // true to turn on native canvas font support in Mozilla 3.5+ and Safari 4+.
  81. // If true, label will be drawn with canvas tag native support for fonts.
  82. // If false, label will be drawn with Hershey font metrics.
  83. this.enableFontSupport = true;
  84. // prop: pt2px
  85. // Point to pixel scaling factor, used for computing height of bounding box
  86. // around a label. The labels text renderer has a default setting of 1.4, which
  87. // should be suitable for most fonts. Leave as null to use default. If tops of
  88. // letters appear clipped, increase this. If bounding box seems too big, decrease.
  89. // This is an issue only with the native font renderering capabilities of Mozilla
  90. // 3.5 and Safari 4 since they do not provide a method to determine the font height.
  91. this.pt2px = null;
  92. this._elem;
  93. this._ctx;
  94. this._plotWidth;
  95. this._plotHeight;
  96. this._plotDimensions = {height:null, width:null};
  97. $.extend(true, this, options);
  98. if (options.angle == null && this.axis != 'xaxis' && this.axis != 'x2axis') {
  99. this.angle = -90;
  100. }
  101. var ropts = {fontSize:this.fontSize, fontWeight:this.fontWeight, fontStretch:this.fontStretch, fillStyle:this.textColor, angle:this.getAngleRad(), fontFamily:this.fontFamily};
  102. if (this.pt2px) {
  103. ropts.pt2px = this.pt2px;
  104. }
  105. if (this.enableFontSupport) {
  106. function support_canvas_text() {
  107. return !!(document.createElement('canvas').getContext && typeof document.createElement('canvas').getContext('2d').fillText == 'function');
  108. }
  109. if (support_canvas_text()) {
  110. this._textRenderer = new $.jqplot.CanvasFontRenderer(ropts);
  111. }
  112. else {
  113. this._textRenderer = new $.jqplot.CanvasTextRenderer(ropts);
  114. }
  115. }
  116. else {
  117. this._textRenderer = new $.jqplot.CanvasTextRenderer(ropts);
  118. }
  119. };
  120. $.jqplot.CanvasAxisLabelRenderer.prototype.init = function(options) {
  121. $.extend(true, this, options);
  122. this._textRenderer.init({fontSize:this.fontSize, fontWeight:this.fontWeight, fontStretch:this.fontStretch, fillStyle:this.textColor, angle:this.getAngleRad(), fontFamily:this.fontFamily});
  123. };
  124. // return width along the x axis
  125. // will check first to see if an element exists.
  126. // if not, will return the computed text box width.
  127. $.jqplot.CanvasAxisLabelRenderer.prototype.getWidth = function(ctx) {
  128. if (this._elem) {
  129. return this._elem.outerWidth(true);
  130. }
  131. else {
  132. var tr = this._textRenderer;
  133. var l = tr.getWidth(ctx);
  134. var h = tr.getHeight(ctx);
  135. var w = Math.abs(Math.sin(tr.angle)*h) + Math.abs(Math.cos(tr.angle)*l);
  136. return w;
  137. }
  138. };
  139. // return height along the y axis.
  140. $.jqplot.CanvasAxisLabelRenderer.prototype.getHeight = function(ctx) {
  141. if (this._elem) {
  142. return this._elem.outerHeight(true);
  143. }
  144. else {
  145. var tr = this._textRenderer;
  146. var l = tr.getWidth(ctx);
  147. var h = tr.getHeight(ctx);
  148. var w = Math.abs(Math.cos(tr.angle)*h) + Math.abs(Math.sin(tr.angle)*l);
  149. return w;
  150. }
  151. };
  152. $.jqplot.CanvasAxisLabelRenderer.prototype.getAngleRad = function() {
  153. var a = this.angle * Math.PI/180;
  154. return a;
  155. };
  156. $.jqplot.CanvasAxisLabelRenderer.prototype.draw = function(ctx, plot) {
  157. // Memory Leaks patch
  158. if (this._elem) {
  159. if ($.jqplot.use_excanvas) {
  160. window.G_vmlCanvasManager.uninitElement(this._elem.get(0));
  161. }
  162. this._elem.emptyForce();
  163. this._elem = null;
  164. }
  165. // create a canvas here, but can't draw on it untill it is appended
  166. // to dom for IE compatability.
  167. var elem = plot.canvasManager.getCanvas();
  168. this._textRenderer.setText(this.label, ctx);
  169. var w = this.getWidth(ctx);
  170. var h = this.getHeight(ctx);
  171. elem.width = w;
  172. elem.height = h;
  173. elem.style.width = w;
  174. elem.style.height = h;
  175. elem = plot.canvasManager.initCanvas(elem);
  176. this._elem = $(elem);
  177. this._elem.css({ position: 'absolute'});
  178. this._elem.addClass('jqplot-'+this.axis+'-label');
  179. elem = null;
  180. return this._elem;
  181. };
  182. $.jqplot.CanvasAxisLabelRenderer.prototype.pack = function() {
  183. this._textRenderer.draw(this._elem.get(0).getContext("2d"), this.label);
  184. };
  185. })(jQuery);