| 1 |
define(["dojo/_base/lang", "dojo/_base/declare", "dojo/Stateful"], function(lang, declare, Stateful){ |
| 2 |
return declare("dojox.dgauges.LinearScaler", Stateful, { |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
minimum: 0, |
| 11 |
|
| 12 |
|
| 13 |
maximum: 100, |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
snapInterval: 1, |
| 19 |
|
| 20 |
|
| 21 |
majorTickInterval: NaN, |
| 22 |
|
| 23 |
|
| 24 |
minorTickInterval: NaN, |
| 25 |
|
| 26 |
|
| 27 |
minorTicksEnabled: true, |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
majorTicks: null, |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
minorTicks: null, |
| 36 |
|
| 37 |
_computedMajorTickInterval: NaN, |
| 38 |
_computedMinorTickInterval: NaN, |
| 39 |
|
| 40 |
constructor: function(){ |
| 41 |
this.watchedProperties = ["minimum", "maximum", "majorTickInterval", "minorTickInterval", "snapInterval", "minorTicksEnabled"]; |
| 42 |
}, |
| 43 |
|
| 44 |
_buildMinorTickItems: function(){ |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
var mt = this.majorTicks; |
| 50 |
var minorTickCache = []; |
| 51 |
if(this.maximum > this.minimum){ |
| 52 |
var majorTickCount = Math.floor((this.maximum - this.minimum) / this.getComputedMajorTickInterval()) + 1; |
| 53 |
var minorTickCount = Math.floor(this.getComputedMajorTickInterval() / this.getComputedMinorTickInterval()); |
| 54 |
var data; |
| 55 |
for(var i = 0; i < majorTickCount - 1; i++){ |
| 56 |
for(var j = 1; j < minorTickCount; j++){ |
| 57 |
data = {scaler: this}; |
| 58 |
data.isMinor = true; |
| 59 |
data.value = mt[i].value + j * this.getComputedMinorTickInterval(); |
| 60 |
data.position = (Number(data.value) - this.minimum) / (this.maximum - this.minimum); |
| 61 |
minorTickCache.push(data); |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
return minorTickCache; |
| 66 |
}, |
| 67 |
|
| 68 |
_buildMajorTickItems: function(){ |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
var majorTickCache = []; |
| 74 |
if(this.maximum > this.minimum){ |
| 75 |
var majorTickCount = Math.floor((this.maximum - this.minimum) / this.getComputedMajorTickInterval()) + 1; |
| 76 |
var data; |
| 77 |
for(var i = 0; i < majorTickCount; i++){ |
| 78 |
data = {scaler: this}; |
| 79 |
data.isMinor = false; |
| 80 |
data.value = this.minimum + i * this.getComputedMajorTickInterval(); |
| 81 |
data.position = (Number(data.value) - this.minimum) / (this.maximum - this.minimum); |
| 82 |
majorTickCache.push(data); |
| 83 |
} |
| 84 |
} |
| 85 |
return majorTickCache; |
| 86 |
}, |
| 87 |
|
| 88 |
getComputedMajorTickInterval: function(){ |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
if(!isNaN(this.majorTickInterval)){ |
| 94 |
return this.majorTickInterval; |
| 95 |
} |
| 96 |
if(isNaN(this._computedMajorTickInterval)){ |
| 97 |
this._computedMajorTickInterval = (this.maximum - this.minimum) / 10; |
| 98 |
} |
| 99 |
return this._computedMajorTickInterval; |
| 100 |
}, |
| 101 |
|
| 102 |
getComputedMinorTickInterval: function(){ |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
if(!isNaN(this.minorTickInterval)){ |
| 108 |
return this.minorTickInterval; |
| 109 |
} |
| 110 |
if(isNaN(this._computedMinorTickInterval)){ |
| 111 |
this._computedMinorTickInterval = this.getComputedMajorTickInterval() / 5; |
| 112 |
} |
| 113 |
return this._computedMinorTickInterval; |
| 114 |
}, |
| 115 |
|
| 116 |
computeTicks: function(){ |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
this.majorTicks = this._buildMajorTickItems(); |
| 122 |
this.minorTicks = this.minorTicksEnabled ? this._buildMinorTickItems() : []; |
| 123 |
return this.majorTicks.concat(this.minorTicks); |
| 124 |
}, |
| 125 |
|
| 126 |
positionForValue: function(value){ |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
var position; |
| 134 |
if(value == null || isNaN(value) || value <= this.minimum){ |
| 135 |
position = 0; |
| 136 |
} |
| 137 |
if(value >= this.maximum){ |
| 138 |
position = 1; |
| 139 |
} |
| 140 |
if(isNaN(position)){ |
| 141 |
position = (value - this.minimum) / (this.maximum - this.minimum); |
| 142 |
} |
| 143 |
return position; |
| 144 |
}, |
| 145 |
|
| 146 |
valueForPosition: function(position){ |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
var range = Math.abs(this.minimum - this.maximum); |
| 154 |
var value = this.minimum + range * position; |
| 155 |
if(!isNaN(this.snapInterval) && this.snapInterval > 0){ |
| 156 |
value = Math.round((value - this.minimum) / this.snapInterval) * this.snapInterval + this.minimum; |
| 157 |
} |
| 158 |
return value; |
| 159 |
} |
| 160 |
}); |
| 161 |
}); |