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.axis.CategoryAxis;
037    import org.jfree.chart.plot.CategoryPlot;
038    import org.jfree.chart.plot.PlotOrientation;
039    import org.jfree.chart.renderer.category.BarRenderer;
040    import org.jfree.data.category.CategoryDataset;
041    import org.jfree.data.category.DefaultCategoryDataset;
042    import org.jfree.ui.RectangleInsets;
043    
044    /**
045     * A JavaBean that displays a bar chart.
046     */
047    public class JBarChart extends AbstractCategoryChart {
048       
049        /**
050         * Creates a new bar chart bean.
051         */
052        public JBarChart() {
053            super();
054        }
055    
056        /**
057         * Creates a default chart.
058         * 
059         * @return The default chart.
060         */
061        protected JFreeChart createDefaultChart() {
062            DefaultCategoryDataset dataset = new DefaultCategoryDataset();
063            dataset.setValue(5.0, "Series 1", "Category A");
064            dataset.setValue(6.0, "Series 1", "Category B");
065            dataset.setValue(7.0, "Series 2", "Category A");
066            dataset.setValue(4.0, "Series 2", "Category B");
067            dataset.setValue(3.0, "Series 3", "Category A");
068            dataset.setValue(9.0, "Series 3", "Category B");
069            JFreeChart chart = ChartFactory.createBarChart("JBarChart - Title", 
070                    "Category", "Value", dataset, PlotOrientation.VERTICAL, true, 
071                    true, false);
072            CategoryPlot plot = (CategoryPlot) chart.getPlot();
073            plot.setBackgroundPaint(Color.lightGray);
074            plot.setDomainGridlinePaint(Color.white);
075            plot.setRangeGridlinePaint(Color.white);
076            plot.setAxisOffset(new RectangleInsets(4, 4, 4, 4));
077            
078            CategoryAxis axis = plot.getDomainAxis();
079            axis.setCategoryMargin(0.05);
080            
081            BarRenderer renderer = (BarRenderer) plot.getRenderer();
082            renderer.setItemMargin(0.0);
083            renderer.setDrawBarOutline(false);
084            return chart;
085        }
086        
087        /**
088         * Returns the dataset used by the chart.
089         * 
090         * @return The dataset (possibly <code>null</code>).
091         * 
092         * @see #setDataset(CategoryDataset)
093         */
094        public CategoryDataset getDataset() {
095            CategoryDataset result = null;
096            CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
097            if (plot != null) {
098                result = plot.getDataset();
099            }
100            return result;
101        }
102        
103        /**
104         * Sets the dataset used by the chart and fires a 
105         * {@link PropertyChangeEvent} for the <code>dataset</code> property.
106         * 
107         * @param dataset  the dataset (<code>null</code> permitted).
108         * 
109         * @see #getDataset()
110         */
111        public void setDataset(CategoryDataset dataset) {
112            CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
113            if (plot != null) {
114                CategoryDataset old = plot.getDataset();
115                plot.setDataset(dataset);
116                firePropertyChange("dataset", old, dataset);
117            }
118        }
119        
120        /**
121         * Returns the flag that controls whether or not the bar outlines are
122         * drawn.
123         * 
124         * @return A boolean.
125         * 
126         * @see #setBarOutlineVisible(boolean)
127         */
128        public boolean isBarOutlineVisible() {
129            CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
130            if (plot == null) {
131                return false;
132            }
133            BarRenderer renderer = (BarRenderer) plot.getRenderer();
134            if (renderer == null) {
135                return false;
136            }
137            return renderer.isDrawBarOutline();
138        }
139        
140        /**
141         * Sets the flag that controls whether or not the bar outlines are drawn 
142         * and fires a {@link PropertyChangeEvent} for the 
143         * <code>barOutlineVisible</code> property.
144         * 
145         * @param visible  the new flag value.
146         * 
147         * @see #isBarOutlineVisible()
148         */
149        public void setBarOutlineVisible(boolean visible) {
150            CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
151            if (plot == null) {
152                return;
153            }
154            BarRenderer renderer = (BarRenderer) plot.getRenderer();
155            if (renderer == null) {
156                return;
157            }
158            boolean old = renderer.isDrawBarOutline();
159            renderer.setDrawBarOutline(visible);
160            firePropertyChange("barOutlineVisible", old, visible);
161        }
162    
163        /**
164         * Returns the overall margin between bars within each category.
165         * 
166         * @return The item margin.
167         * 
168         * @see #setBarItemMargin(double)
169         */
170        public double getBarItemMargin() {
171            CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
172            if (plot == null) {
173                return 0.0;
174            }
175            BarRenderer renderer = (BarRenderer) plot.getRenderer();
176            if (renderer == null) {
177                return 0.0;
178            }
179            return renderer.getItemMargin();
180        }
181        
182        /**
183         * Sets the margin between items within each category and fires a 
184         * {@link PropertyChangeEvent} for the <code>barItemMargin</code> property.
185         * 
186         * @param margin  the new margin value.
187         * 
188         * @see #getBarItemMargin()
189         */
190        public void setBarItemMargin(double margin) {
191            CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
192            if (plot == null) {
193                return;
194            }
195            BarRenderer renderer = (BarRenderer) plot.getRenderer();
196            if (renderer == null) {
197                return;
198            }
199            double old = renderer.getItemMargin();
200            renderer.setItemMargin(margin);
201            firePropertyChange("barItemMargin", old, margin);
202        }
203        
204        /**
205         * Returns the base value for the bars.
206         * 
207         * @return The item margin.
208         * 
209         * @see #setBarBaseValue(double)
210         */
211        public double getBarBaseValue() {
212            CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
213            if (plot == null) {
214                return 0.0;
215            }
216            BarRenderer renderer = (BarRenderer) plot.getRenderer();
217            if (renderer == null) {
218                return 0.0;
219            }
220            return renderer.getBase();
221        }
222        
223        /**
224         * Sets the bar base value and fires a {@link PropertyChangeEvent} for 
225         * the <code>barBaseValue</code> property.
226         * 
227         * @param base  the new base value.
228         * 
229         * @see #getBarBaseValue()
230         */
231        public void setBarBaseValue(double base) {
232            CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
233            if (plot == null) {
234                return;
235            }
236            BarRenderer renderer = (BarRenderer) plot.getRenderer();
237            if (renderer == null) {
238                return;
239            }
240            double old = renderer.getBase();
241            renderer.setBase(base);
242            firePropertyChange("barBaseValue", old, base);
243        }
244    
245    }