|
1 /* |
|
2 YUI 3.10.3 (build 2fb5187) |
|
3 Copyright 2013 Yahoo! Inc. All rights reserved. |
|
4 Licensed under the BSD License. |
|
5 http://yuilibrary.com/license/ |
|
6 */ |
|
7 |
|
8 YUI.add('axis-numeric-base', function (Y, NAME) { |
|
9 |
|
10 /** |
|
11 * Provides functionality for the handling of numeric axis data for a chart. |
|
12 * |
|
13 * @module charts |
|
14 * @submodule axis-numeric-base |
|
15 */ |
|
16 |
|
17 /** |
|
18 * NumericImpl contains logic for numeric data. NumericImpl is used by the following classes: |
|
19 * <ul> |
|
20 * <li>{{#crossLink "NumericAxisBase"}}{{/crossLink}}</li> |
|
21 * <li>{{#crossLink "NumericAxis"}}{{/crossLink}}</li> |
|
22 * </ul> |
|
23 * |
|
24 * @class NumericImpl |
|
25 * @constructor |
|
26 * @submodule axis-numeric-base |
|
27 */ |
|
28 function NumericImpl() |
|
29 { |
|
30 } |
|
31 |
|
32 NumericImpl.NAME = "numericImpl"; |
|
33 |
|
34 NumericImpl.ATTRS = { |
|
35 /** |
|
36 * Indicates whether 0 should always be displayed. |
|
37 * |
|
38 * @attribute alwaysShowZero |
|
39 * @type Boolean |
|
40 */ |
|
41 alwaysShowZero: { |
|
42 value: true |
|
43 }, |
|
44 |
|
45 /** |
|
46 * Method used for formatting a label. This attribute allows for the default label formatting method to overridden. |
|
47 * The method use would need to implement the arguments below and return a `String` or an `HTMLElement`. The default |
|
48 * implementation of the method returns a `String`. The output of this method will be rendered to the DOM using |
|
49 * `appendChild`. If you override the `labelFunction` method and return an html string, you will also need to override |
|
50 * the Data' `appendLabelFunction` to accept html as a `String`. |
|
51 * <dl> |
|
52 * <dt>val</dt><dd>Label to be formatted. (`String`)</dd> |
|
53 * <dt>format</dt><dd>Object containing properties used to format the label. (optional)</dd> |
|
54 * </dl> |
|
55 * |
|
56 * @attribute labelFunction |
|
57 * @type Function |
|
58 */ |
|
59 |
|
60 /** |
|
61 * Object containing properties used by the `labelFunction` to format a |
|
62 * label. |
|
63 * |
|
64 * @attribute labelFormat |
|
65 * @type Object |
|
66 */ |
|
67 labelFormat: { |
|
68 value: { |
|
69 prefix: "", |
|
70 thousandsSeparator: "", |
|
71 decimalSeparator: "", |
|
72 decimalPlaces: "0", |
|
73 suffix: "" |
|
74 } |
|
75 }, |
|
76 |
|
77 /** |
|
78 *Indicates how to round unit values. |
|
79 * <dl> |
|
80 * <dt>niceNumber</dt><dd>Units will be smoothed based on the number of ticks and data range.</dd> |
|
81 * <dt>auto</dt><dd>If the range is greater than 1, the units will be rounded.</dd> |
|
82 * <dt>numeric value</dt><dd>Units will be equal to the numeric value.</dd> |
|
83 * <dt>null</dt><dd>No rounding will occur.</dd> |
|
84 * </dl> |
|
85 * |
|
86 * @attribute roundingMethod |
|
87 * @type String |
|
88 * @default niceNumber |
|
89 */ |
|
90 roundingMethod: { |
|
91 value: "niceNumber" |
|
92 } |
|
93 }; |
|
94 |
|
95 NumericImpl.prototype = { |
|
96 /** |
|
97 * @method initializer |
|
98 * @private |
|
99 */ |
|
100 initializer: function() { |
|
101 this.after("alwaysShowZeroChange", this._keyChangeHandler); |
|
102 this.after("roundingMethodChange", this._keyChangeHandler); |
|
103 }, |
|
104 |
|
105 /** |
|
106 * Formats a label based on the axis type and optionally specified format. |
|
107 * |
|
108 * @method |
|
109 * @param {Object} value |
|
110 * @param {Object} format Pattern used to format the value. |
|
111 * @return String |
|
112 */ |
|
113 formatLabel: function(val, format) |
|
114 { |
|
115 if(format) |
|
116 { |
|
117 return Y.DataType.Number.format(val, format); |
|
118 } |
|
119 return val; |
|
120 }, |
|
121 |
|
122 /** |
|
123 * Returns the sum of all values per key. |
|
124 * |
|
125 * @method getTotalByKey |
|
126 * @param {String} key The identifier for the array whose values will be calculated. |
|
127 * @return Number |
|
128 */ |
|
129 getTotalByKey: function(key) |
|
130 { |
|
131 var total = 0, |
|
132 values = this.getDataByKey(key), |
|
133 i = 0, |
|
134 val, |
|
135 len = values ? values.length : 0; |
|
136 for(; i < len; ++i) |
|
137 { |
|
138 val = parseFloat(values[i]); |
|
139 if(!isNaN(val)) |
|
140 { |
|
141 total += val; |
|
142 } |
|
143 } |
|
144 return total; |
|
145 }, |
|
146 |
|
147 /** |
|
148 * Type of data used in `Data`. |
|
149 * |
|
150 * @property _type |
|
151 * @readOnly |
|
152 * @private |
|
153 */ |
|
154 _type: "numeric", |
|
155 |
|
156 /** |
|
157 * Helper method for getting a `roundingUnit` when calculating the minimum and maximum values. |
|
158 * |
|
159 * @method _getMinimumUnit |
|
160 * @param {Number} max Maximum number |
|
161 * @param {Number} min Minimum number |
|
162 * @param {Number} units Number of units on the axis |
|
163 * @return Number |
|
164 * @private |
|
165 */ |
|
166 _getMinimumUnit:function(max, min, units) |
|
167 { |
|
168 return this._getNiceNumber(Math.ceil((max - min)/units)); |
|
169 }, |
|
170 |
|
171 /** |
|
172 * Calculates a nice rounding unit based on the range. |
|
173 * |
|
174 * @method _getNiceNumber |
|
175 * @param {Number} roundingUnit The calculated rounding unit. |
|
176 * @return Number |
|
177 * @private |
|
178 */ |
|
179 _getNiceNumber: function(roundingUnit) |
|
180 { |
|
181 var tempMajorUnit = roundingUnit, |
|
182 order = Math.ceil(Math.log(tempMajorUnit) * 0.4342944819032518), |
|
183 roundedMajorUnit = Math.pow(10, order), |
|
184 roundedDiff; |
|
185 |
|
186 if (roundedMajorUnit / 2 >= tempMajorUnit) |
|
187 { |
|
188 roundedDiff = Math.floor((roundedMajorUnit / 2 - tempMajorUnit) / (Math.pow(10,order-1)/2)); |
|
189 tempMajorUnit = roundedMajorUnit/2 - roundedDiff*Math.pow(10,order-1)/2; |
|
190 } |
|
191 else |
|
192 { |
|
193 tempMajorUnit = roundedMajorUnit; |
|
194 } |
|
195 if(!isNaN(tempMajorUnit)) |
|
196 { |
|
197 return tempMajorUnit; |
|
198 } |
|
199 return roundingUnit; |
|
200 |
|
201 }, |
|
202 |
|
203 /** |
|
204 * Calculates the maximum and minimum values for the `Data`. |
|
205 * |
|
206 * @method _updateMinAndMax |
|
207 * @private |
|
208 */ |
|
209 _updateMinAndMax: function() |
|
210 { |
|
211 var data = this.get("data"), |
|
212 max, |
|
213 min, |
|
214 len, |
|
215 num, |
|
216 i = 0, |
|
217 setMax = this.get("setMax"), |
|
218 setMin = this.get("setMin"); |
|
219 if(!setMax || !setMin) |
|
220 { |
|
221 if(data && data.length && data.length > 0) |
|
222 { |
|
223 len = data.length; |
|
224 for(; i < len; i++) |
|
225 { |
|
226 num = data[i]; |
|
227 if(isNaN(num)) |
|
228 { |
|
229 max = setMax ? this._setMaximum : max; |
|
230 min = setMin ? this._setMinimum : min; |
|
231 continue; |
|
232 } |
|
233 |
|
234 if(setMin) |
|
235 { |
|
236 min = this._setMinimum; |
|
237 } |
|
238 else if(min === undefined) |
|
239 { |
|
240 min = num; |
|
241 } |
|
242 else |
|
243 { |
|
244 min = Math.min(num, min); |
|
245 } |
|
246 if(setMax) |
|
247 { |
|
248 max = this._setMaximum; |
|
249 } |
|
250 else if(max === undefined) |
|
251 { |
|
252 max = num; |
|
253 } |
|
254 else |
|
255 { |
|
256 max = Math.max(num, max); |
|
257 } |
|
258 |
|
259 this._actualMaximum = max; |
|
260 this._actualMinimum = min; |
|
261 } |
|
262 } |
|
263 this._roundMinAndMax(min, max, setMin, setMax); |
|
264 } |
|
265 }, |
|
266 |
|
267 /** |
|
268 * Rounds the mimimum and maximum values based on the `roundingUnit` attribute. |
|
269 * |
|
270 * @method _roundMinAndMax |
|
271 * @param {Number} min Minimum value |
|
272 * @param {Number} max Maximum value |
|
273 * @private |
|
274 */ |
|
275 _roundMinAndMax: function(min, max, setMin, setMax) |
|
276 { |
|
277 var roundingUnit, |
|
278 minimumRange, |
|
279 minGreaterThanZero = min >= 0, |
|
280 maxGreaterThanZero = max > 0, |
|
281 dataRangeGreater, |
|
282 maxRound, |
|
283 minRound, |
|
284 topTicks, |
|
285 botTicks, |
|
286 tempMax, |
|
287 tempMin, |
|
288 units = this.getTotalMajorUnits() - 1, |
|
289 alwaysShowZero = this.get("alwaysShowZero"), |
|
290 roundingMethod = this.get("roundingMethod"), |
|
291 useIntegers = (max - min)/units >= 1; |
|
292 if(roundingMethod) |
|
293 { |
|
294 if(roundingMethod === "niceNumber") |
|
295 { |
|
296 roundingUnit = this._getMinimumUnit(max, min, units); |
|
297 if(minGreaterThanZero && maxGreaterThanZero) |
|
298 { |
|
299 if((alwaysShowZero || min < roundingUnit) && !setMin) |
|
300 { |
|
301 min = 0; |
|
302 roundingUnit = this._getMinimumUnit(max, min, units); |
|
303 } |
|
304 else |
|
305 { |
|
306 min = this._roundDownToNearest(min, roundingUnit); |
|
307 } |
|
308 if(setMax) |
|
309 { |
|
310 if(!alwaysShowZero) |
|
311 { |
|
312 min = max - (roundingUnit * units); |
|
313 } |
|
314 } |
|
315 else if(setMin) |
|
316 { |
|
317 max = min + (roundingUnit * units); |
|
318 } |
|
319 else |
|
320 { |
|
321 max = this._roundUpToNearest(max, roundingUnit); |
|
322 } |
|
323 } |
|
324 else if(maxGreaterThanZero && !minGreaterThanZero) |
|
325 { |
|
326 if(alwaysShowZero) |
|
327 { |
|
328 topTicks = Math.round(units/((-1 * min)/max + 1)); |
|
329 topTicks = Math.max(Math.min(topTicks, units - 1), 1); |
|
330 botTicks = units - topTicks; |
|
331 tempMax = Math.ceil( max/topTicks ); |
|
332 tempMin = Math.floor( min/botTicks ) * -1; |
|
333 |
|
334 if(setMin) |
|
335 { |
|
336 while(tempMin < tempMax && botTicks >= 0) |
|
337 { |
|
338 botTicks--; |
|
339 topTicks++; |
|
340 tempMax = Math.ceil( max/topTicks ); |
|
341 tempMin = Math.floor( min/botTicks ) * -1; |
|
342 } |
|
343 //if there are any bottom ticks left calcualate the maximum by multiplying by the tempMin value |
|
344 //if not, it's impossible to ensure that a zero is shown. skip it |
|
345 if(botTicks > 0) |
|
346 { |
|
347 max = tempMin * topTicks; |
|
348 } |
|
349 else |
|
350 { |
|
351 max = min + (roundingUnit * units); |
|
352 } |
|
353 } |
|
354 else if(setMax) |
|
355 { |
|
356 while(tempMax < tempMin && topTicks >= 0) |
|
357 { |
|
358 botTicks++; |
|
359 topTicks--; |
|
360 tempMin = Math.floor( min/botTicks ) * -1; |
|
361 tempMax = Math.ceil( max/topTicks ); |
|
362 } |
|
363 //if there are any top ticks left calcualate the minimum by multiplying by the tempMax value |
|
364 //if not, it's impossible to ensure that a zero is shown. skip it |
|
365 if(topTicks > 0) |
|
366 { |
|
367 min = tempMax * botTicks * -1; |
|
368 } |
|
369 else |
|
370 { |
|
371 min = max - (roundingUnit * units); |
|
372 } |
|
373 } |
|
374 else |
|
375 { |
|
376 roundingUnit = Math.max(tempMax, tempMin); |
|
377 roundingUnit = this._getNiceNumber(roundingUnit); |
|
378 max = roundingUnit * topTicks; |
|
379 min = roundingUnit * botTicks * -1; |
|
380 } |
|
381 } |
|
382 else |
|
383 { |
|
384 if(setMax) |
|
385 { |
|
386 min = max - (roundingUnit * units); |
|
387 } |
|
388 else if(setMin) |
|
389 { |
|
390 max = min + (roundingUnit * units); |
|
391 } |
|
392 else |
|
393 { |
|
394 min = this._roundDownToNearest(min, roundingUnit); |
|
395 max = this._roundUpToNearest(max, roundingUnit); |
|
396 } |
|
397 } |
|
398 } |
|
399 else |
|
400 { |
|
401 if(setMin) |
|
402 { |
|
403 if(alwaysShowZero) |
|
404 { |
|
405 max = 0; |
|
406 } |
|
407 else |
|
408 { |
|
409 max = min + (roundingUnit * units); |
|
410 } |
|
411 } |
|
412 else if(!setMax) |
|
413 { |
|
414 if(alwaysShowZero || max === 0 || max + roundingUnit > 0) |
|
415 { |
|
416 max = 0; |
|
417 roundingUnit = this._getMinimumUnit(max, min, units); |
|
418 min = max - (roundingUnit * units); |
|
419 } |
|
420 else |
|
421 { |
|
422 min = this._roundDownToNearest(min, roundingUnit); |
|
423 max = this._roundUpToNearest(max, roundingUnit); |
|
424 } |
|
425 } |
|
426 else |
|
427 { |
|
428 min = max - (roundingUnit * units); |
|
429 } |
|
430 } |
|
431 } |
|
432 else if(roundingMethod === "auto") |
|
433 { |
|
434 if(minGreaterThanZero && maxGreaterThanZero) |
|
435 { |
|
436 if((alwaysShowZero || min < (max-min)/units) && !setMin) |
|
437 { |
|
438 min = 0; |
|
439 } |
|
440 |
|
441 roundingUnit = (max - min)/units; |
|
442 if(useIntegers) |
|
443 { |
|
444 roundingUnit = Math.ceil(roundingUnit); |
|
445 max = min + (roundingUnit * units); |
|
446 } |
|
447 else |
|
448 { |
|
449 max = min + Math.ceil(roundingUnit * units * 100000)/100000; |
|
450 |
|
451 } |
|
452 } |
|
453 else if(maxGreaterThanZero && !minGreaterThanZero) |
|
454 { |
|
455 if(alwaysShowZero) |
|
456 { |
|
457 topTicks = Math.round( units / ( (-1 * min) /max + 1) ); |
|
458 topTicks = Math.max(Math.min(topTicks, units - 1), 1); |
|
459 botTicks = units - topTicks; |
|
460 |
|
461 if(useIntegers) |
|
462 { |
|
463 tempMax = Math.ceil( max/topTicks ); |
|
464 tempMin = Math.floor( min/botTicks ) * -1; |
|
465 roundingUnit = Math.max(tempMax, tempMin); |
|
466 max = roundingUnit * topTicks; |
|
467 min = roundingUnit * botTicks * -1; |
|
468 } |
|
469 else |
|
470 { |
|
471 tempMax = max/topTicks; |
|
472 tempMin = min/botTicks * -1; |
|
473 roundingUnit = Math.max(tempMax, tempMin); |
|
474 max = Math.ceil(roundingUnit * topTicks * 100000)/100000; |
|
475 min = Math.ceil(roundingUnit * botTicks * 100000)/100000 * -1; |
|
476 } |
|
477 } |
|
478 else |
|
479 { |
|
480 roundingUnit = (max - min)/units; |
|
481 if(useIntegers) |
|
482 { |
|
483 roundingUnit = Math.ceil(roundingUnit); |
|
484 } |
|
485 min = Math.round(this._roundDownToNearest(min, roundingUnit) * 100000)/100000; |
|
486 max = Math.round(this._roundUpToNearest(max, roundingUnit) * 100000)/100000; |
|
487 } |
|
488 } |
|
489 else |
|
490 { |
|
491 roundingUnit = (max - min)/units; |
|
492 if(useIntegers) |
|
493 { |
|
494 roundingUnit = Math.ceil(roundingUnit); |
|
495 } |
|
496 if(alwaysShowZero || max === 0 || max + roundingUnit > 0) |
|
497 { |
|
498 max = 0; |
|
499 roundingUnit = (max - min)/units; |
|
500 if(useIntegers) |
|
501 { |
|
502 Math.ceil(roundingUnit); |
|
503 min = max - (roundingUnit * units); |
|
504 } |
|
505 else |
|
506 { |
|
507 min = max - Math.ceil(roundingUnit * units * 100000)/100000; |
|
508 } |
|
509 } |
|
510 else |
|
511 { |
|
512 min = this._roundDownToNearest(min, roundingUnit); |
|
513 max = this._roundUpToNearest(max, roundingUnit); |
|
514 } |
|
515 |
|
516 } |
|
517 } |
|
518 else if(!isNaN(roundingMethod) && isFinite(roundingMethod)) |
|
519 { |
|
520 roundingUnit = roundingMethod; |
|
521 minimumRange = roundingUnit * units; |
|
522 dataRangeGreater = (max - min) > minimumRange; |
|
523 minRound = this._roundDownToNearest(min, roundingUnit); |
|
524 maxRound = this._roundUpToNearest(max, roundingUnit); |
|
525 if(setMax) |
|
526 { |
|
527 min = max - minimumRange; |
|
528 } |
|
529 else if(setMin) |
|
530 { |
|
531 max = min + minimumRange; |
|
532 } |
|
533 else if(minGreaterThanZero && maxGreaterThanZero) |
|
534 { |
|
535 if(alwaysShowZero || minRound <= 0) |
|
536 { |
|
537 min = 0; |
|
538 } |
|
539 else |
|
540 { |
|
541 min = minRound; |
|
542 } |
|
543 max = min + minimumRange; |
|
544 } |
|
545 else if(maxGreaterThanZero && !minGreaterThanZero) |
|
546 { |
|
547 min = minRound; |
|
548 max = maxRound; |
|
549 } |
|
550 else |
|
551 { |
|
552 if(alwaysShowZero || maxRound >= 0) |
|
553 { |
|
554 max = 0; |
|
555 } |
|
556 else |
|
557 { |
|
558 max = maxRound; |
|
559 } |
|
560 min = max - minimumRange; |
|
561 } |
|
562 } |
|
563 } |
|
564 this._dataMaximum = max; |
|
565 this._dataMinimum = min; |
|
566 }, |
|
567 |
|
568 /** |
|
569 * Rounds a Number to the nearest multiple of an input. For example, by rounding |
|
570 * 16 to the nearest 10, you will receive 20. Similar to the built-in function Math.round(). |
|
571 * |
|
572 * @method _roundToNearest |
|
573 * @param {Number} number Number to round |
|
574 * @param {Number} nearest Multiple to round towards. |
|
575 * @return Number |
|
576 * @private |
|
577 */ |
|
578 _roundToNearest: function(number, nearest) |
|
579 { |
|
580 nearest = nearest || 1; |
|
581 var roundedNumber = Math.round(this._roundToPrecision(number / nearest, 10)) * nearest; |
|
582 return this._roundToPrecision(roundedNumber, 10); |
|
583 }, |
|
584 |
|
585 /** |
|
586 * Rounds a Number up to the nearest multiple of an input. For example, by rounding |
|
587 * 16 up to the nearest 10, you will receive 20. Similar to the built-in function Math.ceil(). |
|
588 * |
|
589 * @method _roundUpToNearest |
|
590 * @param {Number} number Number to round |
|
591 * @param {Number} nearest Multiple to round towards. |
|
592 * @return Number |
|
593 * @private |
|
594 */ |
|
595 _roundUpToNearest: function(number, nearest) |
|
596 { |
|
597 nearest = nearest || 1; |
|
598 return Math.ceil(this._roundToPrecision(number / nearest, 10)) * nearest; |
|
599 }, |
|
600 |
|
601 /** |
|
602 * Rounds a Number down to the nearest multiple of an input. For example, by rounding |
|
603 * 16 down to the nearest 10, you will receive 10. Similar to the built-in function Math.floor(). |
|
604 * |
|
605 * @method _roundDownToNearest |
|
606 * @param {Number} number Number to round |
|
607 * @param {Number} nearest Multiple to round towards. |
|
608 * @return Number |
|
609 * @private |
|
610 */ |
|
611 _roundDownToNearest: function(number, nearest) |
|
612 { |
|
613 nearest = nearest || 1; |
|
614 return Math.floor(this._roundToPrecision(number / nearest, 10)) * nearest; |
|
615 }, |
|
616 |
|
617 /** |
|
618 * Rounds a number to a certain level of precision. Useful for limiting the number of |
|
619 * decimal places on a fractional number. |
|
620 * |
|
621 * @method _roundToPrecision |
|
622 * @param {Number} number Number to round |
|
623 * @param {Number} precision Multiple to round towards. |
|
624 * @return Number |
|
625 * @private |
|
626 */ |
|
627 _roundToPrecision: function(number, precision) |
|
628 { |
|
629 precision = precision || 0; |
|
630 var decimalPlaces = Math.pow(10, precision); |
|
631 return Math.round(decimalPlaces * number) / decimalPlaces; |
|
632 } |
|
633 }; |
|
634 |
|
635 Y.NumericImpl = NumericImpl; |
|
636 |
|
637 /** |
|
638 * NumericAxisBase manages numeric data for an axis. |
|
639 * |
|
640 * @class NumericAxisBase |
|
641 * @constructor |
|
642 * @extends AxisBase |
|
643 * @uses NumericImpl |
|
644 * @param {Object} config (optional) Configuration parameters. |
|
645 * @submodule axis-numeric-base |
|
646 */ |
|
647 Y.NumericAxisBase = Y.Base.create("numericAxisBase", Y.AxisBase, [Y.NumericImpl]); |
|
648 |
|
649 |
|
650 }, '3.10.3', {"requires": ["axis-base"]}); |