KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > chart > ChartFactory


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  */

11 package com.tonbeller.jpivot.chart;
12
13 import org.jfree.chart.JFreeChart;
14 import org.jfree.chart.axis.CategoryAxis;
15 import org.jfree.chart.axis.CategoryAxis3D;
16 import org.jfree.chart.axis.DateAxis;
17 import org.jfree.chart.axis.NumberAxis;
18 import org.jfree.chart.axis.NumberAxis3D;
19 import org.jfree.chart.axis.ValueAxis;
20 import org.jfree.chart.labels.CategoryToolTipGenerator;
21 import org.jfree.chart.labels.PieToolTipGenerator;
22 import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
23 import org.jfree.chart.labels.StandardPieToolTipGenerator;
24 import org.jfree.chart.labels.StandardXYToolTipGenerator;
25 import org.jfree.chart.labels.XYToolTipGenerator;
26 import org.jfree.chart.plot.CategoryPlot;
27 import org.jfree.chart.plot.MultiplePiePlot;
28 import org.jfree.chart.plot.PiePlot;
29 import org.jfree.chart.plot.PiePlot3D;
30 import org.jfree.chart.plot.PlotOrientation;
31 import org.jfree.chart.plot.XYPlot;
32 import org.jfree.chart.renderer.category.AreaRenderer;
33 import org.jfree.chart.renderer.category.BarRenderer;
34 import org.jfree.chart.renderer.category.BarRenderer3D;
35 import org.jfree.chart.renderer.category.CategoryItemRenderer;
36 import org.jfree.chart.renderer.category.LineAndShapeRenderer;
37 import org.jfree.chart.renderer.category.StackedAreaRenderer;
38 import org.jfree.chart.renderer.category.StackedBarRenderer;
39 import org.jfree.chart.renderer.category.StackedBarRenderer3D;
40 import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
41 import org.jfree.chart.urls.CategoryURLGenerator;
42 import org.jfree.chart.urls.PieURLGenerator;
43 import org.jfree.chart.urls.StandardXYURLGenerator;
44 import org.jfree.chart.urls.XYURLGenerator;
45 import org.jfree.data.category.CategoryDataset;
46 import org.jfree.data.xy.XYDataset;
47 import org.jfree.util.TableOrder;
48 /**
49  * Utility methods for creating charts.
50  * This class is derived from JFreeChart ChartFactory class.
51  *
52  */

