BarGauge.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. define("dojox/gauges/BarGauge", ["dojo/_base/declare","dojo/_base/lang","dojo/_base/array","dojo/_base/html","dojo/_base/event","dojox/gfx",
  2. "./_Gauge","./BarLineIndicator", "dojo/dom-geometry"],
  3. function(declare, lang, arr, html, event, gfx, Gauge, BarLineIndicator, domGeometry) {
  4. /*=====
  5. Gauge = dojox.gauges._Gauge;
  6. =====*/
  7. return declare("dojox.gauges.BarGauge", Gauge, {
  8. // summary:
  9. // a bar graph built using the dojox.gfx package.
  10. //
  11. // description:
  12. // using dojo.gfx (and thus either SVG or VML based on what is supported), this widget
  13. // builds a bar graph component, used to display numerical data in a familiar format.
  14. //
  15. // usage:
  16. // <script type="text/javascript">
  17. // require(["dojox/gauges/BarGauge"]);
  18. // </script>
  19. // ...
  20. // <div dojoType="dojox.gauges.BarGauge"
  21. // id="testBarGauge"
  22. // barGaugeHeight="55"
  23. // dataY="25"
  24. // dataHeight="25"
  25. // dataWidth="225">
  26. // </div>
  27. // dataX: Number
  28. // x position of data area (default 5)
  29. dataX: 5,
  30. // dataY: Number
  31. // y position of data area (default 5)
  32. dataY: 5,
  33. // dataWidth: Number
  34. // width of data area (default is bar graph width - 10)
  35. dataWidth: 0,
  36. // dataHeight: Number
  37. // height of data area (default is bar graph width - 10)
  38. dataHeight: 0,
  39. // _defaultIndicator: Object
  40. // override of dojox.gauges._Gauge._defaultIndicator
  41. _defaultIndicator: BarLineIndicator,
  42. startup: function(){
  43. // handle settings from HTML by making sure all the options are
  44. // converted correctly to numbers
  45. //
  46. // also connects mouse handling events
  47. if(this.getChildren){
  48. arr.forEach(this.getChildren(), function(child){ child.startup(); });
  49. }
  50. if(!this.dataWidth){this.dataWidth = this.gaugeWidth - 10;}
  51. if(!this.dataHeight){this.dataHeight = this.gaugeHeight - 10;}
  52. this.inherited(arguments);
  53. },
  54. _getPosition: function(/*Number*/value){
  55. // summary:
  56. // This is a helper function used to determine the position that represents
  57. // a given value on the bar graph
  58. // value: Number
  59. // A value to be converted to a position for this bar graph.
  60. return this.dataX + Math.floor((value - this.min)/(this.max - this.min)*this.dataWidth);
  61. },
  62. _getValueForPosition: function(/*Number*/pos){
  63. // summary:
  64. // This is a helper function used to determine the value represented by
  65. // a position on the bar graph
  66. // pos: Number
  67. // A position to be converted to a value.
  68. return (pos - this.dataX)*(this.max - this.min)/this.dataWidth + this.min;
  69. },
  70. drawRange: function(/*dojox.gfx.Group*/ group, /*Object*/range){
  71. // summary:
  72. // This function is used to draw (or redraw) a range
  73. // description:
  74. // Draws a range (colored area on the background of the gauge)
  75. // based on the given arguments.
  76. // group:
  77. // The GFX group where the range must be drawn.
  78. // range:
  79. // A range is either a dojox.gauges.Range or an object
  80. // with similar parameters (low, high, hover, etc.).
  81. if(range.shape){
  82. range.shape.parent.remove(range.shape);
  83. range.shape = null;
  84. }
  85. var x1 = this._getPosition(range.low);
  86. var x2 = this._getPosition(range.high);
  87. var path = group.createRect({
  88. x: x1,
  89. y: this.dataY,
  90. width: x2 - x1,
  91. height: this.dataHeight
  92. });
  93. if(lang.isArray(range.color) || lang.isString(range.color)){
  94. path.setStroke({color: range.color});
  95. path.setFill(range.color);
  96. }else if(range.color.type){
  97. // Color is a gradient
  98. var y = this.dataY + this.dataHeight/2;
  99. range.color.x1 = x1;
  100. range.color.x2 = x2;
  101. range.color.y1 = y;
  102. range.color.y2 = y;
  103. path.setFill(range.color);
  104. path.setStroke({color: range.color.colors[0].color});
  105. }else if (gfx.svg){
  106. // We've defined a style rather than an explicit color
  107. path.setStroke({color: "green"}); // Arbitrary color, just have to indicate
  108. path.setFill("green"); // that we want it filled
  109. path.getEventSource().setAttribute("class", range.color.style);
  110. }
  111. path.connect("onmouseover", lang.hitch(this, this._handleMouseOverRange, range));
  112. path.connect("onmouseout", lang.hitch(this, this._handleMouseOutRange, range));
  113. range.shape = path;
  114. },
  115. getRangeUnderMouse: function(/*Object*/e){
  116. // summary:
  117. // Determines which range the mouse is currently over
  118. // e: Object
  119. // The event object as received by the mouse handling functions below.
  120. var range = null;
  121. var pos = domGeometry.getContentBox(this.gaugeContent);
  122. var x = e.clientX - pos.x;
  123. var value = this._getValueForPosition(x);
  124. if(this._rangeData){
  125. for(var i=0; (i<this._rangeData.length) && !range; i++){
  126. if((Number(this._rangeData[i].low) <= value) && (Number(this._rangeData[i].high) >= value)){
  127. range = this._rangeData[i];
  128. }
  129. }
  130. }
  131. return range;
  132. },
  133. _dragIndicator: function(/*Object*/widget, /*Object*/ e){
  134. // summary:
  135. // Handles the dragging of an indicator to the event position, including moving/re-drawing
  136. // get angle for mouse position
  137. this._dragIndicatorAt(widget, e.pageX, e.pageY);
  138. event.stop(e);
  139. },
  140. _dragIndicatorAt: function(/*Object*/ widget, x, y){
  141. // summary:
  142. // Handles the dragging of an indicator, including moving/re-drawing
  143. // get new value based on mouse position
  144. var pos = domGeometry.position(widget.gaugeContent, true);
  145. var xl = x - pos.x;
  146. var value = widget._getValueForPosition(xl);
  147. if(value < widget.min){value = widget.min;}
  148. if(value > widget.max){value = widget.max;}
  149. // update the indicator
  150. widget._drag.value = value;
  151. // callback
  152. widget._drag.onDragMove(widget._drag);
  153. // redraw/move indicator(s)
  154. widget._drag.draw(this._indicatorsGroup, true);
  155. widget._drag.valueChanged();
  156. }
  157. });
  158. });