Debian Codegraph @

dojox/charting/plot2d/CartesianBase.js

raw symbols SCIP
1 define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/connect", "dojo/has",
2 "./Base", "../scaler/primitive", "dojox/gfx", "dojox/gfx/fx", "dojox/lang/utils"],
3 function(lang, declare, hub, has, Base, primitive, gfx, fx, du){
4 /*=====
5 declare("dojox.charting.plot2d.__CartesianCtorArgs", dojox.charting.plot2d.__PlotCtorArgs, {
6 // hAxis: String?
7 // The horizontal axis name.
8 hAxis: "x",
9
10 // vAxis: String?
11 // The vertical axis name
12 vAxis: "y",
13
14 // labels: Boolean?
15 // For plots that support labels, whether or not to draw labels for each data item. Default is false.
16 labels: false,
17
18 // fixed: Boolean?
19 // Whether a fixed precision must be applied to data values for display. Default is true.
20 fixed: true,
21
22 // precision: Number?
23 // The precision at which to round data values for display. Default is 0.
24 precision: 1,
25
26 // labelOffset: Number?
27 // The amount in pixels by which to offset labels when using "outside" labelStyle. Default is 10.
28 labelOffset: 10,
29
30 // labelStyle: String?
31 // Options as to where to draw labels. This must be either "inside" or "outside". By default
32 // the labels are drawn "inside" the shape representing the data point (a column rectangle for a Columns plot
33 // or a marker for a Line plot for instance). When "outside" is used the labels are drawn above the data point shape.
34 labelStyle: "inside",
35
36 // htmlLabels: Boolean?
37 // Whether or not to use HTML to render slice labels. Default is true.
38 htmlLabels: true,
39
40 // omitLabels: Boolean?
41 // Whether labels that do not fit in an item render are omitted or not. This applies only when labelStyle
42 // is "inside". Default is false.
43 omitLabels: true,
44
45 // labelFunc: Function?
46 // An optional function to use to compute label text. It takes precedence over
47 // the default text when available.
48 // | function labelFunc(value, fixed, precision) {}
49 // `value` is the data value to display
50 // `fixed` is true if fixed precision must be applied.
51 // `precision` is the requested precision to be applied.
52 labelFunc: null
53 });
54 =====*/
55
56 var alwaysFalse = function(){ return false; }
57
58 return declare("dojox.charting.plot2d.CartesianBase", Base, {
59 baseParams: {
60 hAxis: "x",
61 vAxis: "y",
62 labels: false,
63 labelOffset: 10,
64 fixed: true,
65 precision: 1,
66 labelStyle: "inside",
67 htmlLabels: true, // use HTML to draw labels
68 omitLabels: true,
69 labelFunc: null
70 },
71
72 // summary:
73 // Base class for cartesian plot types.
74 constructor: function(chart, kwArgs){
75 // summary:
76 // Create a cartesian base plot for cartesian charts.
77 // chart: dojox/chart/Chart
78 // The chart this plot belongs to.
79 // kwArgs: dojox.charting.plot2d.__CartesianCtorArgs?
80 // An optional arguments object to help define the plot.
81 this.axes = ["hAxis", "vAxis"];
82 this.zoom = null;
83 this.zoomQueue = []; // zooming action task queue
84 this.lastWindow = {vscale: 1, hscale: 1, xoffset: 0, yoffset: 0};
85 this.hAxis = (kwArgs && kwArgs.hAxis) || "x";
86 this.vAxis = (kwArgs && kwArgs.vAxis) || "y";
87 this.series = [];
88 this.opt = lang.clone(this.baseParams);
89 du.updateWithObject(this.opt, kwArgs);
90 },
91 clear: function(){
92 // summary:
93 // Clear out all of the information tied to this plot.
94 // returns: dojox/charting/plot2d/CartesianBase
95 // A reference to this plot for functional chaining.
96 this.inherited(arguments);
97 this._hAxis = null;
98 this._vAxis = null;
99 return this; // dojox/charting/plot2d/CartesianBase
100 },
101 cleanGroup: function(creator, noClip){
102 this.inherited(arguments);
103 if(!noClip && this.chart._nativeClip){
104 var offsets = this.chart.offsets, dim = this.chart.dim;
105 var w = Math.max(0, dim.width - offsets.l - offsets.r),
106 h = Math.max(0, dim.height - offsets.t - offsets.b);
107 this.group.setClip({ x: offsets.l, y: offsets.t, width: w, height: h });
108 if(!this._clippedGroup){
109 this._clippedGroup = this.group.createGroup();
110 }
111 }
112 },
113 purgeGroup: function(){
114 this.inherited(arguments);
115 this._clippedGroup = null;
116 },
117 getGroup: function(){
118 return this._clippedGroup || this.group;
119 },
120 setAxis: function(axis){
121 // summary:
122 // Set an axis for this plot.
123 // axis: dojox/charting/axis2d/Base
124 // The axis to set.
125 // returns: dojox/charting/plot2d/CartesianBase
126 // A reference to this plot for functional chaining.
127 if(axis){
128 this[axis.vertical ? "_vAxis" : "_hAxis"] = axis;
129 }
130 return this; // dojox/charting/plot2d/CartesianBase
131 },
132 toPage: function(coord){
133 // summary:
134 // Compute page coordinates from plot axis data coordinates.
135 // coord: Object?
136 // The coordinates in plot axis data coordinate space. For cartesian charts that is of the following form:
137 // `{ hAxisName: 50, vAxisName: 200 }`
138 // If not provided return the transform method instead of the result of the transformation.
139 // returns: Object
140 // The resulting page pixel coordinates. That is of the following form:
141 // `{ x: 50, y: 200 }`
142 var ah = this._hAxis, av = this._vAxis,
143 sh = ah.getScaler(), sv = av.getScaler(),
144 th = sh.scaler.getTransformerFromModel(sh),
145 tv = sv.scaler.getTransformerFromModel(sv),
146 c = this.chart.getCoords(),
147 o = this.chart.offsets, dim = this.chart.dim;
148 var t = function(coord){
149 var r = {};
150 r.x = th(coord[ah.name]) + c.x + o.l;
151 r.y = c.y + dim.height - o.b - tv(coord[av.name]);
152 return r;
153 };
154 // if no coord return the function so that we can capture the current transforms
155 // and reuse them later on
156 return coord?t(coord):t; // Object
157 },
158 toData: function(coord){
159 // summary:
160 // Compute plot axis data coordinates from page coordinates.
161 // coord: Object
162 // The pixel coordinate in page coordinate space. That is of the following form:
163 // `{ x: 50, y: 200 }`
164 // If not provided return the tranform method instead of the result of the transformation.
165 // returns: Object
166 // The resulting plot axis data coordinates. For cartesian charts that is of the following form:
167 // `{ hAxisName: 50, vAxisName: 200 }`
168 var ah = this._hAxis, av = this._vAxis,
169 sh = ah.getScaler(), sv = av.getScaler(),
170 th = sh.scaler.getTransformerFromPlot(sh),
171 tv = sv.scaler.getTransformerFromPlot(sv),
172 c = this.chart.getCoords(),
173 o = this.chart.offsets, dim = this.chart.dim;
174 var t = function(coord){
175 var r = {};
176 r[ah.name] = th(coord.x - c.x - o.l);
177 r[av.name] = tv(c.y + dim.height - coord.y - o.b);
178 return r;
179 };
180 // if no coord return the function so that we can capture the current transforms
181 // and reuse them later on
182 return coord?t(coord):t; // Object
183 },
184 isDirty: function(){
185 // summary:
186 // Returns whether or not this plot needs to be rendered.
187 // returns: Boolean
188 // The state of the plot.
189 return this.dirty || this._hAxis && this._hAxis.dirty || this._vAxis && this._vAxis.dirty; // Boolean
190 },
191 createLabel: function(group, value, bbox, theme){
192 if(this.opt.labels){
193 var x, y, label = this.opt.labelFunc?this.opt.labelFunc.apply(this, [value, this.opt.fixed, this.opt.precision]):
194 this._getLabel(isNaN(value.y)?value:value.y);
195 if(this.opt.labelStyle == "inside"){
196 var lbox = gfx._base._getTextBox(label, { font: theme.series.font } );
197 x = bbox.x + bbox.width / 2;
198 y = bbox.y + bbox.height / 2 + lbox.h / 4;
199 if(lbox.w > bbox.width || lbox.h > bbox.height){
200 return;
201 }
202
203 }else{
204 x = bbox.x + bbox.width / 2;
205 y = bbox.y - this.opt.labelOffset;
206 }
207 this.renderLabel(group, x, y, label, theme, this.opt.labelStyle == "inside");
208 }
209 },
210 performZoom: function(dim, offsets){
211 // summary:
212 // Create/alter any zooming windows on this plot.
213 // dim: Object
214 // An object of the form { width, height }.
215 // offsets: Object
216 // An object of the form { l, r, t, b }.
217 // returns: dojox/charting/plot2d/CartesianBase
218 // A reference to this plot for functional chaining.
219
220 // get current zooming various
221 var vs = this._vAxis.scale || 1,
222 hs = this._hAxis.scale || 1,
223 vOffset = dim.height - offsets.b,
224 hBounds = this._hScaler.bounds,
225 xOffset = (hBounds.from - hBounds.lower) * hBounds.scale,
226 vBounds = this._vScaler.bounds,
227 yOffset = (vBounds.from - vBounds.lower) * vBounds.scale,
228 // get incremental zooming various
229 rVScale = vs / this.lastWindow.vscale,
230 rHScale = hs / this.lastWindow.hscale,
231 rXOffset = (this.lastWindow.xoffset - xOffset)/
232 ((this.lastWindow.hscale == 1)? hs : this.lastWindow.hscale),
233 rYOffset = (yOffset - this.lastWindow.yoffset)/
234 ((this.lastWindow.vscale == 1)? vs : this.lastWindow.vscale),
235
236 shape = this.getGroup(),
237 anim = fx.animateTransform(lang.delegate({
238 shape: shape,
239 duration: 1200,
240 transform:[
241 {name:"translate", start:[0, 0], end: [offsets.l * (1 - rHScale), vOffset * (1 - rVScale)]},
242 {name:"scale", start:[1, 1], end: [rHScale, rVScale]},
243 {name:"original"},
244 {name:"translate", start: [0, 0], end: [rXOffset, rYOffset]}
245 ]}, this.zoom));
246
247 lang.mixin(this.lastWindow, {vscale: vs, hscale: hs, xoffset: xOffset, yoffset: yOffset});
248 //add anim to zooming action queue,
249 //in order to avoid several zooming action happened at the same time
250 this.zoomQueue.push(anim);
251 //perform each anim one by one in zoomQueue
252 hub.connect(anim, "onEnd", this, function(){
253 this.zoom = null;
254 this.zoomQueue.shift();
255 if(this.zoomQueue.length > 0){
256 this.zoomQueue[0].play();
257 }
258 });
259 if(this.zoomQueue.length == 1){
260 this.zoomQueue[0].play();
261 }
262 return this; // dojox/charting/plot2d/CartesianBase
263 },
264 initializeScalers: function(dim, stats){
265 // summary:
266 // Initializes scalers using attached axes.
267 // dim: Object
268 // Size of a plot area in pixels as {width, height}.
269 // stats: Object
270 // Min/max of data in both directions as {hmin, hmax, vmin, vmax}.
271 // returns: dojox/charting/plot2d/CartesianBase
272 // A reference to this plot for functional chaining.
273 if(this._hAxis){
274 if(!this._hAxis.initialized()){
275 this._hAxis.calculate(stats.hmin, stats.hmax, dim.width);
276 }
277 this._hScaler = this._hAxis.getScaler();
278 }else{
279 this._hScaler = primitive.buildScaler(stats.hmin, stats.hmax, dim.width);
280 }
281 if(this._vAxis){
282 if(!this._vAxis.initialized()){
283 this._vAxis.calculate(stats.vmin, stats.vmax, dim.height);
284 }
285 this._vScaler = this._vAxis.getScaler();
286 }else{
287 this._vScaler = primitive.buildScaler(stats.vmin, stats.vmax, dim.height);
288 }
289 return this; // dojox/charting/plot2d/CartesianBase
290 },
291 isNullValue: function(value){
292 if(value === null || typeof value == "undefined"){
293 return true;
294 }
295 var h = this._hAxis ? this._hAxis.isNullValue : alwaysFalse,
296 v = this._vAxis ? this._vAxis.isNullValue : alwaysFalse;
297 if(typeof value == "number"){
298 return h(1) || v(value);
299 }
300 return h(isNaN(value.x) ? 1 : value.x) || value.y === null || v(value.y);
301 }
302 });
303 });