53 public class ChartFactory {
54     
55     // set url prefix to empty string
56
static String JavaDoc urlPrefix = "";
57     
58     /**
59      * Creates a chart containing multiple pie charts, from a TableDataset.
60      *
61      * @param title the chart title.
62      * @param data the dataset for the chart.
63      * @param extractType <code>PER_ROW</code> or <code>PER_COLUMN</code> (defined in
64      * {@link PiePlot}).
65      * @param legend a flag specifying whether or not a legend is required.
66      * @param tooltips configure chart to generate tool tips?
67      * @param urls configure chart to generate URLs?
68      *
69      * @return a pie chart.
70      */

71     public static JFreeChart createPieChart(String JavaDoc title,
72                                             java.awt.Font JavaDoc titleFont,
73                                             CategoryDataset data,
74                                             TableOrder order,
75                                             boolean legend,
76                                             boolean tooltips,
77                                             boolean urls,
78                                                                                         PieURLGenerator urlGenerator
79                                             ) {
80
81         MultiplePiePlot plot = new MultiplePiePlot(data);
82                 plot.setDataExtractOrder(order);
83                 
84                 PiePlot pp= (PiePlot) plot.getPieChart().getPlot();
85                 //pp.setInsets(new Insets(0, 5, 5, 5));
86
pp.setBackgroundPaint(null);
87                 // no outline around each piechart
88
pp.setOutlineStroke(null);
89                 //plot.setOutlineStroke(null);
90
PieToolTipGenerator tooltipGenerator = null;
91         if (tooltips) {
92             tooltipGenerator = new StandardPieToolTipGenerator();
93         }
94                
95         //PieURLGenerator urlGenerator = null;
96
if (!urls) {
97             urlGenerator = null;
98         }
99                 
100                 pp.setToolTipGenerator(tooltipGenerator);
101                 pp.setLabelGenerator(null);
102         pp.setURLGenerator(urlGenerator);
103                 
104         JFreeChart chart = new JFreeChart(title, titleFont, plot, legend);
105
106         return chart;
107
108     }
109         /**
110      * Creates a sample dataset for the demo.
111      *
112      * @return A sample dataset.
113      */

114         
115         public static JFreeChart create3DPieChart(String JavaDoc title,
116                                             java.awt.Font JavaDoc titleFont,
117                                             CategoryDataset data,
118                                             TableOrder order,
119                                             boolean legend,
120                                             boolean tooltips,
121                                             boolean urls,
122                                                                                         PieURLGenerator urlGenerator
123                                             ) {
124        
125                 MultiplePiePlot plot = new MultiplePiePlot(data);
126                 plot.setDataExtractOrder(order);
127                 
128                 //plot.setOutlineStroke(null);
129

130                 JFreeChart pieChart = new JFreeChart(new PiePlot3D(null));
131         pieChart.setBackgroundPaint(null);
132                 plot.setPieChart(pieChart);
133                 
134                 PiePlot3D pp = (PiePlot3D) plot.getPieChart().getPlot();
135                 pp.setBackgroundPaint(null);
136                 //pp.setInsets(new Insets(0, 5, 5, 5));
137

138                 // no outline around each piechart
139
pp.setOutlineStroke(null);
140                 
141                 PieToolTipGenerator tooltipGenerator = null;
142         if (tooltips) {
143             tooltipGenerator = new StandardPieToolTipGenerator();
144         }
145                 
146                 if (!urls) {
147             urlGenerator = null;
148         }
149                 
150                 pp.setToolTipGenerator(tooltipGenerator);
151                 pp.setLabelGenerator(null);
152         pp.setURLGenerator(urlGenerator);
153         JFreeChart chart = new JFreeChart(title, titleFont, plot, legend);
154
155         return chart;
156
157     }
158     /**
159      * Creates a vertical bar chart with default settings.
160      *
161      * @param title the chart title.
162      * @param categoryAxisLabel the label for the category axis.
163      * @param valueAxisLabel the label for the value axis.
164      * @param data the dataset for the chart.
165      * @param legend a flag specifying whether or not a legend is required.
166      * @param tooltips configure chart to generate tool tips?
167      * @param urls configure chart to generate URLs?
168      *
169      * @return a vertical bar chart.
170      */

171     public static JFreeChart createBarChart(String JavaDoc title,
172                                                     java.awt.Font JavaDoc titleFont,
173                                                     String JavaDoc categoryAxisLabel,
174                                                     String JavaDoc valueAxisLabel,
175                                                     CategoryDataset data,
176                                                     PlotOrientation orientation,
177                                                     boolean legend,
178                                                     boolean tooltips,
179                                                     boolean urls,
180                                                     CategoryURLGenerator urlGenerator
181                                                     ) {
182
183         CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel);
184         ValueAxis valueAxis = new NumberAxis(valueAxisLabel);
185         BarRenderer renderer = new BarRenderer();
186
187         if (tooltips) {
188             renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
189         }
190         if (urls) {
191             renderer.setItemURLGenerator(urlGenerator);
192         }
193         CategoryPlot plot = new CategoryPlot(data, categoryAxis, valueAxis, renderer);
194         plot.setOrientation(orientation);
195         JFreeChart chart = new JFreeChart(title, titleFont, plot, legend);
196
197         return chart;
198
199     }
200
201     /**
202      * Creates a vertical 3D-effect bar chart with default settings.
203      *
204      * @param title the chart title.
205      * @param categoryAxisLabel the label for the category axis.
206      * @param valueAxisLabel the label for the value axis.
207      * @param data the dataset for the chart.
208      * @param legend a flag specifying whether or not a legend is required.
209      * @param tooltips configure chart to generate tool tips?
210      * @param urls configure chart to generate URLs?
211      *
212      * @return a vertical 3D-effect bar chart.
213      */

