001: package org.openi.chart;
002:
003: //Contributor: i18n : Pedro Casals Fradera (2006/06/22)
004: import java.awt.Color;
005: import java.awt.Font;
006: import java.awt.Paint;
007: import java.io.FileNotFoundException;
008: import java.io.IOException;
009: import java.io.OutputStream;
010: import java.text.SimpleDateFormat;
011: import java.util.Locale;
012:
013: import org.apache.log4j.Logger;
014: import org.jfree.chart.ChartUtilities;
015: import org.jfree.chart.JFreeChart;
016: import org.jfree.chart.axis.CategoryAxis;
017: import org.jfree.chart.axis.DateAxis;
018: import org.jfree.chart.axis.NumberAxis;
019: import org.jfree.chart.axis.ValueAxis;
020: import org.jfree.chart.labels.PieToolTipGenerator;
021: import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
022: import org.jfree.chart.plot.CategoryPlot;
023: import org.jfree.chart.plot.MultiplePiePlot;
024: import org.jfree.chart.plot.PiePlot;
025: import org.jfree.chart.plot.PiePlot3D;
026: import org.jfree.chart.plot.PlotOrientation;
027: import org.jfree.chart.plot.XYPlot;
028: import org.jfree.chart.renderer.category.LineAndShapeRenderer;
029: import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
030: import org.jfree.chart.renderer.xy.XYItemRenderer;
031: import org.jfree.chart.title.LegendTitle;
032: import org.jfree.chart.urls.CategoryURLGenerator;
033: import org.jfree.chart.urls.PieURLGenerator;
034: import org.jfree.data.category.CategoryDataset;
035: import org.jfree.data.xy.XYDataset;
036: import org.jfree.ui.RectangleInsets;
037: import org.jfree.util.TableOrder;
038: import org.jfree.util.UnitType;
039: import org.openi.analysis.Analysis;
040: import org.openi.xmla.DatasetAdapter;
041:
042: import com.tonbeller.jpivot.chart.ChartFactory;
043: import com.tonbeller.jpivot.olap.model.OlapException;
044: import com.tonbeller.jpivot.olap.model.OlapModel;
045:
046: /**
047: * This is a badly needed an extracted factory class to properly test chart creation.
048: * Before was couple with the chart component.
049: *
050: */
051: public class EnhancedChartFactory extends ChartFactory {
052: private static Logger logger = Logger
053: .getLogger(EnhancedChartFactory.class);
054:
055: public EnhancedChartFactory() {
056: super ();
057: }
058:
059: /**
060: * wrapper method that uses openi domain objects as input, returns chart.
061: *
062: * @param analysis
063: * @param dataset
064: * @return
065: * @throws OlapException
066: */
067: public static JFreeChart createChart(Analysis analysis,
068: OlapModel olapModel, Locale locale) throws OlapException {
069: logger.debug("creating chart for analysis: "
070: + analysis.getAnalysisTitle());
071:
072: JFreeChart chart = EnhancedChartFactory.createChart(olapModel,
073: analysis.getChartType(), analysis.getChartTitle(),
074: analysis.getHorizAxisLabel(), analysis
075: .getVertAxisLabel(), analysis.isShowLegend(),
076: true, analysis.isDrillThroughEnabled(),
077: new Font(analysis.getFontName(), analysis
078: .getFontStyle(), analysis.getFontSize()),
079: new Color(analysis.getBgColorR(), analysis
080: .getBgColorG(), analysis.getBgColorB()),
081: new Font(analysis.getSlicerFontName(), analysis
082: .getSlicerFontStyle(), analysis
083: .getSlicerFontSize()), new Font(analysis
084: .getAxisFontName(),
085: analysis.getAxisFontStyle(), analysis
086: .getAxisFontSize()), new Font(analysis
087: .getAxisTickFontName(), analysis
088: .getAxisTickFontStyle(), analysis
089: .getAxisTickFontSize()), new Font(analysis
090: .getLegendFontName(), analysis
091: .getLegendFontStyle(), analysis
092: .getLegendFontSize()), analysis
093: .getLegendPosition(), analysis
094: .getTickLabelRotate(), 1.0f, analysis
095: .isShowSlicer(), analysis.getSlicerPosition(),
096: analysis.getSlicerAlignment(),
097: analysis.getShowPareto(), locale);
098:
099: return chart;
100: }
101:
102: /**
103: * method used to stream chart directly to the output stream.
104: * Intended for use in a servlet (e.g. - dashboard servlet).
105: *
106: * @param out
107: * @param analysis
108: * @param dataset
109: * @param width
110: * @param height
111: * @throws FileNotFoundException
112: * @throws IOException
113: * @throws OlapException
114: */
115: public static void createChart(OutputStream out, Analysis analysis,
116: OlapModel olapModel, int width, int height, Locale locale)
117: throws FileNotFoundException, IOException, OlapException {
118: logger.debug("writing chart for analysis: "
119: + analysis.getAnalysisTitle() + " " + width + "x"
120: + height);
121: logger.debug("to stream type: " + out.getClass().getName());
122:
123: float quality = 1.0f;
124: JFreeChart chart = EnhancedChartFactory.createChart(analysis,
125: olapModel, locale);
126: ChartUtilities.writeChartAsJPEG(out, quality, chart, width,
127: height);
128: }
129:
130: /**
131: * factory method used in the wcf EnhancedChartComponent, also used internally by
132: * other wrapper createChart methods
133: *
134: * @return
135: * @throws OlapException
136: */
137: public static JFreeChart createChart(OlapModel olapModel,
138: int chartType, String chartTitle, String horizAxisLabel,
139: String vertAxisLabel, boolean showLegend,
140: boolean showTooltips, boolean drillThroughEnabled,
141: Font titleFont, Paint bgPaint, Font slicerFont,
142: Font axisFont, Font axisTickFont, Font legendFont,
143: int legendPosition, double tickLabelRotate,
144: float foregroundAlpha, boolean showSlicer,
145: int slicerPosition, int slicerAlignment,
146: boolean showPareto, Locale locale) throws OlapException {
147: logger.debug("trying to create chartType: " + chartType
148: + " with locale=" + locale.getLanguage());
149:
150: JFreeChart chart = null;
151:
152: // Font titleFont = new Font(fontName, fontStyle, fontSize);
153:
154: //jpivotPieURLGenerator pieUrlgenerator = new jpivotPieURLGenerator(PiePlot.PER_ROW);
155: CategoryURLGenerator urlGenerator = null;
156:
157: DatasetAdapter adapter = new DatasetAdapter(locale);
158: CategoryDataset dataset = null;
159:
160: switch (chartType) {
161: case 1:
162: dataset = adapter.buildCategoryDataset(olapModel);
163: chart = EnhancedChartFactory.createBarChart(chartTitle,
164: titleFont, horizAxisLabel, vertAxisLabel, dataset,
165: PlotOrientation.VERTICAL, showLegend, showTooltips,
166: drillThroughEnabled, urlGenerator);
167:
168: break;
169:
170: case 2:
171: dataset = adapter.buildCategoryDataset(olapModel);
172: chart = EnhancedChartFactory.createBarChart3D(chartTitle,
173: titleFont, horizAxisLabel, vertAxisLabel, dataset,
174: PlotOrientation.VERTICAL, showLegend, showTooltips,
175: drillThroughEnabled, urlGenerator);
176:
177: break;
178:
179: case 3:
180: dataset = adapter.buildCategoryDataset(olapModel);
181: chart = EnhancedChartFactory.createBarChart(chartTitle,
182: titleFont, horizAxisLabel, vertAxisLabel, dataset,
183: PlotOrientation.HORIZONTAL, showLegend,
184: showTooltips, drillThroughEnabled, urlGenerator);
185:
186: break;
187:
188: case 4:
189: dataset = adapter.buildCategoryDataset(olapModel);
190: chart = EnhancedChartFactory.createBarChart3D(chartTitle,
191: titleFont, horizAxisLabel, vertAxisLabel, dataset,
192: PlotOrientation.HORIZONTAL, showLegend,
193: showTooltips, drillThroughEnabled, urlGenerator);
194:
195: break;
196:
197: case 5:
198: dataset = adapter.buildCategoryDataset(olapModel);
199: chart = EnhancedChartFactory.createStackedBarChart(
200: chartTitle, titleFont, horizAxisLabel,
201: vertAxisLabel, dataset, PlotOrientation.VERTICAL,
202: showLegend, showTooltips, drillThroughEnabled,
203: urlGenerator);
204:
205: break;
206:
207: case 6:
208: dataset = adapter.buildCategoryDataset(olapModel);
209: chart = EnhancedChartFactory.createStackedBarChart3D(
210: chartTitle, titleFont, horizAxisLabel,
211: vertAxisLabel, dataset, PlotOrientation.VERTICAL,
212: showLegend, showTooltips, drillThroughEnabled,
213: urlGenerator);
214:
215: break;
216:
217: case 7:
218: dataset = adapter.buildCategoryDataset(olapModel);
219: chart = EnhancedChartFactory.createStackedBarChart(
220: chartTitle, titleFont, horizAxisLabel,
221: vertAxisLabel, dataset, PlotOrientation.HORIZONTAL,
222: showLegend, showTooltips, drillThroughEnabled,
223: urlGenerator);
224:
225: break;
226:
227: case 8:
228: dataset = adapter.buildCategoryDataset(olapModel);
229: chart = EnhancedChartFactory.createStackedBarChart3D(
230: chartTitle, titleFont, horizAxisLabel,
231: vertAxisLabel, dataset, PlotOrientation.HORIZONTAL,
232: showLegend, showTooltips, drillThroughEnabled,
233: urlGenerator);
234:
235: break;
236:
237: case 9:
238: dataset = adapter.buildCategoryDataset(olapModel);
239: chart = EnhancedChartFactory.createLineChart(chartTitle,
240: titleFont, horizAxisLabel, vertAxisLabel, dataset,
241: PlotOrientation.VERTICAL, showLegend, showTooltips,
242: drillThroughEnabled, urlGenerator);
243:
244: break;
245:
246: case 10:
247: dataset = adapter.buildCategoryDataset(olapModel);
248: chart = EnhancedChartFactory.createLineChart(chartTitle,
249: titleFont, horizAxisLabel, vertAxisLabel, dataset,
250: PlotOrientation.HORIZONTAL, showLegend,
251: showTooltips, drillThroughEnabled, urlGenerator);
252:
253: break;
254:
255: case 11:
256: dataset = adapter.buildCategoryDataset(olapModel);
257: chart = EnhancedChartFactory.createAreaChart(chartTitle,
258: titleFont, horizAxisLabel, vertAxisLabel, dataset,
259: PlotOrientation.VERTICAL, showLegend, showTooltips,
260: drillThroughEnabled, urlGenerator);
261:
262: break;
263:
264: case 12:
265: dataset = adapter.buildCategoryDataset(olapModel);
266: chart = EnhancedChartFactory.createAreaChart(chartTitle,
267: titleFont, horizAxisLabel, vertAxisLabel, dataset,
268: PlotOrientation.HORIZONTAL, showLegend,
269: showTooltips, drillThroughEnabled, urlGenerator);
270:
271: break;
272:
273: case 13:
274: dataset = adapter.buildCategoryDataset(olapModel);
275: chart = EnhancedChartFactory.createStackedAreaChart(
276: chartTitle, titleFont, horizAxisLabel,
277: vertAxisLabel, dataset, PlotOrientation.VERTICAL,
278: showLegend, showTooltips, drillThroughEnabled,
279: urlGenerator);
280:
281: break;
282:
283: case 14:
284: dataset = adapter.buildCategoryDataset(olapModel);
285: chart = EnhancedChartFactory.createStackedAreaChart(
286: chartTitle, titleFont, horizAxisLabel,
287: vertAxisLabel, dataset, PlotOrientation.HORIZONTAL,
288: showLegend, showTooltips, drillThroughEnabled,
289: urlGenerator);
290:
291: break;
292:
293: case 15:
294: dataset = adapter.buildCategoryDataset(olapModel);
295: chart = EnhancedChartFactory
296: .createPieChart(chartTitle, titleFont, dataset,
297: TableOrder.BY_COLUMN, showLegend,
298: showTooltips, drillThroughEnabled, null);
299:
300: break;
301:
302: case 16:
303: dataset = adapter.buildCategoryDataset(olapModel);
304: chart = EnhancedChartFactory.createPieChart(chartTitle,
305: titleFont, dataset, TableOrder.BY_ROW, showLegend,
306: showTooltips, drillThroughEnabled, null);
307:
308: break;
309:
310: case 17:
311: dataset = adapter.buildCategoryDataset(olapModel);
312: chart = EnhancedChartFactory
313: .create3DPieChart(chartTitle, titleFont, dataset,
314: TableOrder.BY_COLUMN, showLegend,
315: showTooltips, drillThroughEnabled, null);
316:
317: break;
318:
319: case 18:
320: dataset = adapter.buildCategoryDataset(olapModel);
321: chart = EnhancedChartFactory.create3DPieChart(chartTitle,
322: titleFont, dataset, TableOrder.BY_ROW, showLegend,
323: showTooltips, drillThroughEnabled, null);
324:
325: break;
326:
327: case 19:
328: chart = EnhancedChartFactory.createTimeChart(chartTitle,
329: horizAxisLabel, vertAxisLabel, new DatasetAdapter(
330: locale).buildXYDataset(olapModel));
331: break;
332:
333: default:
334: throw new OlapException(
335: "An unknown Chart Type was requested: " + chartType);
336: }
337:
338: // chart.setTitle("customized title");
339: ChartCustomizer customizer = new ChartCustomizer();
340: customizer.customizeChart(chart, bgPaint, axisFont,
341: axisTickFont, legendFont, legendPosition,
342: tickLabelRotate, foregroundAlpha);
343:
344: if (showPareto) {
345: if (chart.getPlot() instanceof CategoryPlot) {
346: customizer.generatePareto((CategoryPlot) chart
347: .getPlot());
348: }
349: }
350:
351: if (adapter.getNumberFormatter() != null) {
352: if (chart.getPlot() instanceof CategoryPlot) {
353: customizer.formatNumberAxis(adapter
354: .getNumberFormatter(), (CategoryPlot) chart
355: .getPlot());
356: }
357: }
358:
359: logger.debug("showSlicer=" + showSlicer);
360: if (showSlicer) {
361: customizer
362: .customizeSlicer(chart, olapModel.getResult(),
363: slicerFont, slicerPosition,
364: slicerAlignment, locale);
365: }
366:
367: return chart;
368: }
369:
370: public static JFreeChart createTimeChart(String title,
371: String horizAxisLabel, String vertAxisLabel,
372: XYDataset dataset) {
373: logger.debug("creating time series chart");
374:
375: JFreeChart chart = org.jfree.chart.ChartFactory
376: .createTimeSeriesChart(title, horizAxisLabel,
377: vertAxisLabel, dataset, true, true, false);
378:
379: chart.setBackgroundPaint(Color.white);
380:
381: //StandardLegend sl = (StandardLegend) chart.getLegend();
382: LegendTitle sl = (LegendTitle) chart.getLegend();
383: //sl.setDisplaySeriesShapes(true);
384:
385: XYPlot plot = chart.getXYPlot();
386: plot.setBackgroundPaint(Color.lightGray);
387: plot.setDomainGridlinePaint(Color.white);
388: plot.setRangeGridlinePaint(Color.white);
389: //plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
390: plot.setAxisOffset(new RectangleInsets(UnitType.ABSOLUTE, 5.0,
391: 5.0, 5.0, 5.0));
392: plot.setDomainCrosshairVisible(false);
393: plot.setRangeCrosshairVisible(false);
394:
395: XYItemRenderer renderer = plot.getRenderer();
396:
397: if (renderer instanceof StandardXYItemRenderer) {
398: StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
399: //rr.setPlotShapes(true);
400: rr.setPlotImages(true);
401: rr.setShapesFilled(true);
402: rr.setItemLabelsVisible(true);
403: }
404:
405: DateAxis axis = (DateAxis) plot.getDomainAxis();
406: axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
407:
408: return chart;
409: }
410:
411: public static JFreeChart createLineChart(String title,
412: java.awt.Font titleFont, String categoryAxisLabel,
413: String valueAxisLabel, CategoryDataset data,
414: PlotOrientation orientation, boolean legend,
415: boolean tooltips, boolean urls,
416: CategoryURLGenerator urlGenerator) {
417: CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel);
418: ValueAxis valueAxis = new NumberAxis(valueAxisLabel);
419:
420: LineAndShapeRenderer renderer = new LineAndShapeRenderer();
421:
422: // renderer.setLinesVisible(true);
423: // renderer.setShapesVisible(false);
424: if (tooltips) {
425: renderer
426: .setToolTipGenerator(new StandardCategoryToolTipGenerator());
427: }
428:
429: if (urls) {
430: renderer.setItemURLGenerator(urlGenerator);
431: }
432:
433: CategoryPlot plot = new CategoryPlot(data, categoryAxis,
434: valueAxis, renderer);
435: plot.setOrientation(orientation);
436:
437: JFreeChart chart = new JFreeChart(title, titleFont, plot,
438: legend);
439:
440: return chart;
441: }
442:
443: public static JFreeChart create3DPieChart(String title,
444: java.awt.Font titleFont, CategoryDataset data,
445: TableOrder order, boolean legend, boolean tooltips,
446: boolean urls, PieURLGenerator urlGenerator) {
447: MultiplePiePlot plot = new MultiplePiePlot(data);
448: plot.setDataExtractOrder(order);
449:
450: //plot.setOutlineStroke(null);
451: JFreeChart pieChart = new JFreeChart(new PiePlot3D(null));
452: pieChart.setBackgroundPaint(null);
453: plot.setPieChart(pieChart);
454:
455: PiePlot3D pp = (PiePlot3D) plot.getPieChart().getPlot();
456: pp.setBackgroundPaint(null);
457: //pp.setInsets(new Insets(0, 5, 5, 5));
458:
459: // no outline around each piechart
460: pp.setOutlineStroke(null);
461:
462: PieToolTipGenerator tooltipGenerator = null;
463:
464: if (tooltips) {
465: // tooltipGenerator = new StandardPieToolTipGenerator();
466: }
467:
468: if (!urls) {
469: urlGenerator = null;
470: }
471:
472: pp.setToolTipGenerator(tooltipGenerator);
473: pp.setLabelGenerator(null);
474: pp.setURLGenerator(urlGenerator);
475:
476: JFreeChart chart = new JFreeChart(title, titleFont, plot,
477: legend);
478:
479: return chart;
480: }
481:
482: public static JFreeChart createPieChart(String title,
483: java.awt.Font titleFont, CategoryDataset data,
484: TableOrder order, boolean legend, boolean tooltips,
485: boolean urls, PieURLGenerator urlGenerator) {
486: MultiplePiePlot plot = new MultiplePiePlot(data);
487: plot.setDataExtractOrder(order);
488:
489: PiePlot pp = (PiePlot) plot.getPieChart().getPlot();
490: //pp.setInsets(new Insets(0, 5, 5, 5));
491: pp.setBackgroundPaint(null);
492: // no outline around each piechart
493: pp.setOutlineStroke(null);
494:
495: //plot.setOutlineStroke(null);
496: PieToolTipGenerator tooltipGenerator = null;
497:
498: if (tooltips) {
499: // tooltipGenerator = new StandardPieToolTipGenerator();
500: }
501:
502: //PieURLGenerator urlGenerator = null;
503: if (!urls) {
504: urlGenerator = null;
505: }
506:
507: // pp.setToolTipGenerator(tooltipGenerator);
508: pp.setLabelGenerator(null);
509: pp.setURLGenerator(urlGenerator);
510:
511: JFreeChart chart = new JFreeChart(title, titleFont, plot,
512: legend);
513:
514: return chart;
515: }
516:
517: }
|