KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > gui > chart > ZoomableChart


1 /*
2  * ZoomableChart.java of project jchart2d, a chart enriched
3  * by zoom functionality in x dimension.
4  * Copyright 2006 (C) Achim Westermann, created on 23:58:31.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA*
19  * If you modify or optimize the code in a useful way please let me know.
20  * Achim.Westermann@gmx.de
21  *
22  */

23 package info.monitorenter.gui.chart;
24
25 import info.monitorenter.gui.chart.rangepolicies.RangePolicyFixedViewport;
26 import info.monitorenter.util.Range;
27
28 import java.awt.Color JavaDoc;
29 import java.awt.Graphics JavaDoc;
30 import java.awt.Graphics2D JavaDoc;
31 import java.awt.event.MouseEvent JavaDoc;
32 import java.awt.event.MouseListener JavaDoc;
33 import java.awt.event.MouseMotionListener JavaDoc;
34 import java.awt.geom.Point2D JavaDoc;
35 import java.awt.geom.Rectangle2D JavaDoc;
36
37 /**
38  * {@link info.monitorenter.gui.chart.Chart2D} enriched by a zoom-functionality
39  * in the x dimension.
40  * <p>
41  *
42  * @author Alessio Sambarino (Contributor)
43  *
44  * @version $Revision: 1.1 $
45  */

46 public class ZoomableChart extends Chart2D implements MouseListener JavaDoc, MouseMotionListener JavaDoc {
47
48   /** Needed to set the initial zooming area to the data bounds. */
49   private boolean m_firstTime = true;
50
51   /** The starting point of the mouse drag operation (click, then move). */
52   private Point2D JavaDoc m_startPoint;
53
54   /** The area to zoom. */
55   private Rectangle2D JavaDoc m_zoomArea;
56
57   /**
58    * The maximum value of the data bounds of this chart in x dimension that is
59    * needed to reset the zooming.
60    */

61   private double m_zoomMax;
62
63   /**
64    * The minimum value of the data bounds of this chart in x dimension that is
65    * needed to reset the zooming.
66    */

67   private double m_zoomMin;
68
69   /**
70    * Defcon.
71    * <p>
72    */

73   public ZoomableChart() {
74
75     super();
76
77     addMouseListener(this);
78     addMouseMotionListener(this);
79   }
80
81   /**
82    * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
83    */

84   public void mouseClicked(final MouseEvent JavaDoc e) {
85     // nop.
86
}
87
88   /**
89    * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
90    */

91   public void mouseDragged(final MouseEvent JavaDoc e) {
92
93     if ((e.getY() < 20) || (e.getY() > this.getYChartStart())) {
94       return;
95     }
96     double startX;
97     double endX;
98     double dimX;
99     double startY = this.m_startPoint.getY();
100     double endY = getYChartStart();
101     double dimY = endY - startY;
102
103     if (e.getX() > this.m_startPoint.getX()) {
104       startX = this.m_startPoint.getX();
105       endX = e.getX();
106     } else {
107       startX = e.getX();
108       endX = this.m_startPoint.getX();
109     }
110
111     if (startX < this.getXChartStart()) {
112       startX = this.getXChartStart();
113     }
114
115     if (endX > (this.getWidth() - 20)) {
116       endX = this.getWidth() - 20;
117     }
118
119     dimX = endX - startX;
120
121     this.m_zoomArea = new Rectangle2D.Double JavaDoc(startX, startY, dimX, dimY);
122
123     repaint();
124   }
125
126   /**
127    * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
128    */

129   public void mouseEntered(final MouseEvent JavaDoc e) {
130     // nop.
131
}
132
133   /**
134    * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
135    */

136   public void mouseExited(final MouseEvent JavaDoc e) {
137     // nop.
138
}
139
140   /**
141    * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
142    */

143   public void mouseMoved(final MouseEvent JavaDoc e) {
144     // nop.
145
}
146
147   /**
148    * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
149    */

150   public void mousePressed(final MouseEvent JavaDoc e) {
151     this.m_startPoint = new Point2D.Double JavaDoc(e.getX(), 20);
152   }
153
154   /**
155    * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
156    */

157   public void mouseReleased(final MouseEvent JavaDoc e) {
158
159     if (this.m_zoomArea == null) {
160       return;
161     }
162
163     double startPx = this.m_zoomArea.getX();
164     double endPx = this.m_zoomArea.getX() + this.m_zoomArea.getWidth();
165
166     Range range = getAxisX().getRangePolicy().getRange();
167
168     double max = range.getMax();
169     double min = range.getMin();
170
171     if (max == Double.MAX_VALUE) {
172       range = getAxisX().getRange();
173       max = range.getMax();
174       min = range.getMin();
175     }
176
177     double xAxisDomain = max - min;
178     double xAxisLength = getWidth() - 20 - this.getXChartStart();
179
180     double xAxisMin = min + xAxisDomain / xAxisLength * (startPx - this.getXChartStart());
181     double xAxisMax = min + xAxisDomain / xAxisLength * (endPx - this.getXChartStart());
182
183     zoom(xAxisMin, xAxisMax);
184   }
185
186   /**
187    * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
188    */

189   public void paintComponent(final Graphics JavaDoc g) {
190
191     super.paintComponent(g);
192
193     Graphics2D JavaDoc g2 = (Graphics2D JavaDoc) g;
194
195     if (this.m_firstTime) {
196       Range range = getAxisX().getRange();
197       this.m_zoomMin = range.getMin();
198       this.m_zoomMax = range.getMax();
199       this.m_firstTime = false;
200     }
201
202     if (this.m_zoomArea != null) {
203       g2.draw(this.m_zoomArea);
204       g2.setPaint(new Color JavaDoc(255, 255, 0, 100));
205       g2.fill(this.m_zoomArea);
206     }
207   }
208
209   /**
210    * Zooms to the selected bounds.
211    * <p>
212    *
213    * @param min
214    * the lower bound.
215    *
216    * @param max
217    * the upper bound.
218    */

219   public void zoom(final double min, final double max) {
220
221     this.m_zoomArea = null;
222
223     IAxis axis = getAxisX();
224     // IRangePolicy
225
axis.setRangePolicy(new RangePolicyFixedViewport());
226     axis.setRange(new Range(min, max));
227
228   }
229
230   /**
231    * Resets the zooming area to a range that displays all data.
232    * <p>
233    */

234   public void zoomAll() {
235
236     this.getAxisX().setRangePolicy(
237         new RangePolicyFixedViewport(new Range(this.m_zoomMin, this.m_zoomMax)));
238   }
239 }
240
Popular Tags