214     public static JFreeChart createBarChart3D(String JavaDoc title,
215                                                       java.awt.Font JavaDoc titleFont,
216                                                       String JavaDoc categoryAxisLabel,
217                                                       String JavaDoc valueAxisLabel,
218                                                       CategoryDataset data,
219                                                       PlotOrientation orientation,
220                                                       boolean legend,
221                                                       boolean tooltips,
222                                                       boolean urls,
223                                                       CategoryURLGenerator urlGenerator) {
224
225         CategoryAxis categoryAxis = new CategoryAxis3D(categoryAxisLabel);
226         ValueAxis valueAxis = new NumberAxis3D(valueAxisLabel);
227
228         BarRenderer3D renderer = new BarRenderer3D();
229                 
230                 //renderer.setLabelGenerator(new StandardCategoryLabelGenerator());
231
if (tooltips) {
232                         renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
233         }
234         if (urls) {
235             renderer.setItemURLGenerator(urlGenerator);
236         }
237
238         CategoryPlot plot = new CategoryPlot(data, categoryAxis, valueAxis, renderer);
239         plot.setOrientation(orientation);
240         plot.setForegroundAlpha(0.75f);
241
242         JFreeChart chart = new JFreeChart(title, titleFont, plot, legend);
243
244         return chart;
245
246     }
247     
248     /**
249      * Creates a stacked vertical bar chart with default settings.
250      *
251      * @param title the chart title.
252      * @param domainAxisLabel the label for the category axis.
253      * @param rangeAxisLabel the label for the value axis.
254      * @param data the dataset for the chart.
255      * @param legend a flag specifying whether or not a legend is required.
256      * @param tooltips configure chart to generate tool tips?
257      * @param urls configure chart to generate URLs?
258      *
259      * @return The chart.
260      */

261     public static JFreeChart createStackedBarChart(String JavaDoc title,
262                                                            java.awt.Font JavaDoc titleFont,
263                                                            String JavaDoc domainAxisLabel,
264                                                            String JavaDoc rangeAxisLabel,
265                                                            CategoryDataset data,
266                                                            PlotOrientation orientation,
267                                                            boolean legend,
268                                                            boolean tooltips,
269                                                            boolean urls,
270                                                         CategoryURLGenerator urlGenerator) {
271         
272         CategoryAxis categoryAxis = new CategoryAxis(domainAxisLabel);
273         ValueAxis valueAxis = new NumberAxis(rangeAxisLabel);
274
275         // create the renderer...
276
StackedBarRenderer renderer = new StackedBarRenderer();
277         if (tooltips) {
278             renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
279         }
280         if (urls) {
281             renderer.setItemURLGenerator(urlGenerator);
282         }
283
284         CategoryPlot plot = new CategoryPlot(data, categoryAxis, valueAxis, renderer);
285         plot.setOrientation(orientation);
286         JFreeChart chart = new JFreeChart(title, titleFont, plot, legend);
287
288         return chart;
289             
290     }
291                                                              
292     /**
293      * Creates a stacked vertical bar chart with default settings.
294      *
295      * @param title the chart title.
296      * @param categoryAxisLabel the label for the category axis.
297      * @param valueAxisLabel the label for the value axis.
298      * @param data the dataset for the chart.
299      * @param legend a flag specifying whether or not a legend is required.
300      * @param tooltips configure chart to generate tool tips?
301      * @param urls configure chart to generate URLs?
302      *
303      * @return a stacked vertical bar chart.
304      */

