KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DifferenceChartDemo.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: DifferenceChartDemo.java,v 1.2 2003/07/21 10:23:13 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 10-Jul-2003 : Version 1 (DG);
35  *
36  */

37
38 package org.jfree.chart.demo;
39
40 import java.awt.Color JavaDoc;
41
42 import org.jfree.chart.ChartFactory;
43 import org.jfree.chart.ChartPanel;
44 import org.jfree.chart.JFreeChart;
45 import org.jfree.chart.Spacer;
46 import org.jfree.chart.axis.DateAxis;
47 import org.jfree.chart.axis.ValueAxis;
48 import org.jfree.chart.plot.XYPlot;
49 import org.jfree.chart.renderer.XYDifferenceRenderer;
50 import org.jfree.data.XYDataset;
51 import org.jfree.data.time.Day;
52 import org.jfree.data.time.TimeSeries;
53 import org.jfree.data.time.TimeSeriesCollection;
54 import org.jfree.ui.ApplicationFrame;
55 import org.jfree.ui.RefineryUtilities;
56
57 /**
58  * A simple demonstration of the XYDifferenceRenderer.
59  *
60  * @author David Gilbert
61  */

62 public class DifferenceChartDemo extends ApplicationFrame {
63
64     /**
65      * Creates a new demo.
66      *
67      * @param title the frame title.
68      */

69     public DifferenceChartDemo(String JavaDoc title) {
70
71         super(title);
72
73         TimeSeries series1 = new TimeSeries("Random 1");
74         TimeSeries series2 = new TimeSeries("Random 2");
75         double value1 = 0.0;
76         double value2 = 0.0;
77         Day day = new Day();
78         for (int i = 0; i < 200; i++) {
79             value1 = value1 + Math.random() - 0.5;
80             value2 = value2 + Math.random() - 0.5;
81             series1.add(day, value1);
82             series2.add(day, value2);
83             day = (Day) day.next();
84         }
85
86         TimeSeriesCollection dataset = new TimeSeriesCollection();
87         dataset.addSeries(series1);
88         dataset.addSeries(series2);
89
90         JFreeChart chart = createChart(dataset);
91
92         ChartPanel chartPanel = new ChartPanel(chart);
93         chartPanel.setPreferredSize(new java.awt.Dimension JavaDoc(500, 270));
94         setContentPane(chartPanel);
95
96     }
97
98     /**
99      * Creates a chart.
100      *
101      * @param dataset the dataset.
102      *
103      * @return The chart.
104      */

105     private JFreeChart createChart(XYDataset dataset) {
106         JFreeChart chart = ChartFactory.createTimeSeriesChart(
107             "Difference Chart Demo",
108             "Time", "Value",
109             dataset,
110             true, // legend
111
true, // tool tips
112
false // URLs
113
);
114         chart.setBackgroundPaint(Color.white);
115        
116         XYPlot plot = chart.getXYPlot();
117         plot.setRenderer(new XYDifferenceRenderer(Color.green, Color.red, false));
118         plot.setBackgroundPaint(Color.lightGray);
119         plot.setDomainGridlinePaint(Color.white);
120         plot.setRangeGridlinePaint(Color.white);
121         plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
122         
123         ValueAxis domainAxis = new DateAxis("Time");
124         domainAxis.setLowerMargin(0.0);
125         domainAxis.setUpperMargin(0.0);
126         plot.setDomainAxis(domainAxis);
127         plot.setForegroundAlpha(0.5f);
128         return chart;
129     }
130     
131     /**
132      * Starting point for the demonstration application.
133      *
134      * @param args ignored.
135      */

136     public static void main(String JavaDoc[] args) {
137
138         DifferenceChartDemo demo = new DifferenceChartDemo("Difference Chart Demo");
139         demo.pack();
140         RefineryUtilities.centerFrameOnScreen(demo);
141         demo.setVisible(true);
142
143     }
144
145 }
146
Popular Tags