Debian Codegraph @

dojox/charting/axis2d/Default.js

raw symbols SCIP
1 define(["dojo/_base/lang", "dojo/_base/array", "dojo/sniff", "dojo/_base/declare",
2 "dojo/_base/connect", "dojo/dom-geometry", "./Invisible",
3 "../scaler/linear", "./common", "dojox/gfx", "dojox/lang/utils", "dojox/lang/functional",
4 "dojo/has!dojo-bidi?../bidi/axis2d/Default"],
5 function(lang, arr, has, declare, connect, domGeom, Invisible,
6 lin, acommon, g, du, df, BidiDefault){
7
8 /*=====
9 var __AxisCtorArgs = {
10 // summary:
11 // Optional arguments used in the definition of an axis.
12 // vertical: Boolean?
13 // A flag that says whether an axis is vertical (i.e. y axis) or horizontal. Default is false (horizontal).
14 // fixUpper: String?
15 // Align the greatest value on the axis with the specified tick level. Options are "major", "minor", "micro", or "none". Defaults to "none".
16 // fixLower: String?
17 // Align the smallest value on the axis with the specified tick level. Options are "major", "minor", "micro", or "none". Defaults to "none".
18 // natural: Boolean?
19 // Ensure tick marks are made on "natural" numbers. Defaults to false.
20 // leftBottom: Boolean?
21 // Deprecated: use position instead. The position of a vertical axis; if true, will be placed against the left-bottom corner of the chart. Defaults to true.
22 // includeZero: Boolean?
23 // Include 0 on the axis rendering. Default is false.
24 // fixed: Boolean?
25 // Force all axis labels to be fixed numbers. Default is true.
26 // majorLabels: Boolean?
27 // Flag to draw labels at major ticks. Default is true.
28 // minorTicks: Boolean?
29 // Flag to draw minor ticks on an axis. Default is true.
30 // minorLabels: Boolean?
31 // Flag to labels on minor ticks when there is enough space. Default is true.
32 // microTicks: Boolean?
33 // Flag to draw micro ticks on an axis. Default is false.
34 // htmlLabels: Boolean?
35 // Flag to use HTML (as opposed to the native vector graphics engine) to draw labels. Default is true.
36 // min: Number?
37 // The smallest value on an axis. Default is 0.
38 // max: Number?
39 // The largest value on an axis. Default is 1.
40 // from: Number?
41 // Force the chart to render data visible from this value. Default is 0.
42 // to: Number?
43 // Force the chart to render data visible to this value. Default is 1.
44 // majorTickStep: Number?
45 // The amount to skip before a major tick is drawn. When not set the major ticks step is computed from
46 // the data range.
47 // minorTickStep: Number?
48 // The amount to skip before a minor tick is drawn. When not set the minor ticks step is computed from
49 // the data range.
50 // microTickStep: Number?
51 // The amount to skip before a micro tick is drawn. When not set the micro ticks step is computed from
52 // labels: Object[]?
53 // An array of labels for major ticks, with corresponding numeric values, ordered by value.
54 // labelFunc: Function?
55 // An optional function to use to compute label text. It takes precedence over
56 // the default text when available. The function must be of the following form:
57 // | function labelFunc(text, value, precision) {}
58 // `text` is the already pre-formatted text. Pre-formatting is done using `dojo/number` is available, `Date.toFixed` otherwise.
59 // `value` is the raw axis value.
60 // `precision` is the requested precision to be applied.
61 // maxLabelSize: Number?
62 // The maximum size, in pixels, for a label. To be used with the optional label function.
63 // stroke: dojox.gfx.Stroke?
64 // An optional stroke to be used for drawing an axis.
65 // majorTick: Object?
66 // An object containing a dojox.gfx.Stroke, and a length (number) for a major tick.
67 // minorTick: Object?
68 // An object containing a dojox.gfx.Stroke, and a length (number) for a minor tick.
69 // microTick: Object?
70 // An object containing a dojox.gfx.Stroke, and a length (number) for a micro tick.
71 // tick: Object?
72 // An object containing a dojox.gfx.Stroke, and a length (number) for a tick.
73 // font: String?
74 // An optional font definition (as used in the CSS font property) for labels.
75 // fontColor: String|dojo.Color?
76 // An optional color to be used in drawing labels.
77 // titleGap: Number?
78 // An optional grap between axis title and axis label
79 // titleFont: String?
80 // An optional font definition for axis title
81 // titleFontColor: String?
82 // An optional axis title color
83 // titleOrientation: String?
84 // An optional orientation for axis title. "axis" means the title facing the axis, "away" means facing away.
85 // If no value is set "axis" is used.
86 // enableCache: Boolean?
87 // Whether the ticks and labels are cached from one rendering to another. This improves the rendering performance of
88 // successive rendering but penalize the first rendering. For labels it is only working with gfx labels
89 // not html ones. Default false.
90 // dropLabels: Boolean?
91 // Whether the axis automatically drops labels at regular interval or not to avoid labels overlapping.
92 // This gives better results but require more computations. You can disable it to save computation
93 // time when you know your labels won't overlap. Default is true.
94 // labelSizeChange: Boolean?
95 // Indicates to the axis whether the axis labels are changing their size on zoom. If false this allows to
96 // optimize the axis by avoiding recomputing labels maximum size on zoom actions. Default is false.
97 // position: String?
98 // The position of the axis. Values: "leftOrBottom", "center" or "rightOrTop". Default is "leftOrBottom".
99 };
100 =====*/
101
102 var centerAnchorLimit = 45; // in degrees
103
104 var Default = declare(has("dojo-bidi")? "dojox.charting.axis2d.NonBidiDefault" : "dojox.charting.axis2d.Default", Invisible, {
105 // summary:
106 // The default axis object used in dojox.charting. See dojox.charting.Chart.addAxis for details.
107
108 // defaultParams: Object
109 // The default parameters used to define any axis.
110 // optionalParams: Object
111 // Any optional parameters needed to define an axis.
112
113 /*=====
114 // TODO: the documentation tools need these to be pre-defined in order to pick them up
115 // correctly, but the code here is partially predicated on whether or not the properties
116 // actually exist. For now, we will leave these undocumented but in the code for later. -- TRT
117
118 // opt: Object
119 // The actual options used to define this axis, created at initialization.
120 // scaler: Object
121 // The calculated helper object to tell charts how to draw an axis and any data.
122 // ticks: Object
123 // The calculated tick object that helps a chart draw the scaling on an axis.
124 // dirty: Boolean
125 // The state of the axis (whether it needs to be redrawn or not)
126 // scale: Number
127 // The current scale of the axis.
128 // offset: Number
129 // The current offset of the axis.
130
131 opt: null,
132 scaler: null,
133 ticks: null,
134 dirty: true,
135 scale: 1,
136 offset: 0,
137 =====*/
138 defaultParams: {
139 vertical: false, // true for vertical axis
140 fixUpper: "none", // align the upper on ticks: "major", "minor", "micro", "none"
141 fixLower: "none", // align the lower on ticks: "major", "minor", "micro", "none"
142 natural: false, // all tick marks should be made on natural numbers
143 leftBottom: true, // position of the axis, used with "vertical" - deprecated: use position instead
144 includeZero: false, // 0 should be included
145 fixed: true, // all labels are fixed numbers
146 majorLabels: true, // draw major labels
147 minorTicks: true, // draw minor ticks
148 minorLabels: true, // draw minor labels
149 microTicks: false, // draw micro ticks
150 rotation: 0, // label rotation angle in degrees
151 htmlLabels: true, // use HTML to draw labels
152 enableCache: false, // whether we cache or not
153 dropLabels: true, // whether we automatically drop overlapping labels or not
154 labelSizeChange: false, // whether the labels size change on zoom
155 position: "leftOrBottom" // position of the axis: "leftOrBottom" (default), "center" or "rightOrTop"
156 },
157 optionalParams: {
158 min: 0, // minimal value on this axis
159 max: 1, // maximal value on this axis
160 from: 0, // visible from this value
161 to: 1, // visible to this value
162 majorTickStep: 4, // major tick step
163 minorTickStep: 2, // minor tick step
164 microTickStep: 1, // micro tick step
165 labels: [], // array of labels for major ticks
166 // with corresponding numeric values
167 // ordered by values
168 labelFunc: null, // function to compute label values
169 maxLabelSize: 0, // size in px. For use with labelFunc
170 maxLabelCharCount: 0, // size in word count.
171 trailingSymbol: null,
172
173 // TODO: add support for minRange!
174 // minRange: 1, // smallest distance from min allowed on the axis
175
176 // theme components
177 stroke: {}, // stroke for an axis
178 majorTick: {}, // stroke + length for a tick
179 minorTick: {}, // stroke + length for a tick
180 microTick: {}, // stroke + length for a tick
181 tick: {}, // stroke + length for a tick
182 font: "", // font for labels
183 fontColor: "", // color for labels as a string
184 title: "", // axis title
185 titleGap: 0, // gap between axis title and axis label
186 titleFont: "", // axis title font
187 titleFontColor: "", // axis title font color
188 titleOrientation: "" // "axis" means the title facing the axis, "away" means facing away
189 },
190
191 constructor: function(chart, kwArgs){
192 // summary:
193 // The constructor for an axis.
194 // chart: dojox/charting/Chart
195 // The chart the axis belongs to.
196 // kwArgs: __AxisCtorArgs?
197 // Any optional keyword arguments to be used to define this axis.
198 this.opt = lang.clone(this.defaultParams);
199 du.updateWithObject(this.opt, kwArgs);
200 du.updateWithPattern(this.opt, kwArgs, this.optionalParams);
201 if(this.opt.enableCache){
202 this._textFreePool = [];
203 this._lineFreePool = [];
204 this._textUsePool = [];
205 this._lineUsePool = [];
206 }
207 this._invalidMaxLabelSize = true;
208 // replace deprecated leftBotton to position
209 if(!(kwArgs && ('position' in kwArgs))){
210 this.opt.position = this.opt.leftBottom ? "leftOrBottom" : "rightOrTop";
211 }
212 this.renderingOptions = { "shape-rendering": "crispEdges" };
213 },
214 setWindow: function(scale, offset){
215 // summary:
216 // Set the drawing "window" for the axis.
217 // scale: Number
218 // The new scale for the axis.
219 // offset: Number
220 // The new offset for the axis.
221 // returns: dojox/charting/axis2d/Default
222 // The reference to the axis for functional chaining.
223 if(scale != this.scale){
224 // if scale changed we need to recompute new max label size
225 this._invalidMaxLabelSize = true;
226 }
227 return this.inherited(arguments);
228 },
229
230 _groupLabelWidth: function(labels, font, wcLimit){
231 if(!labels.length){
232 return 0;
233 }
234 if(labels.length > 50){
235 // let's avoid degenerated cases
236 labels.length = 50;
237 }
238 if(lang.isObject(labels[0])){
239 labels = df.map(labels, function(label){ return label.text; });
240 }
241 if(wcLimit){
242 labels = df.map(labels, function(label){
243 return lang.trim(label).length == 0 ? "" : label.substring(0, wcLimit) + this.trailingSymbol;
244 }, this);
245 }
246 var s = labels.join("<br>");
247 return g._base._getTextBox(s, {font: font}).w || 0;
248 },
249
250 _getMaxLabelSize: function(min, max, span, rotation, font, size){
251 if(this._maxLabelSize == null && arguments.length == 6){
252 var o = this.opt;
253 // everything might have changed, reset the minMinorStep value
254 this.scaler.minMinorStep = this._prevMinMinorStep = 0;
255 var ob = lang.clone(o);
256 delete ob.to;
257 delete ob.from;
258 // build all the ticks from min, to max not from to to _but_ using the step
259 // that would be used if we where just displaying from to to from.
260 var sb = lin.buildScaler(min, max, span, ob, o.to - o.from);
261 sb.minMinorStep = 0;
262 this._majorStart = sb.major.start;
263 // we build all the ticks not only the ones we need to draw in order to get
264 // a correct drop rate computation that works for any offset of this scale
265 var tb = lin.buildTicks(sb, o);
266 // if there is not tick at all tb is null
267 if(size && tb){
268 var majLabelW = 0, minLabelW = 0; // non rotated versions
269 // we first collect all labels when needed
270 var tickLabelFunc = function(tick){
271 if(tick.label){
272 this.push(tick.label);
273 }
274 };
275 var labels = [];
276 if(this.opt.majorLabels){
277 arr.forEach(tb.major, tickLabelFunc, labels);
278 majLabelW = this._groupLabelWidth(labels, font, ob.maxLabelCharCount);
279 if(ob.maxLabelSize){
280 majLabelW = Math.min(ob.maxLabelSize, majLabelW);
281 }
282 }
283 // do the minor labels computation only if dropLabels is set
284 labels = [];
285 if(this.opt.dropLabels && this.opt.minorLabels){
286 arr.forEach(tb.minor, tickLabelFunc, labels);
287 minLabelW = this._groupLabelWidth(labels, font, ob.maxLabelCharCount);
288 if(ob.maxLabelSize){
289 minLabelW = Math.min(ob.maxLabelSize, minLabelW);
290 }
291 }
292 this._maxLabelSize = {
293 majLabelW: majLabelW, minLabelW: minLabelW,
294 majLabelH: size, minLabelH: size
295 };
296 }else{
297 this._maxLabelSize = null;
298 }
299 }
300 return this._maxLabelSize;
301 },
302
303 calculate: function(min, max, span){
304 this.inherited(arguments);
305 // when the scale has not changed there is no reason for minMinorStep to change
306 this.scaler.minMinorStep = this._prevMinMinorStep;
307 // we want to recompute the dropping mechanism only when the scale or the size of the axis is changing
308 // not when for example when we scroll (otherwise effect would be weird)
309 if((this._invalidMaxLabelSize || span != this._oldSpan) && (min != Infinity && max != -Infinity)){
310 this._invalidMaxLabelSize = false;
311 if(this.opt.labelSizeChange){
312 this._maxLabelSize = null;
313 }
314 this._oldSpan = span;
315 var o = this.opt;
316 var ta = this.chart.theme.axis, rotation = o.rotation % 360,
317 labelGap = this.chart.theme.axis.tick.labelGap,
318 // TODO: we use one font --- of major tick, we need to use major and minor fonts
319 font = o.font || (ta.majorTick && ta.majorTick.font) || (ta.tick && ta.tick.font),
320 size = font ? g.normalizedLength(g.splitFontString(font).size) : 0,
321 // even if we don't drop label we need to compute max size for offsets
322 labelW = this._getMaxLabelSize(min, max, span, rotation, font, size);
323 if(typeof labelGap != "number"){
324 labelGap = 4; // in pixels
325 }
326 if(labelW && o.dropLabels){
327 var cosr = Math.abs(Math.cos(rotation * Math.PI / 180)),
328 sinr = Math.abs(Math.sin(rotation * Math.PI / 180));
329 var majLabelW, minLabelW;
330 if(rotation < 0){
331 rotation += 360;
332 }
333 switch(rotation){
334 case 0:
335 case 180:
336 // trivial cases: horizontal labels
337 if(this.vertical){
338 majLabelW = minLabelW = size;
339 }else{
340 majLabelW = labelW.majLabelW;
341 minLabelW = labelW.minLabelW;
342 }
343 break;
344 case 90:
345 case 270:
346 // trivial cases: vertical
347 if(this.vertical){
348 majLabelW = labelW.majLabelW;
349 minLabelW = labelW.minLabelW;
350 }else{
351 majLabelW = minLabelW = size;
352 }
353 break;
354 default:
355 // all major labels are parallel they can't collapse except if the two ticks are
356 // closer than the height of the text * cos(90-rotation)
357 majLabelW = this.vertical ? Math.min(labelW.majLabelW, size / cosr) : Math.min(labelW.majLabelW, size / sinr);
358 // for minor labels we need to rotated them
359 var gap1 = Math.sqrt(labelW.minLabelW * labelW.minLabelW + size * size),
360 gap2 = this.vertical ? size * cosr + labelW.minLabelW * sinr : labelW.minLabelW * cosr + size * sinr;
361 minLabelW = Math.min(gap1, gap2);
362 break;
363 }
364 // we need to check both minor and major labels fit a minor step
365 this.scaler.minMinorStep = this._prevMinMinorStep = Math.max(majLabelW, minLabelW) + labelGap;
366 var canMinorLabel = this.scaler.minMinorStep <= this.scaler.minor.tick * this.scaler.bounds.scale;
367 if(!canMinorLabel){
368 // we can't place minor labels, let's see if we can place major ones
369 // in a major step and if not which skip interval we must follow
370 this._skipInterval = Math.floor((majLabelW + labelGap) / (this.scaler.major.tick * this.scaler.bounds.scale));
371 }else{
372 // everything fit well
373 this._skipInterval = 0;
374 }
375 }else{
376 // drop label disabled
377 this._skipInterval = 0;
378 }
379 }
380 // computes the tick subset we need for that scale/offset
381 this.ticks = lin.buildTicks(this.scaler, this.opt);
382 return this;
383 },
384
385 getOffsets: function(){
386 // summary:
387 // Get the physical offset values for this axis (used in drawing data series). This method is not
388 // supposed to be called by the users but internally.
389 // returns: Object
390 // The calculated offsets in the form of { l, r, t, b } (left, right, top, bottom).
391 var s = this.scaler, offsets = { l: 0, r: 0, t: 0, b: 0 };
392 if(!s){
393 return offsets;
394 }
395 var o = this.opt,
396 ta = this.chart.theme.axis,
397 labelGap = this.chart.theme.axis.tick.labelGap,
398 // TODO: we use one font --- of major tick, we need to use major and minor fonts
399 taTitleFont = o.titleFont || (ta.title && ta.title.font),
400 taTitleGap = (o.titleGap==0) ? 0 : o.titleGap || (ta.title && ta.title.gap),
401 taMajorTick = this.chart.theme.getTick("major", o),
402 taMinorTick = this.chart.theme.getTick("minor", o),
403 tsize = taTitleFont ? g.normalizedLength(g.splitFontString(taTitleFont).size) : 0,
404 rotation = o.rotation % 360, position = o.position,
405 leftBottom = position !== "rightOrTop",
406 cosr = Math.abs(Math.cos(rotation * Math.PI / 180)),
407 sinr = Math.abs(Math.sin(rotation * Math.PI / 180));
408 this.trailingSymbol = (o.trailingSymbol === undefined || o.trailingSymbol === null) ?
409 this.trailingSymbol : o.trailingSymbol;
410 if(typeof labelGap != "number"){
411 labelGap = 4; // in pixels
412 }
413 if(rotation < 0){
414 rotation += 360;
415 }
416 var maxLabelSize = this._getMaxLabelSize(); // don't need parameters, calculate has been called before => we use cached value
417 if(maxLabelSize){
418 var side;
419 var labelWidth = Math.ceil(Math.max(maxLabelSize.majLabelW, maxLabelSize.minLabelW)) + 1,
420 size = Math.ceil(Math.max(maxLabelSize.majLabelH, maxLabelSize.minLabelH)) + 1;
421 if(this.vertical){
422 side = leftBottom ? "l" : "r";
423 switch(rotation){
424 case 0:
425 case 180:
426 offsets[side] = position === "center" ? 0 : labelWidth;
427 offsets.t = offsets.b = size / 2;
428 break;
429 case 90:
430 case 270:
431 offsets[side] = size;
432 offsets.t = offsets.b = labelWidth / 2;
433 break;
434 default:
435 if(rotation <= centerAnchorLimit || (180 < rotation && rotation <= (180 + centerAnchorLimit))){
436 offsets[side] = size * sinr / 2 + labelWidth * cosr;
437 offsets[leftBottom ? "t" : "b"] = size * cosr / 2 + labelWidth * sinr;
438 offsets[leftBottom ? "b" : "t"] = size * cosr / 2;
439 }else if(rotation > (360 - centerAnchorLimit) || (180 > rotation && rotation > (180 - centerAnchorLimit))){
440 offsets[side] = size * sinr / 2 + labelWidth * cosr;
441 offsets[leftBottom ? "b" : "t"] = size * cosr / 2 + labelWidth * sinr;
442 offsets[leftBottom ? "t" : "b"] = size * cosr / 2;
443 }else if(rotation < 90 || (180 < rotation && rotation < 270)){
444 offsets[side] = size * sinr + labelWidth * cosr;
445 offsets[leftBottom ? "t" : "b"] = size * cosr + labelWidth * sinr;
446 }else{
447 offsets[side] = size * sinr + labelWidth * cosr;
448 offsets[leftBottom ? "b" : "t"] = size * cosr + labelWidth * sinr;
449 }
450 break;
451 }
452 if(position === "center"){
453 offsets[side] = 0;
454 }
455 else{
456 offsets[side] += labelGap + Math.max(taMajorTick.length > 0?taMajorTick.length:0,
457 taMinorTick.length > 0?taMinorTick.length:0) + (o.title ? (tsize + taTitleGap) : 0);
458 }
459 }else{
460 side = leftBottom ? "b" : "t";
461 switch(rotation){
462 case 0:
463 case 180:
464 offsets[side] = position === "center" ? 0 : size;
465 offsets.l = offsets.r = labelWidth / 2;
466 break;
467 case 90:
468 case 270:
469 offsets[side] = labelWidth;
470 offsets.l = offsets.r = size / 2;
471 break;
472 default:
473 if((90 - centerAnchorLimit) <= rotation && rotation <= 90 || (270 - centerAnchorLimit) <= rotation && rotation <= 270){
474 offsets[side] = size * cosr / 2 + labelWidth * sinr;
475 offsets[leftBottom ? "r" : "l"] = size * sinr / 2 + labelWidth * cosr;
476 offsets[leftBottom ? "l" : "r"] = size * sinr / 2;
477 }else if(90 <= rotation && rotation <= (90 + centerAnchorLimit) || 270 <= rotation && rotation <= (270 + centerAnchorLimit)){
478 offsets[side] = size * cosr / 2 + labelWidth * sinr;
479 offsets[leftBottom ? "l" : "r"] = size * sinr / 2 + labelWidth * cosr;
480 offsets[leftBottom ? "r" : "l"] = size * sinr / 2;
481 }else if(rotation < centerAnchorLimit || (180 < rotation && rotation < (180 + centerAnchorLimit))){
482 offsets[side] = size * cosr + labelWidth * sinr;
483 offsets[leftBottom ? "r" : "l"] = size * sinr + labelWidth * cosr;
484 }else{
485 offsets[side] = size * cosr + labelWidth * sinr;
486 offsets[leftBottom ? "l" : "r"] = size * sinr + labelWidth * cosr;
487 }
488 break;
489 }
490 if(position === "center"){
491 offsets[side] = 0;
492 }
493 else{
494 offsets[side] += labelGap + Math.max(taMajorTick.length > 0?taMajorTick.length:0,
495 taMinorTick.length > 0?taMinorTick.length:0) + (o.title ? (tsize + taTitleGap) : 0);
496 }
497 }
498 }
499 return offsets; // Object
500 },
501 cleanGroup: function(creator){
502 if(this.opt.enableCache && this.group){
503 this._lineFreePool = this._lineFreePool.concat(this._lineUsePool);
504 this._lineUsePool = [];
505 this._textFreePool = this._textFreePool.concat(this._textUsePool);
506 this._textUsePool = [];
507 }
508 this.inherited(arguments);
509 },
510 createText: function(labelType, creator, x, y, align, textContent, font, fontColor, labelWidth){
511 if(!this.opt.enableCache || labelType=="html"){
512 return acommon.createText[labelType](
513 this.chart,
514 creator,
515 x,
516 y,
517 align,
518 textContent,
519 font,
520 fontColor,
521 labelWidth
522 );
523 }
524 var text;
525 if(this._textFreePool.length > 0){
526 text = this._textFreePool.pop();
527 text.setShape({x: x, y: y, text: textContent, align: align});
528 // For now all items share the same font, no need to re-set it
529 //.setFont(font).setFill(fontColor);
530 // was cleared, add it back
531 creator.add(text);
532 }else{
533 text = acommon.createText[labelType](
534 this.chart,
535 creator,
536 x,
537 y,
538 align,
539 textContent,
540 font,
541 fontColor
542 );
543 }
544 this._textUsePool.push(text);
545 return text;
546 },
547 createLine: function(creator, params){
548 var line;
549 if(this.opt.enableCache && this._lineFreePool.length > 0){
550 line = this._lineFreePool.pop();
551 line.setShape(params);
552 // was cleared, add it back
553 creator.add(line);
554 }else{
555 line = creator.createLine(params);
556 }
557 if(this.opt.enableCache){
558 this._lineUsePool.push(line);
559 }
560 return line;
561 },
562 render: function(dim, offsets){
563 // summary:
564 // Render/draw the axis.
565 // dim: Object
566 // An object of the form { width, height}.
567 // offsets: Object
568 // An object of the form { l, r, t, b }.
569 // returns: dojox/charting/axis2d/Default
570 // The reference to the axis for functional chaining.
571
572 var isRtl = this._isRtl(); // chart mirroring
573 if(!this.dirty || !this.scaler){
574 return this; // dojox/charting/axis2d/Default
575 }
576 // prepare variable
577 var o = this.opt, ta = this.chart.theme.axis, position = o.position,
578 leftBottom = position !== "rightOrTop", rotation = o.rotation % 360,
579 start, stop, titlePos, titleRotation=0, titleOffset, axisVector, tickVector, anchorOffset, labelOffset, labelAlign,
580 labelGap = this.chart.theme.axis.tick.labelGap,
581 // TODO: we use one font --- of major tick, we need to use major and minor fonts
582 taFont = o.font || (ta.majorTick && ta.majorTick.font) || (ta.tick && ta.tick.font),
583 taTitleFont = o.titleFont || (ta.title && ta.title.font),
584 // TODO: we use one font color --- we need to use different colors
585 taFontColor = o.fontColor || (ta.majorTick && ta.majorTick.fontColor) || (ta.tick && ta.tick.fontColor) || "black",
586 taTitleFontColor = o.titleFontColor || (ta.title && ta.title.fontColor) || "black",
587 taTitleGap = (o.titleGap==0) ? 0 : o.titleGap || (ta.title && ta.title.gap) || 15,
588 taTitleOrientation = o.titleOrientation || (ta.title && ta.title.orientation) || "axis",
589 taMajorTick = this.chart.theme.getTick("major", o),
590 taMinorTick = this.chart.theme.getTick("minor", o),
591 taMicroTick = this.chart.theme.getTick("micro", o),
592
593 taStroke = "stroke" in o ? o.stroke : ta.stroke,
594 size = taFont ? g.normalizedLength(g.splitFontString(taFont).size) : 0,
595 cosr = Math.abs(Math.cos(rotation * Math.PI / 180)),
596 sinr = Math.abs(Math.sin(rotation * Math.PI / 180)),
597 tsize = taTitleFont ? g.normalizedLength(g.splitFontString(taTitleFont).size) : 0;
598 if(typeof labelGap != "number"){
599 labelGap = 4; // in pixels
600 }
601 if(rotation < 0){
602 rotation += 360;
603 }
604 var cachedLabelW = this._getMaxLabelSize();
605 cachedLabelW = cachedLabelW && cachedLabelW.majLabelW;
606 if(this.vertical){
607 start = {y: dim.height - offsets.b};
608 stop = {y: offsets.t};
609 titlePos = {y: (dim.height - offsets.b + offsets.t)/2};
610 titleOffset = size * sinr + (cachedLabelW || 0) * cosr + labelGap + Math.max(taMajorTick.length > 0?taMajorTick.length:0,
611 taMinorTick.length > 0?taMinorTick.length:0) +
612 tsize + taTitleGap;
613 axisVector = {x: 0, y: -1};
614 labelOffset = {x: 0, y: 0};
615 tickVector = {x: 1, y: 0};
616 anchorOffset = {x: labelGap, y: 0};
617 switch(rotation){
618 case 0:
619 labelAlign = "end";
620 labelOffset.y = size * 0.4;
621 break;
622 case 90:
623 labelAlign = "middle";
624 labelOffset.x = -size;
625 break;
626 case 180:
627 labelAlign = "start";
628 labelOffset.y = -size * 0.4;
629 break;
630 case 270:
631 labelAlign = "middle";
632 break;
633 default:
634 if(rotation < centerAnchorLimit){
635 labelAlign = "end";
636 labelOffset.y = size * 0.4;
637 }else if(rotation < 90){
638 labelAlign = "end";
639 labelOffset.y = size * 0.4;
640 }else if(rotation < (180 - centerAnchorLimit)){
641 labelAlign = "start";
642 }else if(rotation < (180 + centerAnchorLimit)){
643 labelAlign = "start";
644 labelOffset.y = -size * 0.4;
645 }else if(rotation < 270){
646 labelAlign = "start";
647 labelOffset.x = leftBottom ? 0 : size * 0.4;
648 }else if(rotation < (360 - centerAnchorLimit)){
649 labelAlign = "end";
650 labelOffset.x = leftBottom ? 0 : size * 0.4;
651 }else{
652 labelAlign = "end";
653 labelOffset.y = size * 0.4;
654 }
655 }
656 if(leftBottom){
657 start.x = stop.x = position === "center" ? dim.width/2 : offsets.l;
658 titleRotation = (taTitleOrientation && taTitleOrientation == "away") ? 90 : 270;
659 titlePos.x = offsets.l - titleOffset + (titleRotation == 270 ? tsize : 0);
660 tickVector.x = -1;
661 anchorOffset.x = -anchorOffset.x;
662 }else{
663 start.x = stop.x = dim.width - offsets.r;
664 titleRotation = (taTitleOrientation && taTitleOrientation == "axis") ? 90 : 270;
665 titlePos.x = dim.width - offsets.r + titleOffset - (titleRotation == 270 ? 0 : tsize);
666 switch(labelAlign){
667 case "start":
668 labelAlign = "end";
669 break;
670 case "end":
671 labelAlign = "start";
672 break;
673 case "middle":
674 labelOffset.x += size;
675 break;
676 }
677 }
678 }else{
679 start = {x: offsets.l};
680 stop = {x: dim.width - offsets.r};
681 titlePos = {x: (dim.width - offsets.r + offsets.l)/2};
682 titleOffset = size * cosr + (cachedLabelW || 0) * sinr + labelGap + Math.max(taMajorTick.length > 0?taMajorTick.length:0,
683 taMinorTick.length > 0?taMinorTick.length:0) +
684 tsize + taTitleGap;
685 axisVector = {x: isRtl ? -1 : 1, y: 0}; // chart mirroring
686 labelOffset = {x: 0, y: 0};
687 tickVector = {x: 0, y: 1};
688 anchorOffset = {x: 0, y: labelGap};
689 switch(rotation){
690 case 0:
691 labelAlign = "middle";
692 labelOffset.y = size;
693 break;
694 case 90:
695 labelAlign = "start";
696 labelOffset.x = -size * 0.4;
697 break;
698 case 180:
699 labelAlign = "middle";
700 break;
701 case 270:
702 labelAlign = "end";
703 labelOffset.x = size * 0.4;
704 break;
705 default:
706 if(rotation < (90 - centerAnchorLimit)){
707 labelAlign = "start";
708 labelOffset.y = leftBottom ? size : 0;
709 }else if(rotation < (90 + centerAnchorLimit)){
710 labelAlign = "start";
711 labelOffset.x = -size * 0.4;
712 }else if(rotation < 180){
713 labelAlign = "start";
714 labelOffset.y = leftBottom ? 0 : -size;
715 }else if(rotation < (270 - centerAnchorLimit)){
716 labelAlign = "end";
717 labelOffset.y = leftBottom ? 0 : -size;
718 }else if(rotation < (270 + centerAnchorLimit)){
719 labelAlign = "end";
720 labelOffset.y = leftBottom ? size * 0.4 : 0;
721 }else{
722 labelAlign = "end";
723 labelOffset.y = leftBottom ? size : 0;
724 }
725 }
726 if(leftBottom){
727 start.y = stop.y = position === "center" ? dim.height/2 : dim.height - offsets.b;
728 titleRotation = (taTitleOrientation && taTitleOrientation == "axis") ? 180 : 0;
729 titlePos.y = dim.height - offsets.b + titleOffset - (titleRotation ? tsize : 0);
730 }else{
731 start.y = stop.y = offsets.t;
732 titleRotation = (taTitleOrientation && taTitleOrientation == "away") ? 180 : 0;
733 titlePos.y = offsets.t - titleOffset + (titleRotation ? 0 : tsize);
734 tickVector.y = -1;
735 anchorOffset.y = -anchorOffset.y;
736 switch(labelAlign){
737 case "start":
738 labelAlign = "end";
739 break;
740 case "end":
741 labelAlign = "start";
742 break;
743 case "middle":
744 labelOffset.y -= size;
745 break;
746 }
747 }
748 }
749
750 // render shapes
751
752 this.cleanGroup();
753
754 var s = this.group,
755 c = this.scaler,
756 t = this.ticks,
757 f = lin.getTransformerFromModel(this.scaler),
758 // GFX Canvas now supports labels, so let's _not_ fallback to HTML anymore on canvas, just use
759 // HTML labels if explicitly asked + no rotation + no IE + no Opera
760 labelType = (!o.title || !titleRotation) && !rotation && this.opt.htmlLabels && !has("ie") && !has("opera") ? "html" : "gfx",
761 dx = tickVector.x * taMajorTick.length,
762 dy = tickVector.y * taMajorTick.length,
763 skip = this._skipInterval;
764
765 s.createLine({
766 x1: start.x,
767 y1: start.y,
768 x2: stop.x,
769 y2: stop.y
770 }).setStroke(taStroke);
771
772 //create axis title
773 if(o.title){
774 var axisTitle = acommon.createText[labelType](
775 this.chart,
776 s,
777 titlePos.x,
778 titlePos.y,
779 "middle",
780 o.title,
781 taTitleFont,
782 taTitleFontColor
783 );
784 if(labelType == "html"){
785 this.htmlElements.push(axisTitle);
786 }else{
787 //as soon as rotation is provided, labelType won't be "html"
788 //rotate gfx labels
789 axisTitle.setTransform(g.matrix.rotategAt(titleRotation, titlePos.x, titlePos.y));
790 }
791 }
792
793 // go out nicely instead of try/catch
794 if(t == null){
795 this.dirty = false;
796 return this;
797 }
798
799 var rel = (t.major.length > 0)?(t.major[0].value - this._majorStart) / c.major.tick:0;
800 var canLabel = this.opt.majorLabels;
801 arr.forEach(t.major, function(tick, i){
802 var offset = f(tick.value), elem,
803 x = (isRtl ? stop.x : start.x) + axisVector.x * offset, // chart mirroring
804 y = start.y + axisVector.y * offset;
805 i += rel;
806 this.createLine(s, {
807 x1: x, y1: y,
808 x2: x + dx,
809 y2: y + dy
810 }).setStroke(taMajorTick);
811 if(tick.label && (!skip || (i - (1 + skip)) % (1 + skip) == 0)){
812 var label = o.maxLabelCharCount ? this.getTextWithLimitCharCount(tick.label, taFont, o.maxLabelCharCount) : {
813 text: tick.label,
814 truncated: false
815 };
816 label = o.maxLabelSize ? this.getTextWithLimitLength(label.text, taFont, o.maxLabelSize, label.truncated) : label;
817 elem = this.createText(labelType,
818 s,
819 x + (taMajorTick.length > 0 ? dx : 0) + anchorOffset.x + (rotation ? 0 : labelOffset.x),
820 y + (taMajorTick.length > 0 ? dy : 0) + anchorOffset.y + (rotation ? 0 : labelOffset.y),
821 labelAlign,
822 label.text,
823 taFont,
824 taFontColor
825 //cachedLabelW
826 );
827 // if bidi support was required, the textDir is "auto" and truncation
828 // took place, we need to update the dir of the element for cases as:
829 // Fool label: 111111W (W for bidi character)
830 // truncated label: 11...
831 // in this case for auto textDir the dir will be "ltr" which is wrong.
832 if(label.truncated){
833 this.chart.formatTruncatedLabel(elem, tick.label, labelType);
834 }
835 label.truncated && this.labelTooltip(elem, this.chart, tick.label, label.text, taFont, labelType);
836 if(labelType == "html"){
837 this.htmlElements.push(elem);
838 }else if(rotation){
839 elem.setTransform([
840 {dx: labelOffset.x, dy: labelOffset.y},
841 g.matrix.rotategAt(
842 rotation,
843 x + (taMajorTick.length > 0 ? dx : 0) + anchorOffset.x,
844 y + (taMajorTick.length > 0 ? dy : 0) + anchorOffset.y
845 )
846 ]);
847 }
848 }
849 }, this);
850
851 dx = tickVector.x * taMinorTick.length;
852 dy = tickVector.y * taMinorTick.length;
853 canLabel = this.opt.minorLabels && c.minMinorStep <= c.minor.tick * c.bounds.scale;
854 arr.forEach(t.minor, function(tick){
855 var offset = f(tick.value), elem,
856 x = (isRtl ? stop.x : start.x) + axisVector.x * offset,
857 y = start.y + axisVector.y * offset; // chart mirroring
858 this.createLine(s, {
859 x1: x, y1: y,
860 x2: x + dx,
861 y2: y + dy
862 }).setStroke(taMinorTick);
863 if(canLabel && tick.label){
864 var label = o.maxLabelCharCount ? this.getTextWithLimitCharCount(tick.label, taFont, o.maxLabelCharCount) : {
865 text: tick.label,
866 truncated: false
867 };
868 label = o.maxLabelSize ? this.getTextWithLimitLength(label.text, taFont, o.maxLabelSize, label.truncated) : label;
869 elem = this.createText(labelType,
870 s,
871 x + (taMinorTick.length > 0 ? dx : 0) + anchorOffset.x + (rotation ? 0 : labelOffset.x),
872 y + (taMinorTick.length > 0 ? dy : 0) + anchorOffset.y + (rotation ? 0 : labelOffset.y),
873 labelAlign,
874 label.text,
875 taFont,
876 taFontColor
877 //cachedLabelW
878 );
879 // if bidi support was required, the textDir is "auto" and truncation
880 // took place, we need to update the dir of the element for cases as:
881 // Fool label: 111111W (W for bidi character)
882 // truncated label: 11...
883 // in this case for auto textDir the dir will be "ltr" which is wrong.
884 if(label.truncated){
885 this.chart.formatTruncatedLabel(elem, tick.label, labelType);
886 }
887 label.truncated && this.labelTooltip(elem, this.chart, tick.label, label.text, taFont, labelType);
888 if(labelType == "html"){
889 this.htmlElements.push(elem);
890 }else if(rotation){
891 elem.setTransform([
892 {dx: labelOffset.x, dy: labelOffset.y},
893 g.matrix.rotategAt(
894 rotation,
895 x + (taMinorTick.length > 0 ? dx : 0) + anchorOffset.x,
896 y + (taMinorTick.length > 0 ? dy : 0) + anchorOffset.y
897 )
898 ]);
899 }
900 }
901 }, this);
902
903 dx = tickVector.x * taMicroTick.length;
904 dy = tickVector.y * taMicroTick.length;
905 arr.forEach(t.micro, function(tick){
906 var offset = f(tick.value),
907 x = start.x + axisVector.x * offset,
908 y = start.y + axisVector.y * offset;
909 this.createLine(s, {
910 x1: x, y1: y,
911 x2: x + dx,
912 y2: y + dy
913 }).setStroke(taMicroTick);
914 }, this);
915
916 this.dirty = false;
917 return this; // dojox/charting/axis2d/Default
918 },
919 labelTooltip: function(elem, chart, label, truncatedLabel, font, elemType){
920 var modules = ["dijit/Tooltip"];
921 var aroundRect = {type: "rect"}, position = ["above", "below"],
922 fontWidth = g._base._getTextBox(truncatedLabel, {font: font}).w || 0,
923 fontHeight = font ? g.normalizedLength(g.splitFontString(font).size) : 0;
924 if(elemType == "html"){
925 lang.mixin(aroundRect, domGeom.position(elem.firstChild, true));
926 aroundRect.width = Math.ceil(fontWidth);
927 aroundRect.height = Math.ceil(fontHeight);
928 this._events.push({
929 shape: dojo,
930 handle: connect.connect(elem.firstChild, "onmouseover", this, function(e){
931 require(modules, function(Tooltip){
932 Tooltip.show(label, aroundRect, position);
933 });
934 })
935 });
936 this._events.push({
937 shape: dojo,
938 handle: connect.connect(elem.firstChild, "onmouseout", this, function(e){
939 require(modules, function(Tooltip){
940 Tooltip.hide(aroundRect);
941 });
942 })
943 });
944 }else{
945 var shp = elem.getShape(),
946 lt = chart.getCoords();
947 aroundRect = lang.mixin(aroundRect, {
948 x: shp.x - fontWidth / 2,
949 y: shp.y
950 });
951 aroundRect.x += lt.x;
952 aroundRect.y += lt.y;
953 aroundRect.x = Math.round(aroundRect.x);
954 aroundRect.y = Math.round(aroundRect.y);
955 aroundRect.width = Math.ceil(fontWidth);
956 aroundRect.height = Math.ceil(fontHeight);
957 this._events.push({
958 shape: elem,
959 handle: elem.connect("onmouseenter", this, function(e){
960 require(modules, function(Tooltip){
961 Tooltip.show(label, aroundRect, position);
962 });
963 })
964 });
965 this._events.push({
966 shape: elem,
967 handle: elem.connect("onmouseleave", this, function(e){
968 require(modules, function(Tooltip){
969 Tooltip.hide(aroundRect);
970 });
971 })
972 });
973 }
974 },
975 _isRtl: function(){
976 return false;
977 }
978 });
979 return has("dojo-bidi")? declare("dojox.charting.axis2d.Default", [Default, BidiDefault]) : Default;
980 });