KickJava   Java API By Example, From Geeks To Geeks.

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


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  * OverlaidCategoryChartDemo.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: OverlaidCategoryChartDemo.java,v 1.19 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 13-May-2002 : Version 1 (JB);
35  * 25-Jun-2002 : Removed unnecessary imports (DG);
36  * 11-Oct-2002 : Fixed errors reported by Checkstyle (DG);
37  *
38  */

39
40 package org.jfree.chart.demo;
41
42 import java.awt.Color JavaDoc;
43 import java.awt.Font JavaDoc;
44 import java.text.DecimalFormat JavaDoc;
45
46 import org.jfree.chart.ChartPanel;
47 import org.jfree.chart.JFreeChart;
48 import org.jfree.chart.axis.CategoryAxis;
49 import org.jfree.chart.axis.NumberAxis;
50 import org.jfree.chart.axis.NumberTickUnit;
51 import org.jfree.chart.plot.CategoryPlot;
52 import org.jfree.chart.plot.DatasetRenderingOrder;
53 import org.jfree.chart.renderer.IntervalBarRenderer;
54 import org.jfree.chart.renderer.LineAndShapeRenderer;
55 import org.jfree.data.CategoryDataset;
56 import org.jfree.data.DatasetUtilities;
57 import org.jfree.data.DefaultIntervalCategoryDataset;
58 import org.jfree.ui.ApplicationFrame;
59 import org.jfree.ui.RefineryUtilities;
60
61 /**
62  * An overlaid category chart.
63  *
64  * @author Jeremy Bowman
65  */

66 public class OverlaidCategoryChartDemo extends ApplicationFrame {
67
68     /** The categories. */
69     //private static final String[] CATEGORIES = { "1", "3", "5", "10", "20" };
70

71     /** The bar colors. */
72     private static Color JavaDoc[] barColors = null;
73
74     /** The dot colors. */
75     private static Color JavaDoc[] dotColors = null;
76
77     /** The line colors. */
78     private static Color JavaDoc[] lineColors = null;
79
80     /** The label font. */
81     private static Font JavaDoc labelFont = null;
82
83     /** The bold label font. */
84     //private static Font boldLabelFont = null;
85

86     /** The title font. */
87     private static Font JavaDoc titleFont = null;
88
89     /** The chart. */
90     private JFreeChart chart = null;
91
92     static {
93         barColors = new Color JavaDoc[1];
94         barColors[0] = new Color JavaDoc(51, 102, 153);
95         dotColors = new Color JavaDoc[1];
96         dotColors[0] = Color.white;
97         lineColors = new Color JavaDoc[4];
98         lineColors[0] = Color.red;
99         lineColors[1] = Color.blue;
100         lineColors[2] = Color.yellow;
101         lineColors[3] = Color.magenta;
102         labelFont = new Font JavaDoc("Helvetica", Font.PLAIN, 10);
103         //boldLabelFont = new Font("Helvetica", Font.BOLD, 10);
104
titleFont = new Font JavaDoc("Helvetica", Font.BOLD, 14);
105     }
106
107     /**
108      * Creates a new demo.
109      *
110      * @param title the frame title.
111      */

112     public OverlaidCategoryChartDemo(String JavaDoc title) {
113
114         super(title);
115         DefaultIntervalCategoryDataset barData = null;
116         double[][] lows = {{-.0315, .0159, .0306, .0453, .0557}};
117         double[][] highs = {{.1931, .1457, .1310, .1163, .1059}};
118         barData = new DefaultIntervalCategoryDataset(lows, highs);
119
120         double[][] vals = {{0.0808, 0.0808, 0.0808, 0.0808, 0.0808}};
121         CategoryDataset dotData = DatasetUtilities.createCategoryDataset(
122             "Series ",
123             "Category ",
124             vals
125         );
126
127         double[][] lineVals = new double[4][5];
128         for (int i = 0; i < 4; i++) {
129             for (int j = 0; j < 5; j++) {
130                 lineVals[i][j] = (Math.random() * 0.56) - 0.18;
131             }
132         }
133         CategoryDataset lineData = DatasetUtilities.createCategoryDataset("Series ", "Category ",
134                                                                           lineVals);
135
136         String JavaDoc ctitle = "Strategie Sicherheit";
137         String JavaDoc xTitle = "Zeitraum (in Jahren)";
138         String JavaDoc yTitle = "Performance";
139         CategoryAxis xAxis = new CategoryAxis(xTitle);
140         xAxis.setLabelFont(titleFont);
141         xAxis.setTickLabelFont(labelFont);
142         xAxis.setTickMarksVisible(false);
143         NumberAxis yAxis = new NumberAxis(yTitle);
144         yAxis.setLabelFont(titleFont);
145         yAxis.setTickLabelFont(labelFont);
146         yAxis.setRange(-0.2, 0.4);
147         DecimalFormat JavaDoc formatter = new DecimalFormat JavaDoc("0.##%");
148         yAxis.setTickUnit(new NumberTickUnit(0.05, formatter));
149
150         IntervalBarRenderer barRenderer = new IntervalBarRenderer();
151         barRenderer.setItemLabelsVisible(Boolean.TRUE);
152
153         CategoryPlot plot = new CategoryPlot(barData, xAxis, yAxis, barRenderer);
154         plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);
155         
156         plot.setBackgroundPaint(Color.lightGray);
157         plot.setOutlinePaint(Color.black);
158
159         LineAndShapeRenderer dotRenderer = new LineAndShapeRenderer(LineAndShapeRenderer.SHAPES);
160         dotRenderer.setItemLabelsVisible(Boolean.TRUE);
161         
162         plot.setSecondaryDataset(0, dotData);
163         plot.setSecondaryRenderer(0, dotRenderer);
164
165         LineAndShapeRenderer lineRenderer
166             = new LineAndShapeRenderer(LineAndShapeRenderer.SHAPES_AND_LINES);
167         plot.setSecondaryDataset(1, lineData);
168         plot.setSecondaryRenderer(1, lineRenderer);
169
170         chart = new JFreeChart(ctitle, titleFont, plot, false);
171         chart.setBackgroundPaint(Color.white);
172
173         ChartPanel chartPanel = new ChartPanel(chart);
174         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
175         setContentPane(chartPanel);
176
177     }
178
179     // ****************************************************************************
180
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
181
// * Please note that commercial support and documentation is available from: *
182
// * *
183
// * http://www.object-refinery.com/jfreechart/support.html *
184
// * *
185
// * This is not only a great service for developers, but is a VERY IMPORTANT *
186
// * source of funding for the JFreeChart project. Please support us so that *
187
// * we can continue developing free software. *
188
// ****************************************************************************
189

190     /**
191      * Starting point for the demo.
192      *
193      * @param args ignored.
194      */

195     public static void main(String JavaDoc[] args) {
196
197         OverlaidCategoryChartDemo demo
198              = new OverlaidCategoryChartDemo("Overlaid Category Chart Demo");
199         demo.pack();
200         RefineryUtilities.centerFrameOnScreen(demo);
201         demo.setVisible(true);
202
203     }
204
205 }
206
Popular Tags