1 6 7 package com.sun.japex.report; 8 9 10 import java.awt.Color ; 11 import java.text.SimpleDateFormat ; 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 ; 33 34 public class LineChart 35 { 36 37 38 public static void main(String [] args) { 39 try { 40 XYDataset dataset = createDataset(); 41 JFreeChart chart = createChart("test", dataset); 42 43 ChartUtilities.saveChartAsJPEG(new File ("chart.jpg"), chart, 500, 300); 45 } catch (Exception e) { 46 e.printStackTrace(); 47 } 48 } 49 50 57 public static JFreeChart createChart(String title, XYDataset dataset) { 58 59 JFreeChart chart = ChartFactory.createTimeSeriesChart( 60 title, "Date", "TPS", dataset, true, true, false ); 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 plot.setAxisOffset(new Spacer(1, 5.0, 5.0, 5.0, 5.0)); 78 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(null); 93 return chart; 94 95 } 96 97 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 |