KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > plot > PeriodMarkerPlot


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ---------------------
23  * PeriodMarkerPlot.java
24  * ---------------------
25  * (C) Copyright 2002, by Sylvain Vieujot and Contributors.
26  *
27  * Original Author: Sylvain Vieujot;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  * Arnaud Lelievre;
30  *
31  * $Id: PeriodMarkerPlot.java,v 1.12 2003/10/30 16:49:39 mungady Exp $
32  *
33  * Changes
34  * -------
35  * 08-Jan-2002 : Version 1, thanks to SV. Added parameter for tooltips so that the code will
36  * compile in the current development version - tooltips ignored at this point (DG);
37  * 09-Apr-2002 : Removed empty version tag to eliminate Javadoc warning, and updated import
38  * statements (DG);
39  * 23-Apr-2002 : Moved dataset from JFreeChart to Plot (DG);
40  * 13-Jun-2002 : Removed commented out code (DG);
41  * 25-Jun-2002 : Removed redundant import (DG);
42  * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
43  * 08-Sep-2003 : Added internationalization via use of properties resourceBundle (RFE 690236) (AL);
44  *
45  */

46
47 package org.jfree.chart.plot;
48
49 import java.awt.AlphaComposite JavaDoc;
50 import java.awt.Color JavaDoc;
51 import java.awt.Composite JavaDoc;
52 import java.awt.Graphics2D JavaDoc;
53 import java.awt.Insets JavaDoc;
54 import java.awt.Paint JavaDoc;
55 import java.awt.Shape JavaDoc;
56 import java.awt.geom.Rectangle2D JavaDoc;
57 import java.util.ResourceBundle JavaDoc;
58
59 import org.jfree.chart.ChartRenderingInfo;
60 import org.jfree.chart.LegendItemCollection;
61 import org.jfree.chart.axis.AxisSpace;
62 import org.jfree.chart.axis.ValueAxis;
63 import org.jfree.chart.renderer.AbstractRenderer;
64 import org.jfree.data.XYDataset;
65
66 /**
67  * A plot that marks time periods, for use in overlaid plots.
68  *
69  * @author sylvain
70  */

