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