| 1 |
define(["dojo/_base/declare", "dojo/Stateful"], function(declare, Stateful){ |
| 2 |
return declare("dojox.dgauges.MultiLinearScaler", Stateful, { |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
majorTickValues: null, |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
minorTickCount: 4, |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
majorTicks: null, |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
minorTicks: null, |
| 29 |
_snapIntervalPrecision: null, |
| 30 |
_snapCount: 4, |
| 31 |
_snapIntervalPrecision: 6, |
| 32 |
|
| 33 |
constructor: function(){ |
| 34 |
this.watchedProperties = ["majorTickValues", "snapCount", "minorTickCount"]; |
| 35 |
}, |
| 36 |
|
| 37 |
computeTicks: function(){ |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
this.majorTicks = []; |
| 43 |
this.minorTicks = []; |
| 44 |
var ti; |
| 45 |
var step = 1 / (this.majorTickValues.length - 1); |
| 46 |
var minorStep = step / (this.minorTickCount + 1); |
| 47 |
var currentIndex = 0; |
| 48 |
var minorInterval; |
| 49 |
var currentMajorValue; |
| 50 |
var v; |
| 51 |
for(var i = 0; i < this.majorTickValues.length; i++){ |
| 52 |
v = this.majorTickValues[i]; |
| 53 |
ti = {scaler: this}; |
| 54 |
ti.position = currentIndex * step; |
| 55 |
ti.value = v; |
| 56 |
ti.isMinor = false; |
| 57 |
this.majorTicks.push(ti); |
| 58 |
if(currentIndex < this.majorTickValues.length - 1){ |
| 59 |
currentMajorValue = Number(v); |
| 60 |
minorInterval = (Number(this.majorTickValues[i + 1]) - currentMajorValue) / (this.minorTickCount + 1); |
| 61 |
for(var j = 1; j <= this.minorTickCount; j++){ |
| 62 |
ti = {scaler: this}; |
| 63 |
ti.isMinor = true; |
| 64 |
ti.position = currentIndex * step + j * minorStep; |
| 65 |
ti.value = currentMajorValue + minorInterval * j; |
| 66 |
this.minorTicks.push(ti); |
| 67 |
} |
| 68 |
} |
| 69 |
currentIndex++; |
| 70 |
} |
| 71 |
return this.majorTicks.concat(this.minorTicks); |
| 72 |
}, |
| 73 |
|
| 74 |
positionForValue: function(value){ |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
if(!this.majorTickValues){ |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
if(!this.majorTicks){ |
| 86 |
this.computeTicks(); |
| 87 |
} |
| 88 |
|
| 89 |
var minmax = this._getMinMax(value); |
| 90 |
var position = (value - minmax[0].value) / (minmax[1].value - minmax[0].value); |
| 91 |
return minmax[0].position + position * (minmax[1].position - minmax[0].position); |
| 92 |
}, |
| 93 |
|
| 94 |
valueForPosition: function(position){ |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
if(this.majorTicks == null){ |
| 102 |
return 0; |
| 103 |
} |
| 104 |
var minmax = this._getMinMax(position, "position"); |
| 105 |
var value = (position - minmax[0].position) / (minmax[1].position - minmax[0].position); |
| 106 |
return minmax[0].value + value * (minmax[1].value - minmax[0].value); |
| 107 |
}, |
| 108 |
|
| 109 |
_getMinMax: function(v, property){ |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
if(!property){ |
| 115 |
property = "value"; |
| 116 |
} |
| 117 |
var res = []; |
| 118 |
var pre; |
| 119 |
var post; |
| 120 |
var left = 0; |
| 121 |
var right = this.majorTicks.length - 1; |
| 122 |
var center; |
| 123 |
|
| 124 |
if(v <= this.majorTicks[0][property] || v >= this.majorTicks[right][property]){ |
| 125 |
res[0] = this.majorTicks[0]; |
| 126 |
res[1] = this.majorTicks[right]; |
| 127 |
return res; |
| 128 |
} |
| 129 |
|
| 130 |
while (true){ |
| 131 |
center = Math.floor((left + right) / 2); |
| 132 |
|
| 133 |
if(this.majorTicks[center][property] <= v){ |
| 134 |
if(this.majorTicks[center + 1][property] >= v){ |
| 135 |
res[0] = this.majorTicks[center]; |
| 136 |
res[1] = this.majorTicks[center + 1]; |
| 137 |
return res; |
| 138 |
}else{ |
| 139 |
left = center + 1; |
| 140 |
continue; |
| 141 |
} |
| 142 |
}else{ |
| 143 |
right = center; |
| 144 |
continue; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
}); |
| 149 |
}); |