Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / chart / axis / Category.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.chart.axis.Category
17  * @extends Ext.chart.axis.Axis
18  *
19  * A type of axis that displays items in categories. This axis is generally used to
20  * display categorical information like names of items, month names, quarters, etc.
21  * but no quantitative values. For that other type of information <em>Number</em>
22  * axis are more suitable.
23  *
24  * As with other axis you can set the position of the axis and its title. For example:
25  *
26  * {@img Ext.chart.axis.Category/Ext.chart.axis.Category.png Ext.chart.axis.Category chart axis}
27  *
28  *     var store = Ext.create('Ext.data.JsonStore', {
29  *         fields: ['name', 'data1', 'data2', 'data3', 'data4', 'data5'],
30  *         data: [
31  *             {'name':'metric one', 'data1':10, 'data2':12, 'data3':14, 'data4':8, 'data5':13},
32  *             {'name':'metric two', 'data1':7, 'data2':8, 'data3':16, 'data4':10, 'data5':3},
33  *             {'name':'metric three', 'data1':5, 'data2':2, 'data3':14, 'data4':12, 'data5':7},
34  *             {'name':'metric four', 'data1':2, 'data2':14, 'data3':6, 'data4':1, 'data5':23},
35  *             {'name':'metric five', 'data1':27, 'data2':38, 'data3':36, 'data4':13, 'data5':33}                                                
36  *         ]
37  *     });
38  *  
39  *     Ext.create('Ext.chart.Chart', {
40  *         renderTo: Ext.getBody(),
41  *         width: 500,
42  *         height: 300,
43  *         store: store,
44  *         axes: [{
45  *             type: 'Numeric',
46  *             grid: true,
47  *             position: 'left',
48  *             fields: ['data1', 'data2', 'data3', 'data4', 'data5'],
49  *             title: 'Sample Values',
50  *             grid: {
51  *                 odd: {
52  *                     opacity: 1,
53  *                     fill: '#ddd',
54  *                     stroke: '#bbb',
55  *                     'stroke-width': 1
56  *                 }
57  *             },
58  *             minimum: 0,
59  *             adjustMinimumByMajorUnit: 0
60  *         }, {
61  *             type: 'Category',
62  *             position: 'bottom',
63  *             fields: ['name'],
64  *             title: 'Sample Metrics',
65  *             grid: true,
66  *             label: {
67  *                 rotate: {
68  *                     degrees: 315
69  *                 }
70  *             }
71  *         }],
72  *         series: [{
73  *             type: 'area',
74  *             highlight: false,
75  *             axis: 'left',
76  *             xField: 'name',
77  *             yField: ['data1', 'data2', 'data3', 'data4', 'data5'],
78  *             style: {
79  *                 opacity: 0.93
80  *             }
81  *         }]
82  *     });
83  *
84  * In this example with set the category axis to the bottom of the surface, bound the axis to
85  * the <em>name</em> property and set as title <em>Month of the Year</em>.
86  */
87
88 Ext.define('Ext.chart.axis.Category', {
89
90     /* Begin Definitions */
91
92     extend: 'Ext.chart.axis.Axis',
93
94     alternateClassName: 'Ext.chart.CategoryAxis',
95
96     alias: 'axis.category',
97
98     /* End Definitions */
99
100     /**
101      * A list of category names to display along this axis.
102      *
103      * @property categoryNames
104      * @type Array
105      */
106     categoryNames: null,
107
108     /**
109      * Indicates whether or not to calculate the number of categories (ticks and
110      * labels) when there is not enough room to display all labels on the axis.
111      * If set to true, the axis will determine the number of categories to plot.
112      * If not, all categories will be plotted.
113      *
114      * @property calculateCategoryCount
115      * @type Boolean
116      */
117     calculateCategoryCount: false,
118
119     // @private creates an array of labels to be used when rendering.
120     setLabels: function() {
121         var store = this.chart.store,
122             fields = this.fields,
123             ln = fields.length,
124             i;
125
126         this.labels = [];
127         store.each(function(record) {
128             for (i = 0; i < ln; i++) {
129                 this.labels.push(record.get(fields[i]));
130             }
131         }, this);
132     },
133
134     // @private calculates labels positions and marker positions for rendering.
135     applyData: function() {
136         this.callParent();
137         this.setLabels();
138         var count = this.chart.store.getCount();
139         return {
140             from: 0,
141             to: count,
142             power: 1,
143             step: 1,
144             steps: count - 1
145         };
146     }
147 });
148