KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > japex > report > LineChart


1 /*
2  * LineChart.java
3  *
4  * Created on April 7, 2005, 11:09 PM
5  */

6
7 package com.sun.japex.report;
8
9
10 import java.awt.Color JavaDoc;
11 import java.text.SimpleDateFormat JavaDoc;
12
13
14 import org.jfree.chart.ChartFactory;
15 import org.jfree.chart.ChartPanel;
16 import org.jfree.chart.ChartUtilities;
17 import org.jfree.chart.JFreeChart;
18 import org.jfree.chart.axis.DateAxis;
19 import org.jfree.chart.plot.XYPlot;
20 import org.jfree.chart.renderer.xy.XYItemRenderer;
21 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
22 import org.jfree.data.time.Month;
23 import org.jfree.data.time.TimeSeries;
24 import org.jfree.data.time.TimeSeriesCollection;
25 import org.jfree.data.xy.XYDataset;
26 import org.jfree.ui.ApplicationFrame;
27 import org.jfree.ui.RectangleInsets;
28 import org.jfree.ui.RefineryUtilities;
29 import org.jfree.ui.Spacer;
30 import org.jfree.util.UnitType;
31
32 import java.io.File JavaDoc;
33
34 public class LineChart
35     {
36
37   
38     public static void main(String JavaDoc[] args) {
39         try {
40             XYDataset dataset = createDataset();
41             JFreeChart chart = createChart("test", dataset);
42
43             // Converts chart in JPEG file named chart.jpg
44
ChartUtilities.saveChartAsJPEG(new File JavaDoc("chart.jpg"), chart, 500, 300);
45         } catch (Exception JavaDoc e) {
46             e.printStackTrace();
47         }
48     }
49     
50     /**
51      * Creates a chart.
52      *
53      * @param dataset a dataset.
54      *
55      * @return A chart.
56      */

57     public static JFreeChart createChart(String JavaDoc title, XYDataset dataset) {
58
59         JFreeChart chart = ChartFactory.createTimeSeriesChart(
60             title, // title
61
"Date", // x-axis label
62
"TPS", // y-axis label
63
dataset, // data
64
true, // create legend?
65
true, // generate tooltips?
66
false // generate URLs?
67
);
68
69         chart.setBackgroundPaint(Color.lightGray);
70         XYPlot plot = (XYPlot) chart.getPlot();
71         plot.setBackgroundPaint(Color.white);
72         plot.setDomainGridlinePaint(Color.lightGray);
73         plot.setRangeGridlinePaint(Color.lightGray);
74         
75         
76         //upgrade Jfreechart and jcommon to use Rectangle instead of spacer
77
plot.setAxisOffset(new Spacer(1, 5.0, 5.0, 5.0, 5.0));
78         //plot.setAxisOffset(new RectangleInsets(UnitType.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
79

80         plot.setDomainCrosshairVisible(true);
81         plot.setRangeCrosshairVisible(true);
82         
83         XYItemRenderer r = plot.getRenderer();
84         if (r instanceof XYLineAndShapeRenderer) {
85             XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
86             renderer.setDefaultShapesVisible(true);
87             renderer.setDefaultShapesFilled(true);
88         }
89         
90         DateAxis axis = (DateAxis) plot.getDomainAxis();
91         //axis.setDateFormatOverride(new SimpleDateFormat("DDD-MM-yy"));
92
axis.setDateFormatOverride(null);
93         return chart;
94
95     }
96
97     /**
98      * Creates a dataset, consisting of two series of monthly data.
99      *
100      * @return the dataset.
101      */

102     private static XYDataset createDataset() {
103
104         TimeSeries s1 = new TimeSeries("L&G European Index Trust", Month.class);
105         s1.add(new Month(2, 2001), 181.8);
106         s1.add(new Month(3, 2001), 167.3);
107         s1.add(new Month(4, 2001), 153.8);
108         s1.add(new Month(5, 2001), 167.6);
109         s1.add(new Month(6, 2001), 158.8);
110         s1.add(new Month(7, 2001), 148.3);
111         s1.add(new Month(8, 2001), 153.9);
112         s1.add(new Month(9, 2001), 142.7);
113         s1.add(new Month(10, 2001), 123.2);
114         s1.add(new Month(11, 2001), 131.8);
115         s1.add(new Month(12, 2001), 139.6);
116         s1.add(new Month(1, 2002), 142.9);
117         s1.add(new Month(2, 2002), 138.7);
118         s1.add(new Month(3, 2002), 137.3);
119         s1.add(new Month(4, 2002), 143.9);
120         s1.add(new Month(5, 2002), 139.8);
121         s1.add(new Month(6, 2002), 137.0);
122         s1.add(new Month(7, 2002), 132.8);
123
124         TimeSeriesCollection dataset = new TimeSeriesCollection();
125         dataset.addSeries(s1);
126
127         dataset.setDomainIsPointsInTime(true);
128
129         return dataset;
130
131     }
132     
133 }
134
Popular Tags