source: Dev/branches/rest-dojo-ui/client/dojox/charting/plot2d/Base.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 8.7 KB
Line 
1define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/connect",
2                "../Element", "./_PlotEvents", "dojo/_base/array",
3                "../scaler/primitive", "./common", "dojox/gfx/fx"],
4        function(lang, declare, hub, Element, PlotEvents, arr, primitive, common, fx){
5/*=====
6var Element = dojox.charting.Element;
7var PlotEvents = dojox.charting.plot2d._PlotEvents;
8dojox.charting.plot2d.__PlotCtorArgs = function(){
9        //      summary:
10        //              The base keyword arguments object for plot constructors.
11        //              Note that the parameters for this may change based on the
12        //              specific plot type (see the corresponding plot type for
13        //              details).
14}
15=====*/
16return declare("dojox.charting.plot2d.Base", [Element, PlotEvents], {
17        constructor: function(chart, kwArgs){
18                //      summary:
19                //              Create a base plot for charting.
20                //      chart: dojox.chart.Chart
21                //              The chart this plot belongs to.
22                //      kwArgs: dojox.charting.plot2d.__PlotCtorArgs?
23                //              An optional arguments object to help define the plot.
24                this.zoom = null,
25                this.zoomQueue = [];    // zooming action task queue
26                this.lastWindow = {vscale: 1, hscale: 1, xoffset: 0, yoffset: 0};
27        },
28        clear: function(){
29                //      summary:
30                //              Clear out all of the information tied to this plot.
31                //      returns: dojox.charting.plot2d.Base
32                //              A reference to this plot for functional chaining.
33                this.series = [];
34                this._hAxis = null;
35                this._vAxis = null;
36                this.dirty = true;
37                return this;    //      dojox.charting.plot2d.Base
38        },
39        setAxis: function(axis){
40                //      summary:
41                //              Set an axis for this plot.
42                //      axis: dojox.charting.axis2d.Base
43                //              The axis to set.
44                //      returns: dojox.charting.plot2d.Base
45                //              A reference to this plot for functional chaining.
46                if(axis){
47                        this[axis.vertical ? "_vAxis" : "_hAxis"] = axis;
48                }
49                return this;    //      dojox.charting.plot2d.Base
50        },
51        toPage: function(coord){
52                //      summary:
53                //              Compute page coordinates from plot axis data coordinates.
54                //      coord: Object?
55                //              The coordinates in plot axis data coordinate space. For cartesian charts that is of the following form:
56                //                      `{ hAxisName: 50, vAxisName: 200 }`
57                //              If not provided return the tranform method instead of the result of the transformation.
58                //      returns: Object
59                //              The resulting page pixel coordinates. That is of the following form:
60                //                      `{ x: 50, y: 200 }`
61                var ah = this._hAxis, av = this._vAxis,
62                        sh = ah.getScaler(), sv = av.getScaler(), 
63                        th = sh.scaler.getTransformerFromModel(sh),
64                        tv = sv.scaler.getTransformerFromModel(sv),
65                        c = this.chart.getCoords(),
66                        o = this.chart.offsets, dim = this.chart.dim;
67                var t = function(coord){
68                        var r = {};
69                        r.x = th(coord[ah.name]) + c.x + o.l;
70                        r.y = c.y + dim.height - o.b - tv(coord[av.name]);
71                        return r;
72                };
73                // if no coord return the function so that we can capture the current transforms
74                // and reuse them later on
75                return coord?t(coord):t;
76        },
77        toData: function(coord){
78                //      summary:
79                //              Compute plot axis data coordinates from page coordinates.
80                //      coord: Object
81                //              The pixel coordinate in page coordinate space. That is of the following form:
82                //                      `{ x: 50, y: 200 }`
83                //              If not provided return the tranform method instead of the result of the transformation.
84                //      returns: Object
85                //              The resulting plot axis data coordinates. For cartesian charts that is of the following form:
86                //                      `{ hAxisName: 50, vAxisName: 200 }`
87                var ah = this._hAxis, av = this._vAxis,
88                        sh = ah.getScaler(), sv = av.getScaler(), 
89                        th = sh.scaler.getTransformerFromPlot(sh),
90                        tv = sv.scaler.getTransformerFromPlot(sv),
91                        c = this.chart.getCoords(),
92                        o = this.chart.offsets, dim = this.chart.dim;
93                var t = function(coord){
94                        var r = {};
95                        r[ah.name] = th(coord.x - c.x - o.l);
96                        r[av.name] = tv(c.y + dim.height - coord.y  - o.b);
97                        return r;
98                };
99                // if no coord return the function so that we can capture the current transforms
100                // and reuse them later on
101                return coord?t(coord):t;
102        },
103        addSeries: function(run){
104                //      summary:
105                //              Add a data series to this plot.
106                //      run: dojox.charting.Series
107                //              The series to be added.
108                //      returns: dojox.charting.plot2d.Base
109                //              A reference to this plot for functional chaining.
110                this.series.push(run);
111                return this;    //      dojox.charting.plot2d.Base
112        },
113        getSeriesStats: function(){
114                //      summary:
115                //              Calculate the min/max on all attached series in both directions.
116                //      returns: Object
117                //              {hmin, hmax, vmin, vmax} min/max in both directions.
118                return common.collectSimpleStats(this.series);
119        },
120        calculateAxes: function(dim){
121                //      summary:
122                //              Stub function for running the axis calculations (depricated).
123                //      dim: Object
124                //              An object of the form { width, height }
125                //      returns: dojox.charting.plot2d.Base
126                //              A reference to this plot for functional chaining.
127                this.initializeScalers(dim, this.getSeriesStats());
128                return this;    //      dojox.charting.plot2d.Base
129        },
130        isDirty: function(){
131                //      summary:
132                //              Returns whether or not this plot needs to be rendered.
133                //      returns: Boolean
134                //              The state of the plot.
135                return this.dirty || this._hAxis && this._hAxis.dirty || this._vAxis && this._vAxis.dirty;      //      Boolean
136        },
137        isDataDirty: function(){
138                //      summary:
139                //              Returns whether or not any of this plot's data series need to be rendered.
140                //      returns: Boolean
141                //              Flag indicating if any of this plot's series are invalid and need rendering.
142                return arr.some(this.series, function(item){ return item.dirty; });     //      Boolean
143        },
144        performZoom: function(dim, offsets){
145                //      summary:
146                //              Create/alter any zooming windows on this plot.
147                //      dim: Object
148                //              An object of the form { width, height }.
149                //      offsets: Object
150                //              An object of the form { l, r, t, b }.
151                //      returns: dojox.charting.plot2d.Base
152                //              A reference to this plot for functional chaining.
153
154                // get current zooming various
155                var vs = this._vAxis.scale || 1,
156                        hs = this._hAxis.scale || 1,
157                        vOffset = dim.height - offsets.b,
158                        hBounds = this._hScaler.bounds,
159                        xOffset = (hBounds.from - hBounds.lower) * hBounds.scale,
160                        vBounds = this._vScaler.bounds,
161                        yOffset = (vBounds.from - vBounds.lower) * vBounds.scale,
162                        // get incremental zooming various
163                        rVScale = vs / this.lastWindow.vscale,
164                        rHScale = hs / this.lastWindow.hscale,
165                        rXOffset = (this.lastWindow.xoffset - xOffset)/
166                                ((this.lastWindow.hscale == 1)? hs : this.lastWindow.hscale),
167                        rYOffset = (yOffset - this.lastWindow.yoffset)/
168                                ((this.lastWindow.vscale == 1)? vs : this.lastWindow.vscale),
169
170                        shape = this.group,
171                        anim = fx.animateTransform(lang.delegate({
172                                shape: shape,
173                                duration: 1200,
174                                transform:[
175                                        {name:"translate", start:[0, 0], end: [offsets.l * (1 - rHScale), vOffset * (1 - rVScale)]},
176                                        {name:"scale", start:[1, 1], end: [rHScale, rVScale]},
177                                        {name:"original"},
178                                        {name:"translate", start: [0, 0], end: [rXOffset, rYOffset]}
179                                ]}, this.zoom));
180
181                lang.mixin(this.lastWindow, {vscale: vs, hscale: hs, xoffset: xOffset, yoffset: yOffset});
182                //add anim to zooming action queue,
183                //in order to avoid several zooming action happened at the same time
184                this.zoomQueue.push(anim);
185                //perform each anim one by one in zoomQueue
186                hub.connect(anim, "onEnd", this, function(){
187                        this.zoom = null;
188                        this.zoomQueue.shift();
189                        if(this.zoomQueue.length > 0){
190                                this.zoomQueue[0].play();
191                        }
192                });
193                if(this.zoomQueue.length == 1){
194                        this.zoomQueue[0].play();
195                }
196                return this;    //      dojox.charting.plot2d.Base
197        },
198        render: function(dim, offsets){
199                //      summary:
200                //              Render the plot on the chart.
201                //      dim: Object
202                //              An object of the form { width, height }.
203                //      offsets: Object
204                //              An object of the form { l, r, t, b }.
205                //      returns: dojox.charting.plot2d.Base
206                //              A reference to this plot for functional chaining.
207                return this;    //      dojox.charting.plot2d.Base
208        },
209        getRequiredColors: function(){
210                //      summary:
211                //              Get how many data series we have, so we know how many colors to use.
212                //      returns: Number
213                //              The number of colors needed.
214                return this.series.length;      //      Number
215        },
216        initializeScalers: function(dim, stats){
217                //      summary:
218                //              Initializes scalers using attached axes.
219                //      dim: Object:
220                //              Size of a plot area in pixels as {width, height}.
221                //      stats: Object:
222                //              Min/max of data in both directions as {hmin, hmax, vmin, vmax}.
223                //      returns: dojox.charting.plot2d.Base
224                //              A reference to this plot for functional chaining.
225                if(this._hAxis){
226                        if(!this._hAxis.initialized()){
227                                this._hAxis.calculate(stats.hmin, stats.hmax, dim.width);
228                        }
229                        this._hScaler = this._hAxis.getScaler();
230                }else{
231                        this._hScaler = primitive.buildScaler(stats.hmin, stats.hmax, dim.width);
232                }
233                if(this._vAxis){
234                        if(!this._vAxis.initialized()){
235                                this._vAxis.calculate(stats.vmin, stats.vmax, dim.height);
236                        }
237                        this._vScaler = this._vAxis.getScaler();
238                }else{
239                        this._vScaler = primitive.buildScaler(stats.vmin, stats.vmax, dim.height);
240                }
241                return this;    //      dojox.charting.plot2d.Base
242        }
243});
244});
Note: See TracBrowser for help on using the repository browser.