| 1 |
define(["dojo/_base/kernel","dojo/_base/declare","dojo/_base/array","dojo/_base/lang","dojo/_base/html","dojo/_base/event", |
| 2 |
"dojox/gfx", "./_Gauge","./AnalogLineIndicator", "dojo/dom-geometry"], |
| 3 |
function(dojo, declare, arr, lang, html, event, |
| 4 |
gfx, Gauge, AnalogLineIndicator, domGeometry) { |
| 5 |
|
| 6 |
return declare("dojox.gauges.AnalogGauge",Gauge,{ |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
startAngle: -90, |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
endAngle: 90, |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
cx: 0, |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
cy: 0, |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
radius: 0, |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
orientation: "clockwise", |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
_defaultIndicator: AnalogLineIndicator, |
| 61 |
|
| 62 |
startup: function(){ |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
if(this.getChildren){ |
| 69 |
arr.forEach(this.getChildren(), function(child){ child.startup(); }); |
| 70 |
} |
| 71 |
|
| 72 |
this.startAngle = Number(this.startAngle); |
| 73 |
this.endAngle = Number(this.endAngle); |
| 74 |
|
| 75 |
this.cx = Number(this.cx); |
| 76 |
if(!this.cx){this.cx = this.width/2;} |
| 77 |
this.cy = Number(this.cy); |
| 78 |
if(!this.cy){this.cy = this.height/2;} |
| 79 |
this.radius = Number(this.radius); |
| 80 |
if(!this.radius){this.radius = Math.min(this.cx,this.cy) - 25;} |
| 81 |
|
| 82 |
|
| 83 |
this.inherited(arguments); |
| 84 |
}, |
| 85 |
|
| 86 |
_getAngle: function(value){ |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
var v = Number(value); |
| 94 |
var angle; |
| 95 |
if (value == null || isNaN(v) || v <= this.min) |
| 96 |
angle = this._mod360(this.startAngle); |
| 97 |
else |
| 98 |
if (v >= this.max) |
| 99 |
angle = this._mod360(this.endAngle); |
| 100 |
else { |
| 101 |
var startAngle = this._mod360(this.startAngle); |
| 102 |
var relativeValue = (v - this.min); |
| 103 |
if (this.orientation != 'clockwise') |
| 104 |
relativeValue = -relativeValue; |
| 105 |
|
| 106 |
angle = this._mod360(startAngle + this._getAngleRange() * relativeValue / Math.abs(this.min - this.max)); |
| 107 |
} |
| 108 |
|
| 109 |
return angle; |
| 110 |
}, |
| 111 |
|
| 112 |
_getValueForAngle: function(angle){ |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
var startAngle = this._mod360(this.startAngle); |
| 119 |
var endAngle = this._mod360(this.endAngle); |
| 120 |
|
| 121 |
if (!this._angleInRange(angle)){ |
| 122 |
|
| 123 |
var min1 = this._mod360(startAngle - angle); |
| 124 |
var min2 = 360 - min1; |
| 125 |
var max1 = this._mod360(endAngle - angle); |
| 126 |
var max2 = 360 - max1; |
| 127 |
if (Math.min(min1, min2) < Math.min(max1, max2)) |
| 128 |
return this.min; |
| 129 |
else |
| 130 |
return this.max; |
| 131 |
} |
| 132 |
else { |
| 133 |
var range = Math.abs(this.max - this.min); |
| 134 |
var relativeAngle = this._mod360(this.orientation == 'clockwise' ? |
| 135 |
(angle - startAngle): (-angle + startAngle)); |
| 136 |
return this.min + range * relativeAngle / this._getAngleRange(); |
| 137 |
} |
| 138 |
}, |
| 139 |
|
| 140 |
_getAngleRange: function(){ |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
var range; |
| 145 |
var startAngle = this._mod360(this.startAngle); |
| 146 |
var endAngle = this._mod360(this.endAngle); |
| 147 |
if (startAngle == endAngle) |
| 148 |
return 360; |
| 149 |
if (this.orientation == 'clockwise'){ |
| 150 |
if (endAngle < startAngle) |
| 151 |
range = 360 - (startAngle - endAngle); |
| 152 |
else |
| 153 |
range = endAngle - startAngle; |
| 154 |
} |
| 155 |
else { |
| 156 |
if (endAngle < startAngle) |
| 157 |
range = startAngle - endAngle; |
| 158 |
else |
| 159 |
range = 360 - (endAngle - startAngle); |
| 160 |
} |
| 161 |
return range; |
| 162 |
}, |
| 163 |
|
| 164 |
_angleInRange: function(value){ |
| 165 |
|
| 166 |
|
| 167 |
var startAngle = this._mod360(this.startAngle); |
| 168 |
var endAngle = this._mod360(this.endAngle); |
| 169 |
if (startAngle == endAngle) |
| 170 |
return true; |
| 171 |
value = this._mod360(value); |
| 172 |
if (this.orientation == "clockwise"){ |
| 173 |
if (startAngle < endAngle) |
| 174 |
return value >= startAngle && value <= endAngle; |
| 175 |
else |
| 176 |
return !(value > endAngle && value < startAngle); |
| 177 |
} |
| 178 |
else { |
| 179 |
if (startAngle < endAngle) |
| 180 |
return !(value > startAngle && value < endAngle); |
| 181 |
else |
| 182 |
return value >= endAngle && value <= startAngle; |
| 183 |
} |
| 184 |
}, |
| 185 |
|
| 186 |
_isScaleCircular: function(){ |
| 187 |
|
| 188 |
|
| 189 |
return (this._mod360(this.startAngle) == this._mod360(this.endAngle)); |
| 190 |
}, |
| 191 |
|
| 192 |
_mod360:function(v){ |
| 193 |
|
| 194 |
|
| 195 |
while (v>360) v = v - 360; |
| 196 |
while (v<0) v = v + 360; |
| 197 |
return v; |
| 198 |
}, |
| 199 |
|
| 200 |
_getRadians: function(angle){ |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
return angle*Math.PI/180; |
| 206 |
}, |
| 207 |
|
| 208 |
_getDegrees: function(radians){ |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
return radians*180/Math.PI; |
| 214 |
}, |
| 215 |
|
| 216 |
|
| 217 |
drawRange: function( group, range){ |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
var path; |
| 229 |
if(range.shape){ |
| 230 |
range.shape.parent.remove(range.shape); |
| 231 |
range.shape = null; |
| 232 |
} |
| 233 |
var a1, a2; |
| 234 |
if((range.low == this.min) && (range.high == this.max) && ((this._mod360(this.endAngle) == this._mod360(this.startAngle)))){ |
| 235 |
path = group.createCircle({cx: this.cx, cy: this.cy, r: this.radius}); |
| 236 |
}else{ |
| 237 |
|
| 238 |
|
| 239 |
a1 = this._getRadians(this._getAngle(range.low)); |
| 240 |
a2 = this._getRadians(this._getAngle(range.high)); |
| 241 |
if (this.orientation == 'cclockwise') |
| 242 |
{ |
| 243 |
var a = a2; |
| 244 |
a2 = a1; |
| 245 |
a1 = a; |
| 246 |
} |
| 247 |
|
| 248 |
var x1=this.cx+this.radius*Math.sin(a1), |
| 249 |
y1=this.cy-this.radius*Math.cos(a1), |
| 250 |
x2=this.cx+this.radius*Math.sin(a2), |
| 251 |
y2=this.cy-this.radius*Math.cos(a2), |
| 252 |
big=0 |
| 253 |
; |
| 254 |
|
| 255 |
var arange; |
| 256 |
if (a1<=a2) |
| 257 |
arange = a2-a1; |
| 258 |
else |
| 259 |
arange = 2*Math.PI-a1+a2; |
| 260 |
if(arange>Math.PI){big=1;} |
| 261 |
|
| 262 |
path = group.createPath(); |
| 263 |
if(range.size){ |
| 264 |
path.moveTo(this.cx+(this.radius-range.size)*Math.sin(a1), |
| 265 |
this.cy-(this.radius-range.size)*Math.cos(a1)); |
| 266 |
}else{ |
| 267 |
path.moveTo(this.cx,this.cy); |
| 268 |
} |
| 269 |
path.lineTo(x1,y1); |
| 270 |
path.arcTo(this.radius,this.radius,0,big,1,x2,y2); |
| 271 |
if(range.size){ |
| 272 |
path.lineTo(this.cx+(this.radius-range.size)*Math.sin(a2), |
| 273 |
this.cy-(this.radius-range.size)*Math.cos(a2)); |
| 274 |
path.arcTo((this.radius-range.size),(this.radius-range.size),0,big,0, |
| 275 |
this.cx+(this.radius-range.size)*Math.sin(a1), |
| 276 |
this.cy-(this.radius-range.size)*Math.cos(a1)); |
| 277 |
} |
| 278 |
path.closePath(); |
| 279 |
} |
| 280 |
|
| 281 |
if(lang.isArray(range.color) || lang.isString(range.color)){ |
| 282 |
path.setStroke({color: range.color}); |
| 283 |
path.setFill(range.color); |
| 284 |
}else if(range.color.type){ |
| 285 |
|
| 286 |
a1 = this._getRadians(this._getAngle(range.low)); |
| 287 |
a2 = this._getRadians(this._getAngle(range.high)); |
| 288 |
range.color.x1 = this.cx+(this.radius*Math.sin(a1))/2; |
| 289 |
range.color.x2 = this.cx+(this.radius*Math.sin(a2))/2; |
| 290 |
range.color.y1 = this.cy-(this.radius*Math.cos(a1))/2; |
| 291 |
range.color.y2 = this.cy-(this.radius*Math.cos(a2))/2; |
| 292 |
path.setFill(range.color); |
| 293 |
path.setStroke({color: range.color.colors[0].color}); |
| 294 |
}else if (gfx.svg){ |
| 295 |
|
| 296 |
path.setStroke({color: "green"}); |
| 297 |
path.setFill("green"); |
| 298 |
path.getEventSource().setAttribute("class", range.color.style); |
| 299 |
} |
| 300 |
|
| 301 |
path.connect("onmouseover", lang.hitch(this, this._handleMouseOverRange, range)); |
| 302 |
path.connect("onmouseout", lang.hitch(this, this._handleMouseOutRange, range)); |
| 303 |
|
| 304 |
range.shape = path; |
| 305 |
}, |
| 306 |
|
| 307 |
getRangeUnderMouse: function(e){ |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
var range = null, |
| 313 |
pos = domGeometry.getContentBox(this.gaugeContent), |
| 314 |
x = e.clientX - pos.x, |
| 315 |
y = e.clientY - pos.y, |
| 316 |
r = Math.sqrt((y - this.cy)*(y - this.cy) + (x - this.cx)*(x - this.cx)) |
| 317 |
; |
| 318 |
if(r < this.radius){ |
| 319 |
var angle = this._getDegrees(Math.atan2(y - this.cy, x - this.cx) + Math.PI/2), |
| 320 |
|
| 321 |
value = this._getValueForAngle(angle) |
| 322 |
; |
| 323 |
if(this._rangeData){ |
| 324 |
for(var i=0; (i<this._rangeData.length) && !range; i++){ |
| 325 |
if((Number(this._rangeData[i].low) <= value) && (Number(this._rangeData[i].high) >= value)){ |
| 326 |
range = this._rangeData[i]; |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
return range; |
| 332 |
}, |
| 333 |
|
| 334 |
_dragIndicator: function( widget, e){ |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
this._dragIndicatorAt(widget, e.pageX, e.pageY); |
| 339 |
event.stop(e); |
| 340 |
}, |
| 341 |
|
| 342 |
_dragIndicatorAt: function( widget, x,y){ |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
var pos = domGeometry.position(widget.gaugeContent, true), |
| 347 |
xf = x - pos.x, |
| 348 |
yf = y - pos.y, |
| 349 |
angle = widget._getDegrees(Math.atan2(yf - widget.cy, xf - widget.cx) + Math.PI/2); |
| 350 |
|
| 351 |
|
| 352 |
var value = widget._getValueForAngle(angle); |
| 353 |
value = Math.min(Math.max(value, widget.min), widget.max); |
| 354 |
|
| 355 |
widget._drag.value = widget._drag.currentValue = value; |
| 356 |
|
| 357 |
widget._drag.onDragMove(widget._drag); |
| 358 |
|
| 359 |
widget._drag.draw(this._indicatorsGroup, true); |
| 360 |
widget._drag.valueChanged(); |
| 361 |
} |
| 362 |
|
| 363 |
}); |
| 364 |
}); |