KickJava   Java API By Example, From Geeks To Geeks.

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


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  * WaterfallChartDemo2.java
24  * ------------------------
25  * (C) Copyright 2003 by Object Refinery Limited and Contributors.
26  *
27  * Original Author: Darshan Shah;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: WaterfallChartDemo2.java,v 1.4 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 20-Oct-2003 : Version 1, based on code contributed by Darshan Shah (DG);
35  *
36  */

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

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

69     public WaterfallChartDemo2(String JavaDoc title) {
70
71         super(title);
72         
73         CategoryDataset dataset = createDataset();
74         JFreeChart chart = createChart(dataset);
75         
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      * Creates a sample dataset for the demo.
84      *
85      * @return A sample dataset.
86      */

87     private CategoryDataset createDataset() {
88          
89         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
90         dataset.addValue(-890.76, "S1", "PY OM");
91         dataset.addValue(-3021.51, "S1", "Vol CM");
92         dataset.addValue(-218.32, "S1", "Price");
93         dataset.addValue(221.75, "S1", "Mat Inf");
94         dataset.addValue(-1504.1, "S1", "Oth Inf");
95         dataset.addValue(7103.8, "S1", "VCP");
96         dataset.addValue(3503.3, "S1", "CM FX");
97         dataset.addValue(-6561.97, "S1", "Base");
98         dataset.addValue(-824.25, "S1", "OM");
99
100         return dataset;
101         
102     }
103     
104     /**
105      * Returns the chart.
106      *
107      * @param dataset the dataset.
108      *
109      * @return The chart.
110      */

111     private JFreeChart createChart(CategoryDataset dataset) {
112         
113         CategoryAxis xAxis = new CategoryAxis("Category");
114         NumberAxis yAxis = new NumberAxis("$ in Thousands");
115         yAxis.setLowerMargin(0.10);
116         yAxis.setUpperMargin(0.10);
117         
118         // create a custom tick unit collection...
119
DecimalFormat JavaDoc formatter = new DecimalFormat JavaDoc("##,###");
120         formatter.setNegativePrefix("(");
121         formatter.setNegativeSuffix(")");
122         TickUnits standardUnits = new TickUnits();
123         standardUnits.add(new NumberTickUnit(200, formatter));
124         standardUnits.add(new NumberTickUnit(500, formatter));
125         standardUnits.add(new NumberTickUnit(1000, formatter));
126         standardUnits.add(new NumberTickUnit(2000, formatter));
127         standardUnits.add(new NumberTickUnit(5000, formatter));
128
129         yAxis.setStandardTickUnits(standardUnits);
130
131         // ****************************************************************************
132
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
133
// * Please note that commercial support and documentation is available from: *
134
// * *
135
// * http://www.object-refinery.com/jfreechart/support.html *
136
// * *
137
// * This is not only a great service for developers, but is a VERY IMPORTANT *
138
// * source of funding for the JFreeChart project. Please support us so that *
139
// * we can continue developing free software. *
140
// ****************************************************************************
141

142         DecimalFormat JavaDoc labelFormatter = new DecimalFormat JavaDoc("##,###");
143         labelFormatter.setNegativePrefix("(");
144         labelFormatter.setNegativeSuffix(")");
145
146         WaterfallBarRenderer renderer = new WaterfallBarRenderer();
147         renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator(labelFormatter));
148         renderer.setItemLabelsVisible(Boolean.TRUE);
149
150         CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);
151         plot.setBackgroundPaint(Color.lightGray);
152         plot.setRangeGridlinePaint(Color.white);
153         plot.setRangeGridlinesVisible(true);
154         Marker baseline = new Marker(0.0,
155                                      Color.blue,
156                                      new java.awt.BasicStroke JavaDoc(1.1f),
157                                      Color.blue,
158                                      1.0f);
159         plot.addRangeMarker(baseline, Layer.FOREGROUND);
160
161         JFreeChart chart = new JFreeChart("OM WaterFall Chart",
162                                           JFreeChart.DEFAULT_TITLE_FONT, plot, false);
163         chart.setBackgroundPaint(Color.white);
164         return chart;
165     }
166
167     /**
168      * Starting point for the demo.
169      *
170      * @param args ignored.
171      */

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