KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SurveyResultsDemo3.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: SurveyResultsDemo3.java,v 1.6 2003/11/28 10:57:33 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 import java.text.DecimalFormat 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.axis.CategoryLabelPosition;
48 import org.jfree.chart.axis.ExtendedCategoryAxis;
49 import org.jfree.chart.axis.ValueAxis;
50 import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
51 import org.jfree.chart.plot.CategoryPlot;
52 import org.jfree.chart.plot.PlotOrientation;
53 import org.jfree.chart.renderer.BarRenderer;
54 import org.jfree.chart.renderer.ItemLabelAnchor;
55 import org.jfree.chart.renderer.ItemLabelPosition;
56 import org.jfree.data.CategoryDataset;
57 import org.jfree.data.DefaultCategoryDataset;
58 import org.jfree.text.TextBlockAnchor;
59 import org.jfree.ui.ApplicationFrame;
60 import org.jfree.ui.RectangleAnchor;
61 import org.jfree.ui.RefineryUtilities;
62 import org.jfree.ui.TextAnchor;
63
64 /**
65  * A chart showing...
66  *
67  * @author David Gilbert
68  */

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

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

95     private CategoryDataset createDataset() {
96         
97         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
98         dataset.addValue(2.61, "Results", "Sm.");
99         dataset.addValue(2.70, "Results", "Med.");
100         dataset.addValue(2.90, "Results", "Lg.");
101         dataset.addValue(2.74, "Results", "All");
102         return dataset;
103
104     }
105     
106     /**
107      * Creates a chart.
108      *
109      * @param dataset the dataset.
110      *
111      * @return The chart.
112      */

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

184     /**
185      * Starting point for the demonstration application.
186      *
187      * @param args ignored.
188      */

189     public static void main(String JavaDoc[] args) {
190
191         SurveyResultsDemo3 demo = new SurveyResultsDemo3("Survey Results Demo 3");
192         demo.pack();
193         RefineryUtilities.centerFrameOnScreen(demo);
194         demo.setVisible(true);
195
196     }
197
198 }
199
Popular Tags