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