-
Notifications
You must be signed in to change notification settings - Fork 11
/
LinearScaler.js
161 lines (151 loc) · 5.27 KB
/
LinearScaler.js
1
2
3
4
5
6
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
define(["dojo/_base/lang", "dojo/_base/declare", "dojo/Stateful"], function(lang, declare, Stateful){
return declare("dojox.dgauges.LinearScaler", Stateful, {
// summary:
// The linear scaler. This scaler creates major and minor ticks regularly between
// a minimum and a maximum.
// Scalers are responsible for tick generation and various data-transform operations.
// minimum: Number
// The minimum value of the scaler. Default is 0.
minimum: 0,
// maximum: Number
// The maximum value of the scaler. Default is 100.
maximum: 100,
// snapInterval:
// Specifies the increment value to be used as snap values on this scale
// during user interaction.
// Default is 1.
snapInterval: 1,
// majorTickInterval: Number
// The interval between two major ticks.
majorTickInterval: NaN,
// minorTickInterval: Number
// The interval between two minor ticks.
minorTickInterval: NaN,
// minorTicksEnabled: Boolean
// If false, minor ticks are not generated. Default is true.
minorTicksEnabled: true,
// majorTicks:
// The array of generated major ticks. You should not set this
// property when using the scaler.
majorTicks: null,
// minorTicks:
// The array of generated minor ticks. You should not set this
// property when using the scaler.
minorTicks: null,
_computedMajorTickInterval: NaN,
_computedMinorTickInterval: NaN,
constructor: function(){
this.watchedProperties = ["minimum", "maximum", "majorTickInterval", "minorTickInterval", "snapInterval", "minorTicksEnabled"];
},
_buildMinorTickItems: function(){
// summary:
// Internal method.
// tags:
// private
var mt = this.majorTicks;
var minorTickCache = [];
if(this.maximum > this.minimum){
var majorTickCount = Math.floor((this.maximum - this.minimum) / this.getComputedMajorTickInterval()) + 1;
var minorTickCount = Math.floor(this.getComputedMajorTickInterval() / this.getComputedMinorTickInterval());
var data;
for(var i = 0; i < majorTickCount - 1; i++){
for(var j = 1; j < minorTickCount; j++){
data = {scaler: this};
data.isMinor = true;
data.value = mt[i].value + j * this.getComputedMinorTickInterval();
data.position = (Number(data.value) - this.minimum) / (this.maximum - this.minimum);
minorTickCache.push(data);
}
}
}
return minorTickCache;
},
_buildMajorTickItems: function(){
// summary:
// Internal method.
// tags:
// private
var majorTickCache = [];
if(this.maximum > this.minimum){
var majorTickCount = Math.floor((this.maximum - this.minimum) / this.getComputedMajorTickInterval()) + 1;
var data;
for(var i = 0; i < majorTickCount; i++){
data = {scaler: this};
data.isMinor = false;
data.value = this.minimum + i * this.getComputedMajorTickInterval();
data.position = (Number(data.value) - this.minimum) / (this.maximum - this.minimum);
majorTickCache.push(data);
}
}
return majorTickCache;
},
getComputedMajorTickInterval: function(){
// summary:
// The computed or user defined major tick interval.
// returns: Number
// The major tick interval used for ticks generation.
if(!isNaN(this.majorTickInterval)){
return this.majorTickInterval;
}
if(isNaN(this._computedMajorTickInterval)){
this._computedMajorTickInterval = (this.maximum - this.minimum) / 10;
}
return this._computedMajorTickInterval;
},
getComputedMinorTickInterval: function(){
// summary:
// The computed or user defined minor tick interval.
// returns: Number
// The minor tick interval used for ticks generation.
if(!isNaN(this.minorTickInterval)){
return this.minorTickInterval;
}
if(isNaN(this._computedMinorTickInterval)){
this._computedMinorTickInterval = this.getComputedMajorTickInterval() / 5;
}
return this._computedMinorTickInterval;
},
computeTicks: function(){
// summary:
// Creates or re-creates the ticks for this scaler.
// returns: Array
// An array containing all ticks (major then minor ticks).
this.majorTicks = this._buildMajorTickItems();
this.minorTicks = this.minorTicksEnabled ? this._buildMinorTickItems() : [];
return this.majorTicks.concat(this.minorTicks);
},
positionForValue: function(value){
// summary:
// Transforms a value into a relative position between 0 and 1.
// value: Number
// A value to transform.
// returns: Number
// The position between 0 and 1.
var position;
if(value == null || isNaN(value) || value <= this.minimum){
position = 0;
}
if(value >= this.maximum){
position = 1;
}
if(isNaN(position)){
position = (value - this.minimum) / (this.maximum - this.minimum);
}
return position;
},
valueForPosition: function(position){
// summary:
// Transforms a relative position (between 0 and 1) into a value.
// position: Number
// A relative position to transform.
// returns: Number
// The transformed value between minimum and maximum.
var range = Math.abs(this.minimum - this.maximum);
var value = this.minimum + range * position;
if(!isNaN(this.snapInterval) && this.snapInterval > 0){
value = Math.round((value - this.minimum) / this.snapInterval) * this.snapInterval + this.minimum;
}
return value;
}
});
});