jqplot.blockRenderer.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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.BlockRenderer
  34. * Plugin renderer to draw a x-y block chart. A Block chart has data points displayed as
  35. * colored squares with a text label inside. Data must be supplied in the form:
  36. *
  37. * > [[x1, y1, "label 1", {css}], [x2, y2, "label 2", {css}], ...]
  38. *
  39. * The label and css object are optional. If the label is ommitted, the
  40. * box will collapse unless a css height and/or width is specified.
  41. *
  42. * The css object is an object specifying css properties
  43. * such as:
  44. *
  45. * > {background:'#4f98a5', border:'3px solid gray', padding:'1px'}
  46. *
  47. * Note that css properties specified with the data point override defaults
  48. * specified with the series.
  49. *
  50. */
  51. $.jqplot.BlockRenderer = function(){
  52. $.jqplot.LineRenderer.call(this);
  53. };
  54. $.jqplot.BlockRenderer.prototype = new $.jqplot.LineRenderer();
  55. $.jqplot.BlockRenderer.prototype.constructor = $.jqplot.BlockRenderer;
  56. // called with scope of a series
  57. $.jqplot.BlockRenderer.prototype.init = function(options) {
  58. // Group: Properties
  59. //
  60. // prop: css
  61. // default css styles that will be applied to all data blocks.
  62. // these values will be overridden by css styles supplied with the
  63. // individulal data points.
  64. this.css = {padding:'2px', border:'1px solid #999', textAlign:'center'};
  65. // prop: escapeHtml
  66. // true to escape html in the box label.
  67. this.escapeHtml = false;
  68. // prop: insertBreaks
  69. // true to turn spaces in data block label into html breaks <br />.
  70. this.insertBreaks = true;
  71. // prop: varyBlockColors
  72. // true to vary the color of each block in this series according to
  73. // the seriesColors array. False to set each block to the color
  74. // specified on this series. This has no effect if a css background color
  75. // option is specified in the renderer css options.
  76. this.varyBlockColors = false;
  77. $.extend(true, this, options);
  78. if (this.css.backgroundColor) {
  79. this.color = this.css.backgroundColor;
  80. }
  81. else if (this.css.background) {
  82. this.color = this.css.background;
  83. }
  84. else if (!this.varyBlockColors) {
  85. this.css.background = this.color;
  86. }
  87. this.canvas = new $.jqplot.BlockCanvas();
  88. this.shadowCanvas = new $.jqplot.BlockCanvas();
  89. this.canvas._plotDimensions = this._plotDimensions;
  90. this.shadowCanvas._plotDimensions = this._plotDimensions;
  91. this._type = 'block';
  92. // group: Methods
  93. //
  94. // Method: moveBlock
  95. // Moves an individual block. More efficient than redrawing
  96. // the whole series by calling plot.drawSeries().
  97. // Properties:
  98. // idx - the 0 based index of the block or point in this series.
  99. // x - the x coordinate in data units (value on x axis) to move the block to.
  100. // y - the y coordinate in data units (value on the y axis) to move the block to.
  101. // duration - optional parameter to create an animated movement. Can be a
  102. // number (higher is slower animation) or 'fast', 'normal' or 'slow'. If not
  103. // provided, the element is moved without any animation.
  104. this.moveBlock = function (idx, x, y, duration) {
  105. // update plotData, stackData, data and gridData
  106. // x and y are in data coordinates.
  107. var el = this.canvas._elem.children(':eq('+idx+')');
  108. this.data[idx][0] = x;
  109. this.data[idx][1] = y;
  110. this._plotData[idx][0] = x;
  111. this._plotData[idx][1] = y;
  112. this._stackData[idx][0] = x;
  113. this._stackData[idx][1] = y;
  114. this.gridData[idx][0] = this._xaxis.series_u2p(x);
  115. this.gridData[idx][1] = this._yaxis.series_u2p(y);
  116. var w = el.outerWidth();
  117. var h = el.outerHeight();
  118. var left = this.gridData[idx][0] - w/2 + 'px';
  119. var top = this.gridData[idx][1] - h/2 + 'px';
  120. if (duration) {
  121. if (parseInt(duration, 10)) {
  122. duration = parseInt(duration, 10);
  123. }
  124. el.animate({left:left, top:top}, duration);
  125. }
  126. else {
  127. el.css({left:left, top:top});
  128. }
  129. el = null;
  130. };
  131. };
  132. // called with scope of series
  133. $.jqplot.BlockRenderer.prototype.draw = function (ctx, gd, options) {
  134. if (this.plugins.pointLabels) {
  135. this.plugins.pointLabels.show = false;
  136. }
  137. var i, el, d, gd, t, css, w, h, left, top;
  138. var opts = (options != undefined) ? options : {};
  139. var colorGenerator = new $.jqplot.ColorGenerator(this.seriesColors);
  140. this.canvas._elem.empty();
  141. for (i=0; i<this.gridData.length; i++) {
  142. d = this.data[i];
  143. gd = this.gridData[i];
  144. t = '';
  145. css = {};
  146. if (typeof d[2] == 'string') {
  147. t = d[2];
  148. }
  149. else if (typeof d[2] == 'object') {
  150. css = d[2];
  151. }
  152. if (typeof d[3] == 'object') {
  153. css = d[3];
  154. }
  155. if (this.insertBreaks){
  156. t = t.replace(/ /g, '<br />');
  157. }
  158. css = $.extend(true, {}, this.css, css);
  159. // create a div
  160. el = $('<div style="position:absolute;margin-left:auto;margin-right:auto;"></div>');
  161. this.canvas._elem.append(el);
  162. // set text
  163. this.escapeHtml ? el.text(t) : el.html(t);
  164. // style it
  165. // remove styles we don't want overridden.
  166. delete css.position;
  167. delete css.marginRight;
  168. delete css.marginLeft;
  169. if (!css.background && !css.backgroundColor && !css.backgroundImage){
  170. css.background = colorGenerator.next();
  171. }
  172. el.css(css);
  173. w = el.outerWidth();
  174. h = el.outerHeight();
  175. left = gd[0] - w/2 + 'px';
  176. top = gd[1] - h/2 + 'px';
  177. el.css({left:left, top:top});
  178. el = null;
  179. }
  180. };
  181. $.jqplot.BlockCanvas = function() {
  182. $.jqplot.ElemContainer.call(this);
  183. this._ctx;
  184. };
  185. $.jqplot.BlockCanvas.prototype = new $.jqplot.ElemContainer();
  186. $.jqplot.BlockCanvas.prototype.constructor = $.jqplot.BlockCanvas;
  187. $.jqplot.BlockCanvas.prototype.createElement = function(offsets, clss, plotDimensions) {
  188. this._offsets = offsets;
  189. var klass = 'jqplot-blockCanvas';
  190. if (clss != undefined) {
  191. klass = clss;
  192. }
  193. var elem;
  194. // if this canvas already has a dom element, don't make a new one.
  195. if (this._elem) {
  196. elem = this._elem.get(0);
  197. }
  198. else {
  199. elem = document.createElement('div');
  200. }
  201. // if new plotDimensions supplied, use them.
  202. if (plotDimensions != undefined) {
  203. this._plotDimensions = plotDimensions;
  204. }
  205. var w = this._plotDimensions.width - this._offsets.left - this._offsets.right + 'px';
  206. var h = this._plotDimensions.height - this._offsets.top - this._offsets.bottom + 'px';
  207. this._elem = $(elem);
  208. this._elem.css({ position: 'absolute', width:w, height:h, left: this._offsets.left, top: this._offsets.top });
  209. this._elem.addClass(klass);
  210. return this._elem;
  211. };
  212. $.jqplot.BlockCanvas.prototype.setContext = function() {
  213. this._ctx = {
  214. canvas:{
  215. width:0,
  216. height:0
  217. },
  218. clearRect:function(){return null;}
  219. };
  220. return this._ctx;
  221. };
  222. })(jQuery);