KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > chart > ChartCreator


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

20 package org.openi.chart;
21
22 import com.tonbeller.jpivot.olap.model.OlapException;
23 import com.tonbeller.jpivot.olap.model.OlapModel;
24
25 import org.apache.log4j.Logger;
26
27 import org.jfree.chart.ChartFactory;
28 import org.jfree.chart.ChartPanel;
29 import org.jfree.chart.JFreeChart;
30 import org.jfree.chart.Legend;
31 import org.jfree.chart.StandardLegend;
32 import org.jfree.chart.axis.CategoryAxis;
33 import org.jfree.chart.axis.CategoryLabelPositions;
34 import org.jfree.chart.axis.DateAxis;
35 import org.jfree.chart.axis.NumberAxis;
36 import org.jfree.chart.plot.CategoryPlot;
37 import org.jfree.chart.plot.CombinedDomainCategoryPlot;
38 import org.jfree.chart.plot.PlotOrientation;
39 import org.jfree.chart.plot.XYPlot;
40 import org.jfree.chart.renderer.category.BarRenderer;
41 import org.jfree.chart.renderer.category.CategoryItemRenderer;
42 import org.jfree.chart.renderer.category.LineAndShapeRenderer;
43 import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
44 import org.jfree.chart.renderer.xy.XYItemRenderer;
45
46 import org.jfree.data.category.CategoryDataset;
47 import org.jfree.data.category.DefaultCategoryDataset;
48 import org.jfree.data.xy.XYDataset;
49
50 import org.jfree.ui.ApplicationFrame;
51 import org.jfree.ui.RefineryUtilities;
52 import org.jfree.ui.Spacer;
53
54 import org.jfree.util.TableOrder;
55
56 import org.openi.xmla.DatasetAdapter;
57 import org.openi.xmla.XmlaConnector;
58
59 import org.xml.sax.SAXException JavaDoc;
60
61 import java.awt.Color JavaDoc;
62 import java.awt.Dimension JavaDoc;
63 import java.awt.Font JavaDoc;
64 import java.awt.Paint JavaDoc;
65
66 import java.io.IOException JavaDoc;
67
68 import java.text.SimpleDateFormat JavaDoc;
69
70 import java.util.ArrayList JavaDoc;
71 import java.util.Iterator JavaDoc;
72 import java.util.List JavaDoc;
73 import java.util.Locale JavaDoc;
74
75 import javax.swing.JPanel JavaDoc;
76
77
78 /**
79  * A simple demonstration application showing how to create a bar chart.
80  *
81  * Notice this is not a unit test.
82  */

