001 /* ======================================================
002 * Orson : a free chart beans library based on JFreeChart
003 * ======================================================
004 *
005 * (C) Copyright 2007, by Object Refinery Limited.
006 *
007 * Project Info: not-yet-released
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
029 package org.jfree.beans;
030
031 import java.awt.Color;
032 import java.beans.PropertyChangeEvent;
033
034 import org.jfree.chart.ChartFactory;
035 import org.jfree.chart.JFreeChart;
036 import org.jfree.chart.plot.XYPlot;
037 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
038 import org.jfree.data.time.Day;
039 import org.jfree.data.time.RegularTimePeriod;
040 import org.jfree.data.time.TimeSeries;
041 import org.jfree.data.time.TimeSeriesCollection;
042 import org.jfree.data.xy.XYDataset;
043 import org.jfree.ui.RectangleInsets;
044
045 /**
046 * A JavaBean that displays a time series chart.
047 */
048 public class JTimeSeriesChart extends AbstractXYChart {
049
050 /**
051 * Creates a new pie chart bean.
052 */
053 public JTimeSeriesChart() {
054 super();
055 }
056
057 /**
058 * Creates a default chart.
059 *
060 * @return The default chart.
061 */
062 protected JFreeChart createDefaultChart() {
063 TimeSeriesCollection dataset = new TimeSeriesCollection();
064 TimeSeries s1 = new TimeSeries("Series 1");
065 RegularTimePeriod t = new Day();
066 double y = 100.0;
067 for (int i = 0; i < 20; i++) {
068 s1.add(t, y); t = t.next();
069 y = y * (1.0 + (Math.random() / 45.0));
070 }
071 dataset.addSeries(s1);
072 JFreeChart chart = ChartFactory.createTimeSeriesChart(
073 "JTimeSeriesChart - Title", "Date/Time", "Value", dataset, true,
074 true, false);
075 XYPlot plot = (XYPlot) chart.getPlot();
076 plot.setBackgroundPaint(Color.lightGray);
077 plot.setDomainGridlinePaint(Color.white);
078 plot.setRangeGridlinePaint(Color.white);
079 plot.setAxisOffset(new RectangleInsets(4, 4, 4, 4));
080 XYLineAndShapeRenderer r = (XYLineAndShapeRenderer) plot.getRenderer();
081 r.setUseFillPaint(true);
082 r.setBaseFillPaint(Color.white);
083 return chart;
084 }
085
086 /**
087 * Returns the dataset used by the chart.
088 *
089 * @return The dataset (possibly <code>null</code>).
090 *
091 * @see #setDataset(XYDataset)
092 */
093 public XYDataset getDataset() {
094 XYDataset result = null;
095 XYPlot plot = (XYPlot) this.chart.getPlot();
096 if (plot != null) {
097 result = plot.getDataset();
098 }
099 return result;
100 }
101
102 /**
103 * Sets the dataset used by the chart and fires a
104 * {@link PropertyChangeEvent} for the <code>dataset</code> property.
105 *
106 * @param dataset the dataset (<code>null</code> permitted).
107 *
108 * @see #getDataset()
109 */
110 public void setDataset(XYDataset dataset) {
111 XYPlot plot = (XYPlot) this.chart.getPlot();
112 if (plot != null) {
113 XYDataset old = plot.getDataset();
114 plot.setDataset(dataset);
115 firePropertyChange("dataset", old, dataset);
116 }
117 }
118
119 /**
120 * Returns <code>true</code> if a shape is drawn to indicate each data
121 * item, and <code>false</code> otherwise.
122 *
123 * @return A boolean.
124 *
125 * @see #setShapesVisible(boolean)
126 */
127 public boolean getShapesVisible() {
128 XYPlot plot = (XYPlot) this.chart.getPlot();
129 if (plot == null) {
130 return false;
131 }
132 XYLineAndShapeRenderer r = (XYLineAndShapeRenderer) plot.getRenderer();
133 if (r == null) {
134 return false;
135 }
136 return r.getBaseShapesVisible();
137 }
138
139 /**
140 * Sets a flag that controls whether or not shapes are drawn to highlight
141 * each data item and fires a {@link PropertyChangeEvent} for the
142 * <code>shapesVisible</code> property.
143 *
144 * @param visible the new flag value.
145 *
146 * @see #getShapesVisible()
147 */
148 public void setShapesVisible(boolean visible) {
149 XYPlot plot = (XYPlot) this.chart.getPlot();
150 if (plot == null) {
151 return;
152 }
153 XYLineAndShapeRenderer r = (XYLineAndShapeRenderer) plot.getRenderer();
154 if (r == null) {
155 return;
156 }
157 boolean old = r.getBaseShapesVisible();
158 r.setBaseShapesVisible(visible);
159 firePropertyChange("shapesVisible", old, visible);
160 }
161
162 }