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     * SWTBarChartDemo1.java
029     * ---------------------
030     * (C) Copyright 2006, 2007, by Object Refinery Limited and Contributors.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):
034     *
035     * Changes
036     * -------
037     * 23-Aug-2006 : New class (DG);
038     * 
039     */
040    
041    package org.jfree.experimental.chart.swt.demo;
042    
043    import java.awt.Color;
044    
045    import org.eclipse.swt.SWT;
046    import org.eclipse.swt.layout.FillLayout;
047    import org.eclipse.swt.widgets.Display;
048    import org.eclipse.swt.widgets.Shell;
049    import org.jfree.chart.ChartFactory;
050    import org.jfree.chart.JFreeChart;
051    import org.jfree.chart.axis.CategoryAxis;
052    import org.jfree.chart.axis.CategoryLabelPositions;
053    import org.jfree.chart.axis.NumberAxis;
054    import org.jfree.chart.plot.CategoryPlot;
055    import org.jfree.chart.plot.PlotOrientation;
056    import org.jfree.chart.renderer.category.BarRenderer;
057    import org.jfree.data.category.CategoryDataset;
058    import org.jfree.data.category.DefaultCategoryDataset;
059    import org.jfree.experimental.chart.swt.ChartComposite;
060    
061    /**
062     * An SWT demo.
063     */
064    public class SWTBarChartDemo1 {
065        
066        /**
067         * Returns a sample dataset.
068         * 
069         * @return The dataset.
070         */
071        private static CategoryDataset createDataset() {
072            
073            // row keys...
074            String series1 = "First";
075            String series2 = "Second";
076            String series3 = "Third";
077    
078            // column keys...
079            String category1 = "Category 1";
080            String category2 = "Category 2";
081            String category3 = "Category 3";
082            String category4 = "Category 4";
083            String category5 = "Category 5";
084    
085            // create the dataset...
086            DefaultCategoryDataset dataset = new DefaultCategoryDataset();
087    
088            dataset.addValue(1.0, series1, category1);
089            dataset.addValue(4.0, series1, category2);
090            dataset.addValue(3.0, series1, category3);
091            dataset.addValue(5.0, series1, category4);
092            dataset.addValue(5.0, series1, category5);
093    
094            dataset.addValue(5.0, series2, category1);
095            dataset.addValue(7.0, series2, category2);
096            dataset.addValue(6.0, series2, category3);
097            dataset.addValue(8.0, series2, category4);
098            dataset.addValue(4.0, series2, category5);
099    
100            dataset.addValue(4.0, series3, category1);
101            dataset.addValue(3.0, series3, category2);
102            dataset.addValue(2.0, series3, category3);
103            dataset.addValue(3.0, series3, category4);
104            dataset.addValue(6.0, series3, category5);
105            
106            return dataset;
107            
108        }
109        
110        /**
111         * Creates a sample chart.
112         * 
113         * @param dataset  the dataset.
114         * 
115         * @return The chart.
116         */
117        private static JFreeChart createChart(CategoryDataset dataset) {
118            
119            // create the chart...
120            JFreeChart chart = ChartFactory.createBarChart(
121                "Bar Chart Demo",         // chart title
122                "Category",               // domain axis label
123                "Value",                  // range axis label
124                dataset,                  // data
125                PlotOrientation.VERTICAL, // orientation
126                true,                     // include legend
127                true,                     // tooltips?
128                false                     // URLs?
129            );
130    
131            // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
132    
133            // set the background color for the chart...
134            chart.setBackgroundPaint(Color.white);
135    
136            // get a reference to the plot for further customisation...
137            CategoryPlot plot = (CategoryPlot) chart.getPlot();
138            plot.setBackgroundPaint(Color.lightGray);
139            plot.setDomainGridlinePaint(Color.white);
140            plot.setDomainGridlinesVisible(true);
141            plot.setRangeGridlinePaint(Color.white);
142    
143            // set the range axis to display integers only...
144            final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
145            rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
146    
147            // disable bar outlines...
148            BarRenderer renderer = (BarRenderer) plot.getRenderer();
149            renderer.setDrawBarOutline(false);
150    
151            CategoryAxis domainAxis = plot.getDomainAxis();
152            domainAxis.setCategoryLabelPositions(
153                CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
154            );
155            // OPTIONAL CUSTOMISATION COMPLETED.
156            
157            return chart;
158            
159        }
160        
161        /**
162         * Starting point for the demonstration application.
163         *
164         * @param args  ignored.
165         */
166        public static void main( String[] args ) 
167        {
168            JFreeChart chart = createChart(createDataset());
169            Display display = new Display();
170            Shell shell = new Shell(display);
171            shell.setSize(600, 300);
172            shell.setLayout(new FillLayout());
173            shell.setText("Test for jfreechart running with SWT");
174            final ChartComposite frame = new ChartComposite(shell, SWT.NONE, chart,
175                    true);
176            frame.pack();
177            shell.open();
178            while (!shell.isDisposed()) {
179                if (!display.readAndDispatch())
180                    display.sleep();
181            }
182        }
183    
184    }
185