jqplot.canvasAxisLabelRenderer.js 8.0 KB

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