Upgrade to ExtJS 4.0.7 - Released 10/19/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     /**
29      * Zooms the chart to the specified selection range.
30      * Can be used with a selection mask. For example:
31      *
32      *     items: {
33      *         xtype: 'chart',
34      *         animate: true,
35      *         store: store1,
36      *         mask: 'horizontal',
37      *         listeners: {
38      *             select: {
39      *                 fn: function(me, selection) {
40      *                     me.setZoom(selection);
41      *                     me.mask.hide();
42      *                 }
43      *             }
44      *         }
45      *     }
46      */
47     setZoom: function(zoomConfig) {
48         var me = this,
49             axes = me.axes,
50             bbox = me.chartBBox,
51             xScale = 1 / bbox.width,
52             yScale = 1 / bbox.height,
53             zoomer = {
54                 x : zoomConfig.x * xScale,
55                 y : zoomConfig.y * yScale,
56                 width : zoomConfig.width * xScale,
57                 height : zoomConfig.height * yScale
58             };
59         axes.each(function(axis) {
60             var ends = axis.calcEnds();
61             if (axis.position == 'bottom' || axis.position == 'top') {
62                 var from = (ends.to - ends.from) * zoomer.x + ends.from,
63                     to = (ends.to - ends.from) * zoomer.width + from;
64                 axis.minimum = from;
65                 axis.maximum = to;
66             } else {
67                 var to = (ends.to - ends.from) * (1 - zoomer.y) + ends.from,
68                     from = to - (ends.to - ends.from) * zoomer.height;
69                 axis.minimum = from;
70                 axis.maximum = to;
71             }
72         });
73         me.redraw(false);
74     },
75
76     /**
77      * Restores the zoom to the original value. This can be used to reset
78      * the previous zoom state set by `setZoom`. For example:
79      *
80      *     myChart.restoreZoom();
81      */
82     restoreZoom: function() {
83         this.store = this.substore = this.originalStore;
84         this.redraw(true);
85     }
86
87 });
88