|
525
|
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', function (Y, NAME) { |
|
|
9 |
|
|
|
10 |
/** |
|
|
11 |
* Provides functionality for drawing a numeric axis for use with a chart. |
|
|
12 |
* |
|
|
13 |
* @module charts |
|
|
14 |
* @submodule axis-numeric |
|
|
15 |
*/ |
|
|
16 |
Y_Lang = Y.Lang; |
|
|
17 |
/** |
|
|
18 |
* NumericAxis draws a numeric axis. |
|
|
19 |
* |
|
|
20 |
* @class NumericAxis |
|
|
21 |
* @constructor |
|
|
22 |
* @extends Axis |
|
|
23 |
* @uses NumericImpl |
|
|
24 |
* @param {Object} config (optional) Configuration parameters. |
|
|
25 |
* @submodule axis-numeric |
|
|
26 |
*/ |
|
|
27 |
Y.NumericAxis = Y.Base.create("numericAxis", Y.Axis, [Y.NumericImpl], { |
|
|
28 |
/** |
|
|
29 |
* Calculates and returns a value based on the number of labels and the index of |
|
|
30 |
* the current label. |
|
|
31 |
* |
|
|
32 |
* @method getLabelByIndex |
|
|
33 |
* @param {Number} i Index of the label. |
|
|
34 |
* @param {Number} l Total number of labels. |
|
|
35 |
* @return String |
|
|
36 |
* @private |
|
|
37 |
*/ |
|
|
38 |
_getLabelByIndex: function(i, l) |
|
|
39 |
{ |
|
|
40 |
var min = this.get("minimum"), |
|
|
41 |
max = this.get("maximum"), |
|
|
42 |
increm = (max - min)/(l-1), |
|
|
43 |
label, |
|
|
44 |
roundingMethod = this.get("roundingMethod"); |
|
|
45 |
l -= 1; |
|
|
46 |
//respect the min and max. calculate all other labels. |
|
|
47 |
if(i === 0) |
|
|
48 |
{ |
|
|
49 |
label = min; |
|
|
50 |
} |
|
|
51 |
else if(i === l) |
|
|
52 |
{ |
|
|
53 |
label = max; |
|
|
54 |
} |
|
|
55 |
else |
|
|
56 |
{ |
|
|
57 |
label = (i * increm); |
|
|
58 |
if(roundingMethod === "niceNumber") |
|
|
59 |
{ |
|
|
60 |
label = this._roundToNearest(label, increm); |
|
|
61 |
} |
|
|
62 |
label += min; |
|
|
63 |
} |
|
|
64 |
return parseFloat(label); |
|
|
65 |
}, |
|
|
66 |
|
|
|
67 |
/** |
|
|
68 |
* Calculates points based off the majorUnit count or distance of the Axis. |
|
|
69 |
* |
|
|
70 |
* @method _getPoints |
|
|
71 |
* @param {Object} startPoint An object literal containing the x and y coordinates of the first |
|
|
72 |
* point on the axis. |
|
|
73 |
* @param {Number} len The number of points on an axis. |
|
|
74 |
* @param {Number} edgeOffset The distance from the start of the axis and the point. |
|
|
75 |
* @param {Number} majorUnitDistance The distance between points on an axis. |
|
|
76 |
* @param {String} direction Indicates whether the axis is horizontal or vertical. |
|
|
77 |
* @return Array |
|
|
78 |
* @private |
|
|
79 |
*/ |
|
|
80 |
_getPoints: function(startPoint, len, edgeOffset, majorUnitDistance, direction) |
|
|
81 |
{ |
|
|
82 |
var points = Y.NumericAxis.superclass._getPoints.apply(this, arguments); |
|
|
83 |
if(direction === "vertical") |
|
|
84 |
{ |
|
|
85 |
points.reverse(); |
|
|
86 |
} |
|
|
87 |
return points; |
|
|
88 |
}, |
|
|
89 |
|
|
|
90 |
/** |
|
|
91 |
* Calculates the position of ticks and labels based on an array of specified label values. Returns |
|
|
92 |
* an object containing an array of values to be used for labels and an array of objects containing |
|
|
93 |
* x and y coordinates for each label. |
|
|
94 |
* |
|
|
95 |
* @method _getDataFromLabelValues |
|
|
96 |
* @param {Object} startPoint An object containing the x and y coordinates for the start of the axis. |
|
|
97 |
* @param {Array} labelValues An array containing values to be used for determining the number and |
|
|
98 |
* position of labels and ticks on the axis. |
|
|
99 |
* @param {Number} edgeOffset The distance, in pixels, on either edge of the axis. |
|
|
100 |
* @param {Number} layoutLength The length, in pixels, of the axis. If the axis is vertical, the length |
|
|
101 |
* is equal to the height. If the axis is horizontal, the length is equal to the width. |
|
|
102 |
* @return Object |
|
|
103 |
* @private |
|
|
104 |
*/ |
|
|
105 |
_getDataFromLabelValues: function(startPoint, labelValues, edgeOffset, layoutLength, direction) |
|
|
106 |
{ |
|
|
107 |
var points = [], |
|
|
108 |
labelValue, |
|
|
109 |
i, |
|
|
110 |
len = labelValues.length, |
|
|
111 |
staticCoord, |
|
|
112 |
dynamicCoord, |
|
|
113 |
constantVal, |
|
|
114 |
newPoint, |
|
|
115 |
max = this.get("maximum"), |
|
|
116 |
min = this.get("minimum"), |
|
|
117 |
values = [], |
|
|
118 |
scaleFactor = (layoutLength - (edgeOffset * 2)) / (max - min); |
|
|
119 |
if(direction === "vertical") |
|
|
120 |
{ |
|
|
121 |
staticCoord = "x"; |
|
|
122 |
dynamicCoord = "y"; |
|
|
123 |
} |
|
|
124 |
else |
|
|
125 |
{ |
|
|
126 |
staticCoord = "y"; |
|
|
127 |
dynamicCoord = "x"; |
|
|
128 |
} |
|
|
129 |
constantVal = startPoint[staticCoord]; |
|
|
130 |
for(i = 0; i < len; i = i + 1) |
|
|
131 |
{ |
|
|
132 |
labelValue = labelValues[i]; |
|
|
133 |
if(Y.Lang.isNumber(labelValue) && labelValue >= min && labelValue <= max) |
|
|
134 |
{ |
|
|
135 |
newPoint = {}; |
|
|
136 |
newPoint[staticCoord] = constantVal; |
|
|
137 |
newPoint[dynamicCoord] = (layoutLength - edgeOffset) - (labelValue - min) * scaleFactor; |
|
|
138 |
points.push(newPoint); |
|
|
139 |
values.push(labelValue); |
|
|
140 |
} |
|
|
141 |
} |
|
|
142 |
return { |
|
|
143 |
points: points, |
|
|
144 |
values: values |
|
|
145 |
}; |
|
|
146 |
}, |
|
|
147 |
|
|
|
148 |
/** |
|
|
149 |
* Checks to see if data extends beyond the range of the axis. If so, |
|
|
150 |
* that data will need to be hidden. This method is internal, temporary and subject |
|
|
151 |
* to removal in the future. |
|
|
152 |
* |
|
|
153 |
* @method _hasDataOverflow |
|
|
154 |
* @protected |
|
|
155 |
* @return Boolean |
|
|
156 |
*/ |
|
|
157 |
_hasDataOverflow: function() |
|
|
158 |
{ |
|
|
159 |
var roundingMethod, |
|
|
160 |
min, |
|
|
161 |
max; |
|
|
162 |
if(this.get("setMin") || this.get("setMax")) |
|
|
163 |
{ |
|
|
164 |
return true; |
|
|
165 |
} |
|
|
166 |
roundingMethod = this.get("roundingMethod"); |
|
|
167 |
min = this._actualMinimum; |
|
|
168 |
max = this._actualMaximum; |
|
|
169 |
if(Y_Lang.isNumber(roundingMethod) && |
|
|
170 |
((Y_Lang.isNumber(max) && max > this._dataMaximum) || (Y_Lang.isNumber(min) && min < this._dataMinimum))) |
|
|
171 |
{ |
|
|
172 |
return true; |
|
|
173 |
} |
|
|
174 |
return false; |
|
|
175 |
} |
|
|
176 |
}); |
|
|
177 |
|
|
|
178 |
|
|
|
179 |
|
|
|
180 |
}, '3.10.3', {"requires": ["axis", "axis-numeric-base"]}); |