001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2014, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * ------------------------- 028 * TimeSeriesChartDemo1.java 029 * ------------------------- 030 * (C) Copyright 2003-2014, by Object Refinery Limited and Contributors. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): ; 034 * 035 * Changes 036 * ------- 037 * 09-Mar-2005 : Version 1, copied from the demo collection that ships with 038 * the JFreeChart Developer Guide (DG); 039 * 040 */ 041 042package org.jfree.chart.demo; 043 044import java.awt.Color; 045import java.text.SimpleDateFormat; 046 047import javax.swing.JPanel; 048 049import org.jfree.chart.ChartFactory; 050import org.jfree.chart.ChartPanel; 051import org.jfree.chart.JFreeChart; 052import org.jfree.chart.StandardChartTheme; 053import org.jfree.chart.axis.DateAxis; 054import org.jfree.chart.plot.XYPlot; 055import org.jfree.chart.renderer.xy.XYItemRenderer; 056import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; 057import org.jfree.data.time.Month; 058import org.jfree.data.time.TimeSeries; 059import org.jfree.data.time.TimeSeriesCollection; 060import org.jfree.data.xy.XYDataset; 061import org.jfree.ui.ApplicationFrame; 062import org.jfree.ui.RectangleInsets; 063import org.jfree.ui.RefineryUtilities; 064 065/** 066 * An example of a time series chart. For the most part, default settings are 067 * used, except that the renderer is modified to show filled shapes (as well as 068 * lines) at each data point. 069 */ 070public class TimeSeriesChartDemo1 extends ApplicationFrame { 071 072 private static final long serialVersionUID = 1L; 073 074 static { 075 // set a theme using the new shadow generator feature available in 076 // 1.0.14 - for backwards compatibility it is not enabled by default 077 ChartFactory.setChartTheme(new StandardChartTheme("JFree/Shadow", 078 true)); 079 } 080 081 /** 082 * A demonstration application showing how to create a simple time series 083 * chart. This example uses monthly data. 084 * 085 * @param title the frame title. 086 */ 087 public TimeSeriesChartDemo1(String title) { 088 super(title); 089 ChartPanel chartPanel = (ChartPanel) createDemoPanel(); 090 chartPanel.setPreferredSize(new java.awt.Dimension(500, 270)); 091 setContentPane(chartPanel); 092 } 093 094 /** 095 * Creates a chart. 096 * 097 * @param dataset a dataset. 098 * 099 * @return A chart. 100 */ 101 private static JFreeChart createChart(XYDataset dataset) { 102 103 JFreeChart chart = ChartFactory.createTimeSeriesChart( 104 "Legal & General Unit Trust Prices", // title 105 "Date", // x-axis label 106 "Price Per Unit", // y-axis label 107 dataset, // data 108 true, // create legend? 109 true, // generate tooltips? 110 false // generate URLs? 111 ); 112 113 chart.setBackgroundPaint(Color.white); 114 115 XYPlot plot = (XYPlot) chart.getPlot(); 116 plot.setBackgroundPaint(Color.lightGray); 117 plot.setDomainGridlinePaint(Color.white); 118 plot.setRangeGridlinePaint(Color.white); 119 plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); 120 plot.setDomainCrosshairVisible(true); 121 plot.setRangeCrosshairVisible(true); 122 123 XYItemRenderer r = plot.getRenderer(); 124 if (r instanceof XYLineAndShapeRenderer) { 125 XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r; 126 renderer.setBaseShapesVisible(true); 127 renderer.setBaseShapesFilled(true); 128 renderer.setDrawSeriesLineAsPath(true); 129 } 130 131 DateAxis axis = (DateAxis) plot.getDomainAxis(); 132 axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy")); 133 134 return chart; 135 136 } 137 138 /** 139 * Creates a dataset, consisting of two series of monthly data. 140 * 141 * @return The dataset. 142 */ 143 private static XYDataset createDataset() { 144 145 TimeSeries s1 = new TimeSeries("L&G European Index Trust"); 146 s1.add(new Month(2, 2001), 181.8); 147 s1.add(new Month(3, 2001), 167.3); 148 s1.add(new Month(4, 2001), 153.8); 149 s1.add(new Month(5, 2001), 167.6); 150 s1.add(new Month(6, 2001), 158.8); 151 s1.add(new Month(7, 2001), 148.3); 152 s1.add(new Month(8, 2001), 153.9); 153 s1.add(new Month(9, 2001), 142.7); 154 s1.add(new Month(10, 2001), 123.2); 155 s1.add(new Month(11, 2001), 131.8); 156 s1.add(new Month(12, 2001), 139.6); 157 s1.add(new Month(1, 2002), 142.9); 158 s1.add(new Month(2, 2002), 138.7); 159 s1.add(new Month(3, 2002), 137.3); 160 s1.add(new Month(4, 2002), 143.9); 161 s1.add(new Month(5, 2002), 139.8); 162 s1.add(new Month(6, 2002), 137.0); 163 s1.add(new Month(7, 2002), 132.8); 164 165 TimeSeries s2 = new TimeSeries("L&G UK Index Trust"); 166 s2.add(new Month(2, 2001), 129.6); 167 s2.add(new Month(3, 2001), 123.2); 168 s2.add(new Month(4, 2001), 117.2); 169 s2.add(new Month(5, 2001), 124.1); 170 s2.add(new Month(6, 2001), 122.6); 171 s2.add(new Month(7, 2001), 119.2); 172 s2.add(new Month(8, 2001), 116.5); 173 s2.add(new Month(9, 2001), 112.7); 174 s2.add(new Month(10, 2001), 101.5); 175 s2.add(new Month(11, 2001), 106.1); 176 s2.add(new Month(12, 2001), 110.3); 177 s2.add(new Month(1, 2002), 111.7); 178 s2.add(new Month(2, 2002), 111.0); 179 s2.add(new Month(3, 2002), 109.6); 180 s2.add(new Month(4, 2002), 113.2); 181 s2.add(new Month(5, 2002), 111.6); 182 s2.add(new Month(6, 2002), 108.8); 183 s2.add(new Month(7, 2002), 101.6); 184 185 // ****************************************************************** 186 // More than 150 demo applications are included with the JFreeChart 187 // Developer Guide...for more information, see: 188 // 189 // > http://www.object-refinery.com/jfreechart/guide.html 190 // 191 // ****************************************************************** 192 193 TimeSeriesCollection dataset = new TimeSeriesCollection(); 194 dataset.addSeries(s1); 195 dataset.addSeries(s2); 196 197 return dataset; 198 199 } 200 201 /** 202 * Creates a panel for the demo (used by SuperDemo.java). 203 * 204 * @return A panel. 205 */ 206 public static JPanel createDemoPanel() { 207 JFreeChart chart = createChart(createDataset()); 208 ChartPanel panel = new ChartPanel(chart); 209 panel.setFillZoomRectangle(true); 210 panel.setMouseWheelEnabled(true); 211 return panel; 212 } 213 214 /** 215 * Starting point for the demonstration application. 216 * 217 * @param args ignored. 218 */ 219 public static void main(String[] args) { 220 221 TimeSeriesChartDemo1 demo = new TimeSeriesChartDemo1( 222 "Time Series Chart Demo 1"); 223 demo.pack(); 224 RefineryUtilities.centerFrameOnScreen(demo); 225 demo.setVisible(true); 226 227 } 228 229}