Debian Codegraph @

dojox/dgauges/ScaleBase.js

raw symbols SCIP
1 define(["dojo/_base/lang", "dojo/_base/declare", "dojox/gfx", "dojo/_base/array", "dojox/widget/_Invalidating", "dojo/_base/sniff"],
2 function(lang, declare, gfx, array, _Invalidating, has){
3 return declare("dojox.dgauges.ScaleBase", _Invalidating, {
4 // summary:
5 // The ScaleBase class is the base class for the circular and rectangular scales.
6 // A scaler must be set to use this class. A scaler is responsible for
7 // tick generation and various data-transform operations.
8
9 // scaler: Object
10 // The scaler used for tick generation and data-transform operations.
11 // This property is mandatory for using the scale.
12 scaler: null,
13 // font: Object
14 // The font used for the ticks labels.
15 // This is null by default which means this scale use the font defined
16 // on the gauge.
17 font: null,
18 // labelPosition: String
19 // See CircularScale and RectangularScale for valid values.
20 labelPosition: null,
21 // labelGap: Number
22 // The label gap between the ticks and their labels. Default value is 1.
23 labelGap: 1,
24 // tickStroke: Object
25 // The GFX stroke used by the default tickShapeFunc implementation.
26 tickStroke: null,
27 _gauge: null,
28 _gfxGroup: null,
29 _bgGroup: null,
30 _fgGroup: null,
31 _indicators: null,
32 _indicatorsIndex: null,
33 _indicatorsRenderers: null,
34
35 constructor: function(){
36 this._indicators = [];
37 this._indicatorsIndex = {};
38 this._indicatorsRenderers = {};
39 this._gauge = null;
40 this._gfxGroup = null;
41 // Fix for #1, IE<9 don't render correctly stroke with width<1
42 this.tickStroke = {color: "black", width: has("ie") <= 8 ? 1 : 0.5};
43
44 this.addInvalidatingProperties(["scaler", "font", "labelGap", "labelPosition", "tickShapeFunc", "tickLabelFunc", "tickStroke"]);
45
46 this.watch("scaler", lang.hitch(this, this._watchScaler));
47 },
48
49 postscript: function(mixin){
50 // summary:
51 // Internal method.
52 // tags:
53 // private
54 this.inherited(arguments);
55 if(mixin && mixin.scaler){
56 this._watchScaler("scaler", null, mixin.scaler);
57 }
58 },
59
60 _watchers: null,
61
62 _watchScaler: function(name, oldValue, newValue){
63 // summary:
64 // Internal method.
65 // tags:
66 // private
67 array.forEach(this._watchers, lang.hitch(this, function(entry){
68 entry.unwatch();
69 }));
70
71 // Get the properties declared by the watched object
72 var props = newValue.watchedProperties;
73 this._watchers = [];
74 array.forEach(props, lang.hitch(this, function(entry){
75 this._watchers.push(newValue.watch(entry, lang.hitch(this, this.invalidateRendering)));
76 }));
77 },
78
79 _getFont: function(){
80 // summary:
81 // Internal method.
82 // tags:
83 // private
84 var font = this.font;
85 if(!font){
86 font = this._gauge.font;
87 }
88 if(!font){
89 font = gfx.defaultFont;
90 }
91 return font;
92 },
93
94 positionForValue: function(value){
95 // summary:
96 // See CircularScale and Rectangular for more informations.
97 // value: Number
98 // The value to convert.
99 // returns: Number
100 // The position corresponding to the value.
101 return 0;
102 },
103
104 valueForPosition: function(position){
105 // summary:
106 // See CircularScale and Rectangular for more informations.
107 // position: Number
108 // The position to convert.
109 // returns: Number
110 // The value corresponding to the position.
111 },
112
113 tickLabelFunc: function(tickItem){
114 // summary:
115 // Customize the text of ticks labels.
116 // tickItem: Object
117 // An object containing the tick informations.
118 // returns: String
119 // The text to be aligned with the tick. If null, the tick has no label.
120 if(tickItem.isMinor){
121 return null;
122 }else{
123 return String(tickItem.value);
124 }
125 },
126
127 tickShapeFunc: function(group, scale, tickItem){
128 // summary:
129 // Customize the shape of ticks.
130 // group: dojox/gfx/Group
131 // The GFX group used for drawing the tick.
132 // scale: dojox/dgauges/ScaleBase
133 // The scale being processed.
134 // tickItem: Object
135 // An object containing the tick informations.
136 return group.createLine({
137 x1: 0,
138 y1: 0,
139 x2: tickItem.isMinor ? 6 : 10,
140 y2: 0
141 }).setStroke(this.tickStroke);
142 },
143
144 getIndicatorRenderer: function(name){
145 // summary:
146 // Gets the GFX shape of an indicator.
147 // name: String
148 // The name of the indicator as defined using addIndicator.
149 // returns: dojox/gfx/canvas/Shape
150 // The GFX shape of the indicator.
151 return this._indicatorsRenderers[name];
152 },
153
154 removeIndicator: function(name){
155 // summary:
156 // Removes an indicator.
157 // name: String
158 // The name of the indicator as defined using addIndicator.
159 // returns: IndicatorBase
160 // The removed indicator.
161 var indicator = this._indicatorsIndex[name];
162 if(indicator){
163 indicator._gfxGroup.removeShape();
164 var idx = this._indicators.indexOf(indicator);
165 this._indicators.splice(idx, 1);
166
167 indicator._disconnectListeners();
168
169 delete this._indicatorsIndex[name];
170 delete this._indicatorsRenderers[name];
171 }
172 if(this._gauge){
173 this._gauge._resetMainIndicator();
174 }
175 this.invalidateRendering();
176 return indicator;
177 },
178
179 getIndicator: function(name){
180 // summary:
181 // Get an indicator instance.
182 // name: String
183 // The name of the indicator as defined using addIndicator.
184 // returns: IndicatorBase
185 // The indicator associated with the name parameter.
186 return this._indicatorsIndex[name];
187 },
188
189 addIndicator: function(name, indicator, behindScale){
190 // summary:
191 // Add an indicator to the scale. Before calling this function, ensure
192 // this scale has already been added to a gauge using the addElement method
193 // of the gauge.
194 // name: String
195 // The name of the indicator to be added.
196 // indicator: dojox/dgauges/IndicatorBase
197 // The indicator to add to this scale.
198 // behindScale: Boolean
199 // If true, this indicator is drawn behind the scale. Default value is false.
200 if(this._indicatorsIndex[name] && this._indicatorsIndex[name] != indicator){
201 this.removeIndicator(name);
202 }
203
204 this._indicators.push(indicator);
205 this._indicatorsIndex[name] = indicator;
206
207 if(!this._ticksGroup){
208 this._createSubGroups();
209 }
210
211 var group = behindScale ? this._bgGroup : this._fgGroup;
212 indicator._gfxGroup = group.createGroup();
213
214 indicator.scale = this;
215
216 return this.invalidateRendering();
217 },
218
219 _createSubGroups: function(){
220 // summary:
221 // Internal method.
222 // tags:
223 // private
224 if(!this._gfxGroup || this._ticksGroup){
225 return;
226 }
227 this._bgGroup = this._gfxGroup.createGroup();
228 this._ticksGroup = this._gfxGroup.createGroup();
229 this._fgGroup = this._gfxGroup.createGroup();
230 },
231
232 refreshRendering: function(){
233 if(!this._ticksGroup){
234 this._createSubGroups();
235 }
236 }
237 });
238 });
239