jqplot.axisTickRenderer.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. // class: $.jqplot.AxisTickRenderer
  33. // A "tick" object showing the value of a tick/gridline on the plot.
  34. $.jqplot.AxisTickRenderer = function(options) {
  35. // Group: Properties
  36. $.jqplot.ElemContainer.call(this);
  37. // prop: mark
  38. // tick mark on the axis. One of 'inside', 'outside', 'cross', '' or null.
  39. this.mark = 'outside';
  40. // name of the axis associated with this tick
  41. this.axis;
  42. // prop: showMark
  43. // whether or not to show the mark on the axis.
  44. this.showMark = true;
  45. // prop: showGridline
  46. // whether or not to draw the gridline on the grid at this tick.
  47. this.showGridline = true;
  48. // prop: isMinorTick
  49. // if this is a minor tick.
  50. this.isMinorTick = false;
  51. // prop: size
  52. // Length of the tick beyond the grid in pixels.
  53. // DEPRECATED: This has been superceeded by markSize
  54. this.size = 4;
  55. // prop: markSize
  56. // Length of the tick marks in pixels. For 'cross' style, length
  57. // will be stoked above and below axis, so total length will be twice this.
  58. this.markSize = 6;
  59. // prop: show
  60. // whether or not to show the tick (mark and label).
  61. // Setting this to false requires more testing. It is recommended
  62. // to set showLabel and showMark to false instead.
  63. this.show = true;
  64. // prop: showLabel
  65. // whether or not to show the label.
  66. this.showLabel = true;
  67. this.label = null;
  68. this.value = null;
  69. this._styles = {};
  70. // prop: formatter
  71. // A class of a formatter for the tick text. sprintf by default.
  72. this.formatter = $.jqplot.DefaultTickFormatter;
  73. // prop: prefix
  74. // String to prepend to the tick label.
  75. // Prefix is prepended to the formatted tick label.
  76. this.prefix = '';
  77. // prop: suffix
  78. // String to append to the tick label.
  79. // Suffix is appended to the formatted tick label.
  80. this.suffix = '';
  81. // prop: formatString
  82. // string passed to the formatter.
  83. this.formatString = '';
  84. // prop: fontFamily
  85. // css spec for the font-family css attribute.
  86. this.fontFamily;
  87. // prop: fontSize
  88. // css spec for the font-size css attribute.
  89. this.fontSize;
  90. // prop: textColor
  91. // css spec for the color attribute.
  92. this.textColor;
  93. // prop: escapeHTML
  94. // true to escape HTML entities in the label.
  95. this.escapeHTML = false;
  96. this._elem;
  97. this._breakTick = false;
  98. $.extend(true, this, options);
  99. };
  100. $.jqplot.AxisTickRenderer.prototype.init = function(options) {
  101. $.extend(true, this, options);
  102. };
  103. $.jqplot.AxisTickRenderer.prototype = new $.jqplot.ElemContainer();
  104. $.jqplot.AxisTickRenderer.prototype.constructor = $.jqplot.AxisTickRenderer;
  105. $.jqplot.AxisTickRenderer.prototype.setTick = function(value, axisName, isMinor) {
  106. this.value = value;
  107. this.axis = axisName;
  108. if (isMinor) {
  109. this.isMinorTick = true;
  110. }
  111. return this;
  112. };
  113. $.jqplot.AxisTickRenderer.prototype.draw = function() {
  114. if (this.label === null) {
  115. this.label = this.prefix + this.formatter(this.formatString, this.value) + this.suffix;
  116. }
  117. var style = {position: 'absolute'};
  118. if (Number(this.label)) {
  119. style['whitSpace'] = 'nowrap';
  120. }
  121. // Memory Leaks patch
  122. if (this._elem) {
  123. this._elem.emptyForce();
  124. this._elem = null;
  125. }
  126. this._elem = $(document.createElement('div'));
  127. this._elem.addClass("jqplot-"+this.axis+"-tick");
  128. if (!this.escapeHTML) {
  129. this._elem.html(this.label);
  130. }
  131. else {
  132. this._elem.text(this.label);
  133. }
  134. this._elem.css(style);
  135. for (var s in this._styles) {
  136. this._elem.css(s, this._styles[s]);
  137. }
  138. if (this.fontFamily) {
  139. this._elem.css('font-family', this.fontFamily);
  140. }
  141. if (this.fontSize) {
  142. this._elem.css('font-size', this.fontSize);
  143. }
  144. if (this.textColor) {
  145. this._elem.css('color', this.textColor);
  146. }
  147. if (this._breakTick) {
  148. this._elem.addClass('jqplot-breakTick');
  149. }
  150. return this._elem;
  151. };
  152. $.jqplot.DefaultTickFormatter = function (format, val) {
  153. if (typeof val == 'number') {
  154. if (!format) {
  155. format = $.jqplot.config.defaultTickFormatString;
  156. }
  157. return $.jqplot.sprintf(format, val);
  158. }
  159. else {
  160. return String(val);
  161. }
  162. };
  163. $.jqplot.PercentTickFormatter = function (format, val) {
  164. if (typeof val == 'number') {
  165. val = 100 * val;
  166. if (!format) {
  167. format = $.jqplot.config.defaultTickFormatString;
  168. }
  169. return $.jqplot.sprintf(format, val);
  170. }
  171. else {
  172. return String(val);
  173. }
  174. };
  175. $.jqplot.AxisTickRenderer.prototype.pack = function() {
  176. };
  177. })(jQuery);