305     public static JFreeChart createStackedBarChart3D(String JavaDoc title,
306                                                             java.awt.Font JavaDoc titleFont,
307                                                              String JavaDoc categoryAxisLabel,
308                                                              String JavaDoc valueAxisLabel,
309                                                              CategoryDataset data,
310                                                              PlotOrientation orientation,
311                                                              boolean legend,
312                                                              boolean tooltips,
313                                                              boolean urls,
314                                                             CategoryURLGenerator urlGenerator) {
315
316         // create the axes...
317
CategoryAxis categoryAxis = new CategoryAxis3D(categoryAxisLabel);
318         ValueAxis valueAxis = new NumberAxis3D(valueAxisLabel);
319
320         // create the renderer...
321
CategoryItemRenderer renderer = new StackedBarRenderer3D();
322         CategoryToolTipGenerator toolTipGenerator = null;
323         if (tooltips) {
324             toolTipGenerator = new StandardCategoryToolTipGenerator();
325         }
326         if ( urls ) {
327             renderer.setItemURLGenerator(urlGenerator);
328         }
329         renderer.setToolTipGenerator(toolTipGenerator);
330         
331         // create the plot...
332
CategoryPlot plot = new CategoryPlot(data, categoryAxis, valueAxis, renderer);
333         plot.setOrientation(orientation);
334
335         // create the chart...
336
JFreeChart chart = new JFreeChart(title, titleFont, plot, legend);
337
338         return chart;
339
340     }
341
342
343     /**
344      * Creates a line chart with default settings.
345      *
346      * @param title the chart title.
347      * @param categoryAxisLabel the label for the category axis.
348      * @param valueAxisLabel the label for the value axis.
349      * @param data the dataset for the chart.
350      * @param legend a flag specifying whether or not a legend is required.
351      * @param tooltips configure chart to generate tool tips?
352      * @param urls configure chart to generate URLs?
353      *
354      * @return a line chart.
355      */

356     public static JFreeChart createLineChart(String JavaDoc title,
357                                             java.awt.Font JavaDoc titleFont,
358                                              String JavaDoc categoryAxisLabel,
359                                              String JavaDoc valueAxisLabel,
360                                              CategoryDataset data,
361                                              PlotOrientation orientation,
362                                              boolean legend,
363                                              boolean tooltips,
364                                              boolean urls,
365                                             CategoryURLGenerator urlGenerator) {
366
367         CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel);
368         ValueAxis valueAxis = new NumberAxis(valueAxisLabel);
369
370         LineAndShapeRenderer renderer = new LineAndShapeRenderer();
371         renderer.setLinesVisible(true);
372         renderer.setShapesVisible(false);
373         if (tooltips) {
374             renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
375         }
376         if (urls) {
377             renderer.setItemURLGenerator(urlGenerator);
378         }
379         CategoryPlot plot = new CategoryPlot(data, categoryAxis, valueAxis, renderer);
380         plot.setOrientation(orientation);
381         JFreeChart chart = new JFreeChart(title, titleFont, plot, legend);
382
383         return chart;
384
385     }
386
387     /**
388      * Creates an area chart with default settings.
389      *
390      * @param title the chart title.
391      * @param categoryAxisLabel the label for the category axis.
392      * @param valueAxisLabel the label for the value axis.
393      * @param data the dataset for the chart.
394      * @param legend a flag specifying whether or not a legend is required.
395      * @param tooltips configure chart to generate tool tips?
396      * @param urls configure chart to generate URLs?
397      *
398      * @return an area chart.
399      */

400     public static JFreeChart createAreaChart(String JavaDoc title,
401                                             java.awt.Font JavaDoc titleFont,
402                                              String JavaDoc categoryAxisLabel,
403                                              String JavaDoc valueAxisLabel,
404                                              CategoryDataset data,
405                                              PlotOrientation orientation,
406                                              boolean legend,
407                                              boolean tooltips,
408                                              boolean urls,
409                                              CategoryURLGenerator urlGenerator) {
410
411         CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel);
412         categoryAxis.setCategoryMargin(0.0);
413         ValueAxis valueAxis = new NumberAxis(valueAxisLabel);
414         AreaRenderer renderer = new AreaRenderer();
415         if (tooltips) {
416             renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
417                 }
418         if (urls) {
419             renderer.setItemURLGenerator(urlGenerator);
420         }
421         CategoryPlot plot = new CategoryPlot(data, categoryAxis, valueAxis, renderer);
422         plot.setOrientation(orientation);
423         JFreeChart chart = new JFreeChart(title, titleFont, plot, legend);
424
425         return chart;
426
427     }
428
429     /**
430      * Creates a stacked area chart with default settings.
431      *
432      * @param title the chart title.
433      * @param categoryAxisLabel the label for the category axis.
434      * @param valueAxisLabel the label for the value axis.
435      * @param data the dataset for the chart.
436      * @param legend a flag specifying whether or not a legend is required.
437      * @param tooltips configure chart to generate tool tips?
438      * @param urls configure chart to generate URLs?
439      *
440      * @return an area chart.
441      */

