KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SurveyResultsDemo2.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: SurveyResultsDemo2.java,v 1.6 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 31-Oct-2003 : Version 1 (DG);
35  *
36  */

37 package org.jfree.chart.demo;
38
39 import java.awt.Color JavaDoc;
40 import java.awt.Font JavaDoc;
41
42 import org.jfree.chart.ChartFactory;
43 import org.jfree.chart.ChartPanel;
44 import org.jfree.chart.JFreeChart;
45 import org.jfree.chart.TextTitle;
46 import org.jfree.chart.axis.ExtendedCategoryAxis;
47 import org.jfree.chart.axis.ValueAxis;
48 import org.jfree.chart.plot.CategoryPlot;
49 import org.jfree.chart.plot.PlotOrientation;
50 import org.jfree.chart.renderer.BarRenderer;
51 import org.jfree.chart.renderer.ItemLabelAnchor;
52 import org.jfree.chart.renderer.ItemLabelPosition;
53 import org.jfree.data.CategoryDataset;
54 import org.jfree.data.DefaultCategoryDataset;
55 import org.jfree.ui.ApplicationFrame;
56 import org.jfree.ui.RefineryUtilities;
57 import org.jfree.ui.TextAnchor;
58
59 /**
60  * A vertical bar chart.
61  *
62  * @author David Gilbert
63  */

64 public class SurveyResultsDemo2 extends ApplicationFrame {
65
66     /**
67      * Creates a new demo.
68      *
69      * @param title the frame title.
70      */

71     public SurveyResultsDemo2(String JavaDoc title) {
72
73         super(title);
74
75         CategoryDataset dataset = createDataset();
76         JFreeChart chart = createChart(dataset);
77  
78         // add the chart to a panel...
79
ChartPanel chartPanel = new ChartPanel(chart);
80         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(300, 270));
81         setContentPane(chartPanel);
82
83     }
84     
85     /**
86      * Creates a dataset.
87      *
88      * @return The dataset.
89      */

90     private CategoryDataset createDataset() {
91         
92         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
93         dataset.addValue(1.32, "Results", "Sm.");
94         dataset.addValue(0.40, "Results", "Med.");
95         dataset.addValue(2.62, "Results", "Lg.");
96         dataset.addValue(1.44, "Results", "All");
97         return dataset;
98
99     }
100     
101     /**
102      * Creates a chart.
103      *
104      * @param dataset the dataset.
105      *
106      * @return The chart.
107      */

108     private JFreeChart createChart(CategoryDataset dataset) {
109
110         JFreeChart chart = ChartFactory.createBarChart(
111             null, // chart title
112
null, // domain axis label
113
null, // range axis label
114
dataset, // data
115
PlotOrientation.VERTICAL,
116             false, // include legend
117
true,
118             false
119         );
120         
121         chart.setBackgroundPaint(Color.white);
122         chart.getPlot().setOutlinePaint(null);
123         TextTitle title = new TextTitle("Figure 8.5 - Case studies are available");
124         title.setHorizontalAlignment(TextTitle.LEFT);
125         title.setBackgroundPaint(Color.red);
126         title.setPaint(Color.white);
127         
128         chart.setTitle(title);
129         CategoryPlot plot = chart.getCategoryPlot();
130         
131         ValueAxis rangeAxis = plot.getRangeAxis();
132         rangeAxis.setRange(0.0, 5.0);
133         rangeAxis.setVisible(false);
134         
135         ExtendedCategoryAxis domainAxis = new ExtendedCategoryAxis(null);
136         domainAxis.setTickLabelFont(new Font JavaDoc("SansSerif", Font.BOLD, 12));
137         domainAxis.setCategoryMargin(0.30);
138         domainAxis.addSubLabel("Sm.", "(10)");
139         domainAxis.addSubLabel("Med.", "(10)");
140         domainAxis.addSubLabel("Lg.", "(10)");
141         domainAxis.addSubLabel("All", "(10)");
142         plot.setDomainAxis(domainAxis);
143         
144         BarRenderer renderer = (BarRenderer) plot.getRenderer();
145         renderer.setSeriesPaint(0, new Color JavaDoc(0x9C, 0xA4, 0x4A));
146         renderer.setBaseOutlineStroke(null);
147         
148         renderer.setItemLabelsVisible(true);
149         renderer.setItemLabelFont(new Font JavaDoc("SansSerif", Font.PLAIN, 18));
150         ItemLabelPosition position = new ItemLabelPosition(
151             ItemLabelAnchor.INSIDE12, TextAnchor.TOP_CENTER, TextAnchor.CENTER, 0.0
152         );
153         renderer.setPositiveItemLabelPosition(position);
154         renderer.setPositiveItemLabelPositionFallback(new ItemLabelPosition());
155         
156         return chart;
157        
158     }
159
160     // ****************************************************************************
161
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
162
// * Please note that commercial support and documentation is available from: *
163
// * *
164
// * http://www.object-refinery.com/jfreechart/support.html *
165
// * *
166
// * This is not only a great service for developers, but is a VERY IMPORTANT *
167
// * source of funding for the JFreeChart project. Please support us so that *
168
// * we can continue developing free software. *
169
// ****************************************************************************
170

171     /**
172      * Starting point for the demonstration application.
173      *
174      * @param args ignored.
175      */

176     public static void main(String JavaDoc[] args) {
177
178         SurveyResultsDemo2 demo = new SurveyResultsDemo2("Survey Results Demo 2");
179         demo.pack();
180         RefineryUtilities.centerFrameOnScreen(demo);
181         demo.setVisible(true);
182
183     }
184
185 }
186
Popular Tags