KickJava   Java API By Example, From Geeks To Geeks.

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


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  * MeterChartDemo.java
24  * -------------------
25  * (C) Copyright 2002, 2003, by Hari and Contributors.
26  *
27  * Original Author: Hari;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: MeterChartDemo.java,v 1.2 2003/11/28 10:57:35 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 08-Apr-2002 : Version 1, contributed by Hari (DG);
35  * 19-Apr-2002 : Renamed JRefineryUtilities-->RefineryUtilities (DG);
36  * 25-Jun-2002 : Removed unnecessary imports (DG);
37  * 11-Oct-2002 : Fixed errors reported by Checkstyle (DG);
38  *
39  */

40
41 package org.jfree.chart.demo;
42
43 import java.awt.Color JavaDoc;
44 import java.awt.Font JavaDoc;
45 import java.awt.GradientPaint JavaDoc;
46 import java.awt.Insets JavaDoc;
47 import java.awt.event.WindowAdapter JavaDoc;
48 import java.awt.event.WindowEvent JavaDoc;
49
50 import javax.swing.JFrame JavaDoc;
51
52 import org.jfree.chart.ChartFrame;
53 import org.jfree.chart.JFreeChart;
54 import org.jfree.chart.MeterLegend;
55 import org.jfree.chart.plot.DialShape;
56 import org.jfree.chart.plot.MeterPlot;
57 import org.jfree.data.DefaultValueDataset;
58 import org.jfree.data.Range;
59 import org.jfree.ui.RefineryUtilities;
60
61 /**
62  * A meter chart demonstration application.
63  *
64  * @author Hari
65  */

66 public class MeterChartDemo {
67
68     /**
69      * Displays a meter chart.
70      *
71      * @param value the value.
72      * @param shape the dial shape.
73      */

74     void displayMeterChart(double value, DialShape shape) {
75
76         DefaultValueDataset data = new DefaultValueDataset(75.0);
77         MeterPlot plot = new MeterPlot(data);
78         plot.setUnits("Degrees");
79         plot.setRange(new Range(20.0, 140.0));
80         plot.setNormalRange(new Range(70.0, 100.0));
81         plot.setWarningRange(new Range(100.0, 120.0));
82         plot.setCriticalRange(new Range(120.0, 140.0));
83         
84         plot.setDialShape(shape);
85         plot.setNeedlePaint(Color.white);
86         plot.setTickLabelFont(new Font JavaDoc("SansSerif", Font.BOLD, 9));
87
88         plot.setInsets(new Insets JavaDoc(5, 5, 5, 5));
89         JFreeChart chart = new JFreeChart(
90             "Meter Chart",
91             JFreeChart.DEFAULT_TITLE_FONT,
92             plot,
93             false
94         );
95
96         MeterLegend legend = new MeterLegend(chart, "Sample Meter");
97         chart.setLegend(legend);
98
99         chart.setBackgroundPaint(new GradientPaint JavaDoc(0, 0, Color.white, 0, 1000, Color.blue));
100
101         JFrame JavaDoc chartFrame = new ChartFrame("Meter Chart", chart);
102         chartFrame.addWindowListener(new WindowAdapter JavaDoc() {
103           /**
104            * Invoked when a window is in the process of being closed.
105            * The close operation can be overridden at this point.
106            */

107           public void windowClosing(WindowEvent JavaDoc e)
108           {
109             System.exit(0);
110           }
111         });
112         chartFrame.pack();
113         RefineryUtilities.positionFrameRandomly(chartFrame);
114         chartFrame.setSize(250, 250);
115         chartFrame.show();
116
117     }
118
119     // ****************************************************************************
120
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
121
// * Please note that commercial support and documentation is available from: *
122
// * *
123
// * http://www.object-refinery.com/jfreechart/support.html *
124
// * *
125
// * This is not only a great service for developers, but is a VERY IMPORTANT *
126
// * source of funding for the JFreeChart project. Please support us so that *
127
// * we can continue developing free software. *
128
// ****************************************************************************
129

130     /**
131      * Starting point for the meter plot demonstration application.
132      *
133      * @param args used to specify the type and value.
134      */

135     public static void main(String JavaDoc[] args) {
136
137         if (args.length == 0) {
138             System.err.println("Usage: java TestMeter <type> <value>");
139             System.err.println("Type: 0 = PIE");
140             System.err.println("Type: 1 = CIRCLE");
141             System.err.println("Type: 2 = CHORD");
142         }
143         MeterChartDemo h = new MeterChartDemo();
144         double val = 85;
145         DialShape dialShape = DialShape.CIRCLE;
146         if (args.length > 0) {
147             int type = Integer.parseInt(args[0]);
148             if (type == 0) {
149                 dialShape = DialShape.PIE;
150             }
151             else if (type == 1) {
152                 dialShape = DialShape.CIRCLE;
153             }
154             else if (type == 0) {
155                 dialShape = DialShape.CHORD;
156             }
157         }
158         if (args.length > 1) {
159             val = new Double JavaDoc(args[1]).doubleValue();
160         }
161         h.displayMeterChart(val, dialShape);
162
163     }
164
165 }
166
Popular Tags