NOBUG: Fixed file access permissions
[moodle.git] / lib / yuilib / 3.13.0 / axis-category-base / axis-category-base.js
blob1b3207e602dd39f05003d5017be422860115451a
1 /*
2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
8 YUI.add('axis-category-base', function (Y, NAME) {
10 /**
11  * Provides functionality for the handling of category axis data for a chart.
12  *
13  * @module charts
14  * @submodule axis-category-base
15  */
16 var Y_Lang = Y.Lang;
18 /**
19  * CategoryImpl contains logic for managing category data. CategoryImpl is used by the following classes:
20  * <ul>
21  *      <li>{{#crossLink "CategoryAxisBase"}}{{/crossLink}}</li>
22  *      <li>{{#crossLink "CategoryAxis"}}{{/crossLink}}</li>
23  *  </ul>
24  *
25  * @class CategoryImpl
26  * @constructor
27  * @submodule axis-category-base
28  */
29 function CategoryImpl()
33 CategoryImpl.NAME = "categoryImpl";
35 CategoryImpl.ATTRS = {
36     /**
37      * Determines whether and offset is automatically calculated for the edges of the axis.
38      *
39      * @attribute calculateEdgeOffset
40      * @type Boolean
41      */
42     calculateEdgeOffset: {
43         value: true
44     }
46     /**
47      * Method used for formatting a label. This attribute allows for the default label formatting method to overridden.
48      * The method use would need to implement the arguments below and return a `String` or `HTMLElement`.
49      * <dl>
50      *      <dt>val</dt><dd>Label to be formatted. (`String`)</dd>
51      *      <dt>format</dt><dd>Template for formatting label. (optional)</dd>
52      * </dl>
53      *
54      * @attribute labelFunction
55      * @type Function
56      */
59 CategoryImpl.prototype = {
60     /**
61      * Formats a label based on the axis type and optionally specified format.
62      *
63      * @method formatLabel
64      * @param {Object} value
65      * @return String
66      */
67     formatLabel: function(val)
68     {
69         return val;
70     },
72     /**
73      * Object storing key data.
74      *
75      * @property _indices
76      * @private
77      */
78     _indices: null,
80     /**
81      * Constant used to generate unique id.
82      *
83      * @property GUID
84      * @type String
85      * @private
86      */
87     GUID: "yuicategoryaxis",
89     /**
90      * Type of data used in `Data`.
91      *
92      * @property _dataType
93      * @readOnly
94      * @private
95      */
96     _type: "category",
98     /**
99      * Calculates the maximum and minimum values for the `Data`.
100      *
101      * @method _updateMinAndMax
102      * @private
103      */
104     _updateMinAndMax: function()
105     {
106         this._dataMaximum = Math.max(this.get("data").length - 1, 0);
107         this._dataMinimum = 0;
108     },
110     /**
111      * Gets an array of values based on a key.
112      *
113      * @method _getKeyArray
114      * @param {String} key Value key associated with the data array.
115      * @param {Array} data Array in which the data resides.
116      * @return Array
117      * @private
118      */
119     _getKeyArray: function(key, data)
120     {
121         var i = 0,
122             obj,
123             keyArr = [],
124             labels = [],
125             len = data.length;
126         if(!this._indices)
127         {
128             this._indices = {};
129         }
130         for(; i < len; ++i)
131         {
132             obj = data[i];
133             keyArr[i] = i;
134             labels[i] = obj[key];
135         }
136         this._indices[key] = keyArr;
137         return labels;
138     },
140     /**
141      * Returns an array of values based on an identifier key.
142      *
143      * @method getDataByKey
144      * @param {String} value value used to identify the array
145      * @return Array
146      */
147     getDataByKey: function (value)
148     {
149         if(!this._indices)
150         {
151             this.get("keys");
152         }
153         var keys = this._indices;
154         if(keys && keys[value])
155         {
156             return keys[value];
157         }
158         return null;
159     },
161     /**
162      * Returns the total number of majorUnits that will appear on an axis.
163      *
164      * @method getTotalMajorUnits
165      * @param {Object} majorUnit Object containing properties related to the majorUnit.
166      * @param {Number} len Length of the axis.
167      * @return Number
168      */
169     getTotalMajorUnits: function()
170     {
171         return this.get("data").length;
172     },
174     /**
175      * Returns a coordinate corresponding to a data values.
176      *
177      * @method _getCoordFromValue
178      * @param {Number} min The minimum for the axis.
179      * @param {Number} max The maximum for the axis.
180      * @param {length} length The distance that the axis spans.
181      * @param {Number} dataValue A value used to ascertain the coordinate.
182      * @param {Number} offset Value in which to offset the coordinates.
183      * @param {Boolean} reverse Indicates whether the coordinates should start from
184      * the end of an axis. Only used in the numeric implementation.
185      * @return Number
186      * @private
187      */
188     _getCoordFromValue: function(min, max, length, dataValue, offset)
189     {
190         var range,
191             multiplier,
192             valuecoord;
193         if(Y_Lang.isNumber(dataValue))
194         {
195             range = max - min;
196             multiplier = length/range;
197             valuecoord = (dataValue - min) * multiplier;
198             valuecoord = offset + valuecoord;
199         }
200         else
201         {
202             valuecoord = NaN;
203         }
204         return valuecoord;
205     },
207     /**
208      * Returns a value based of a key value and an index.
209      *
210      * @method getKeyValueAt
211      * @param {String} key value used to look up the correct array
212      * @param {Number} index within the array
213      * @return String
214      */
215     getKeyValueAt: function(key, index)
216     {
217         var value = NaN,
218             keys = this.get("keys");
219         if(keys[key] && keys[key][index])
220         {
221             value = keys[key][index];
222         }
223         return value;
224     }
227 Y.CategoryImpl = CategoryImpl;
230  * CategoryAxisBase manages category data for an axis.
232  * @class CategoryAxisBase
233  * @constructor
234  * @extends AxisBase
235  * @uses CategoryImpl
236  * @param {Object} config (optional) Configuration parameters.
237  * @submodule axis-category-base
238  */
239 Y.CategoryAxisBase = Y.Base.create("categoryAxisBase", Y.AxisBase, [Y.CategoryImpl]);
242 }, '3.13.0', {"requires": ["axis-base"]});