Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / chart / axis / Time.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.Time
17  * @extends Ext.chart.axis.Numeric
18  *
19  * A type of axis whose units are measured in time values. Use this axis
20  * for listing dates that you will want to group or dynamically change.
21  * If you just want to display dates as categories then use the
22  * Category class for axis instead.
23  *
24  * For example:
25  *
26  *     axes: [{
27  *         type: 'Time',
28  *         position: 'bottom',
29  *         fields: 'date',
30  *         title: 'Day',
31  *         dateFormat: 'M d',
32  *
33  *         constrain: true,
34  *         fromDate: new Date('1/1/11'),
35  *         toDate: new Date('1/7/11')
36  *     }]
37  *
38  * In this example we're creating a time axis that has as title *Day*.
39  * The field the axis is bound to is `date`.
40  * The date format to use to display the text for the axis labels is `M d`
41  * which is a three letter month abbreviation followed by the day number.
42  * The time axis will show values for dates between `fromDate` and `toDate`.
43  * Since `constrain` is set to true all other values for other dates not between
44  * the fromDate and toDate will not be displayed.
45  *
46  */
47 Ext.define('Ext.chart.axis.Time', {
48
49     /* Begin Definitions */
50
51     extend: 'Ext.chart.axis.Numeric',
52
53     alternateClassName: 'Ext.chart.TimeAxis',
54
55     alias: 'axis.time',
56
57     requires: ['Ext.data.Store', 'Ext.data.JsonStore'],
58
59     /* End Definitions */
60
61     /**
62      * @cfg {String/Boolean} dateFormat
63      * Indicates the format the date will be rendered on.
64      * For example: 'M d' will render the dates as 'Jan 30', etc.
65      * For a list of possible format strings see {@link Ext.Date Date}
66      */
67     dateFormat: false,
68
69     /**
70      * @cfg {Date} fromDate The starting date for the time axis.
71      */
72     fromDate: false,
73
74     /**
75      * @cfg {Date} toDate The ending date for the time axis.
76      */
77     toDate: false,
78
79     /**
80      * @cfg {Array/Boolean} step
81      * An array with two components: The first is the unit of the step (day, month, year, etc).
82      * The second one is the number of units for the step (1, 2, etc.).
83      * Defaults to `[Ext.Date.DAY, 1]`.
84      */
85     step: [Ext.Date.DAY, 1],
86     
87     /**
88      * @cfg {Boolean} constrain
89      * If true, the values of the chart will be rendered only if they belong between the fromDate and toDate.
90      * If false, the time axis will adapt to the new values by adding/removing steps.
91      */
92     constrain: false,
93
94     // Avoid roundtoDecimal call in Numeric Axis's constructor
95     roundToDecimal: false,
96     
97     constructor: function (config) {
98         var me = this, label, f, df;
99         me.callParent([config]);
100         label = me.label || {};
101         df = this.dateFormat;
102         if (df) {
103             if (label.renderer) {
104                 f = label.renderer;
105                 label.renderer = function(v) {
106                     v = f(v);
107                     return Ext.Date.format(new Date(f(v)), df);
108                 };
109             } else {
110                 label.renderer = function(v) {
111                     return Ext.Date.format(new Date(v >> 0), df);
112                 };
113             }
114         }
115     },
116
117     doConstrain: function () {
118         var me = this,
119             store = me.chart.store,
120             data = [],
121             series = me.chart.series.items,
122             math = Math,
123             mmax = math.max,
124             mmin = math.min,
125             fields = me.fields,
126             ln = fields.length,
127             range = me.getRange(),
128             min = range.min, max = range.max, i, l, excludes = [],
129             value, values, rec, data = [];
130         for (i = 0, l = series.length; i < l; i++) {
131             excludes[i] = series[i].__excludes;
132         }
133         store.each(function(record) {
134             for (i = 0; i < ln; i++) {
135                 if (excludes[i]) {
136                     continue;
137                 }
138                 value = record.get(fields[i]);
139                 if (+value < +min) return;
140                 if (+value > +max) return;
141             }
142             data.push(record);
143         })
144         me.chart.substore = Ext.create('Ext.data.JsonStore', { model: store.model, data: data });
145     },
146
147     // Before rendering, set current default step count to be number of records.
148     processView: function () {
149         var me = this;
150         if (me.fromDate) {
151             me.minimum = +me.fromDate;
152         }
153         if (me.toDate) {
154             me.maximum = +me.toDate;
155         }
156         if (me.constrain) {
157             me.doConstrain();
158         }
159      },
160
161     // @private modifies the store and creates the labels for the axes.
162     calcEnds: function() {
163         var me = this, range, step = me.step;
164         if (step) {
165             range = me.getRange();
166             range = Ext.draw.Draw.snapEndsByDateAndStep(new Date(range.min), new Date(range.max), Ext.isNumber(step) ? [Date.MILLI, step]: step);
167             if (me.minimum) {
168                 range.from = me.minimum;
169             }
170             if (me.maximum) {
171                 range.to = me.maximum;
172             }
173             range.step = (range.to - range.from) / range.steps;
174             return range;
175         } else {
176             return me.callParent(arguments);
177         }
178     }
179  });
180
181