KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > demo > WaterfallChartDemo


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * -----------------------
23  * WaterfallChartDemo.java
24  * -----------------------
25  * (C) Copyright 2003 by Object Refinery Limited and Contributors.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): -;
29  *
30  * $Id: WaterfallChartDemo.java,v 1.7 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 21-Oct-2003 : Version 1 (DG);
35  * 06-Nov-2003 : Modified to use ChartFactory (DG);
36  *
37  */

38
39 package org.jfree.chart.demo;
40
41 import java.awt.Color JavaDoc;
42 import java.text.DecimalFormat JavaDoc;
43
44 import org.jfree.chart.ChartFactory;
45 import org.jfree.chart.ChartPanel;
46 import org.jfree.chart.JFreeChart;
47 import org.jfree.chart.Spacer;
48 import org.jfree.chart.axis.NumberTickUnit;
49 import org.jfree.chart.axis.TickUnits;
50 import org.jfree.chart.axis.ValueAxis;
51 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
52 import org.jfree.chart.plot.CategoryPlot;
53 import org.jfree.chart.plot.PlotOrientation;
54 import org.jfree.chart.renderer.BarRenderer;
55 import org.jfree.data.CategoryDataset;
56 import org.jfree.data.DefaultCategoryDataset;
57 import org.jfree.ui.ApplicationFrame;
58 import org.jfree.ui.RefineryUtilities;
59
60 /**
61  * A sample waterfall chart.
62  */

63 public class WaterfallChartDemo extends ApplicationFrame {
64
65     /**
66      * Creates a new WaterFall Chart demo.
67      *
68      * @param title the frame title.
69      */

70     public WaterfallChartDemo(String JavaDoc title) {
71
72         super(title);
73         
74         CategoryDataset dataset = createDataset();
75         JFreeChart chart = createChart(dataset);
76         ChartPanel chartPanel = new ChartPanel(chart);
77         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
78         chartPanel.setEnforceFileExtensions(false);
79         setContentPane(chartPanel);
80        
81     }
82
83     /**
84      * Creates a sample dataset for the demo.
85      *
86      * @return A sample dataset.
87      */

88     private CategoryDataset createDataset() {
89         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
90         dataset.addValue(15.76, "Product 1", "Labour");
91         dataset.addValue(8.66, "Product 1", "Administration");
92         dataset.addValue(4.71, "Product 1", "Marketing");
93         dataset.addValue(3.51, "Product 1", "Distribution");
94         dataset.addValue(32.64, "Product 1", "Total Expense");
95         return dataset;
96     }
97     
98     // ****************************************************************************
99
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
100
// * Please note that commercial support and documentation is available from: *
101
// * *
102
// * http://www.object-refinery.com/jfreechart/support.html *
103
// * *
104
// * This is not only a great service for developers, but is a VERY IMPORTANT *
105
// * source of funding for the JFreeChart project. Please support us so that *
106
// * we can continue developing free software. *
107
// ****************************************************************************
108

109     /**
110      * Returns the chart.
111      *
112      * @param dataset the dataset.
113      *
114      * @return The chart.
115      */

116     private JFreeChart createChart(CategoryDataset dataset) {
117         
118         JFreeChart chart = ChartFactory.createWaterfallChart(
119             "Product Cost Breakdown",
120             "Expense Category",
121             "Cost Per Unit",
122             dataset,
123             PlotOrientation.VERTICAL,
124             true,
125             true,
126             false
127         );
128         chart.setBackgroundPaint(Color.white);
129
130         CategoryPlot plot = chart.getCategoryPlot();
131         plot.setBackgroundPaint(Color.lightGray);
132         plot.setRangeGridlinePaint(Color.white);
133         plot.setRangeGridlinesVisible(true);
134         plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
135         
136         ValueAxis rangeAxis = plot.getRangeAxis();
137         
138         // create a custom tick unit collection...
139
DecimalFormat JavaDoc formatter = new DecimalFormat JavaDoc("##,###");
140         formatter.setNegativePrefix("(");
141         formatter.setNegativeSuffix(")");
142         TickUnits standardUnits = new TickUnits();
143         standardUnits.add(new NumberTickUnit(5, formatter));
144         standardUnits.add(new NumberTickUnit(10, formatter));
145         standardUnits.add(new NumberTickUnit(20, formatter));
146         standardUnits.add(new NumberTickUnit(50, formatter));
147         standardUnits.add(new NumberTickUnit(100, formatter));
148         standardUnits.add(new NumberTickUnit(200, formatter));
149         standardUnits.add(new NumberTickUnit(500, formatter));
150         standardUnits.add(new NumberTickUnit(1000, formatter));
151         standardUnits.add(new NumberTickUnit(2000, formatter));
152         standardUnits.add(new NumberTickUnit(5000, formatter));
153         rangeAxis.setStandardTickUnits(standardUnits);
154
155         BarRenderer renderer = (BarRenderer) plot.getRenderer();
156         renderer.setDrawBarOutline(false);
157
158         DecimalFormat JavaDoc labelFormatter = new DecimalFormat JavaDoc("$##,###.00");
159         labelFormatter.setNegativePrefix("(");
160         labelFormatter.setNegativeSuffix(")");
161         renderer.setItemLabelGenerator(
162             new StandardCategoryItemLabelGenerator(labelFormatter, false)
163         );
164         renderer.setItemLabelsVisible(true);
165
166         return chart;
167     }
168
169     /**
170      * Starting point for the demo.
171      *
172      * @param args ignored.
173      */

174     public static void main(String JavaDoc[] args) {
175         WaterfallChartDemo demo = new WaterfallChartDemo("Waterfall Chart Demo");
176         demo.pack();
177         RefineryUtilities.centerFrameOnScreen(demo);
178         demo.setVisible(true);
179     }
180     
181 }
182
Popular Tags