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.PlotOrientation;
037    import org.jfree.chart.plot.XYPlot;
038    import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
039    import org.jfree.data.xy.XYDataset;
040    import org.jfree.data.xy.XYSeries;
041    import org.jfree.data.xy.XYSeriesCollection;
042    import org.jfree.ui.RectangleInsets;
043    
044    /**
045     * A JavaBean that displays a line chart.
046     */
047    public class JLineChart extends NumericalXYChart {
048        
049        /**
050         * Creates a new pie chart bean.
051         */
052        public JLineChart() {
053            super();
054        }
055        
056        /**
057         * Creates a default chart.
058         * 
059         * @return The default chart.
060         */
061        protected JFreeChart createDefaultChart() {
062            XYSeriesCollection dataset = new XYSeriesCollection();
063            XYSeries s1 = new XYSeries("Series 1");
064            s1.add(1.0, 5.0);
065            s1.add(2.0, 7.0);
066            s1.add(3.0, 3.0);
067            s1.add(4.0, 6.0);
068            dataset.addSeries(s1);
069            JFreeChart chart = ChartFactory.createXYLineChart("JLineChart - Title", 
070                    "X", "Y", dataset, PlotOrientation.VERTICAL, true, true, false);
071            XYPlot plot = (XYPlot) chart.getPlot();
072            plot.setBackgroundPaint(Color.lightGray);
073            plot.setDomainGridlinePaint(Color.white);
074            plot.setRangeGridlinePaint(Color.white);
075            plot.setAxisOffset(new RectangleInsets(4, 4, 4, 4));
076            
077            XYLineAndShapeRenderer r = (XYLineAndShapeRenderer) plot.getRenderer();
078            r.setUseFillPaint(true);
079            r.setBaseFillPaint(Color.white);
080            return chart;
081        }
082        
083        /**
084         * Returns the dataset used by the chart.
085         * 
086         * @return The dataset (possibly <code>null</code>).
087         * 
088         * @see #setDataset(XYDataset)
089         */
090        public XYDataset getDataset() {
091            XYDataset result = null;
092            XYPlot plot = (XYPlot) this.chart.getPlot();
093            if (plot != null) {
094                result = plot.getDataset();
095            }
096            return result;
097        }
098        
099        /**
100         * Sets the dataset used by the chart and sends a 
101         * {@link PropertyChangeEvent} for the <code>dataset</code> property to all
102         * registered listeners.
103         * 
104         * @param dataset  the dataset (<code>null</code> permitted).
105         * 
106         * @see #getDataset()
107         */
108        public void setDataset(XYDataset dataset) {
109            XYPlot plot = (XYPlot) this.chart.getPlot();
110            if (plot != null) {
111                XYDataset old = plot.getDataset();
112                plot.setDataset(dataset);
113                firePropertyChange("dataset", old, dataset);
114            }
115        }
116        
117        /**
118         * Returns <code>true</code> if a shape is drawn to indicate each data 
119         * item, and <code>false</code> otherwise.
120         * 
121         * @return A boolean.
122         * 
123         * @see #setShapesVisible(boolean)
124         */
125        public boolean getShapesVisible() {
126            XYPlot plot = (XYPlot) this.chart.getPlot();
127            if (plot == null) {
128                return false;
129            }
130            XYLineAndShapeRenderer r = (XYLineAndShapeRenderer) plot.getRenderer();
131            if (r == null)  {
132                return false;
133            }
134            return r.getBaseShapesVisible();
135        }
136        
137        /**
138         * Sets a flag that controls whether or not shapes are drawn to highlight
139         * each data item and sends a {@link PropertyChangeEvent} for the 
140         * <code>shapesVisible</code> property to all registered listeners.
141         * 
142         * @param visible  the new flag value.
143         * 
144         * @see #getShapesVisible()
145         */
146        public void setShapesVisible(boolean visible) {
147            XYPlot plot = (XYPlot) this.chart.getPlot();
148            if (plot == null) {
149                return;
150            }
151            XYLineAndShapeRenderer r = (XYLineAndShapeRenderer) plot.getRenderer();
152            if (r == null)  {
153                return;
154            }
155            boolean old = r.getBaseShapesVisible();
156            r.setBaseShapesVisible(visible);       
157            firePropertyChange("shapesVisible", old, visible);
158        }
159        
160    }