71 public class PeriodMarkerPlot extends XYPlot implements ValueAxisPlot {
72
73     /** The resourceBundle for the localization. */
74     static protected ResourceBundle JavaDoc localizationResources =
75                             ResourceBundle.getBundle("org.jfree.chart.plot.LocalizationBundle");
76
77     /**
78      * Creates a new period marker plot.
79      *
80      * @param data the data series.
81      * @param domainAxis the domain axis.
82      * @param rangeAxis the range axis.
83      */

84     public PeriodMarkerPlot(XYDataset data, ValueAxis domainAxis, ValueAxis rangeAxis) {
85         super(data, domainAxis, rangeAxis, null);
86     }
87
88     /**
89      * Returns the legend items (null for this plot).
90      *
91      * @return the legend items.
92      */

93     public LegendItemCollection getLegendItems() {
94         return null;
95     }
96
97     /**
98      * Returns the plot type as a string.
99      *
100      * @return <i>Period Marker Plot</i>.
101      */

102     public String JavaDoc getPlotType() {
103           return localizationResources.getString("Period_Marker_Plot");
104     }
105
106     /**
107      * A convenience method that returns the dataset for the plot, cast as an XYDataset.
108      *
109      * @return a dataset.
110      */

111     public XYDataset getTempXYDataset() {
112         // Usefull until SignalsDataset is included in jcommon.SubSeriesDataset
113
return getDataset();
114     }
115
116     /**
117      * Draws the plot on a Java 2D graphics device (such as the screen or a printer).
118      *
119      * @param g2 the graphics device.
120      * @param plotArea the area within which the plot should be drawn.
121      * @param info an optional info collection object to return data back to the caller.
122      */

123     public void draw(Graphics2D JavaDoc g2, Rectangle2D JavaDoc plotArea, ChartRenderingInfo info) {
124
125         Insets JavaDoc insets = getInsets();
126         if (insets != null) {
127             plotArea = new Rectangle2D.Double JavaDoc(plotArea.getX() + insets.left,
128                 plotArea.getY() + insets.top,
129                 plotArea.getWidth() - insets.left - insets.right,
130                 plotArea.getHeight() - insets.top - insets.bottom);
131         }
132
133         AxisSpace space = new AxisSpace();
134         ValueAxis domainAxis = getDomainAxis();
135         ValueAxis rangeAxis = getRangeAxis();
136         space = domainAxis.reserveSpace(g2, this, plotArea, getDomainAxisEdge(), space);
137         space = rangeAxis.reserveSpace(g2, this, plotArea, getRangeAxisEdge(), space);
138         Rectangle2D JavaDoc dataArea = space.shrink(plotArea, null);
139
140         drawBackground(g2, dataArea);
141
142         // draw the axes
143
double cursor = 0.0;
144         getDomainAxis().draw(g2, cursor, plotArea, dataArea, getDomainAxisEdge());
145         getRangeAxis().draw(g2, cursor, plotArea, dataArea, getRangeAxisEdge());
146
147         Shape JavaDoc originalClip = g2.getClip();
148         g2.clip(dataArea);
149
150         //SignalsDataset data = getDataset();
151
XYDataset data = getTempXYDataset();
152         if (data != null) {
153             int seriesCount = data.getSeriesCount();
154             for (int serie = 0; serie < seriesCount; serie++) {
155                 // area should be remaining area only
156
drawMarkedPeriods(data, serie, g2, dataArea);
157             }
158         }
159
160         drawOutline(g2, dataArea);
161         g2.setClip(originalClip);
162     }
163
164     /**
165      * Draws the marked periods.
166      *
167      * @param data the dataset.
168      * @param serie the series.
169      * @param g2 the graphics device.
170      * @param plotArea the plot area.
171      */

172     private void drawMarkedPeriods(XYDataset data, int serie, Graphics2D JavaDoc g2, Rectangle2D JavaDoc plotArea) {
173
174         Paint JavaDoc thisSeriePaint = Color.gray; // fix later //getFillPaint(0, serie);
175
g2.setPaint(thisSeriePaint);
176         g2.setStroke(AbstractRenderer.DEFAULT_STROKE); // fix later getSeriesStroke(serie));
177

178         float opacity = 0.1f;
179         if (thisSeriePaint instanceof Color JavaDoc) {
180             Color JavaDoc thisSerieColor = (Color JavaDoc) thisSeriePaint;
181             int colorSaturation = thisSerieColor.getRed()
182                                   + thisSerieColor.getGreen() + thisSerieColor.getBlue();
183             if (colorSaturation > 255) {
184                 opacity = opacity * colorSaturation / 255.0f;
185             }
186         }
187         Composite JavaDoc originalComposite = g2.getComposite();
188         g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
189
190         double minY = plotArea.getMinY();
191         double maxY = plotArea.getMaxY();
192
193         int itemCount = data.getItemCount(serie);
194         for (int itemIndex = 0; itemIndex < itemCount; itemIndex++) {
195             if (data.getYValue(serie, itemIndex).doubleValue() == 0) {
196                 // un -marked period
197
continue;
198             }
199             Number JavaDoc xStart;
200             if (itemIndex > 0) {
201                 xStart = new Long JavaDoc((data.getXValue(serie, itemIndex).longValue()
202                                    + data.getXValue(serie, itemIndex - 1).longValue()) / 2);
203             }
204             else {
205                 xStart = data.getXValue(serie, itemIndex);
206             }
207             int j = itemIndex + 1;
208             while (j < itemCount) {
209                 if (data.getYValue(serie, j).doubleValue() == 0) {
210                     break;
211                 }
212                 j++;
213             }
214             itemIndex = j;
215             Number JavaDoc xEnd;
216             if (j < itemCount) {
217                 xEnd = new Long JavaDoc((data.getXValue(serie, j - 1).longValue()
218                                 + data.getXValue(serie, j).longValue()) / 2);
219             }
220             else {
221                 xEnd = data.getXValue(serie, j - 1);
222             }
223
224             double xxStart = getDomainAxis().translateValueToJava2D(xStart.doubleValue(), plotArea,
225                                      getDomainAxisEdge());
226             double xxEnd = getDomainAxis().translateValueToJava2D(xEnd.doubleValue(), plotArea,
227                                     getDomainAxisEdge());
228
229             markPeriod(xxStart, xxEnd, minY, maxY, g2);
230         }
231
232         g2.setComposite(originalComposite);
233
234     }
235
236     /**
237      * Marks a single period.
238      *
239      * @param xStart the start.
240      * @param xEnd the end.
241      * @param minY the minimum y-value.
242      * @param maxY the maximum y-value.
243      * @param g2 the graphics device.
244      */

245     private void markPeriod(double xStart, double xEnd, double minY, double maxY, Graphics2D JavaDoc g2) {
246         g2.fill(new Rectangle2D.Double JavaDoc(xStart, minY, xEnd - xStart, maxY - minY));
247     }
248
249     /**
250      * A zoom method that does nothing. TO BE DONE.
251      *
252      * @param percent the zoom percentage.
253      */

254     public void zoom(double percent) {
255     }
256
257 }
258
Popular Tags