Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / chart / Navigation.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.Navigation
17  *
18  * Handles panning and zooming capabilities.
19  * 
20  * Used as mixin by Ext.chart.Chart.
21  */
22 Ext.define('Ext.chart.Navigation', {
23
24     constructor: function() {
25         this.originalStore = this.store;
26     },
27     
28     //filters the store to the specified interval(s)
29     setZoom: function(zoomConfig) {
30         var me = this,
31             store = me.substore || me.store,
32             bbox = me.chartBBox,
33             len = store.getCount(),
34             from = (zoomConfig.x / bbox.width * len) >> 0,
35             to = Math.ceil(((zoomConfig.x + zoomConfig.width) / bbox.width * len)),
36             recFieldsLen, recFields = [], curField, json = [], obj;
37         
38         store.each(function(rec, i) {
39             if (i < from || i > to) {
40                 return;
41             }
42             obj = {};
43             //get all record field names in a simple array
44             if (!recFields.length) {
45                 rec.fields.each(function(f) {
46                     recFields.push(f.name);
47                 });
48                 recFieldsLen = recFields.length;
49             }
50             //append record values to an aggregation record
51             for (i = 0; i < recFieldsLen; i++) {
52                 curField = recFields[i];
53                 obj[curField] = rec.get(curField);
54             }
55             json.push(obj);
56         });
57         me.store = me.substore = Ext.create('Ext.data.JsonStore', {
58             fields: recFields,
59             data: json
60         });
61         me.redraw(true);
62     },
63
64     restoreZoom: function() {
65         this.store = this.substore = this.originalStore;
66         this.redraw(true);
67     }
68     
69 });