KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.openi.chart;
2
3 import com.tonbeller.jpivot.chart.ChartFactory;
4 import com.tonbeller.jpivot.olap.model.OlapException;
5 import com.tonbeller.jpivot.olap.model.OlapModel;
6
7 import org.apache.log4j.Logger;
8
9 import org.jfree.chart.ChartUtilities;
10 import org.jfree.chart.JFreeChart;
11 import org.jfree.chart.StandardLegend;
12 import org.jfree.chart.axis.CategoryAxis;
13 import org.jfree.chart.axis.DateAxis;
14 import org.jfree.chart.axis.NumberAxis;
15 import org.jfree.chart.axis.ValueAxis;
16 import org.jfree.chart.labels.PieToolTipGenerator;
17 import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
18 import org.jfree.chart.plot.CategoryPlot;
19 import org.jfree.chart.plot.MultiplePiePlot;
20 import org.jfree.chart.plot.PiePlot;
21 import org.jfree.chart.plot.PiePlot3D;
22 import org.jfree.chart.plot.PlotOrientation;
23 import org.jfree.chart.plot.XYPlot;
24 import org.jfree.chart.renderer.category.LineAndShapeRenderer;
25 import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
26 import org.jfree.chart.renderer.xy.XYItemRenderer;
27 import org.jfree.chart.urls.CategoryURLGenerator;
28 import org.jfree.chart.urls.PieURLGenerator;
29
30 import org.jfree.data.category.CategoryDataset;
31 import org.jfree.data.category.DefaultCategoryDataset;
32 import org.jfree.data.xy.XYDataset;
33
34 import org.jfree.ui.Spacer;
35
36 import org.jfree.util.TableOrder;
37
38 import org.openi.analysis.Analysis;
39 import org.openi.xmla.DatasetAdapter;
40
41 import java.awt.Color JavaDoc;
42 import java.awt.Font JavaDoc;
43 import java.awt.Paint JavaDoc;
44
45 import java.io.FileNotFoundException JavaDoc;
46 import java.io.IOException JavaDoc;
47 import java.io.OutputStream JavaDoc;
48
49 import java.text.SimpleDateFormat JavaDoc;
50 import java.util.Locale JavaDoc;
51
52
53 /**
54  * This is a badly needed an extracted factory class to properly test chart creation.
55  * Before was couple with the chart component.
56  *
57  */

