jquery.flot.axislabels.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. CAxis Labels Plugin for flot. :P
  3. Copyright (c) 2010 Xuan Luo
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. (function ($) {
  22. var options = { };
  23. function init(plot) {
  24. // This is kind of a hack. There are no hooks in Flot between
  25. // the creation and measuring of the ticks (setTicks, measureTickLabels
  26. // in setupGrid() ) and the drawing of the ticks and plot box
  27. // (insertAxisLabels in setupGrid() ).
  28. //
  29. // Therefore, we use a trick where we run the draw routine twice:
  30. // the first time to get the tick measurements, so that we can change
  31. // them, and then have it draw it again.
  32. var secondPass = false;
  33. plot.hooks.draw.push(function (plot, ctx) {
  34. if (!secondPass) {
  35. // MEASURE AND SET OPTIONS
  36. $.each(plot.getAxes(), function(axisName, axis) {
  37. var opts = axis.options // Flot 0.7
  38. || plot.getOptions()[axisName]; // Flot 0.6
  39. if (!opts || !opts.axisLabel)
  40. return;
  41. var w, h;
  42. if (opts.axisLabelUseCanvas != false)
  43. opts.axisLabelUseCanvas = true;
  44. if (opts.axisLabelUseCanvas) {
  45. // canvas text
  46. if (!opts.axisLabelFontSizePixels)
  47. opts.axisLabelFontSizePixels = 14;
  48. if (!opts.axisLabelFontFamily)
  49. opts.axisLabelFontFamily = 'sans-serif';
  50. // since we currently always display x as horiz.
  51. // and y as vertical, we only care about the height
  52. w = opts.axisLabelFontSizePixels;
  53. h = opts.axisLabelFontSizePixels;
  54. } else {
  55. // HTML text
  56. var elem = $('<div class="axisLabels" style="position:absolute;">' + opts.axisLabel + '</div>');
  57. plot.getPlaceholder().append(elem);
  58. w = elem.outerWidth(true);
  59. h = elem.outerHeight(true);
  60. elem.remove();
  61. }
  62. if (axisName.charAt(0) == 'x')
  63. axis.labelHeight += h;
  64. else
  65. axis.labelWidth += w;
  66. opts.labelHeight = axis.labelHeight;
  67. opts.labelWidth = axis.labelWidth;
  68. });
  69. // re-draw with new label widths and heights
  70. secondPass = true;
  71. plot.setupGrid();
  72. plot.draw();
  73. } else {
  74. // DRAW
  75. $.each(plot.getAxes(), function(axisName, axis) {
  76. var opts = axis.options // Flot 0.7
  77. || plot.getOptions()[axisName]; // Flot 0.6
  78. if (!opts || !opts.axisLabel)
  79. return;
  80. if (opts.axisLabelUseCanvas) {
  81. // canvas text
  82. var ctx = plot.getCanvas().getContext('2d');
  83. ctx.save();
  84. ctx.font = opts.axisLabelFontSizePixels + 'px ' +
  85. opts.axisLabelFontFamily;
  86. var width = ctx.measureText(opts.axisLabel).width;
  87. var height = opts.axisLabelFontSizePixels;
  88. var x, y;
  89. if (axisName.charAt(0) == 'x') {
  90. x = plot.getPlotOffset().left + plot.width()/2 - width/2;
  91. y = plot.getCanvas().height;
  92. } else {
  93. x = height * 0.72;
  94. y = plot.getPlotOffset().top + plot.height()/2 - width/2;
  95. }
  96. ctx.translate(x, y-3);
  97. ctx.rotate((axisName.charAt(0) == 'x') ? 0 : -Math.PI/2);
  98. ctx.fillText(opts.axisLabel, 0, 0);
  99. ctx.restore();
  100. } else {
  101. // HTML text
  102. plot.getPlaceholder().find('#' + axisName + 'Label').remove();
  103. var elem = $('<div id="' + axisName + 'Label" " class="axisLabels" style="position:absolute;">' + opts.axisLabel + '</div>');
  104. if (axisName.charAt(0) == 'x') {
  105. elem.css('left', plot.getPlotOffset().left + plot.width()/2 - elem.outerWidth()/2 + 'px');
  106. elem.css('bottom', '0px');
  107. } else {
  108. elem.css('top', plot.getPlotOffset().top + plot.height()/2 - elem.outerHeight()/2 + 'px');
  109. elem.css('left', '0px');
  110. }
  111. plot.getPlaceholder().append(elem);
  112. }
  113. });
  114. secondPass = false;
  115. }
  116. });
  117. }
  118. $.plot.plugins.push({
  119. init: init,
  120. options: options,
  121. name: 'axisLabels',
  122. version: '1.0'
  123. });
  124. })(jQuery);