001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ---------------------------
028: * XYTitleAnnotationDemo1.java
029: * ---------------------------
030: * (C) Copyright 2007, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): ;
034: *
035: * $Id: XYTitleAnnotationDemo1.java,v 1.1.2.2 2007/05/25 10:37:10 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 02-Feb-2007 : Version 1 (DG);
040: *
041: */
042:
043: package org.jfree.experimental.chart.demo;
044:
045: import java.awt.Color;
046: import java.awt.Font;
047: import java.text.SimpleDateFormat;
048:
049: import javax.swing.JPanel;
050:
051: import org.jfree.chart.ChartFactory;
052: import org.jfree.chart.ChartPanel;
053: import org.jfree.chart.JFreeChart;
054: import org.jfree.chart.axis.DateAxis;
055: import org.jfree.chart.axis.ValueAxis;
056: import org.jfree.chart.block.BlockBorder;
057: import org.jfree.chart.plot.XYPlot;
058: import org.jfree.chart.renderer.xy.XYItemRenderer;
059: import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
060: import org.jfree.chart.title.LegendTitle;
061: import org.jfree.data.time.Month;
062: import org.jfree.data.time.TimeSeries;
063: import org.jfree.data.time.TimeSeriesCollection;
064: import org.jfree.data.xy.XYDataset;
065: import org.jfree.experimental.chart.annotations.XYTitleAnnotation;
066: import org.jfree.ui.ApplicationFrame;
067: import org.jfree.ui.RectangleAnchor;
068: import org.jfree.ui.RectangleEdge;
069: import org.jfree.ui.RectangleInsets;
070: import org.jfree.ui.RefineryUtilities;
071:
072: /**
073: * An example of a time series chart. For the most part, default settings are
074: * used, except that the renderer is modified to show filled shapes (as well as
075: * lines) at each data point.
076: */
077: public class XYTitleAnnotationDemo1 extends ApplicationFrame {
078:
079: /**
080: * A demonstration application showing how to create a simple time series
081: * chart. This example uses monthly data.
082: *
083: * @param title the frame title.
084: */
085: public XYTitleAnnotationDemo1(String title) {
086: super (title);
087: ChartPanel chartPanel = (ChartPanel) createDemoPanel();
088: chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
089: chartPanel.setMouseZoomable(true, false);
090: setContentPane(chartPanel);
091: }
092:
093: /**
094: * Creates a chart.
095: *
096: * @param dataset a dataset.
097: *
098: * @return A chart.
099: */
100: private static JFreeChart createChart(XYDataset dataset) {
101:
102: JFreeChart chart = ChartFactory.createTimeSeriesChart(
103: "Legal & General Unit Trust Prices", // title
104: "Date", // x-axis label
105: "Price Per Unit", // y-axis label
106: dataset, // data
107: false, // create legend?
108: true, // generate tooltips?
109: false // generate URLs?
110: );
111:
112: chart.setBackgroundPaint(Color.white);
113:
114: XYPlot plot = (XYPlot) chart.getPlot();
115: plot.setBackgroundPaint(Color.lightGray);
116: plot.setDomainGridlinePaint(Color.white);
117: plot.setRangeGridlinePaint(Color.white);
118: plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
119: plot.setDomainCrosshairVisible(true);
120: plot.setRangeCrosshairVisible(true);
121:
122: LegendTitle lt = new LegendTitle(plot);
123: lt.setItemFont(new Font("Dialog", Font.PLAIN, 9));
124: lt.setBackgroundPaint(new Color(200, 200, 255, 100));
125: lt.setFrame(new BlockBorder(Color.white));
126: lt.setPosition(RectangleEdge.BOTTOM);
127: XYTitleAnnotation ta = new XYTitleAnnotation(0.98, 0.02, lt,
128: RectangleAnchor.BOTTOM_RIGHT);
129:
130: ta.setMaxWidth(0.48);
131: plot.addAnnotation(ta);
132:
133: XYItemRenderer r = plot.getRenderer();
134: if (r instanceof XYLineAndShapeRenderer) {
135: XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
136: renderer.setBaseShapesVisible(true);
137: renderer.setBaseShapesFilled(true);
138: }
139:
140: DateAxis axis = (DateAxis) plot.getDomainAxis();
141: axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
142:
143: ValueAxis yAxis = plot.getRangeAxis();
144: yAxis.setLowerMargin(0.35);
145: return chart;
146:
147: }
148:
149: /**
150: * Creates a dataset, consisting of two series of monthly data.
151: *
152: * @return The dataset.
153: */
154: private static XYDataset createDataset() {
155:
156: TimeSeries s1 = new TimeSeries("L&G European Index Trust",
157: Month.class);
158: s1.add(new Month(2, 2001), 181.8);
159: s1.add(new Month(3, 2001), 167.3);
160: s1.add(new Month(4, 2001), 153.8);
161: s1.add(new Month(5, 2001), 167.6);
162: s1.add(new Month(6, 2001), 158.8);
163: s1.add(new Month(7, 2001), 148.3);
164: s1.add(new Month(8, 2001), 153.9);
165: s1.add(new Month(9, 2001), 142.7);
166: s1.add(new Month(10, 2001), 123.2);
167: s1.add(new Month(11, 2001), 131.8);
168: s1.add(new Month(12, 2001), 139.6);
169: s1.add(new Month(1, 2002), 142.9);
170: s1.add(new Month(2, 2002), 138.7);
171: s1.add(new Month(3, 2002), 137.3);
172: s1.add(new Month(4, 2002), 143.9);
173: s1.add(new Month(5, 2002), 139.8);
174: s1.add(new Month(6, 2002), 137.0);
175: s1.add(new Month(7, 2002), 132.8);
176:
177: TimeSeries s2 = new TimeSeries("L&G UK Index Trust",
178: Month.class);
179: s2.add(new Month(2, 2001), 129.6);
180: s2.add(new Month(3, 2001), 123.2);
181: s2.add(new Month(4, 2001), 117.2);
182: s2.add(new Month(5, 2001), 124.1);
183: s2.add(new Month(6, 2001), 122.6);
184: s2.add(new Month(7, 2001), 119.2);
185: s2.add(new Month(8, 2001), 116.5);
186: s2.add(new Month(9, 2001), 112.7);
187: s2.add(new Month(10, 2001), 101.5);
188: s2.add(new Month(11, 2001), 106.1);
189: s2.add(new Month(12, 2001), 110.3);
190: s2.add(new Month(1, 2002), 111.7);
191: s2.add(new Month(2, 2002), 111.0);
192: s2.add(new Month(3, 2002), 109.6);
193: s2.add(new Month(4, 2002), 113.2);
194: s2.add(new Month(5, 2002), 111.6);
195: s2.add(new Month(6, 2002), 108.8);
196: s2.add(new Month(7, 2002), 101.6);
197:
198: TimeSeriesCollection dataset = new TimeSeriesCollection();
199: dataset.addSeries(s1);
200: dataset.addSeries(s2);
201:
202: return dataset;
203:
204: }
205:
206: /**
207: * Creates a panel for the demo (used by SuperDemo.java).
208: *
209: * @return A panel.
210: */
211: public static JPanel createDemoPanel() {
212: JFreeChart chart = createChart(createDataset());
213: return new ChartPanel(chart);
214: }
215:
216: /**
217: * Starting point for the demonstration application.
218: *
219: * @param args ignored.
220: */
221: public static void main(String[] args) {
222:
223: XYTitleAnnotationDemo1 demo = new XYTitleAnnotationDemo1(
224: "XYTitleAnnotationDemo1");
225: demo.pack();
226: RefineryUtilities.centerFrameOnScreen(demo);
227: demo.setVisible(true);
228:
229: }
230:
231: }
|