58 public class EnhancedChartFactory extends ChartFactory {
59     private static Logger logger = Logger.getLogger(EnhancedChartFactory.class);
60
61     public EnhancedChartFactory() {
62         super();
63     }
64
65     /**
66      * wrapper method that uses openi domain objects as input, returns chart.
67      *
68      * @param analysis
69      * @param dataset
70      * @return
71      * @throws OlapException
72      */

73     public static JFreeChart createChart(Analysis analysis,
74         OlapModel olapModel, Locale JavaDoc locale) throws OlapException {
75         logger.debug("creating chart for analysis: " +
76             analysis.getAnalysisTitle());
77         
78         JFreeChart chart = EnhancedChartFactory.createChart(olapModel,
79                 analysis.getChartType(), analysis.getChartTitle(),
80                 analysis.getHorizAxisLabel(), analysis.getVertAxisLabel(),
81                 analysis.isShowLegend(), true,
82                 analysis.isDrillThroughEnabled(),
83             new Font JavaDoc(analysis.getFontName(), analysis.getFontStyle(), analysis.getFontSize()),
84             new Color JavaDoc(analysis.getBgColorR(), analysis.getBgColorG(), analysis.getBgColorB()),
85             new Font JavaDoc(analysis.getSlicerFontName(), analysis.getSlicerFontStyle(), analysis.getSlicerFontSize()),
86             new Font JavaDoc(analysis.getAxisFontName(), analysis.getAxisFontStyle(), analysis.getAxisFontSize()),
87             new Font JavaDoc(analysis.getAxisTickFontName(), analysis.getAxisTickFontStyle(), analysis.getAxisTickFontSize()),
88             new Font JavaDoc(analysis.getLegendFontName(), analysis.getLegendFontStyle(), analysis.getLegendFontSize()),
89             analysis.getLegendPosition(),
90             analysis.getTickLabelRotate(),
91             1.0f,
92             analysis.isShowSlicer(),
93             analysis.getSlicerPosition(),
94             analysis.getSlicerAlignment(),
95             analysis.getShowPareto(),
96             locale);
97         
98         return chart;
99     }
100
101     /**
102      * method used to stream chart directly to the output stream.
103      * Intended for use in a servlet (e.g. - dashboard servlet).
104      *
105      * @param out
106      * @param analysis
107      * @param dataset
108      * @param width
109      * @param height
110      * @throws FileNotFoundException
111      * @throws IOException
112      * @throws OlapException
113      */

114     public static void createChart(OutputStream JavaDoc out, Analysis analysis,
115         OlapModel olapModel, int width, int height, Locale JavaDoc locale)
116         throws FileNotFoundException JavaDoc, IOException JavaDoc, OlapException {
117         logger.debug("writing chart for analysis: " +
118             analysis.getAnalysisTitle() + " " + width + "x" + height);
119         logger.debug("to stream type: " + out.getClass().getName());
120     
121         float quality = 1.0f;
122         JFreeChart chart = EnhancedChartFactory.createChart(analysis, olapModel, locale);
123         ChartUtilities.writeChartAsJPEG(out, quality, chart, width, height);
124     }
125
126     /**
127      * factory method used in the wcf EnhancedChartComponent, also used internally by
128      * other wrapper createChart methods
129      *
130      * @return
131      * @throws OlapException
132      */

133     public static JFreeChart createChart(OlapModel olapModel,
134             int chartType, String JavaDoc chartTitle,
135         String JavaDoc horizAxisLabel, String JavaDoc vertAxisLabel,
136         boolean showLegend, boolean showTooltips,
137         boolean drillThroughEnabled, Font JavaDoc titleFont,
138         Paint JavaDoc bgPaint, Font JavaDoc slicerFont,
139         Font JavaDoc axisFont, Font JavaDoc axisTickFont,
140         Font JavaDoc legendFont, int legendPosition,
141         double tickLabelRotate,
142         float foregroundAlpha, boolean showSlicer,
143         int slicerPosition, int slicerAlignment,
144         boolean showPareto, Locale JavaDoc locale) throws OlapException {
145         logger.debug("trying to create chartType: " + chartType);
146
147         JFreeChart chart = null;
148
149         // Font titleFont = new Font(fontName, fontStyle, fontSize);
150

151         //jpivotPieURLGenerator pieUrlgenerator = new jpivotPieURLGenerator(PiePlot.PER_ROW);
152
CategoryURLGenerator urlGenerator = null;
153         
154         DatasetAdapter adapter = new DatasetAdapter(locale);
155         CategoryDataset dataset = null;
156
157         switch (chartType) {
158         case 1:
159                 dataset = adapter.buildCategoryDataset(olapModel);
160             chart = EnhancedChartFactory.createBarChart(chartTitle, titleFont,
161                     horizAxisLabel, vertAxisLabel, dataset,
162                     PlotOrientation.VERTICAL, showLegend, showTooltips,
163                     drillThroughEnabled, urlGenerator);
164
165             break;
166
167         case 2:
168                 dataset = adapter.buildCategoryDataset(olapModel);
169             chart = EnhancedChartFactory.createBarChart3D(chartTitle,
170                     titleFont, horizAxisLabel, vertAxisLabel, dataset,
171                     PlotOrientation.VERTICAL, showLegend, showTooltips,
172                     drillThroughEnabled, urlGenerator);
173
174             break;
175
176         case 3:
177                 dataset = adapter.buildCategoryDataset(olapModel);
178             chart = EnhancedChartFactory.createBarChart(chartTitle, titleFont,
179                     horizAxisLabel, vertAxisLabel, dataset,
180                     PlotOrientation.HORIZONTAL, showLegend, showTooltips,
181                     drillThroughEnabled, urlGenerator);
182
183             break;
184
185         case 4:
186             dataset = adapter.buildCategoryDataset(olapModel);
187             chart = EnhancedChartFactory.createBarChart3D(chartTitle,
188                     titleFont, horizAxisLabel, vertAxisLabel, dataset,
189                     PlotOrientation.HORIZONTAL, showLegend, showTooltips,
190                     drillThroughEnabled, urlGenerator);
191
192             break;
193
194         case 5:
195             dataset = adapter.buildCategoryDataset(olapModel);
196             chart = EnhancedChartFactory.createStackedBarChart(chartTitle,
197                     titleFont, horizAxisLabel, vertAxisLabel, dataset,
198                     PlotOrientation.VERTICAL, showLegend, showTooltips,
199                     drillThroughEnabled, urlGenerator);
200
201             break;
202
203         case 6:
204             dataset = adapter.buildCategoryDataset(olapModel);
205             chart = EnhancedChartFactory.createStackedBarChart3D(chartTitle,
206                     titleFont, horizAxisLabel, vertAxisLabel, dataset,
207                     PlotOrientation.VERTICAL, showLegend, showTooltips,
208                     drillThroughEnabled, urlGenerator);
209
210             break;
211
212         case 7:
213             dataset = adapter.buildCategoryDataset(olapModel);
214             chart = EnhancedChartFactory.createStackedBarChart(chartTitle,
215                     titleFont, horizAxisLabel, vertAxisLabel, dataset,
216                     PlotOrientation.HORIZONTAL, showLegend, showTooltips,
217                     drillThroughEnabled, urlGenerator);
218
219             break;
220
221         case 8:
222             dataset = adapter.buildCategoryDataset(olapModel);
223             chart = EnhancedChartFactory.createStackedBarChart3D(chartTitle,
224                     titleFont, horizAxisLabel, vertAxisLabel, dataset,
225                     PlotOrientation.HORIZONTAL, showLegend, showTooltips,
226                     drillThroughEnabled, urlGenerator);
227
228             break;
229
230         case 9:
231             dataset = adapter.buildCategoryDataset(olapModel);
232             chart = EnhancedChartFactory.createLineChart(chartTitle, titleFont,
233                     horizAxisLabel, vertAxisLabel, dataset,
234                     PlotOrientation.VERTICAL, showLegend, showTooltips,
235                     drillThroughEnabled, urlGenerator);
236
237             break;
238
239         case 10:
240             dataset = adapter.buildCategoryDataset(olapModel);
241             chart = EnhancedChartFactory.createLineChart(chartTitle, titleFont,
242                     horizAxisLabel, vertAxisLabel, dataset,
243                     PlotOrientation.HORIZONTAL, showLegend, showTooltips,
244                     drillThroughEnabled, urlGenerator);
245
246             break;
247
248         case 11:
249             dataset = adapter.buildCategoryDataset(olapModel);
250             chart = EnhancedChartFactory.createAreaChart(chartTitle, titleFont,
251                     horizAxisLabel, vertAxisLabel, dataset,
252                     PlotOrientation.VERTICAL, showLegend, showTooltips,
253                     drillThroughEnabled, urlGenerator);
254
255             break;
256
257         case 12:
258             dataset = adapter.buildCategoryDataset(olapModel);
259             chart = EnhancedChartFactory.createAreaChart(chartTitle, titleFont,
260                     horizAxisLabel, vertAxisLabel, dataset,
261                     PlotOrientation.HORIZONTAL, showLegend, showTooltips,
262                     drillThroughEnabled, urlGenerator);
263
264             break;
265
266         case 13:
267             dataset = adapter.buildCategoryDataset(olapModel);
268             chart = EnhancedChartFactory.createStackedAreaChart(chartTitle,
269                     titleFont, horizAxisLabel, vertAxisLabel, dataset,
270                     PlotOrientation.VERTICAL, showLegend, showTooltips,
271                     drillThroughEnabled, urlGenerator);
272
273             break;
274
275         case 14:
276             dataset = adapter.buildCategoryDataset(olapModel);
277             chart = EnhancedChartFactory.createStackedAreaChart(chartTitle,
278                     titleFont, horizAxisLabel, vertAxisLabel, dataset,
279                     PlotOrientation.HORIZONTAL, showLegend, showTooltips,
280                     drillThroughEnabled, urlGenerator);
281
282             break;
283
284         case 15:
285             dataset = adapter.buildCategoryDataset(olapModel);
286             chart = EnhancedChartFactory.createPieChart(chartTitle, titleFont,
287                     dataset, TableOrder.BY_COLUMN, showLegend, showTooltips,
288                     drillThroughEnabled, null);
289
290             break;
291
292         case 16:
293             dataset = adapter.buildCategoryDataset(olapModel);
294             chart = EnhancedChartFactory.createPieChart(chartTitle, titleFont,
295                     dataset, TableOrder.BY_ROW, showLegend, showTooltips,
296                     drillThroughEnabled, null);
297
298             break;
299
300         case 17:
301             dataset = adapter.buildCategoryDataset(olapModel);
302             chart = EnhancedChartFactory.create3DPieChart(chartTitle,
303                     titleFont, dataset, TableOrder.BY_COLUMN, showLegend,
304                     showTooltips, drillThroughEnabled, null);
305
306             break;
307
308         case 18:
309             dataset = adapter.buildCategoryDataset(olapModel);
310             chart = EnhancedChartFactory.create3DPieChart(chartTitle,
311                     titleFont, dataset, TableOrder.BY_ROW, showLegend,
312                     showTooltips, drillThroughEnabled, null);
313
314             break;
315
316         case 19:
317             chart = EnhancedChartFactory.createTimeChart(chartTitle,
318                     horizAxisLabel, vertAxisLabel,
319                     new DatasetAdapter(locale).buildXYDataset(olapModel));
320             break;
321
322         default:
323             throw new OlapException("An unknown Chart Type was requested: " +
324                 chartType);
325         }
326
327         // chart.setTitle("customized title");
328
ChartCustomizer customizer = new ChartCustomizer();
329         customizer.customizeChart(chart, bgPaint, axisFont,
330                 axisTickFont, legendFont, legendPosition, tickLabelRotate, foregroundAlpha);
331         
332         if(showPareto){
333                 if(chart.getPlot() instanceof CategoryPlot){
334                     customizer.generatePareto((CategoryPlot)chart.getPlot());
335                 }
336         }
337         
338         if(adapter.getNumberFormatter() != null){
339                 if(chart.getPlot() instanceof CategoryPlot){
340                     customizer.formatNumberAxis(adapter.getNumberFormatter(),
341                             (CategoryPlot)chart.getPlot());
342                 }
343         }
344         
345         logger.debug("showSlicer=" + showSlicer);
346         if(showSlicer){
347                 customizer.customizeSlicer(
348                         chart,
349                         olapModel.getResult(),
350                         slicerFont,
351                         slicerPosition,
352                         slicerAlignment
353                         );
354         }
355         
356         
357         return chart;
358     }
359
360     public static JFreeChart createTimeChart(String JavaDoc title,
361         String JavaDoc horizAxisLabel, String JavaDoc vertAxisLabel, XYDataset dataset) {
362         logger.debug("creating time series chart");
363
364         JFreeChart chart = org.jfree.chart.ChartFactory.createTimeSeriesChart(title,
365                 horizAxisLabel, vertAxisLabel, dataset, true, true, false);
366
367         chart.setBackgroundPaint(Color.white);
368
369         StandardLegend sl = (StandardLegend) chart.getLegend();
370         sl.setDisplaySeriesShapes(true);
371
372         XYPlot plot = chart.getXYPlot();
373         plot.setBackgroundPaint(Color.lightGray);
374         plot.setDomainGridlinePaint(Color.white);
375         plot.setRangeGridlinePaint(Color.white);
376         plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
377         plot.setDomainCrosshairVisible(false);
378         plot.setRangeCrosshairVisible(false);
379
380         XYItemRenderer renderer = plot.getRenderer();
381
382         if (renderer instanceof StandardXYItemRenderer) {
383             StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
384             rr.setPlotShapes(true);
385             rr.setShapesFilled(true);
386             rr.setItemLabelsVisible(true);
387         }
388
389         DateAxis axis = (DateAxis) plot.getDomainAxis();
390         axis.setDateFormatOverride(new SimpleDateFormat JavaDoc("MMM-yyyy"));
391
392         return chart;
393     }
394
395     public static JFreeChart createLineChart(String JavaDoc title,
396         java.awt.Font JavaDoc titleFont, String JavaDoc categoryAxisLabel,
397         String JavaDoc valueAxisLabel, CategoryDataset data,
398         PlotOrientation orientation, boolean legend, boolean tooltips,
399         boolean urls, CategoryURLGenerator urlGenerator) {
400         CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel);
401         ValueAxis valueAxis = new NumberAxis(valueAxisLabel);
402
403         LineAndShapeRenderer renderer = new LineAndShapeRenderer();
404
405         // renderer.setLinesVisible(true);
406
// renderer.setShapesVisible(false);
407
if (tooltips) {
408             renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
409         }
410
411         if (urls) {
412             renderer.setItemURLGenerator(urlGenerator);
413         }
414
415         CategoryPlot plot = new CategoryPlot(data, categoryAxis, valueAxis,
416                 renderer);
417         plot.setOrientation(orientation);
418
419         JFreeChart chart = new JFreeChart(title, titleFont, plot, legend);
420
421         return chart;
422     }
423
424     public static JFreeChart create3DPieChart(String JavaDoc title,
425         java.awt.Font JavaDoc titleFont, CategoryDataset data, TableOrder order,
426         boolean legend, boolean tooltips, boolean urls,
427         PieURLGenerator urlGenerator) {
428         MultiplePiePlot plot = new MultiplePiePlot(data);
429         plot.setDataExtractOrder(order);
430
431         //plot.setOutlineStroke(null);
432
JFreeChart pieChart = new JFreeChart(new PiePlot3D(null));
433         pieChart.setBackgroundPaint(null);
434         plot.setPieChart(pieChart);
435
436         PiePlot3D pp = (PiePlot3D) plot.getPieChart().getPlot();
437         pp.setBackgroundPaint(null);
438         //pp.setInsets(new Insets(0, 5, 5, 5));
439

440         // no outline around each piechart
441
pp.setOutlineStroke(null);
442
443         PieToolTipGenerator tooltipGenerator = null;
444
445         if (tooltips) {
446             // tooltipGenerator = new StandardPieToolTipGenerator();
447
}
448
449         if (!urls) {
450             urlGenerator = null;
451         }
452
453         pp.setToolTipGenerator(tooltipGenerator);
454         pp.setLabelGenerator(null);
455         pp.setURLGenerator(urlGenerator);
456
457         JFreeChart chart = new JFreeChart(title, titleFont, plot, legend);
458
459         return chart;
460     }
461
462     public static JFreeChart createPieChart(String JavaDoc title,
463         java.awt.Font JavaDoc titleFont, CategoryDataset data, TableOrder order,
464         boolean legend, boolean tooltips, boolean urls,
465         PieURLGenerator urlGenerator) {
466         MultiplePiePlot plot = new MultiplePiePlot(data);
467         plot.setDataExtractOrder(order);
468
469         PiePlot pp = (PiePlot) plot.getPieChart().getPlot();
470         //pp.setInsets(new Insets(0, 5, 5, 5));
471
pp.setBackgroundPaint(null);
472         // no outline around each piechart
473
pp.setOutlineStroke(null);
474
475         //plot.setOutlineStroke(null);
476
PieToolTipGenerator tooltipGenerator = null;
477
478         if (tooltips) {
479             // tooltipGenerator = new StandardPieToolTipGenerator();
480
}
481
482         //PieURLGenerator urlGenerator = null;
483
if (!urls) {
484             urlGenerator = null;
485         }
486
487         // pp.setToolTipGenerator(tooltipGenerator);
488
pp.setLabelGenerator(null);
489         pp.setURLGenerator(urlGenerator);
490
491         JFreeChart chart = new JFreeChart(title, titleFont, plot, legend);
492
493         return chart;
494     }
495 }
496
Popular Tags