Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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