442     public static JFreeChart createStackedAreaChart(String JavaDoc title,
443                                                     java.awt.Font JavaDoc titleFont,
444                                                     String JavaDoc categoryAxisLabel,
445                                                     String JavaDoc valueAxisLabel,
446                                                     CategoryDataset data,
447                                                     PlotOrientation orientation,
448                                                     boolean legend,
449                                                     boolean tooltips,
450                                                     boolean urls,
451     CategoryURLGenerator urlGenerator) {
452
453         CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel);
454         ValueAxis valueAxis = new NumberAxis(valueAxisLabel);
455
456         StackedAreaRenderer renderer = new StackedAreaRenderer();
457         if (tooltips) {
458             renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
459         }
460         if (urls) {
461             renderer.setItemURLGenerator(urlGenerator);
462         }
463
464         CategoryPlot plot = new CategoryPlot(data, categoryAxis, valueAxis, renderer);
465         plot.setOrientation(orientation);
466         JFreeChart chart = new JFreeChart(title, titleFont, plot, legend);
467
468         return chart;
469
470     }
471
472
473     /**
474      * Creates and returns a time series chart.
475      * <P>
476      * A time series chart is an XYPlot with a date axis (horizontal) and a number axis (vertical),
477      * and each data item is connected with a line.
478      * <P>
479      * Note that you can supply a TimeSeriesCollection to this method, as it implements the
480      * XYDataset interface.
481      *
482      * @param title the chart title.
483      * @param timeAxisLabel a label for the time axis.
484      * @param valueAxisLabel a label for the value axis.
485      * @param data the dataset for the chart.
486      * @param legend a flag specifying whether or not a legend is required.
487      * @param tooltips configure chart to generate tool tips?
488      * @param urls configure chart to generate URLs?
489      *
490      * @return a time series chart.
491      */

492     public static JFreeChart createTimeSeriesChart(String JavaDoc title,
493                                                 java.awt.Font JavaDoc titleFont,
494                                                    String JavaDoc timeAxisLabel,
495                                                    String JavaDoc valueAxisLabel,
496                                                    XYDataset data,
497                                                    boolean legend,
498                                                    boolean tooltips,
499                                                    boolean urls) {
500
501         ValueAxis timeAxis = new DateAxis(timeAxisLabel);
502         timeAxis.setLowerMargin(0.02); // reduce the default margins on the time axis
503
timeAxis.setUpperMargin(0.02);
504         NumberAxis valueAxis = new NumberAxis(valueAxisLabel);
505         valueAxis.setAutoRangeIncludesZero(false); // override default
506
XYPlot plot = new XYPlot(data, timeAxis, valueAxis, null);
507
508         XYToolTipGenerator tooltipGenerator = null;
509         if (tooltips) {
510             tooltipGenerator = StandardXYToolTipGenerator.getTimeSeriesInstance();
511                             //new StandardXYToolTipGenerator(DateFormat.getDateInstance());
512
}
513
514         XYURLGenerator urlGenerator = null;
515         if (urls) {
516             urlGenerator = new StandardXYURLGenerator();
517         }
518                 
519                 plot.setRenderer(new StandardXYItemRenderer(StandardXYItemRenderer.LINES, tooltipGenerator, urlGenerator));
520         
521                 JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
522
523         return chart;
524
525     }
526
527 }
528
Popular Tags