KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ParetoChartDemo.java
24  * --------------------
25  * (C) Copyright 2003, by Object Refinery Limited.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited).
28  * Contributor(s): -;
29  *
30  * $Id: ParetoChartDemo.java,v 1.13 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 05-Mar-2003 : Version 1 (DG);
35  * 27-Aug-2003 : Moved SortOrder from org.jfree.data --> org.jfree.util (DG);
36  *
37  */

38
39 package org.jfree.chart.demo;
40
41 import java.awt.Color JavaDoc;
42 import java.text.NumberFormat 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.TextTitle;
48 import org.jfree.chart.axis.CategoryAxis;
49 import org.jfree.chart.axis.NumberAxis;
50 import org.jfree.chart.plot.CategoryPlot;
51 import org.jfree.chart.plot.DatasetRenderingOrder;
52 import org.jfree.chart.plot.PlotOrientation;
53 import org.jfree.chart.renderer.LineAndShapeRenderer;
54 import org.jfree.data.CategoryDataset;
55 import org.jfree.data.DataUtilities;
56 import org.jfree.data.DatasetUtilities;
57 import org.jfree.data.DefaultKeyedValues;
58 import org.jfree.data.KeyedValues;
59 import org.jfree.ui.ApplicationFrame;
60 import org.jfree.ui.RefineryUtilities;
61 import org.jfree.util.SortOrder;
62
63 /**
64  * A demo showing the creation of a pareto chart.
65  *
66  * @author David Gilbert
67  */

68 public class ParetoChartDemo extends ApplicationFrame {
69
70     /**
71      * Creates a new demo instance.
72      *
73      * @param title the frame title.
74      */

75     public ParetoChartDemo(String JavaDoc title) {
76
77         super(title);
78
79         DefaultKeyedValues data = new DefaultKeyedValues();
80         data.addValue("C", new Integer JavaDoc(4843));
81         data.addValue("C++", new Integer JavaDoc(2098));
82         data.addValue("C#", new Integer JavaDoc(26));
83         data.addValue("Java", new Integer JavaDoc(1901));
84         data.addValue("Perl", new Integer JavaDoc(2507));
85         data.addValue("PHP", new Integer JavaDoc(1689));
86         data.addValue("Python", new Integer JavaDoc(948));
87         data.addValue("Ruby", new Integer JavaDoc(100));
88         data.addValue("SQL", new Integer JavaDoc(263));
89         data.addValue("Unix Shell", new Integer JavaDoc(485));
90
91         data.sortByValues(SortOrder.DESCENDING);
92         KeyedValues cumulative = DataUtilities.getCumulativePercentages(data);
93         CategoryDataset dataset = DatasetUtilities.createCategoryDataset("Languages", data);
94
95         // create the chart...
96
JFreeChart chart = ChartFactory.createBarChart(
97             "Freshmeat Software Projects", // chart title
98
"Language", // domain axis label
99
"Projects", // range axis label
100
dataset, // data
101
PlotOrientation.VERTICAL,
102             true, // include legend
103
true,
104             false
105         );
106
107         // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
108
chart.addSubtitle(new TextTitle("By Programming Language"));
109         chart.addSubtitle(new TextTitle("As at 5 March 2003"));
110
111         // set the background color for the chart...
112
chart.setBackgroundPaint(new Color JavaDoc(0xBBBBDD));
113
114         // get a reference to the plot for further customisation...
115
CategoryPlot plot = chart.getCategoryPlot();
116
117         CategoryAxis domainAxis = plot.getDomainAxis();
118         domainAxis.setLowerMargin(0.02);
119         domainAxis.setUpperMargin(0.02);
120
121         // set the range axis to display integers only...
122
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
123         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
124
125         LineAndShapeRenderer renderer2 = new LineAndShapeRenderer();
126
127         CategoryDataset dataset2 = DatasetUtilities.createCategoryDataset("Cumulative", cumulative);
128         NumberAxis axis2 = new NumberAxis("Percent");
129         axis2.setNumberFormatOverride(NumberFormat.getPercentInstance());
130         plot.setSecondaryRangeAxis(0, axis2);
131         plot.setSecondaryDataset(0, dataset2);
132         plot.setSecondaryRenderer(0, renderer2);
133         plot.mapSecondaryDatasetToRangeAxis(0, new Integer JavaDoc(0));
134
135         plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);
136         // OPTIONAL CUSTOMISATION COMPLETED.
137

138         // add the chart to a panel...
139
ChartPanel chartPanel = new ChartPanel(chart);
140         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(550, 270));
141         setContentPane(chartPanel);
142
143     }
144
145     // ****************************************************************************
146
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
147
// * Please note that commercial support and documentation is available from: *
148
// * *
149
// * http://www.object-refinery.com/jfreechart/support.html *
150
// * *
151
// * This is not only a great service for developers, but is a VERY IMPORTANT *
152
// * source of funding for the JFreeChart project. Please support us so that *
153
// * we can continue developing free software. *
154
// ****************************************************************************
155

156     /**
157      * Starting point for the demonstration application.
158      *
159      * @param args ignored.
160      */

161     public static void main(String JavaDoc[] args) {
162
163         ParetoChartDemo demo = new ParetoChartDemo("Pareto Chart Demo");
164         demo.pack();
165         RefineryUtilities.centerFrameOnScreen(demo);
166         demo.setVisible(true);
167
168     }
169
170
171 }
172
Popular Tags