Project

General

Profile

Statistics
| Revision:

root / trunk / web / dojo / dojox / charting / Chart2D.js @ 10

History | View | Annotate | Download (13.3 KB)

1
/*
2
        Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3
        Available via Academic Free License >= 2.1 OR the modified BSD license.
4
        see: http://dojotoolkit.org/license for details
5
*/
6

    
7

    
8
if(!dojo._hasResource["dojox.charting.Chart2D"]){
9
dojo._hasResource["dojox.charting.Chart2D"]=true;
10
dojo.provide("dojox.charting.Chart2D");
11
dojo.require("dojox.gfx");
12
dojo.require("dojox.lang.functional");
13
dojo.require("dojox.lang.functional.fold");
14
dojo.require("dojox.lang.functional.reversed");
15
dojo.require("dojox.charting.Theme");
16
dojo.require("dojox.charting.Series");
17
dojo.require("dojox.charting.axis2d.Default");
18
dojo.require("dojox.charting.axis2d.Invisible");
19
dojo.require("dojox.charting.plot2d.Default");
20
dojo.require("dojox.charting.plot2d.Lines");
21
dojo.require("dojox.charting.plot2d.Areas");
22
dojo.require("dojox.charting.plot2d.Markers");
23
dojo.require("dojox.charting.plot2d.MarkersOnly");
24
dojo.require("dojox.charting.plot2d.Scatter");
25
dojo.require("dojox.charting.plot2d.Stacked");
26
dojo.require("dojox.charting.plot2d.StackedLines");
27
dojo.require("dojox.charting.plot2d.StackedAreas");
28
dojo.require("dojox.charting.plot2d.Columns");
29
dojo.require("dojox.charting.plot2d.StackedColumns");
30
dojo.require("dojox.charting.plot2d.ClusteredColumns");
31
dojo.require("dojox.charting.plot2d.Bars");
32
dojo.require("dojox.charting.plot2d.StackedBars");
33
dojo.require("dojox.charting.plot2d.ClusteredBars");
34
dojo.require("dojox.charting.plot2d.Grid");
35
dojo.require("dojox.charting.plot2d.Pie");
36
dojo.require("dojox.charting.plot2d.Bubble");
37
dojo.require("dojox.charting.plot2d.Candlesticks");
38
dojo.require("dojox.charting.plot2d.OHLC");
39
(function(){
40
var df=dojox.lang.functional,dc=dojox.charting,_1=df.lambda("item.clear()"),_2=df.lambda("item.purgeGroup()"),_3=df.lambda("item.destroy()"),_4=df.lambda("item.dirty = false"),_5=df.lambda("item.dirty = true"),_6=df.lambda("item.name");
41
dojo.declare("dojox.charting.Chart2D",null,{constructor:function(_7,_8){
42
if(!_8){
43
_8={};
44
}
45
this.margins=_8.margins?_8.margins:{l:10,t:10,r:10,b:10};
46
this.stroke=_8.stroke;
47
this.fill=_8.fill;
48
this.delayInMs=_8.delayInMs||200;
49
this.theme=null;
50
this.axes={};
51
this.stack=[];
52
this.plots={};
53
this.series=[];
54
this.runs={};
55
this.dirty=true;
56
this.coords=null;
57
this.node=dojo.byId(_7);
58
var _9=dojo.marginBox(_7);
59
this.surface=dojox.gfx.createSurface(this.node,_9.w||400,_9.h||300);
60
},destroy:function(){
61
dojo.forEach(this.series,_3);
62
dojo.forEach(this.stack,_3);
63
df.forIn(this.axes,_3);
64
this.surface.destroy();
65
},getCoords:function(){
66
if(!this.coords){
67
this.coords=dojo.coords(this.node,true);
68
}
69
return this.coords;
70
},setTheme:function(_a){
71
this.theme=_a.clone();
72
this.dirty=true;
73
return this;
74
},addAxis:function(_b,_c){
75
var _d;
76
if(!_c||!("type" in _c)){
77
_d=new dc.axis2d.Default(this,_c);
78
}else{
79
_d=typeof _c.type=="string"?new dc.axis2d[_c.type](this,_c):new _c.type(this,_c);
80
}
81
_d.name=_b;
82
_d.dirty=true;
83
if(_b in this.axes){
84
this.axes[_b].destroy();
85
}
86
this.axes[_b]=_d;
87
this.dirty=true;
88
return this;
89
},getAxis:function(_e){
90
return this.axes[_e];
91
},removeAxis:function(_f){
92
if(_f in this.axes){
93
this.axes[_f].destroy();
94
delete this.axes[_f];
95
this.dirty=true;
96
}
97
return this;
98
},addPlot:function(_10,_11){
99
var _12;
100
if(!_11||!("type" in _11)){
101
_12=new dc.plot2d.Default(this,_11);
102
}else{
103
_12=typeof _11.type=="string"?new dc.plot2d[_11.type](this,_11):new _11.type(this,_11);
104
}
105
_12.name=_10;
106
_12.dirty=true;
107
if(_10 in this.plots){
108
this.stack[this.plots[_10]].destroy();
109
this.stack[this.plots[_10]]=_12;
110
}else{
111
this.plots[_10]=this.stack.length;
112
this.stack.push(_12);
113
}
114
this.dirty=true;
115
return this;
116
},removePlot:function(_13){
117
if(_13 in this.plots){
118
var _14=this.plots[_13];
119
delete this.plots[_13];
120
this.stack[_14].destroy();
121
this.stack.splice(_14,1);
122
df.forIn(this.plots,function(idx,_15,_16){
123
if(idx>_14){
124
_16[_15]=idx-1;
125
}
126
});
127
this.dirty=true;
128
}
129
return this;
130
},getPlotOrder:function(){
131
return df.map(this.stack,_6);
132
},setPlotOrder:function(_17){
133
var _18={},_19=df.filter(_17,function(_1a){
134
if(!(_1a in this.plots)||(_1a in _18)){
135
return false;
136
}
137
_18[_1a]=1;
138
return true;
139
},this);
140
if(_19.length<this.stack.length){
141
df.forEach(this.stack,function(_1b){
142
var _1c=_1b.name;
143
if(!(_1c in _18)){
144
_19.push(_1c);
145
}
146
});
147
}
148
var _1d=df.map(_19,function(_1e){
149
return this.stack[this.plots[_1e]];
150
},this);
151
df.forEach(_1d,function(_1f,i){
152
this.plots[_1f.name]=i;
153
},this);
154
this.stack=_1d;
155
this.dirty=true;
156
return this;
157
},movePlotToFront:function(_20){
158
if(_20 in this.plots){
159
var _21=this.plots[_20];
160
if(_21){
161
var _22=this.getPlotOrder();
162
_22.splice(_21,1);
163
_22.unshift(_20);
164
return this.setPlotOrder(_22);
165
}
166
}
167
return this;
168
},movePlotToBack:function(_23){
169
if(_23 in this.plots){
170
var _24=this.plots[_23];
171
if(_24<this.stack.length-1){
172
var _25=this.getPlotOrder();
173
_25.splice(_24,1);
174
_25.push(_23);
175
return this.setPlotOrder(_25);
176
}
177
}
178
return this;
179
},addSeries:function(_26,_27,_28){
180
var run=new dc.Series(this,_27,_28);
181
run.name=_26;
182
if(_26 in this.runs){
183
this.series[this.runs[_26]].destroy();
184
this.series[this.runs[_26]]=run;
185
}else{
186
this.runs[_26]=this.series.length;
187
this.series.push(run);
188
}
189
this.dirty=true;
190
if(!("ymin" in run)&&"min" in run){
191
run.ymin=run.min;
192
}
193
if(!("ymax" in run)&&"max" in run){
194
run.ymax=run.max;
195
}
196
return this;
197
},removeSeries:function(_29){
198
if(_29 in this.runs){
199
var _2a=this.runs[_29],_2b=this.series[_2a].plot;
200
delete this.runs[_29];
201
this.series[_2a].destroy();
202
this.series.splice(_2a,1);
203
df.forIn(this.runs,function(idx,_2c,_2d){
204
if(idx>_2a){
205
_2d[_2c]=idx-1;
206
}
207
});
208
this.dirty=true;
209
}
210
return this;
211
},updateSeries:function(_2e,_2f){
212
if(_2e in this.runs){
213
var run=this.series[this.runs[_2e]];
214
run.update(_2f);
215
this._invalidateDependentPlots(run.plot,false);
216
this._invalidateDependentPlots(run.plot,true);
217
}
218
return this;
219
},getSeriesOrder:function(_30){
220
return df.map(df.filter(this.series,function(run){
221
return run.plot==_30;
222
}),_6);
223
},setSeriesOrder:function(_31){
224
var _32,_33={},_34=df.filter(_31,function(_35){
225
if(!(_35 in this.runs)||(_35 in _33)){
226
return false;
227
}
228
var run=this.series[this.runs[_35]];
229
if(_32){
230
if(run.plot!=_32){
231
return false;
232
}
233
}else{
234
_32=run.plot;
235
}
236
_33[_35]=1;
237
return true;
238
},this);
239
df.forEach(this.series,function(run){
240
var _36=run.name;
241
if(!(_36 in _33)&&run.plot==_32){
242
_34.push(_36);
243
}
244
});
245
var _37=df.map(_34,function(_38){
246
return this.series[this.runs[_38]];
247
},this);
248
this.series=_37.concat(df.filter(this.series,function(run){
249
return run.plot!=_32;
250
}));
251
df.forEach(this.series,function(run,i){
252
this.runs[run.name]=i;
253
},this);
254
this.dirty=true;
255
return this;
256
},moveSeriesToFront:function(_39){
257
if(_39 in this.runs){
258
var _3a=this.runs[_39],_3b=this.getSeriesOrder(this.series[_3a].plot);
259
if(_39!=_3b[0]){
260
_3b.splice(_3a,1);
261
_3b.unshift(_39);
262
return this.setSeriesOrder(_3b);
263
}
264
}
265
return this;
266
},moveSeriesToBack:function(_3c){
267
if(_3c in this.runs){
268
var _3d=this.runs[_3c],_3e=this.getSeriesOrder(this.series[_3d].plot);
269
if(_3c!=_3e[_3e.length-1]){
270
_3e.splice(_3d,1);
271
_3e.push(_3c);
272
return this.setSeriesOrder(_3e);
273
}
274
}
275
return this;
276
},resize:function(_3f,_40){
277
var box;
278
switch(arguments.length){
279
case 0:
280
box=dojo.marginBox(this.node);
281
break;
282
case 1:
283
box=_3f;
284
break;
285
default:
286
box={w:_3f,h:_40};
287
break;
288
}
289
dojo.marginBox(this.node,box);
290
this.surface.setDimensions(box.w,box.h);
291
this.dirty=true;
292
this.coords=null;
293
return this.render();
294
},getGeometry:function(){
295
var ret={};
296
df.forIn(this.axes,function(_41){
297
if(_41.initialized()){
298
ret[_41.name]={name:_41.name,vertical:_41.vertical,scaler:_41.scaler,ticks:_41.ticks};
299
}
300
});
301
return ret;
302
},setAxisWindow:function(_42,_43,_44,_45){
303
var _46=this.axes[_42];
304
if(_46){
305
_46.setWindow(_43,_44);
306
dojo.forEach(this.stack,function(_47){
307
if(_47.hAxis==_42||_47.vAxis==_42){
308
_47.zoom=_45;
309
}
310
});
311
}
312
return this;
313
},setWindow:function(sx,sy,dx,dy,_48){
314
if(!("plotArea" in this)){
315
this.calculateGeometry();
316
}
317
df.forIn(this.axes,function(_49){
318
var _4a,_4b,_4c=_49.getScaler().bounds,s=_4c.span/(_4c.upper-_4c.lower);
319
if(_49.vertical){
320
_4a=sy;
321
_4b=dy/s/_4a;
322
}else{
323
_4a=sx;
324
_4b=dx/s/_4a;
325
}
326
_49.setWindow(_4a,_4b);
327
});
328
dojo.forEach(this.stack,function(_4d){
329
_4d.zoom=_48;
330
});
331
return this;
332
},zoomIn:function(_4e,_4f){
333
var _50=this.axes[_4e];
334
if(_50){
335
var _51,_52,_53=_50.getScaler().bounds;
336
var _54=Math.min(_4f[0],_4f[1]);
337
var _55=Math.max(_4f[0],_4f[1]);
338
_54=_4f[0]<_53.lower?_53.lower:_54;
339
_55=_4f[1]>_53.upper?_53.upper:_55;
340
_51=(_53.upper-_53.lower)/(_55-_54);
341
_52=_54-_53.lower;
342
this.setAxisWindow(_4e,_51,_52);
343
this.render();
344
}
345
},calculateGeometry:function(){
346
if(this.dirty){
347
return this.fullGeometry();
348
}
349
var _56=dojo.filter(this.stack,function(_57){
350
return _57.dirty||(_57.hAxis&&this.axes[_57.hAxis].dirty)||(_57.vAxis&&this.axes[_57.vAxis].dirty);
351
},this);
352
_58(_56,this.plotArea);
353
return this;
354
},fullGeometry:function(){
355
this._makeDirty();
356
dojo.forEach(this.stack,_1);
357
if(!this.theme){
358
this.setTheme(new dojox.charting.Theme(dojox.charting._def));
359
}
360
dojo.forEach(this.series,function(run){
361
if(!(run.plot in this.plots)){
362
var _59=new dc.plot2d.Default(this,{});
363
_59.name=run.plot;
364
this.plots[run.plot]=this.stack.length;
365
this.stack.push(_59);
366
}
367
this.stack[this.plots[run.plot]].addSeries(run);
368
},this);
369
dojo.forEach(this.stack,function(_5a){
370
if(_5a.hAxis){
371
_5a.setAxis(this.axes[_5a.hAxis]);
372
}
373
if(_5a.vAxis){
374
_5a.setAxis(this.axes[_5a.vAxis]);
375
}
376
},this);
377
var dim=this.dim=this.surface.getDimensions();
378
dim.width=dojox.gfx.normalizedLength(dim.width);
379
dim.height=dojox.gfx.normalizedLength(dim.height);
380
df.forIn(this.axes,_1);
381
_58(this.stack,dim);
382
var _5b=this.offsets={l:0,r:0,t:0,b:0};
383
df.forIn(this.axes,function(_5c){
384
df.forIn(_5c.getOffsets(),function(o,i){
385
_5b[i]+=o;
386
});
387
});
388
df.forIn(this.margins,function(o,i){
389
_5b[i]+=o;
390
});
391
this.plotArea={width:dim.width-_5b.l-_5b.r,height:dim.height-_5b.t-_5b.b};
392
df.forIn(this.axes,_1);
393
_58(this.stack,this.plotArea);
394
return this;
395
},render:function(){
396
if(this.theme){
397
this.theme.clear();
398
}
399
if(this.dirty){
400
return this.fullRender();
401
}
402
this.calculateGeometry();
403
df.forEachRev(this.stack,function(_5d){
404
_5d.render(this.dim,this.offsets);
405
},this);
406
df.forIn(this.axes,function(_5e){
407
_5e.render(this.dim,this.offsets);
408
},this);
409
this._makeClean();
410
if(this.surface.render){
411
this.surface.render();
412
}
413
return this;
414
},fullRender:function(){
415
this.fullGeometry();
416
var _5f=this.offsets,dim=this.dim;
417
dojo.forEach(this.series,_2);
418
df.forIn(this.axes,_2);
419
dojo.forEach(this.stack,_2);
420
this.surface.clear();
421
var t=this.theme,_60=t.plotarea&&t.plotarea.fill,_61=t.plotarea&&t.plotarea.stroke;
422
if(_60){
423
this.surface.createRect({x:_5f.l-1,y:_5f.t-1,width:dim.width-_5f.l-_5f.r+2,height:dim.height-_5f.t-_5f.b+2}).setFill(_60);
424
}
425
if(_61){
426
this.surface.createRect({x:_5f.l,y:_5f.t,width:dim.width-_5f.l-_5f.r+1,height:dim.height-_5f.t-_5f.b+1}).setStroke(_61);
427
}
428
df.foldr(this.stack,function(z,_62){
429
return _62.render(dim,_5f),0;
430
},0);
431
_60=this.fill!==undefined?this.fill:(t.chart&&t.chart.fill);
432
_61=this.stroke!==undefined?this.stroke:(t.chart&&t.chart.stroke);
433
if(_60=="inherit"){
434
var _63=this.node,_60=new dojo.Color(dojo.style(_63,"backgroundColor"));
435
while(_60.a==0&&_63!=document.documentElement){
436
_60=new dojo.Color(dojo.style(_63,"backgroundColor"));
437
_63=_63.parentNode;
438
}
439
}
440
if(_60){
441
if(_5f.l){
442
this.surface.createRect({width:_5f.l,height:dim.height+1}).setFill(_60);
443
}
444
if(_5f.r){
445
this.surface.createRect({x:dim.width-_5f.r,width:_5f.r+1,height:dim.height+2}).setFill(_60);
446
}
447
if(_5f.t){
448
this.surface.createRect({width:dim.width+1,height:_5f.t}).setFill(_60);
449
}
450
if(_5f.b){
451
this.surface.createRect({y:dim.height-_5f.b,width:dim.width+1,height:_5f.b+2}).setFill(_60);
452
}
453
}
454
if(_61){
455
this.surface.createRect({width:dim.width-1,height:dim.height-1}).setStroke(_61);
456
}
457
df.forIn(this.axes,function(_64){
458
_64.render(dim,_5f);
459
});
460
this._makeClean();
461
if(this.surface.render){
462
this.surface.render();
463
}
464
return this;
465
},delayedRender:function(){
466
if(!this._delayedRenderHandle){
467
this._delayedRenderHandle=setTimeout(dojo.hitch(this,function(){
468
clearTimeout(this._delayedRenderHandle);
469
this._delayedRenderHandle=null;
470
this.render();
471
}),this.delayInMs);
472
}
473
return this;
474
},connectToPlot:function(_65,_66,_67){
475
return _65 in this.plots?this.stack[this.plots[_65]].connect(_66,_67):null;
476
},fireEvent:function(_68,_69,_6a){
477
if(_68 in this.runs){
478
var _6b=this.series[this.runs[_68]].plot;
479
if(_6b in this.plots){
480
var _6c=this.stack[this.plots[_6b]];
481
if(_6c){
482
_6c.fireEvent(_68,_69,_6a);
483
}
484
}
485
}
486
return this;
487
},_makeClean:function(){
488
dojo.forEach(this.axes,_4);
489
dojo.forEach(this.stack,_4);
490
dojo.forEach(this.series,_4);
491
this.dirty=false;
492
},_makeDirty:function(){
493
dojo.forEach(this.axes,_5);
494
dojo.forEach(this.stack,_5);
495
dojo.forEach(this.series,_5);
496
this.dirty=true;
497
},_invalidateDependentPlots:function(_6d,_6e){
498
if(_6d in this.plots){
499
var _6f=this.stack[this.plots[_6d]],_70,_71=_6e?"vAxis":"hAxis";
500
if(_6f[_71]){
501
_70=this.axes[_6f[_71]];
502
if(_70&&_70.dependOnData()){
503
_70.dirty=true;
504
dojo.forEach(this.stack,function(p){
505
if(p[_71]&&p[_71]==_6f[_71]){
506
p.dirty=true;
507
}
508
});
509
}
510
}else{
511
_6f.dirty=true;
512
}
513
}
514
}});
515
function _72(_73){
516
return {min:_73.hmin,max:_73.hmax};
517
};
518
function _74(_75){
519
return {min:_75.vmin,max:_75.vmax};
520
};
521
function _76(_77,h){
522
_77.hmin=h.min;
523
_77.hmax=h.max;
524
};
525
function _78(_79,v){
526
_79.vmin=v.min;
527
_79.vmax=v.max;
528
};
529
function _7a(_7b,_7c){
530
if(_7b&&_7c){
531
_7b.min=Math.min(_7b.min,_7c.min);
532
_7b.max=Math.max(_7b.max,_7c.max);
533
}
534
return _7b||_7c;
535
};
536
function _58(_7d,_7e){
537
var _7f={},_80={};
538
dojo.forEach(_7d,function(_81){
539
var _82=_7f[_81.name]=_81.getSeriesStats();
540
if(_81.hAxis){
541
_80[_81.hAxis]=_7a(_80[_81.hAxis],_72(_82));
542
}
543
if(_81.vAxis){
544
_80[_81.vAxis]=_7a(_80[_81.vAxis],_74(_82));
545
}
546
});
547
dojo.forEach(_7d,function(_83){
548
var _84=_7f[_83.name];
549
if(_83.hAxis){
550
_76(_84,_80[_83.hAxis]);
551
}
552
if(_83.vAxis){
553
_78(_84,_80[_83.vAxis]);
554
}
555
_83.initializeScalers(_7e,_84);
556
});
557
};
558
})();
559
}