Debian Codegraph @

dojox/charting/axis2d/Log.js

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