Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

247 rindas
9.5 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.CanvasAxisTickRenderer
  33. * Renderer to draw axis ticks 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. $.jqplot.CanvasAxisTickRenderer = function(options) {
  45. // Group: Properties
  46. // prop: mark
  47. // tick mark on the axis. One of 'inside', 'outside', 'cross', '' or null.
  48. this.mark = 'outside';
  49. // prop: showMark
  50. // wether or not to show the mark on the axis.
  51. this.showMark = true;
  52. // prop: showGridline
  53. // wether or not to draw the gridline on the grid at this tick.
  54. this.showGridline = true;
  55. // prop: isMinorTick
  56. // if this is a minor tick.
  57. this.isMinorTick = false;
  58. // prop: angle
  59. // angle of text, measured clockwise from x axis.
  60. this.angle = 0;
  61. // prop: markSize
  62. // Length of the tick marks in pixels. For 'cross' style, length
  63. // will be stoked above and below axis, so total length will be twice this.
  64. this.markSize = 4;
  65. // prop: show
  66. // wether or not to show the tick (mark and label).
  67. this.show = true;
  68. // prop: showLabel
  69. // wether or not to show the label.
  70. this.showLabel = true;
  71. // prop: labelPosition
  72. // 'auto', 'start', 'middle' or 'end'.
  73. // Whether tick label should be positioned so the start, middle, or end
  74. // of the tick mark.
  75. this.labelPosition = 'auto';
  76. this.label = '';
  77. this.value = null;
  78. this._styles = {};
  79. // prop: formatter
  80. // A class of a formatter for the tick text.
  81. // The default $.jqplot.DefaultTickFormatter uses sprintf.
  82. this.formatter = $.jqplot.DefaultTickFormatter;
  83. // prop: formatString
  84. // string passed to the formatter.
  85. this.formatString = '';
  86. // prop: prefix
  87. // String to prepend to the tick label.
  88. // Prefix is prepended to the formatted tick label.
  89. this.prefix = '';
  90. // prop: fontFamily
  91. // css spec for the font-family css attribute.
  92. this.fontFamily = '"Trebuchet MS", Arial, Helvetica, sans-serif';
  93. // prop: fontSize
  94. // CSS spec for font size.
  95. this.fontSize = '10pt';
  96. // prop: fontWeight
  97. // CSS spec for fontWeight
  98. this.fontWeight = 'normal';
  99. // prop: fontStretch
  100. // Multiplier to condense or expand font width.
  101. // Applies only to browsers which don't support canvas native font rendering.
  102. this.fontStretch = 1.0;
  103. // prop: textColor
  104. // css spec for the color attribute.
  105. this.textColor = '#666666';
  106. // prop: enableFontSupport
  107. // true to turn on native canvas font support in Mozilla 3.5+ and Safari 4+.
  108. // If true, tick label will be drawn with canvas tag native support for fonts.
  109. // If false, tick label will be drawn with Hershey font metrics.
  110. this.enableFontSupport = true;
  111. // prop: pt2px
  112. // Point to pixel scaling factor, used for computing height of bounding box
  113. // around a label. The labels text renderer has a default setting of 1.4, which
  114. // should be suitable for most fonts. Leave as null to use default. If tops of
  115. // letters appear clipped, increase this. If bounding box seems too big, decrease.
  116. // This is an issue only with the native font renderering capabilities of Mozilla
  117. // 3.5 and Safari 4 since they do not provide a method to determine the font height.
  118. this.pt2px = null;
  119. this._elem;
  120. this._ctx;
  121. this._plotWidth;
  122. this._plotHeight;
  123. this._plotDimensions = {height:null, width:null};
  124. $.extend(true, this, options);
  125. var ropts = {fontSize:this.fontSize, fontWeight:this.fontWeight, fontStretch:this.fontStretch, fillStyle:this.textColor, angle:this.getAngleRad(), fontFamily:this.fontFamily};
  126. if (this.pt2px) {
  127. ropts.pt2px = this.pt2px;
  128. }
  129. if (this.enableFontSupport) {
  130. function support_canvas_text() {
  131. return !!(document.createElement('canvas').getContext && typeof document.createElement('canvas').getContext('2d').fillText == 'function');
  132. }
  133. if (support_canvas_text()) {
  134. this._textRenderer = new $.jqplot.CanvasFontRenderer(ropts);
  135. }
  136. else {
  137. this._textRenderer = new $.jqplot.CanvasTextRenderer(ropts);
  138. }
  139. }
  140. else {
  141. this._textRenderer = new $.jqplot.CanvasTextRenderer(ropts);
  142. }
  143. };
  144. $.jqplot.CanvasAxisTickRenderer.prototype.init = function(options) {
  145. $.extend(true, this, options);
  146. this._textRenderer.init({fontSize:this.fontSize, fontWeight:this.fontWeight, fontStretch:this.fontStretch, fillStyle:this.textColor, angle:this.getAngleRad(), fontFamily:this.fontFamily});
  147. };
  148. // return width along the x axis
  149. // will check first to see if an element exists.
  150. // if not, will return the computed text box width.
  151. $.jqplot.CanvasAxisTickRenderer.prototype.getWidth = function(ctx) {
  152. if (this._elem) {
  153. return this._elem.outerWidth(true);
  154. }
  155. else {
  156. var tr = this._textRenderer;
  157. var l = tr.getWidth(ctx);
  158. var h = tr.getHeight(ctx);
  159. var w = Math.abs(Math.sin(tr.angle)*h) + Math.abs(Math.cos(tr.angle)*l);
  160. return w;
  161. }
  162. };
  163. // return height along the y axis.
  164. $.jqplot.CanvasAxisTickRenderer.prototype.getHeight = function(ctx) {
  165. if (this._elem) {
  166. return this._elem.outerHeight(true);
  167. }
  168. else {
  169. var tr = this._textRenderer;
  170. var l = tr.getWidth(ctx);
  171. var h = tr.getHeight(ctx);
  172. var w = Math.abs(Math.cos(tr.angle)*h) + Math.abs(Math.sin(tr.angle)*l);
  173. return w;
  174. }
  175. };
  176. $.jqplot.CanvasAxisTickRenderer.prototype.getAngleRad = function() {
  177. var a = this.angle * Math.PI/180;
  178. return a;
  179. };
  180. $.jqplot.CanvasAxisTickRenderer.prototype.setTick = function(value, axisName, isMinor) {
  181. this.value = value;
  182. if (isMinor) {
  183. this.isMinorTick = true;
  184. }
  185. return this;
  186. };
  187. $.jqplot.CanvasAxisTickRenderer.prototype.draw = function(ctx, plot) {
  188. if (!this.label) {
  189. this.label = this.prefix + this.formatter(this.formatString, this.value);
  190. }
  191. // Memory Leaks patch
  192. if (this._elem) {
  193. if ($.jqplot.use_excanvas) {
  194. window.G_vmlCanvasManager.uninitElement(this._elem.get(0));
  195. }
  196. this._elem.emptyForce();
  197. this._elem = null;
  198. }
  199. // create a canvas here, but can't draw on it untill it is appended
  200. // to dom for IE compatability.
  201. var elem = plot.canvasManager.getCanvas();
  202. this._textRenderer.setText(this.label, ctx);
  203. var w = this.getWidth(ctx);
  204. var h = this.getHeight(ctx);
  205. // canvases seem to need to have width and heigh attributes directly set.
  206. elem.width = w;
  207. elem.height = h;
  208. elem.style.width = w;
  209. elem.style.height = h;
  210. elem.style.textAlign = 'left';
  211. elem.style.position = 'absolute';
  212. elem = plot.canvasManager.initCanvas(elem);
  213. this._elem = $(elem);
  214. this._elem.css(this._styles);
  215. this._elem.addClass('jqplot-'+this.axis+'-tick');
  216. elem = null;
  217. return this._elem;
  218. };
  219. $.jqplot.CanvasAxisTickRenderer.prototype.pack = function() {
  220. this._textRenderer.draw(this._elem.get(0).getContext("2d"), this.label);
  221. };
  222. })(jQuery);