1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors.
6  *
7  * Project Info:  http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
22  * USA.
23  *
24  * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
25  * Other names may be trademarks of their respective owners.]
26  *
27  * ---------------------
28  * SWTBarChartDemo1.java
29  * ---------------------
30  * (C) Copyright 2006-2008, by Object Refinery Limited and Contributors.
31  *
32  * Original Author:  David Gilbert (for Object Refinery Limited);
33  * Contributor(s):   Henry Proudhon (henry.proudhon AT ensmp.fr).
34  *
35  * Changes
36  * -------
37  * 23-Aug-2006 : New class (DG);
38  *
39  */
40 
41 package org.jfree.experimental.chart.swt.demo;
42 
43 import org.eclipse.swt.SWT;
44 import org.eclipse.swt.layout.FillLayout;
45 import org.eclipse.swt.widgets.Display;
46 import org.eclipse.swt.widgets.Shell;
47 import org.jfree.chart.ChartFactory;
48 import org.jfree.chart.JFreeChart;
49 import org.jfree.chart.axis.CategoryAxis;
50 import org.jfree.chart.axis.CategoryLabelPositions;
51 import org.jfree.chart.axis.NumberAxis;
52 import org.jfree.chart.plot.CategoryPlot;
53 import org.jfree.chart.plot.PlotOrientation;
54 import org.jfree.chart.renderer.category.BarRenderer;
55 import org.jfree.chart.renderer.category.StandardBarPainter;
56 import org.jfree.data.category.CategoryDataset;
57 import org.jfree.data.category.DefaultCategoryDataset;
58 import org.jfree.experimental.chart.swt.ChartComposite;
59 
60 /**
61  * An SWT demo.
62  */
63 public class SWTBarChartDemo1 {
64 
65     /**
66      * Returns a sample dataset.
67      *
68      * @return The dataset.
69      */
createDataset()70     private static CategoryDataset createDataset() {
71 
72         // row keys...
73         String series1 = "First";
74         String series2 = "Second";
75         String series3 = "Third";
76 
77         // column keys...
78         String category1 = "Category 1";
79         String category2 = "Category 2";
80         String category3 = "Category 3";
81         String category4 = "Category 4";
82         String category5 = "Category 5";
83 
84         // create the dataset...
85         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
86 
87         dataset.addValue(1.0, series1, category1);
88         dataset.addValue(4.0, series1, category2);
89         dataset.addValue(3.0, series1, category3);
90         dataset.addValue(5.0, series1, category4);
91         dataset.addValue(5.0, series1, category5);
92 
93         dataset.addValue(5.0, series2, category1);
94         dataset.addValue(7.0, series2, category2);
95         dataset.addValue(6.0, series2, category3);
96         dataset.addValue(8.0, series2, category4);
97         dataset.addValue(4.0, series2, category5);
98 
99         dataset.addValue(4.0, series3, category1);
100         dataset.addValue(3.0, series3, category2);
101         dataset.addValue(2.0, series3, category3);
102         dataset.addValue(3.0, series3, category4);
103         dataset.addValue(6.0, series3, category5);
104 
105         return dataset;
106 
107     }
108 
109     /**
110      * Creates a sample chart.
111      *
112      * @param dataset  the dataset.
113      *
114      * @return The chart.
115      */
createChart(CategoryDataset dataset)116     private static JFreeChart createChart(CategoryDataset dataset) {
117 
118         // create the chart...
119         JFreeChart chart = ChartFactory.createBarChart(
120             "SWTBarChartDemo1",       // chart title
121             "Category",               // domain axis label
122             "Value",                  // range axis label
123             dataset,                  // data
124             PlotOrientation.VERTICAL, // orientation
125             true,                     // include legend
126             true,                     // tooltips?
127             false                     // URLs?
128         );
129 
130         // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
131 
132         // get a reference to the plot for further customisation...
133         CategoryPlot plot = (CategoryPlot) chart.getPlot();
134 
135         // set the range axis to display integers only...
136         NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
137         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
138 
139         // disable bar outlines...
140         BarRenderer renderer = (BarRenderer) plot.getRenderer();
141         renderer.setDrawBarOutline(false);
142 
143         // the SWTGraphics2D class doesn't handle GradientPaint well, so
144         // replace the gradient painter from the default theme with a
145         // standard painter...
146         renderer.setBarPainter(new StandardBarPainter());
147 
148         CategoryAxis domainAxis = plot.getDomainAxis();
149         domainAxis.setCategoryLabelPositions(
150                 CategoryLabelPositions.createUpRotationLabelPositions(
151                         Math.PI / 6.0));
152         // OPTIONAL CUSTOMISATION COMPLETED.
153 
154         return chart;
155 
156     }
157 
158     /**
159      * Starting point for the demonstration application.
160      *
161      * @param args  ignored.
162      */
main(String[] args)163     public static void main(String[] args) {
164         JFreeChart chart = createChart(createDataset());
165         Display display = new Display();
166         Shell shell = new Shell(display);
167         shell.setSize(600, 300);
168         shell.setLayout(new FillLayout());
169         shell.setText("Test for jfreechart running with SWT");
170         ChartComposite frame = new ChartComposite(shell, SWT.NONE, chart,
171                 true);
172         frame.pack();
173         shell.open();
174         while (!shell.isDisposed()) {
175             if (!display.readAndDispatch())
176                 display.sleep();
177         }
178     }
179 
180 }
181 
182