Debian Codegraph @

dojox/charting/Element.js

raw symbols SCIP
1 define(["dojo/_base/array", "dojo/dom-construct","dojo/_base/declare", "dojox/gfx", "dojox/gfx/shape"],
2 function(arr, domConstruct, declare, gfx, shape){
3
4 return declare("dojox.charting.Element", null, {
5 // summary:
6 // A base class that is used to build other elements of a chart, such as
7 // a series.
8 // chart: dojox/charting/Chart
9 // The parent chart for this element.
10 // group: dojox/gfx/shape.Group
11 // The visual GFX group representing this element.
12 // htmlElement: Array
13 // Any DOMNodes used as a part of this element (such as HTML-based labels).
14 // dirty: Boolean
15 // A flag indicating whether or not this element needs to be rendered.
16
17 chart: null,
18 group: null,
19 htmlElements: null,
20 dirty: true,
21 renderingOptions: null,
22
23 constructor: function(chart, kwArgs){
24 // summary:
25 // Creates a new charting element.
26 // chart: dojox/charting/Chart
27 // The chart that this element belongs to.
28 this.chart = chart;
29 this.group = null;
30 this.htmlElements = [];
31 this.dirty = true;
32 this.trailingSymbol = "...";
33 this._events = [];
34 if (kwArgs && kwArgs.renderingOptions) {
35 this.renderingOptions = kwArgs.renderingOptions;
36 }
37 },
38 purgeGroup: function(){
39 // summary:
40 // Clear any elements out of our group, and destroy the group.
41 // returns: dojox/charting/Element
42 // A reference to this object for functional chaining.
43 this.destroyHtmlElements();
44 if(this.group){
45 // since 1.7.x we need dispose shape otherwise there is a memoryleak
46 this.getGroup().removeShape();
47 var children = this.getGroup().children;
48 // starting with 1.9 the registry is optional and thus dispose is
49 if(shape.dispose){
50 for(var i = 0; i < children.length;++i){
51 shape.dispose(children[i], true);
52 }
53 }
54 if(this.getGroup().rawNode){
55 domConstruct.empty(this.getGroup().rawNode);
56 }
57 this.getGroup().clear();
58 // starting with 1.9 the registry is optional and thus dispose is
59 if(shape.dispose){
60 shape.dispose(this.getGroup(), true);
61 }
62 if(this.getGroup() != this.group){
63 // we do have an intermediary clipping group (see CartesianBase)
64 if(this.group.rawNode){
65 domConstruct.empty(this.group.rawNode);
66 }
67 this.group.clear();
68 // starting with 1.9 the registry is optional and thus dispose is
69 if(shape.dispose){
70 shape.dispose(this.group, true);
71 }
72 }
73 this.group = null;
74 }
75 this.dirty = true;
76 if(this._events.length){
77 arr.forEach(this._events, function(item){
78 item.shape.disconnect(item.handle);
79 });
80 this._events = [];
81 }
82 return this; // dojox.charting.Element
83 },
84 cleanGroup: function(creator){
85 // summary:
86 // Clean any elements (HTML or GFX-based) out of our group, and create a new one.
87 // creator: dojox/gfx/shape.Surface?
88 // An optional surface to work with.
89 // returns: dojox/charting/Element
90 // A reference to this object for functional chaining.
91 this.destroyHtmlElements();
92 if(!creator){ creator = this.chart.surface; }
93 if(this.group){
94 var bgnode;
95 var children = this.getGroup().children;
96 // starting with 1.9 the registry is optional and thus dispose is
97 if(shape.dispose){
98 for(var i = 0; i < children.length;++i){
99 shape.dispose(children[i], true);
100 }
101 }
102 if(this.getGroup().rawNode){
103 bgnode = this.getGroup().bgNode;
104 domConstruct.empty(this.getGroup().rawNode);
105 }
106 this.getGroup().clear();
107 if(bgnode){
108 this.getGroup().rawNode.appendChild(bgnode);
109 }
110 }else{
111 this.group = creator.createGroup();
112 // in some cases we have a rawNode but this is not an actual DOM element (CanvasWithEvents) so check
113 // the actual rawNode type.
114 if (this.renderingOptions && this.group.rawNode &&
115 this.group.rawNode.namespaceURI == "http://www.w3.org/2000/svg") {
116 for (var key in this.renderingOptions) {
117 this.group.rawNode.setAttribute(key, this.renderingOptions[key]);
118 }
119 }
120 }
121 this.dirty = true;
122 return this; // dojox.charting.Element
123 },
124 getGroup: function(){
125 return this.group;
126 },
127 destroyHtmlElements: function(){
128 // summary:
129 // Destroy any DOMNodes that may have been created as a part of this element.
130 if(this.htmlElements.length){
131 arr.forEach(this.htmlElements, domConstruct.destroy);
132 this.htmlElements = [];
133 }
134 },
135 destroy: function(){
136 // summary:
137 // API addition to conform to the rest of the Dojo Toolkit's standard.
138 this.purgeGroup();
139 },
140 overrideShape: function(shape, params){
141 // summary:
142 // Extension point for overriding the charting shape
143 },
144 //text utilities
145 getTextWidth: function(s, font){
146 return gfx._base._getTextBox(s, {font: font}).w || 0;
147 },
148 getTextWithLimitLength: function(s, font, limitWidth, truncated){
149 // summary:
150 // Get the truncated string based on the limited width in px(dichotomy algorithm)
151 // s: String?
152 // candidate text.
153 // font: String?
154 // text's font style.
155 // limitWidth: Number?
156 // text limited width in px.
157 // truncated: Boolean?
158 // whether the input text(s) has already been truncated.
159 // returns: Object
160 // | {
161 // | text: processed text, maybe truncated or not,
162 // | truncated: whether text has been truncated
163 // | }
164 if(!s || s.length <= 0){
165 return {
166 text: "",
167 truncated: truncated || false
168 };
169 }
170 if(!limitWidth || limitWidth <= 0){
171 return {
172 text: s,
173 truncated: truncated || false
174 };
175 }
176 var delta = 2,
177 //golden section for dichotomy algorithm
178 trucPercentage = 0.618,
179 minStr = s.substring(0,1) + this.trailingSymbol,
180 minWidth = this.getTextWidth(minStr, font);
181 if(limitWidth <= minWidth){
182 return {
183 text: minStr,
184 truncated: true
185 };
186 }
187 var width = this.getTextWidth(s, font);
188 if(width <= limitWidth){
189 return {
190 text: s,
191 truncated: truncated || false
192 };
193 }else{
194 var begin = 0,
195 end = s.length;
196 while(begin < end){
197 if(end - begin <= delta ){
198 while (this.getTextWidth(s.substring(0, begin) + this.trailingSymbol, font) > limitWidth) {
199 begin -= 1;
200 }
201 return {
202 text: (s.substring(0,begin) + this.trailingSymbol),
203 truncated: true
204 };
205 }
206 var index = begin + Math.round((end - begin) * trucPercentage),
207 widthIntercepted = this.getTextWidth(s.substring(0, index), font);
208 if(widthIntercepted < limitWidth){
209 begin = index;
210 end = end;
211 }else{
212 begin = begin;
213 end = index;
214 }
215 }
216 }
217 },
218 getTextWithLimitCharCount: function(s, font, wcLimit, truncated){
219 // summary:
220 // Get the truncated string based on the limited character count(dichotomy algorithm)
221 // s: String?
222 // candidate text.
223 // font: String?
224 // text's font style.
225 // wcLimit: Number?
226 // text limited character count.
227 // truncated: Boolean?
228 // whether the input text(s) has already been truncated.
229 // returns: Object
230 // | {
231 // | text: processed text, maybe truncated or not,
232 // | truncated: whether text has been truncated
233 // | }
234 if (!s || s.length <= 0) {
235 return {
236 text: "",
237 truncated: truncated || false
238 };
239 }
240 if(!wcLimit || wcLimit <= 0 || s.length <= wcLimit){
241 return {
242 text: s,
243 truncated: truncated || false
244 };
245 }
246 return {
247 text: s.substring(0, wcLimit) + this.trailingSymbol,
248 truncated: true
249 };
250 },
251 // fill utilities
252 _plotFill: function(fill, dim, offsets){
253 // process a plot-wide fill
254 if(!fill || !fill.type || !fill.space){
255 return fill;
256 }
257 var space = fill.space, span;
258 switch(fill.type){
259 case "linear":
260 if(space === "plot" || space === "shapeX" || space === "shapeY"){
261 // clone a fill so we can modify properly directly
262 fill = gfx.makeParameters(gfx.defaultLinearGradient, fill);
263 fill.space = space;
264 // process dimensions
265 if(space === "plot" || space === "shapeX"){
266 // process Y
267 span = dim.height - offsets.t - offsets.b;
268 fill.y1 = offsets.t + span * fill.y1 / 100;
269 fill.y2 = offsets.t + span * fill.y2 / 100;
270 }
271 if(space === "plot" || space === "shapeY"){
272 // process X
273 span = dim.width - offsets.l - offsets.r;
274 fill.x1 = offsets.l + span * fill.x1 / 100;
275 fill.x2 = offsets.l + span * fill.x2 / 100;
276 }
277 }
278 break;
279 case "radial":
280 if(space === "plot"){
281 // this one is used exclusively for scatter charts
282 // clone a fill so we can modify properly directly
283 fill = gfx.makeParameters(gfx.defaultRadialGradient, fill);
284 fill.space = space;
285 // process both dimensions
286 var spanX = dim.width - offsets.l - offsets.r,
287 spanY = dim.height - offsets.t - offsets.b;
288 fill.cx = offsets.l + spanX * fill.cx / 100;
289 fill.cy = offsets.t + spanY * fill.cy / 100;
290 fill.r = fill.r * Math.sqrt(spanX * spanX + spanY * spanY) / 200;
291 }
292 break;
293 case "pattern":
294 if(space === "plot" || space === "shapeX" || space === "shapeY"){
295 // clone a fill so we can modify properly directly
296 fill = gfx.makeParameters(gfx.defaultPattern, fill);
297 fill.space = space;
298 // process dimensions
299 if(space === "plot" || space === "shapeX"){
300 // process Y
301 span = dim.height - offsets.t - offsets.b;
302 fill.y = offsets.t + span * fill.y / 100;
303 fill.height = span * fill.height / 100;
304 }
305 if(space === "plot" || space === "shapeY"){
306 // process X
307 span = dim.width - offsets.l - offsets.r;
308 fill.x = offsets.l + span * fill.x / 100;
309 fill.width = span * fill.width / 100;
310 }
311 }
312 break;
313 }
314 return fill;
315 },
316 _shapeFill: function(fill, bbox){
317 // process shape-specific fill
318 if(!fill || !fill.space){
319 return fill;
320 }
321 var space = fill.space, span;
322 switch(fill.type){
323 case "linear":
324 if(space === "shape" || space === "shapeX" || space === "shapeY"){
325 // clone a fill so we can modify properly directly
326 fill = gfx.makeParameters(gfx.defaultLinearGradient, fill);
327 fill.space = space;
328 // process dimensions
329 if(space === "shape" || space === "shapeX"){
330 // process X
331 span = bbox.width;
332 fill.x1 = bbox.x + span * fill.x1 / 100;
333 fill.x2 = bbox.x + span * fill.x2 / 100;
334 }
335 if(space === "shape" || space === "shapeY"){
336 // process Y
337 span = bbox.height;
338 fill.y1 = bbox.y + span * fill.y1 / 100;
339 fill.y2 = bbox.y + span * fill.y2 / 100;
340 }
341 }
342 break;
343 case "radial":
344 if(space === "shape"){
345 // this one is used exclusively for bubble charts and pie charts
346 // clone a fill so we can modify properly directly
347 fill = gfx.makeParameters(gfx.defaultRadialGradient, fill);
348 fill.space = space;
349 // process both dimensions
350 fill.cx = bbox.x + bbox.width / 2;
351 fill.cy = bbox.y + bbox.height / 2;
352 fill.r = fill.r * bbox.width / 200;
353 }
354 break;
355 case "pattern":
356 if(space === "shape" || space === "shapeX" || space === "shapeY"){
357 // clone a fill so we can modify properly directly
358 fill = gfx.makeParameters(gfx.defaultPattern, fill);
359 fill.space = space;
360 // process dimensions
361 if(space === "shape" || space === "shapeX"){
362 // process X
363 span = bbox.width;
364 fill.x = bbox.x + span * fill.x / 100;
365 fill.width = span * fill.width / 100;
366 }
367 if(space === "shape" || space === "shapeY"){
368 // process Y
369 span = bbox.height;
370 fill.y = bbox.y + span * fill.y / 100;
371 fill.height = span * fill.height / 100;
372 }
373 }
374 break;
375 }
376 return fill;
377 },
378 _pseudoRadialFill: function(fill, center, radius, start, end){
379 // process pseudo-radial fills
380 if(!fill || fill.type !== "radial" || fill.space !== "shape"){
381 return fill;
382 }
383 // clone and normalize fill
384 var space = fill.space;
385 fill = gfx.makeParameters(gfx.defaultRadialGradient, fill);
386 fill.space = space;
387 if(arguments.length < 4){
388 // process both dimensions
389 fill.cx = center.x;
390 fill.cy = center.y;
391 fill.r = fill.r * radius / 100;
392 return fill;
393 }
394 // convert to a linear gradient
395 var angle = arguments.length < 5 ? start : (end + start) / 2;
396 return {
397 type: "linear",
398 x1: center.x,
399 y1: center.y,
400 x2: center.x + fill.r * radius * Math.cos(angle) / 100,
401 y2: center.y + fill.r * radius * Math.sin(angle) / 100,
402 colors: fill.colors
403 };
404 }
405 });
406 });