KickJava   Java API By Example, From Geeks To Geeks.

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


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  * IntervalBarChartDemo1.java
24  * --------------------------
25  * (C) Copyright 2002, 2003, by Jeremy Bowman and Contributors.
26  *
27  * Original Author: Jeremy Bowman.
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: IntervalBarChartDemo1.java,v 1.8 2003/11/28 10:57:33 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 29-Apr-2002 : Version 1, contributed by Jeremy Bowman. Name changed to
35  * IntervalBarChartDemo, and the chart is displayed in a frame rather than
36  * saved to a file (DG);
37  * 25-Jun-2002 : Removed unnecessary imports (DG);
38  * 10-Oct-2002 : Fixed errors reported by Checkstyle (DG);
39  *
40  */

41
42 package org.jfree.chart.demo;
43
44 import java.awt.Color JavaDoc;
45 import java.awt.Font JavaDoc;
46 import java.text.DecimalFormat JavaDoc;
47
48 import org.jfree.chart.ChartFrame;
49 import org.jfree.chart.JFreeChart;
50 import org.jfree.chart.axis.CategoryAxis;
51 import org.jfree.chart.axis.NumberAxis;
52 import org.jfree.chart.axis.NumberTickUnit;
53 import org.jfree.chart.labels.IntervalCategoryItemLabelGenerator;
54 import org.jfree.chart.plot.CategoryPlot;
55 import org.jfree.chart.plot.PlotOrientation;
56 import org.jfree.chart.renderer.IntervalBarRenderer;
57 import org.jfree.chart.renderer.ItemLabelAnchor;
58 import org.jfree.chart.renderer.ItemLabelPosition;
59 import org.jfree.data.DefaultIntervalCategoryDataset;
60 import org.jfree.ui.TextAnchor;
61
62 /**
63  * An interval bar chart.
64  *
65  * @author Jeremy Bowman
66  */

67 public class IntervalBarChartDemo1 {
68
69     /** The categories. */
70     private static final String JavaDoc[] CATEGORIES = {"1", "3", "5", "10", "20"};
71
72     /** The label font. */
73     private static Font JavaDoc labelFont = null;
74
75     /** The title font. */
76     private static Font JavaDoc titleFont = null;
77
78     /** The chart. */
79     private JFreeChart chart = null;
80
81     static {
82         labelFont = new Font JavaDoc("Helvetica", Font.PLAIN, 10);
83         titleFont = new Font JavaDoc("Helvetica", Font.BOLD, 14);
84     }
85
86     /**
87      * Creates a new demo.
88      */

89     public IntervalBarChartDemo1() {
90
91         DefaultIntervalCategoryDataset data = null;
92         double[][] lows = {{-.0315, .0159, .0306, .0453, .0557}};
93         double[][] highs = {{.1931, .1457, .1310, .1163, .1059}};
94         data = new DefaultIntervalCategoryDataset(lows, highs);
95         data.setCategoryKeys(CATEGORIES);
96
97         String JavaDoc title = "Strategie Sicherheit";
98         String JavaDoc xTitle = "Zeitraum (in Jahren)";
99         String JavaDoc yTitle = "Performance";
100         CategoryAxis xAxis = new CategoryAxis(xTitle);
101         xAxis.setLabelFont(titleFont);
102         xAxis.setTickLabelFont(labelFont);
103         xAxis.setTickMarksVisible(false);
104         NumberAxis yAxis = new NumberAxis(yTitle);
105         yAxis.setLabelFont(titleFont);
106         yAxis.setTickLabelFont(labelFont);
107         yAxis.setRange(-0.2, 0.40);
108         DecimalFormat JavaDoc formatter = new DecimalFormat JavaDoc("0.##%");
109         yAxis.setTickUnit(new NumberTickUnit(0.05, formatter));
110
111         IntervalBarRenderer renderer = new IntervalBarRenderer();
112         renderer.setSeriesPaint(0, new Color JavaDoc(51, 102, 153));
113         renderer.setItemLabelGenerator(new IntervalCategoryItemLabelGenerator());
114         renderer.setItemLabelsVisible(true);
115         renderer.setItemLabelPaint(Color.white);
116         ItemLabelPosition p = new ItemLabelPosition(
117             ItemLabelAnchor.CENTER, TextAnchor.CENTER, TextAnchor.CENTER, 0.0
118         );
119         renderer.setPositiveItemLabelPosition(p);
120         
121         CategoryPlot plot = new CategoryPlot(data, xAxis, yAxis, renderer);
122         plot.setBackgroundPaint(Color.lightGray);
123         plot.setOutlinePaint(Color.white);
124         plot.setOrientation(PlotOrientation.VERTICAL);
125         
126         chart = new JFreeChart(title, titleFont, plot, false);
127         chart.setBackgroundPaint(Color.white);
128     }
129
130     // ****************************************************************************
131
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
132
// * Please note that commercial support and documentation is available from: *
133
// * *
134
// * http://www.object-refinery.com/jfreechart/support.html *
135
// * *
136
// * This is not only a great service for developers, but is a VERY IMPORTANT *
137
// * source of funding for the JFreeChart project. Please support us so that *
138
// * we can continue developing free software. *
139
// ****************************************************************************
140

141     /**
142      * Returns the chart.
143      *
144      * @return the chart.
145      */

146     public JFreeChart getChart() {
147         return chart;
148     }
149
150     /**
151      * Starting point for the demo.
152      *
153      * @param args ignored.
154      */

155     public static void main(String JavaDoc[] args) {
156         IntervalBarChartDemo1 sample = new IntervalBarChartDemo1();
157         JFreeChart chart = sample.getChart();
158         ChartFrame frame = new ChartFrame("Interval Bar Chart Demo", chart);
159         frame.pack();
160         frame.setVisible(true);
161     }
162 }
163
Popular Tags