KickJava   Java API By Example, From Geeks To Geeks.

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


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

37 package org.jfree.chart.demo;
38
39 import java.awt.BasicStroke JavaDoc;
40 import java.awt.Color JavaDoc;
41 import java.awt.Font JavaDoc;
42
43 import org.jfree.chart.ChartFactory;
44 import org.jfree.chart.ChartPanel;
45 import org.jfree.chart.JFreeChart;
46 import org.jfree.chart.TextTitle;
47 import org.jfree.chart.annotations.CategoryTextAnnotation;
48 import org.jfree.chart.axis.CategoryAnchor;
49 import org.jfree.chart.axis.CategoryAxis;
50 import org.jfree.chart.plot.CategoryPlot;
51 import org.jfree.chart.plot.PlotOrientation;
52 import org.jfree.chart.renderer.CategoryItemRenderer;
53 import org.jfree.chart.renderer.ItemLabelAnchor;
54 import org.jfree.chart.renderer.ItemLabelPosition;
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 import org.jfree.ui.TextAnchor;
60
61 /**
62  * A horizontal bar chart showing survey results.
63  *
64  * @author David Gilbert
65  */

66 public class SurveyResultsDemo extends ApplicationFrame {
67
68     /**
69      * Creates a new demo.
70      *
71      * @param title the frame title.
72      */

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

92     private CategoryDataset createDataset() {
93         
94         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
95         dataset.addValue(2.01, "Results", "Category 1");
96         dataset.addValue(2.02, "Results", "Category 2");
97         dataset.addValue(2.00, "Results", "Category 3");
98         dataset.addValue(1.97, "Results", "Category 4");
99         dataset.addValue(1.44, "Results", "Category 5");
100         dataset.addValue(1.49, "Results", "Category 6");
101         dataset.addValue(1.49, "Results", "Category 7");
102         dataset.addValue(1.48, "Results", "Category 8");
103         dataset.addValue(4.26, "Results", "Category 9");
104         dataset.addValue(4.08, "Results", "Category 10");
105         dataset.addValue(4.03, "Results", "Category 11");
106         dataset.addValue(3.92, "Results", "Category 12");
107         dataset.addValue(3.99, "Results", "Category 13");
108         dataset.addValue(2.23, "Results", "Category 14");
109         dataset.addValue(2.60, "Results", "Overall");
110         return dataset;
111
112     }
113     
114     /**
115      * Creates a chart.
116      *
117      * @param dataset the dataset.
118      *
119      * @return The chart.
120      */

121     private JFreeChart createChart(CategoryDataset dataset) {
122
123         JFreeChart chart = ChartFactory.createBarChart(
124             null, // chart title
125
null, // domain axis label
126
null, // range axis label
127
dataset, // data
128
PlotOrientation.HORIZONTAL,
129             false, // include legend
130
true,
131             false
132         );
133         
134         chart.setBackgroundPaint(Color.white);
135         
136         TextTitle title = new TextTitle("Figure 7 | I. Resources - The site offers users relevant, "
137                                         + "informative and educational resources");
138         title.setHorizontalAlignment(TextTitle.LEFT);
139         title.setBackgroundPaint(Color.red);
140         title.setPaint(Color.white);
141         
142         chart.setTitle(title);
143         CategoryPlot plot = (CategoryPlot) chart.getPlot();
144         plot.setOutlinePaint(null);
145         plot.setDomainGridlinesVisible(true);
146         plot.setDomainGridlinePosition(CategoryAnchor.END);
147         plot.setDomainGridlineStroke(new BasicStroke JavaDoc(0.5f));
148         plot.setDomainGridlinePaint(Color.black);
149
150         plot.setRangeGridlinesVisible(false);
151         plot.clearRangeMarkers();
152         
153         CategoryAxis domainAxis = plot.getDomainAxis();
154         domainAxis.setVisible(false);
155         domainAxis.setCategoryMargin(0.50);
156         
157         plot.getRangeAxis().setVisible(false);
158         
159         CategoryItemRenderer renderer = plot.getRenderer();
160         renderer.setSeriesPaint(0, new Color JavaDoc(0x9C, 0xA4, 0x4A));
161         renderer.setOutlineStroke(null);
162         renderer.setBaseOutlineStroke(null);
163         
164         renderer.setItemLabelsVisible(true);
165         renderer.setItemLabelFont(new Font JavaDoc("SansSerif", Font.BOLD, 10));
166         ItemLabelPosition position = new ItemLabelPosition(
167             ItemLabelAnchor.INSIDE3, TextAnchor.CENTER_RIGHT, TextAnchor.CENTER, 0.0
168         );
169         renderer.setPositiveItemLabelPosition(position);
170         
171         CategoryTextAnnotation a1 = new CategoryTextAnnotation("1. White papers are available.",
172             "Category 1", 0.0);
173         a1.setFont(new Font JavaDoc("SansSerif", Font.BOLD, 12));
174         a1.setTextAnchor(TextAnchor.BOTTOM_LEFT);
175         a1.setCategoryAnchor(CategoryAnchor.START);
176         plot.addAnnotation(a1);
177         
178         CategoryTextAnnotation a2 = new CategoryTextAnnotation("2. White papers enhance users "
179             + "understanding of the firm and its expertise.", "Category 2", 0.0);
180         a2.setFont(new Font JavaDoc("SansSerif", Font.PLAIN, 12));
181         a2.setTextAnchor(TextAnchor.BOTTOM_LEFT);
182         a2.setCategoryAnchor(CategoryAnchor.START);
183         plot.addAnnotation(a2);
184
185         CategoryTextAnnotation a3 = new CategoryTextAnnotation("3. White papers are relevant to "
186             + "the firm's prospects and clients.", "Category 3", 0.0);
187         a3.setFont(new Font JavaDoc("SansSerif", Font.PLAIN, 12));
188         a3.setTextAnchor(TextAnchor.BOTTOM_LEFT);
189         a3.setCategoryAnchor(CategoryAnchor.START);
190         plot.addAnnotation(a3);
191
192         CategoryTextAnnotation a4 = new CategoryTextAnnotation("4. White papers are relevant to "
193             + "the firm's positioning.", "Category 4", 0.0);
194         a4.setFont(new Font JavaDoc("SansSerif", Font.PLAIN, 12));
195         a4.setTextAnchor(TextAnchor.BOTTOM_LEFT);
196         a4.setCategoryAnchor(CategoryAnchor.START);
197         plot.addAnnotation(a4);
198
199         CategoryTextAnnotation a5 = new CategoryTextAnnotation("5. Case studies are available.",
200             "Category 5", 0.0);
201         a5.setFont(new Font JavaDoc("SansSerif", Font.BOLD, 12));
202         a5.setTextAnchor(TextAnchor.BOTTOM_LEFT);
203         a5.setCategoryAnchor(CategoryAnchor.START);
204         plot.addAnnotation(a5);
205         
206         CategoryTextAnnotation a6 = new CategoryTextAnnotation("6. Case studies enhance users "
207             + "understanding of the firm and its expertise.", "Category 6", 0.0);
208         a6.setFont(new Font JavaDoc("SansSerif", Font.PLAIN, 12));
209         a6.setTextAnchor(TextAnchor.BOTTOM_LEFT);
210         a6.setCategoryAnchor(CategoryAnchor.START);
211         plot.addAnnotation(a6);
212
213         CategoryTextAnnotation a7 = new CategoryTextAnnotation("7. Case studies are relevant to "
214             + "the firm's prospects and clients.", "Category 7", 0.0);
215         a7.setFont(new Font JavaDoc("SansSerif", Font.PLAIN, 12));
216         a7.setTextAnchor(TextAnchor.BOTTOM_LEFT);
217         a7.setCategoryAnchor(CategoryAnchor.START);
218         plot.addAnnotation(a7);
219
220         CategoryTextAnnotation a8 = new CategoryTextAnnotation("8. White papers are relevant to "
221             + "the firm's positioning.", "Category 8", 0.0);
222         a8.setFont(new Font JavaDoc("SansSerif", Font.PLAIN, 12));
223         a8.setTextAnchor(TextAnchor.BOTTOM_LEFT);
224         a8.setCategoryAnchor(CategoryAnchor.START);
225         plot.addAnnotation(a8);
226
227         CategoryTextAnnotation a9 = new CategoryTextAnnotation("9. Case studies are available.",
228             "Category 9", 0.0);
229         a9.setFont(new Font JavaDoc("SansSerif", Font.BOLD, 12));
230         a9.setTextAnchor(TextAnchor.BOTTOM_LEFT);
231         a9.setCategoryAnchor(CategoryAnchor.START);
232         plot.addAnnotation(a9);
233         
234         CategoryTextAnnotation a10 = new CategoryTextAnnotation("10. Case studies enhance users "
235             + "understanding of the firm and its expertise.", "Category 10", 0.0);
236         a10.setFont(new Font JavaDoc("SansSerif", Font.PLAIN, 12));
237         a10.setTextAnchor(TextAnchor.BOTTOM_LEFT);
238         a10.setCategoryAnchor(CategoryAnchor.START);
239         plot.addAnnotation(a10);
240
241         CategoryTextAnnotation a11 = new CategoryTextAnnotation("11. Case studies are relevant "
242             + "to the firm's prospects and clients.", "Category 11", 0.0);
243         a11.setFont(new Font JavaDoc("SansSerif", Font.PLAIN, 12));
244         a11.setTextAnchor(TextAnchor.BOTTOM_LEFT);
245         a11.setCategoryAnchor(CategoryAnchor.START);
246         plot.addAnnotation(a11);
247
248         CategoryTextAnnotation a12 = new CategoryTextAnnotation("12. White papers are relevant "
249             + "to the firm's positioning.", "Category 12", 0.0);
250         a12.setFont(new Font JavaDoc("SansSerif", Font.PLAIN, 12));
251         a12.setTextAnchor(TextAnchor.BOTTOM_LEFT);
252         a12.setCategoryAnchor(CategoryAnchor.START);
253         plot.addAnnotation(a12);
254
255         CategoryTextAnnotation a13 = new CategoryTextAnnotation("13. Users can easily access "
256             + "resources based on viewer interest.", "Category 13", 0.0);
257         a13.setFont(new Font JavaDoc("SansSerif", Font.BOLD, 12));
258         a13.setTextAnchor(TextAnchor.BOTTOM_LEFT);
259         a13.setCategoryAnchor(CategoryAnchor.START);
260         plot.addAnnotation(a13);
261
262         CategoryTextAnnotation a14 = new CategoryTextAnnotation("14. Access to additional "
263             + "hyperlinks enhances users's ability to find relevant information.",
264             "Category 14", 0.0);
265         a14.setFont(new Font JavaDoc("SansSerif", Font.BOLD, 12));
266         a14.setTextAnchor(TextAnchor.BOTTOM_LEFT);
267         a14.setCategoryAnchor(CategoryAnchor.START);
268         plot.addAnnotation(a14);
269
270         CategoryTextAnnotation a15 = new CategoryTextAnnotation("15. OVERALL EFFECTIVENESS.",
271             "Overall", 0.0);
272         a15.setFont(new Font JavaDoc("SansSerif", Font.BOLD, 12));
273         a15.setTextAnchor(TextAnchor.BOTTOM_LEFT);
274         a15.setCategoryAnchor(CategoryAnchor.START);
275         plot.addAnnotation(a15);
276
277         return chart;
278        
279     }
280
281     // ****************************************************************************
282
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
283
// * Please note that commercial support and documentation is available from: *
284
// * *
285
// * http://www.object-refinery.com/jfreechart/support.html *
286
// * *
287
// * This is not only a great service for developers, but is a VERY IMPORTANT *
288
// * source of funding for the JFreeChart project. Please support us so that *
289
// * we can continue developing free software. *
290
// ****************************************************************************
291

292     /**
293      * Starting point for the demonstration application.
294      *
295      * @param args ignored.
296      */

297     public static void main(String JavaDoc[] args) {
298
299         SurveyResultsDemo demo = new SurveyResultsDemo("Survey Results Demo");
300         demo.pack();
301         RefineryUtilities.centerFrameOnScreen(demo);
302         demo.setVisible(true);
303
304     }
305
306
307 }
308
Popular Tags