jquery.flot.axislabels.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. Axis label plugin for flot
  3. Derived from:
  4. Axis Labels Plugin for flot.
  5. http://github.com/markrcote/flot-axislabels
  6. Original code is Copyright (c) 2010 Xuan Luo.
  7. Original code was released under the GPLv3 license by Xuan Luo, September 2010.
  8. Original code was rereleased under the MIT license by Xuan Luo, April 2012.
  9. Permission is hereby granted, free of charge, to any person obtaining
  10. a copy of this software and associated documentation files (the
  11. "Software"), to deal in the Software without restriction, including
  12. without limitation the rights to use, copy, modify, merge, publish,
  13. distribute, sublicense, and/or sell copies of the Software, and to
  14. permit persons to whom the Software is furnished to do so, subject to
  15. the following conditions:
  16. The above copyright notice and this permission notice shall be
  17. included in all copies or substantial portions of the Software.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. (function($) {
  27. "use strict";
  28. var options = {
  29. axisLabels: {
  30. show: true
  31. }
  32. };
  33. function AxisLabel(axisName, position, padding, placeholder, axisLabel, surface) {
  34. this.axisName = axisName;
  35. this.position = position;
  36. this.padding = padding;
  37. this.placeholder = placeholder;
  38. this.axisLabel = axisLabel;
  39. this.surface = surface;
  40. this.width = 0;
  41. this.height = 0;
  42. this.elem = null;
  43. }
  44. AxisLabel.prototype.calculateSize = function() {
  45. var axisId = this.axisName + 'Label',
  46. layerId = axisId + 'Layer',
  47. className = axisId + ' axisLabels';
  48. var info = this.surface.getTextInfo(layerId, this.axisLabel, className);
  49. this.labelWidth = info.width;
  50. this.labelHeight = info.height;
  51. if (this.position === 'left' || this.position === 'right') {
  52. this.width = this.labelHeight + this.padding;
  53. this.height = 0;
  54. } else {
  55. this.width = 0;
  56. this.height = this.labelHeight + this.padding;
  57. }
  58. };
  59. AxisLabel.prototype.transforms = function(degrees, x, y, svgLayer) {
  60. var transforms = [], translate, rotate;
  61. if (x !== 0 || y !== 0) {
  62. translate = svgLayer.createSVGTransform();
  63. translate.setTranslate(x, y);
  64. transforms.push(translate);
  65. }
  66. if (degrees !== 0) {
  67. rotate = svgLayer.createSVGTransform();
  68. var centerX = Math.round(this.labelWidth / 2),
  69. centerY = 0;
  70. rotate.setRotate(degrees, centerX, centerY);
  71. transforms.push(rotate);
  72. }
  73. return transforms;
  74. };
  75. AxisLabel.prototype.calculateOffsets = function(box) {
  76. var offsets = {
  77. x: 0,
  78. y: 0,
  79. degrees: 0
  80. };
  81. if (this.position === 'bottom') {
  82. offsets.x = box.left + box.width / 2 - this.labelWidth / 2;
  83. offsets.y = box.top + box.height - this.labelHeight;
  84. } else if (this.position === 'top') {
  85. offsets.x = box.left + box.width / 2 - this.labelWidth / 2;
  86. offsets.y = box.top;
  87. } else if (this.position === 'left') {
  88. offsets.degrees = -90;
  89. offsets.x = box.left - this.labelWidth / 2;
  90. offsets.y = box.height / 2 + box.top;
  91. } else if (this.position === 'right') {
  92. offsets.degrees = 90;
  93. offsets.x = box.left + box.width - this.labelWidth / 2;
  94. offsets.y = box.height / 2 + box.top;
  95. }
  96. offsets.x = Math.round(offsets.x);
  97. offsets.y = Math.round(offsets.y);
  98. return offsets;
  99. };
  100. AxisLabel.prototype.cleanup = function() {
  101. var axisId = this.axisName + 'Label',
  102. layerId = axisId + 'Layer',
  103. className = axisId + ' axisLabels';
  104. this.surface.removeText(layerId, 0, 0, this.axisLabel, className);
  105. };
  106. AxisLabel.prototype.draw = function(box) {
  107. var axisId = this.axisName + 'Label',
  108. layerId = axisId + 'Layer',
  109. className = axisId + ' axisLabels',
  110. offsets = this.calculateOffsets(box),
  111. style = {
  112. position: 'absolute',
  113. bottom: '',
  114. right: '',
  115. display: 'inline-block',
  116. 'white-space': 'nowrap'
  117. };
  118. var layer = this.surface.getSVGLayer(layerId);
  119. var transforms = this.transforms(offsets.degrees, offsets.x, offsets.y, layer.parentNode);
  120. this.surface.addText(layerId, 0, 0, this.axisLabel, className, undefined, undefined, undefined, undefined, transforms);
  121. this.surface.render();
  122. Object.keys(style).forEach(function(key) {
  123. layer.style[key] = style[key];
  124. });
  125. };
  126. function init(plot) {
  127. plot.hooks.processOptions.push(function(plot, options) {
  128. if (!options.axisLabels.show) {
  129. return;
  130. }
  131. var axisLabels = {};
  132. var defaultPadding = 2; // padding between axis and tick labels
  133. plot.hooks.axisReserveSpace.push(function(plot, axis) {
  134. var opts = axis.options;
  135. var axisName = axis.direction + axis.n;
  136. axis.labelHeight += axis.boxPosition.centerY;
  137. axis.labelWidth += axis.boxPosition.centerX;
  138. if (!opts || !opts.axisLabel || !axis.show) {
  139. return;
  140. }
  141. var padding = opts.axisLabelPadding === undefined
  142. ? defaultPadding
  143. : opts.axisLabelPadding;
  144. var axisLabel = axisLabels[axisName];
  145. if (!axisLabel) {
  146. axisLabel = new AxisLabel(axisName,
  147. opts.position, padding,
  148. plot.getPlaceholder()[0], opts.axisLabel, plot.getSurface());
  149. axisLabels[axisName] = axisLabel;
  150. }
  151. axisLabel.calculateSize();
  152. // Incrementing the sizes of the tick labels.
  153. axis.labelHeight += axisLabel.height;
  154. axis.labelWidth += axisLabel.width;
  155. });
  156. // TODO - use the drawAxis hook
  157. plot.hooks.draw.push(function(plot, ctx) {
  158. $.each(plot.getAxes(), function(flotAxisName, axis) {
  159. var opts = axis.options;
  160. if (!opts || !opts.axisLabel || !axis.show) {
  161. return;
  162. }
  163. var axisName = axis.direction + axis.n;
  164. axisLabels[axisName].draw(axis.box);
  165. });
  166. });
  167. plot.hooks.shutdown.push(function(plot, eventHolder) {
  168. for (var axisName in axisLabels) {
  169. axisLabels[axisName].cleanup();
  170. }
  171. });
  172. });
  173. };
  174. $.plot.plugins.push({
  175. init: init,
  176. options: options,
  177. name: 'axisLabels',
  178. version: '3.0'
  179. });
  180. })(jQuery);