83 public class ChartCreator extends ApplicationFrame {
84     private static Logger logger = Logger.getLogger(ChartCreator.class);
85
86     /**
87      * Creates a new demo instance.
88      *
89      * @param title the frame title.
90      */

91     public ChartCreator(String JavaDoc title) {
92         super(title);
93         org.openi.test.Util.setupLog4j();
94
95         try {
96             // CategoryDataset dataset = createDataset();
97
OlapModel olapModel = queryXmla();
98
99             // DefaultCategoryDataset dataset = queryXmla();
100
DatasetAdapter adapter = new DatasetAdapter(Locale.getDefault());
101             XYDataset dataset = adapter.buildXYDataset(olapModel);
102             JFreeChart chart = createTimeChart(dataset);
103             ChartPanel chartPanel = new ChartPanel(chart);
104             chartPanel.setPreferredSize(new Dimension JavaDoc(500, 270));
105             setContentPane(chartPanel);
106         } catch (Exception JavaDoc e) {
107             e.printStackTrace();
108         }
109     }
110
111     /**
112      * @param dataset
113      * @return
114      */

115     private static JFreeChart createTimeChart(XYDataset dataset) {
116         JFreeChart chart = org.jfree.chart.ChartFactory.createTimeSeriesChart("Legal & General Unit Trust Prices",
117                 "Date", "Price Per Unit", dataset, true, true, false);
118
119         chart.setBackgroundPaint(Color.white);
120
121         StandardLegend sl = (StandardLegend) chart.getLegend();
122         sl.setDisplaySeriesShapes(true);
123
124         XYPlot plot = chart.getXYPlot();
125         plot.setBackgroundPaint(Color.lightGray);
126         plot.setDomainGridlinePaint(Color.white);
127         plot.setRangeGridlinePaint(Color.white);
128         plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
129         plot.setDomainCrosshairVisible(false);
130         plot.setRangeCrosshairVisible(false);
131
132         XYItemRenderer renderer = plot.getRenderer();
133
134         if (renderer instanceof StandardXYItemRenderer) {
135             StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
136             rr.setPlotShapes(true);
137             rr.setShapesFilled(true);
138             rr.setItemLabelsVisible(true);
139         }
140
141         DateAxis axis = (DateAxis) plot.getDomainAxis();
142         axis.setDateFormatOverride(new SimpleDateFormat JavaDoc("MMM-yyyy"));
143
144         return chart;
145     }
146
147     private OlapModel queryXmla()
148         throws SAXException JavaDoc, OlapException, IOException JavaDoc {
149         XmlaConnector connector = new XmlaConnector();
150         DatasetAdapter adapter = new DatasetAdapter(Locale.getDefault());
151         String JavaDoc url = "";
152         String JavaDoc catalog = "";
153         String JavaDoc apple = "";
154         String JavaDoc mdx = apple;
155
156         return connector.query(url, catalog, mdx);
157     }
158
159     private JFreeChart createChart(CategoryDataset dataset) {
160         // return createMultipleChart(dataset);
161
logger.debug("looking for date: 2004-06-01 00:00:00");
162
163         SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss");
164
165         try {
166             logger.debug(formatter.parse("2004-06-01 00:00:00"));
167         } catch (Exception JavaDoc e) {
168             e.printStackTrace();
169         }
170
171         return createBarChart(dataset);
172     }
173
174     private JFreeChart createMultipleChart(CategoryDataset dataset) {
175         CategoryDataset dataset1 = createDataset();
176         NumberAxis rangeAxis1 = new NumberAxis("Value");
177         rangeAxis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
178
179         LineAndShapeRenderer renderer1 = new LineAndShapeRenderer();
180
181         // renderer1.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
182
CategoryPlot subplot1 = new CategoryPlot(dataset1, null, rangeAxis1,
183                 renderer1);
184         subplot1.setDomainGridlinesVisible(true);
185
186         CategoryDataset dataset2 = createDataset();
187         NumberAxis rangeAxis2 = new NumberAxis("Value");
188         rangeAxis2.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
189
190         BarRenderer renderer2 = new BarRenderer();
191
192         // renderer2.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
193
CategoryPlot subplot2 = new CategoryPlot(dataset2, null, rangeAxis2,
194                 renderer2);
195         subplot2.setDomainGridlinesVisible(true);
196
197         CategoryAxis domainAxis = new CategoryAxis("Category");
198         CombinedDomainCategoryPlot plot = new CombinedDomainCategoryPlot(domainAxis);
199         plot.add(subplot1, 2);
200         plot.add(subplot2, 1);
201
202         JFreeChart result = new JFreeChart("Combined Domain Category Plot Demo",
203                 new Font JavaDoc("SansSerif", Font.BOLD, 12), plot, true);
204         result.getLegend().setAnchor(Legend.SOUTH);
205
206         return result;
207     }
208
209     /**
210      * Returns a sample dataset.
211      *
212      * @return The dataset.
213      */

214     private static CategoryDataset createDataset() {
215         // row keys...
216
String JavaDoc series1 = "First";
217         String JavaDoc series2 = "Second";
218         String JavaDoc series3 = "Third";
219
220         // column keys...
221
String JavaDoc category1 = "Category 1";
222         String JavaDoc category2 = "Category 2";
223         String JavaDoc category3 = "Category 3";
224         String JavaDoc category4 = "Category 4";
225         String JavaDoc category5 = "Category 5";
226
227         // create the dataset...
228
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
229
230         dataset.addValue(1.0, series1, category1);
231         dataset.addValue(4.0, series1, category2);
232         dataset.addValue(3.0, series1, category3);
233
234         dataset.addValue(5.0, series2, category4);
235         dataset.addValue(5.0, series3, category5);
236
237         dataset.addValue(5.0, series3, category1);
238         dataset.addValue(7.0, series3, category2);
239         dataset.addValue(6.0, series3, category3);
240
241         dataset.addValue(8.0, "series4", category4);
242         dataset.addValue(4.0, "series4", category5);
243
244         dataset.addValue(4.0, "series5", category1);
245         dataset.addValue(3.0, "series5", category2);
246         dataset.addValue(2.0, "series5", category3);
247
248         dataset.addValue(3.0, "series6", category4);
249         dataset.addValue(6.0, "series6", category5);
250
251         return dataset;
252     }
253
254     /**
255      * Creates a sample chart.
256      *
257      * @param dataset the dataset.
258      *
259      * @return The chart.
260      */

261     private static JFreeChart createBarChart(CategoryDataset dataset) {
262         // create the chart...
263
JFreeChart chart = ChartFactory.createBarChart(ChartCreator.class.getName(), // chart title
264
"Category", // domain axis label
265
"Value", // range axis label
266
dataset, // data
267
PlotOrientation.VERTICAL, // orientation
268
true, // include legend
269
true, // tooltips?
270
false // URLs?
271
);
272
273         // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
274
// set the background color for the chart...
275
chart.setBackgroundPaint(Color.white);
276
277         // get a reference to the plot for further customisation...
278
CategoryPlot plot = chart.getCategoryPlot();
279         plot.setBackgroundPaint(Color.lightGray);
280         plot.setDomainGridlinePaint(Color.white);
281         plot.setDomainGridlinesVisible(true);
282         plot.setRangeGridlinePaint(Color.white);
283
284         // set the range axis to display integers only...
285
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
286         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
287
288         // disable bar outlines...
289
BarRenderer renderer = (BarRenderer) plot.getRenderer();
290         renderer.setDrawBarOutline(true);
291
292         // set up gradient paints for series...
293
List JavaDoc paintList = new ArrayList JavaDoc();
294
295         /*
296            paintList.add(new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f,
297                    0.0f, new Color(0, 0, 64)) );
298            paintList.add(new GradientPaint(0.0f, 0.0f, Color.green, 0.0f,
299                    0.0f, new Color(0, 64, 0)) );
300            paintList.add(new GradientPaint(0.0f, 0.0f, Color.red, 0.0f,
301                    0.0f, new Color(64, 0, 0)) );
302          */

303
304         //
305
// paintList.add(new Color());
306
paintList.add(new Color JavaDoc(249, 219, 224));
307         paintList.add(new Color JavaDoc(167, 70, 97));
308         paintList.add(new Color JavaDoc(189, 117, 137));
309         paintList.add(new Color JavaDoc(212, 163, 177));
310         paintList.add(new Color JavaDoc(233, 209, 215));
311         paintList.add(new Color JavaDoc(142, 60, 82));
312         paintList.add(new Color JavaDoc(125, 53, 73));
313         paintList.add(new Color JavaDoc(84, 35, 49));
314
315         setupSeriesPaint(paintList, renderer);
316
317         // plot.setForegroundAlpha(.75f);
318
CategoryAxis domainAxis = plot.getDomainAxis();
319         domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(
320                 Math.PI / 6.0));
321
322         // OPTIONAL CUSTOMISATION COMPLETED.
323
return chart;
324     }
325
326     private static void setupSeriesPaint(List JavaDoc colorPalette,
327         CategoryItemRenderer renderer) {
328         Iterator JavaDoc palette = colorPalette.iterator();
329
330         for (int i = 0; palette.hasNext(); i++) {
331             Paint JavaDoc current = (Paint JavaDoc) palette.next();
332             renderer.setSeriesPaint(i, current);
333         }
334     }
335
336     /**
337      * Creates a panel for the demo (used by SuperDemo.java).
338      *
339      * @return A panel.
340      */

341     public static JPanel JavaDoc createDemoPanel() {
342         // JFreeChart chart = createPieChart(createDataset());
343
JFreeChart chart = createBarChart(createDataset());
344
345         return new ChartPanel(chart);
346     }
347
348     /**
349      * @param dataset
350      * @return
351      */

352     private static JFreeChart createPieChart(CategoryDataset dataset) {
353         JFreeChart chart = ChartFactory.createMultiplePieChart(ChartCreator.class.getName(),
354                 createDataset(), TableOrder.BY_COLUMN, true, true, true);
355         chart.setBackgroundPaint(Color.white);
356
357         return chart;
358     }
359
360     /**
361      * Returns a description of the demo.
362      *
363      * @return A description.
364      */

365     public static String JavaDoc getDemoDescription() {
366         return "A bar chart.";
367     }
368
369     /**
370      * Starting point for the demonstration application.
371      *
372      * @param args ignored.
373      */

374     public static void main(String JavaDoc[] args) {
375         ChartCreator demo = new ChartCreator("Bar Chart Demo");
376         demo.pack();
377         RefineryUtilities.centerFrameOnScreen(demo);
378         demo.setVisible(true);
379     }
380 }
381
Popular Tags