comparison war/scripts/sti/ExtendedDataSource.js @ 3:cf06b77a8bbd

Committed branch of the e4D repos sti-gwt branch 16384. git-svn-id: http://dev.dariah.eu/svn/repos/eu.dariah.de/ap1/sti-gwt-dariah-geobrowser@36 f2b5be40-def6-11e0-8a09-b3c1cc336c6b
author StefanFunk <StefanFunk@f2b5be40-def6-11e0-8a09-b3c1cc336c6b>
date Tue, 17 Jul 2012 13:34:40 +0000
parents
children
comparison
equal deleted inserted replaced
2:2897af43ccc6 3:cf06b77a8bbd
1 /**
2 * small class that represents a time slice of the actual timeplot.
3 * it has a specific date and contains its corrsponding data objects as well
4 * @param {Date} date the date of the timeslice
5 *
6 * @constructor
7 */
8 function TimeSlice(date){
9
10 this.date = date;
11 this.elements = [];
12 this.selected = false;
13
14 };
15
16 /**
17 * data source to allow dynamic and manual creation of time slices, which will be used as input for the Simile Timeplot.
18 * at the end we will get around 200 time slices, which units are depending on the time range of the input data
19 *
20 * @constructor
21 */
22 function ExtendedDataSource(){
23
24 this.timeSlices;
25 this.unit;
26 this.minDate;
27 this.maxDate;
28 this.eventSources;
29 this.events;
30 this.leftSlice;
31 this.rightSlice;
32
33 };
34
35 ExtendedDataSource.prototype = {
36
37 /**
38 * initializes the ExtendedDataSource
39 * @param {Timeplot.ColumnSource[]} dataSources the column sources corresponding to the data sets
40 * @param {Timeplot.DefaultEventSource[]} eventSources the event sources corresponding to the column sources
41 * @param {Array} dataSets all data sets
42 * @param {SimileAjax.DateTime} granularity the time granularity of the given data
43 */
44 initialize: function( dataSources, eventSources, dataSets, granularity ){
45
46 this.minDate = undefined;
47 this.maxDate = undefined;
48
49 for (var i = 0; i < dataSets.length; i++) {
50 for (var j = 0; j < dataSets[i].objects.length; j++) {
51 var o = dataSets[i].objects[j];
52 var timeMin = o.timeStamp;
53 var timeMax = o.timeStamp;
54 if( o.timeStamp == undefined ){
55 timeMin = o.timeSpan.start;
56 timeMax = o.timeSpan.end;
57 }
58 if (this.minDate == undefined || timeMin.getTime() < this.minDate.getTime())
59 this.minDate = timeMin;
60 if (this.maxDate == undefined || timeMax.getTime() > this.maxDate.getTime())
61 this.maxDate = timeMax;
62 }
63 }
64 this.timeSlices = [];
65
66 var time = SimileAjax.DateTime;
67 var u = SimileAjax.NativeDateUnit;
68 var p = this.maxDate - this.minDate;
69
70 var periodUnit = -1;
71 do {
72 periodUnit++;
73 }
74 while( time.gregorianUnitLengths[periodUnit] < p );
75
76 switch (granularity){
77 case time.MILLISECOND:
78 case time.SECOND:
79 if( periodUnit > time.QUARTER )
80 this.unit = periodUnit - 7;
81 else if( periodUnit > time.TWOWEEKS )
82 this.unit = time.DAY;
83 else if( periodUnit > time.HOUR )
84 this.unit = periodUnit - 5;
85 else if( periodUnit > time.HALFMINUTE )
86 this.unit = periodUnit - 4;
87 else
88 this.unit = time.SECOND;
89 break;
90 case time.DAY:
91 if( periodUnit > time.QUARTER )
92 this.unit = periodUnit - 7;
93 else
94 this.unit = time.DAY;
95 break;
96 case time.MONTH:
97 if( periodUnit > time.DECADE )
98 this.unit = periodUnit - 7;
99 else
100 this.unit = time.MONTH;
101 break;
102 case time.YEAR:
103 if( periodUnit > time.CENTURY )
104 this.unit = periodUnit - 7;
105 else
106 this.unit = time.YEAR;
107 break;
108 }
109
110 var timeZone;
111 var t = new Date(this.minDate.getTime() - 0.9 * time.gregorianUnitLengths[this.unit]);
112 do {
113 time.roundDownToInterval(t, this.unit, timeZone, 1, 0);
114 var slice = new TimeSlice(u.cloneValue(t));
115 this.timeSlices.push(slice);
116 time.incrementByInterval(t, this.unit, timeZone);
117 }
118 while (t.getTime() <= this.maxDate.getTime() + 1.1 * time.gregorianUnitLengths[this.unit]);
119
120 for (var i = 0; i < dataSources.length; i++)
121 for (var j = 0; j < this.timeSlices.length; j++)
122 this.timeSlices[j].elements.push([]);
123
124 for(var i=0; i<dataSets.length; i++ ){
125 if( !dataSets[i].hidden ){
126 for(var j=0; j<dataSets[i].objects.length; j++ ){
127 var o = dataSets[i].objects[j];
128 var timeMin = o.timeStamp;
129 var timeMax = o.timeStamp;
130 if( o.timeStamp == undefined ){
131 timeMin = o.timeSpan.start;
132 timeMax = o.timeSpan.end;
133 }
134 for( var k=0; k<this.timeSlices.length-1; k++ ){
135 var t1 = this.timeSlices[k].date.getTime();
136 var t2 = this.timeSlices[k+1].date.getTime();
137 if( ( timeMin >= t1 && timeMin < t2 ) || ( timeMax >= t1 && timeMax < t2 ) || ( timeMin <= t1 && timeMax >= t2 ) ){
138 this.timeSlices[k].elements[i].push(o);
139 }
140 if( k == this.timeSlices.length-2 && ( timeMin >= t2 || timeMax >= t2 ) ){
141 this.timeSlices[k+1].elements[i].push(o);
142 }
143 }
144 }
145 }
146 }
147
148 this.events = [];
149 for(var i=0; i<eventSources.length; i++ ){
150 var eventSet = [];
151 for(var j=0; j<this.timeSlices.length; j++ ){
152 var value = new Array( ""+this.timeSlices[j].elements[i].length );
153 eventSet.push( { date: this.timeSlices[j].date, value: value } );
154 }
155 eventSources[i].loadData( eventSet );
156 this.events.push(eventSet);
157 }
158
159 this.eventSources = eventSources;
160
161 this.leftSlice = 0;
162 this.rightSlice = this.timeSlices.length - 1;
163
164 setZoomLevels(Math.round( (this.timeSlices.length-3)/2 ));
165
166 },
167
168 /**
169 * computes the slice index corresponding to a given time
170 * @param {Date} time the given time
171 * @return the corresponding slice index
172 */
173 getSliceIndex: function( time ){
174 for(var i=0; i<this.timeSlices.length; i++ ){
175 if( time == this.timeSlices[i].date ){
176 return i;
177 }
178 }
179 },
180
181 /**
182 * returns the time of a specific time slice
183 * @param {int} time the given slice index
184 * @return the corresponding slice date
185 */
186 getSliceTime: function( index ){
187 return this.timeSlices[index].date;
188 },
189
190 /**
191 * shifts the actual zoomed range
192 * @param {int} delta the value to shift (negative for left shift, positive for right shift)
193 * @return boolean value, if the range could be shifted
194 */
195 setShift: function( delta ){
196 if( delta == 1 && this.leftSlice != 0 ){
197 this.leftSlice--;
198 this.rightSlice--;
199 return true;
200 }
201 else if( delta == -1 && this.rightSlice != this.timeSlices.length-1 ){
202 this.leftSlice++;
203 this.rightSlice++;
204 return true;
205 }
206 else {
207 return false;
208 }
209 },
210
211 /**
212 * zooms the actual range
213 * @param {int} delta the value to zoom (negative for zoom out, positive for zoom in)
214 * @param {Date} time the corresponding time of the actual mouse position on the plot
215 * @param {Date} leftTime the time of the left border of a selected timerange or null
216 * @param {Date} rightTime the time of the right border of a selected timerange or null
217 * @return boolean value, if the range could be zoomed
218 */
219 setZoom: function( delta, time, leftTime, rightTime ){
220 var n1 = 0;
221 var n2 = 0;
222 var m = -1;
223 if( delta > 0 ){
224 m = 1;
225 if( leftTime != null ){
226 n1 = this.getSliceIndex(leftTime) - this.leftSlice;
227 n2 = this.rightSlice - this.getSliceIndex(rightTime);
228 }
229 else {
230 slice = this.getSliceIndex(time);
231 if( slice == this.leftSlice || slice == this.rightSlice ){
232 return;
233 }
234 n1 = slice - 1 - this.leftSlice;
235 n2 = this.rightSlice - slice - 1;
236 }
237 }
238 else if( delta < 0 ){
239 n1 = this.leftSlice;
240 n2 = this.timeSlices.length - 1 -this.rightSlice;
241 }
242
243 var zoomSlices = 2*delta;
244 if( Math.abs(n1 + n2) < Math.abs(zoomSlices) ){
245 zoomSlices = n1 + n2;
246 }
247
248 if( n1 + n2 == 0 ){
249 return false;
250 }
251
252 var m1 = Math.round( n1 / (n1 + n2) * zoomSlices );
253 var m2 = zoomSlices - m1;
254
255 this.leftSlice += m1;
256 this.rightSlice -= m2;
257
258 return true;
259 },
260
261 /**
262 * resets the plots by loading data of actual zoomed range
263 */
264 reset: function( timeGeometry ){
265 for(var i=0; i<this.eventSources.length; i++ ){
266 this.eventSources[i].loadData( this.events[i].slice( this.leftSlice, this.rightSlice + 1) );
267 if( i+1 < this.eventSources.length ){
268 timeGeometry._earliestDate = null;
269 timeGeometry._latestDate = null;
270 }
271 }
272 },
273
274 /**
275 * Getter for actual zoom
276 * @return actual zoom value
277 */
278 getZoom: function(){
279 if( this.timeSlices == undefined ){
280 return 0;
281 }
282 return Math.round((this.timeSlices.length-3)/2 ) - Math.round((this.rightSlice-this.leftSlice-2)/2);
283 },
284
285 /**
286 * Getter for date of the first timeslice
287 * @return date of the first timeslice
288 */
289 earliest: function(){
290 return this.timeSlices[0].date;
291 },
292
293 /**
294 * Getter for date of the last timeslice
295 * @return date of the last timeslice
296 */
297 latest: function(){
298 return this.timeSlices[ this.timeSlices.length - 1 ].date;
299 }
300
301 };