001: /*********************************************************************************
002: * The contents of this file are subject to the OpenI Public License Version 1.0
003: * ("License"); You may not use this file except in compliance with the
004: * License. You may obtain a copy of the License at
005: * http://www.openi.org/docs/LICENSE.txt
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is: OpenI Open Source
012: *
013: * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
014: * Portions created by Loyalty Matrix, Inc. are
015: * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
016: *
017: * Contributor(s): ______________________________________.
018: *
019: ********************************************************************************/package org.openi.chart;
020:
021: import com.tonbeller.jpivot.olap.model.OlapException;
022: import com.tonbeller.jpivot.olap.model.OlapModel;
023:
024: import org.apache.log4j.Logger;
025:
026: import org.jfree.chart.ChartFactory;
027: import org.jfree.chart.ChartPanel;
028: import org.jfree.chart.JFreeChart;
029: import org.jfree.chart.axis.CategoryAxis;
030: import org.jfree.chart.axis.CategoryLabelPositions;
031: import org.jfree.chart.axis.DateAxis;
032: import org.jfree.chart.axis.NumberAxis;
033: import org.jfree.chart.plot.CategoryPlot;
034: import org.jfree.chart.plot.CombinedDomainCategoryPlot;
035: import org.jfree.chart.plot.PlotOrientation;
036: import org.jfree.chart.plot.XYPlot;
037: import org.jfree.chart.renderer.category.BarRenderer;
038: import org.jfree.chart.renderer.category.CategoryItemRenderer;
039: import org.jfree.chart.renderer.category.LineAndShapeRenderer;
040: import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
041: import org.jfree.chart.renderer.xy.XYItemRenderer;
042: import org.jfree.chart.title.LegendTitle;
043:
044: import org.jfree.data.category.CategoryDataset;
045: import org.jfree.data.category.DefaultCategoryDataset;
046: import org.jfree.data.xy.XYDataset;
047:
048: import org.jfree.ui.ApplicationFrame;
049: import org.jfree.ui.RefineryUtilities;
050:
051: import org.jfree.util.TableOrder;
052:
053: import org.openi.analysis.Datasource;
054: import org.openi.xmla.DatasetAdapter;
055: import org.openi.xmla.XmlaConnector;
056:
057: import org.xml.sax.SAXException;
058:
059: import java.awt.Color;
060: import java.awt.Dimension;
061: import java.awt.Font;
062: import java.awt.Paint;
063:
064: import java.io.IOException;
065:
066: import java.text.SimpleDateFormat;
067:
068: import java.util.ArrayList;
069: import java.util.Iterator;
070: import java.util.List;
071: import java.util.Locale;
072:
073: import javax.swing.JPanel;
074:
075: /**
076: * A simple demonstration application showing how to create a bar chart.
077: *
078: * Notice this is not a unit test.
079: */
080: public class ChartCreator extends ApplicationFrame {
081: private static Logger logger = Logger.getLogger(ChartCreator.class);
082:
083: /**
084: * Creates a new demo instance.
085: *
086: * @param title the frame title.
087: */
088: public ChartCreator(String title) {
089: super (title);
090: org.openi.test.Util.setupLog4j();
091:
092: try {
093: // CategoryDataset dataset = createDataset();
094: OlapModel olapModel = queryXmla();
095:
096: // DefaultCategoryDataset dataset = queryXmla();
097: DatasetAdapter adapter = new DatasetAdapter(Locale
098: .getDefault());
099: XYDataset dataset = adapter.buildXYDataset(olapModel);
100: JFreeChart chart = createTimeChart(dataset);
101: ChartPanel chartPanel = new ChartPanel(chart);
102: chartPanel.setPreferredSize(new Dimension(500, 270));
103: setContentPane(chartPanel);
104: } catch (Exception e) {
105: e.printStackTrace();
106: }
107: }
108:
109: /**
110: * @param dataset
111: * @return
112: */
113: private static JFreeChart createTimeChart(XYDataset dataset) {
114: JFreeChart chart = org.jfree.chart.ChartFactory
115: .createTimeSeriesChart(
116: "Legal & General Unit Trust Prices", "Date",
117: "Price Per Unit", dataset, true, true, false);
118:
119: chart.setBackgroundPaint(Color.white);
120:
121: // StandardLegend legend = (StandardLegend) chart.getLegend();
122: // LegendTitle legend = chart.getLegend();
123: // upgrade 1.0.1
124: // legend.setDisplaySeriesShapes(true);
125:
126: XYPlot plot = chart.getXYPlot();
127: plot.setBackgroundPaint(Color.lightGray);
128: plot.setDomainGridlinePaint(Color.white);
129: plot.setRangeGridlinePaint(Color.white);
130: //no more Spacer class?
131: // plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
132: plot.setDomainCrosshairVisible(false);
133: plot.setRangeCrosshairVisible(false);
134:
135: XYItemRenderer renderer = plot.getRenderer();
136:
137: if (renderer instanceof StandardXYItemRenderer) {
138: StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
139: // missing in 1.0.1 upgrade:
140: // rr.setPlotShapes(true);
141: rr.setShapesFilled(true);
142: rr.setItemLabelsVisible(true);
143: }
144:
145: DateAxis axis = (DateAxis) plot.getDomainAxis();
146: axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
147:
148: return chart;
149: }
150:
151: private OlapModel queryXmla() throws SAXException, OlapException,
152: IOException {
153: XmlaConnector connector = new XmlaConnector();
154: DatasetAdapter adapter = new DatasetAdapter(Locale.getDefault());
155: String url = "http://olap02/xmla/msxisapi.dll";
156: String catalog = "Apple Weekly";
157: String apple = "select {[Measures]} on columns, {Dimensions(1)} on rows from [Running Totals]";
158: String mdx = apple;
159:
160: return connector.query(new Datasource(url, catalog), mdx);
161: }
162:
163: private JFreeChart createChart(CategoryDataset dataset) {
164: // return createMultipleChart(dataset);
165: logger.debug("looking for date: 2004-06-01 00:00:00");
166:
167: SimpleDateFormat formatter = new SimpleDateFormat(
168: "yyyy-MM-dd HH:mm:ss");
169:
170: try {
171: logger.debug(formatter.parse("2004-06-01 00:00:00"));
172: } catch (Exception e) {
173: e.printStackTrace();
174: }
175:
176: return createBarChart(dataset);
177: }
178:
179: private JFreeChart createMultipleChart(CategoryDataset dataset) {
180: CategoryDataset dataset1 = createDataset();
181: NumberAxis rangeAxis1 = new NumberAxis("Value");
182: rangeAxis1.setStandardTickUnits(NumberAxis
183: .createIntegerTickUnits());
184:
185: LineAndShapeRenderer renderer1 = new LineAndShapeRenderer();
186:
187: // renderer1.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
188: CategoryPlot subplot1 = new CategoryPlot(dataset1, null,
189: rangeAxis1, renderer1);
190: subplot1.setDomainGridlinesVisible(true);
191:
192: CategoryDataset dataset2 = createDataset();
193: NumberAxis rangeAxis2 = new NumberAxis("Value");
194: rangeAxis2.setStandardTickUnits(NumberAxis
195: .createIntegerTickUnits());
196:
197: BarRenderer renderer2 = new BarRenderer();
198:
199: // renderer2.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
200: CategoryPlot subplot2 = new CategoryPlot(dataset2, null,
201: rangeAxis2, renderer2);
202: subplot2.setDomainGridlinesVisible(true);
203:
204: CategoryAxis domainAxis = new CategoryAxis("Category");
205: CombinedDomainCategoryPlot plot = new CombinedDomainCategoryPlot(
206: domainAxis);
207: plot.add(subplot1, 2);
208: plot.add(subplot2, 1);
209:
210: JFreeChart result = new JFreeChart(
211: "Combined Domain Category Plot Demo", new Font(
212: "SansSerif", Font.BOLD, 12), plot, true);
213: // upgrade to jfreechart 1.0.1
214: // result.getLegend().setAnchor(Legend.SOUTH);
215:
216: return result;
217: }
218:
219: /**
220: * Returns a sample dataset.
221: *
222: * @return The dataset.
223: */
224: private static CategoryDataset createDataset() {
225: // row keys...
226: String series1 = "First";
227: String series2 = "Second";
228: String series3 = "Third";
229:
230: // column keys...
231: String category1 = "Category 1";
232: String category2 = "Category 2";
233: String category3 = "Category 3";
234: String category4 = "Category 4";
235: String category5 = "Category 5";
236:
237: // create the dataset...
238: DefaultCategoryDataset dataset = new DefaultCategoryDataset();
239:
240: dataset.addValue(1.0, series1, category1);
241: dataset.addValue(4.0, series1, category2);
242: dataset.addValue(3.0, series1, category3);
243:
244: dataset.addValue(5.0, series2, category4);
245: dataset.addValue(5.0, series3, category5);
246:
247: dataset.addValue(5.0, series3, category1);
248: dataset.addValue(7.0, series3, category2);
249: dataset.addValue(6.0, series3, category3);
250:
251: dataset.addValue(8.0, "series4", category4);
252: dataset.addValue(4.0, "series4", category5);
253:
254: dataset.addValue(4.0, "series5", category1);
255: dataset.addValue(3.0, "series5", category2);
256: dataset.addValue(2.0, "series5", category3);
257:
258: dataset.addValue(3.0, "series6", category4);
259: dataset.addValue(6.0, "series6", category5);
260:
261: return dataset;
262: }
263:
264: /**
265: * Creates a sample chart.
266: *
267: * @param dataset the dataset.
268: *
269: * @return The chart.
270: */
271: private static JFreeChart createBarChart(CategoryDataset dataset) {
272: // create the chart...
273: JFreeChart chart = ChartFactory.createBarChart(
274: ChartCreator.class.getName(), // chart title
275: "Category", // domain axis label
276: "Value", // range axis label
277: dataset, // data
278: PlotOrientation.VERTICAL, // orientation
279: true, // include legend
280: true, // tooltips?
281: false // URLs?
282: );
283:
284: // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
285: // set the background color for the chart...
286: chart.setBackgroundPaint(Color.white);
287:
288: // get a reference to the plot for further customisation...
289: CategoryPlot plot = chart.getCategoryPlot();
290: plot.setBackgroundPaint(Color.lightGray);
291: plot.setDomainGridlinePaint(Color.white);
292: plot.setDomainGridlinesVisible(true);
293: plot.setRangeGridlinePaint(Color.white);
294:
295: // set the range axis to display integers only...
296: final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
297: rangeAxis.setStandardTickUnits(NumberAxis
298: .createIntegerTickUnits());
299:
300: // disable bar outlines...
301: BarRenderer renderer = (BarRenderer) plot.getRenderer();
302: renderer.setDrawBarOutline(true);
303:
304: // set up gradient paints for series...
305: List paintList = new ArrayList();
306:
307: /*
308: paintList.add(new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f,
309: 0.0f, new Color(0, 0, 64)) );
310: paintList.add(new GradientPaint(0.0f, 0.0f, Color.green, 0.0f,
311: 0.0f, new Color(0, 64, 0)) );
312: paintList.add(new GradientPaint(0.0f, 0.0f, Color.red, 0.0f,
313: 0.0f, new Color(64, 0, 0)) );
314: */
315:
316: //
317: // paintList.add(new Color());
318: paintList.add(new Color(249, 219, 224));
319: paintList.add(new Color(167, 70, 97));
320: paintList.add(new Color(189, 117, 137));
321: paintList.add(new Color(212, 163, 177));
322: paintList.add(new Color(233, 209, 215));
323: paintList.add(new Color(142, 60, 82));
324: paintList.add(new Color(125, 53, 73));
325: paintList.add(new Color(84, 35, 49));
326:
327: setupSeriesPaint(paintList, renderer);
328:
329: // plot.setForegroundAlpha(.75f);
330: CategoryAxis domainAxis = plot.getDomainAxis();
331: domainAxis.setCategoryLabelPositions(CategoryLabelPositions
332: .createUpRotationLabelPositions(Math.PI / 6.0));
333:
334: // OPTIONAL CUSTOMISATION COMPLETED.
335: return chart;
336: }
337:
338: private static void setupSeriesPaint(List colorPalette,
339: CategoryItemRenderer renderer) {
340: Iterator palette = colorPalette.iterator();
341:
342: for (int i = 0; palette.hasNext(); i++) {
343: Paint current = (Paint) palette.next();
344: renderer.setSeriesPaint(i, current);
345: }
346: }
347:
348: /**
349: * Creates a panel for the demo (used by SuperDemo.java).
350: *
351: * @return A panel.
352: */
353: public static JPanel createDemoPanel() {
354: // JFreeChart chart = createPieChart(createDataset());
355: JFreeChart chart = createBarChart(createDataset());
356:
357: return new ChartPanel(chart);
358: }
359:
360: /**
361: * @param dataset
362: * @return
363: */
364: private static JFreeChart createPieChart(CategoryDataset dataset) {
365: JFreeChart chart = ChartFactory.createMultiplePieChart(
366: ChartCreator.class.getName(), createDataset(),
367: TableOrder.BY_COLUMN, true, true, true);
368: chart.setBackgroundPaint(Color.white);
369:
370: return chart;
371: }
372:
373: /**
374: * Returns a description of the demo.
375: *
376: * @return A description.
377: */
378: public static String getDemoDescription() {
379: return "A bar chart.";
380: }
381:
382: /**
383: * Starting point for the demonstration application.
384: *
385: * @param args ignored.
386: */
387: public static void main(String[] args) {
388: ChartCreator demo = new ChartCreator("Bar Chart Demo");
389: demo.pack();
390: RefineryUtilities.centerFrameOnScreen(demo);
391: demo.setVisible(true);
392: }
393: }
|