1 38 package org.jfree.chart.demo; 39 40 import java.awt.BasicStroke ; 41 import java.awt.Color ; 42 import java.awt.geom.Ellipse2D ; 43 import java.text.NumberFormat ; 44 import java.text.ParseException ; 45 import java.text.SimpleDateFormat ; 46 import java.util.Locale ; 47 48 import org.jfree.chart.ChartPanel; 49 import org.jfree.chart.JFreeChart; 50 import org.jfree.chart.axis.DateAxis; 51 import org.jfree.chart.axis.NumberAxis; 52 import org.jfree.chart.labels.TimeSeriesToolTipGenerator; 53 import org.jfree.chart.plot.XYPlot; 54 import org.jfree.chart.renderer.AreaXYRenderer; 55 import org.jfree.chart.renderer.StackedAreaXYRenderer; 56 import org.jfree.data.DefaultTableXYDataset; 57 import org.jfree.data.TableXYDataset; 58 import org.jfree.data.XYSeries; 59 import org.jfree.ui.ApplicationFrame; 60 import org.jfree.ui.RefineryUtilities; 61 62 67 public class StackedAreaXYChartDemo extends ApplicationFrame { 68 69 74 public StackedAreaXYChartDemo(String title) { 75 76 super(title); 77 78 SimpleDateFormat sdf = new SimpleDateFormat ("dd-MMM-yyyy", Locale.UK); 79 XYSeries series1 = new XYSeries("Series 1", false); 80 XYSeries series2 = new XYSeries("Series 2", false); 81 XYSeries series3 = new XYSeries("Series 3", false); 82 try { 83 series1.add(sdf.parse("03-Jul-2003").getTime(), 115); 84 series1.add(sdf.parse("04-Jul-2003").getTime(), 120); 85 series1.add(sdf.parse("07-Jul-2003").getTime(), 125); 86 series1.add(sdf.parse("08-Jul-2003").getTime(), 160); 87 series1.add(sdf.parse("09-Jul-2003").getTime(), 175); 88 series1.add(sdf.parse("10-Jul-2003").getTime(), 140); 89 series1.add(sdf.parse("11-Jul-2003").getTime(), 145); 90 series1.add(sdf.parse("14-Jul-2003").getTime(), 150); 91 series1.add(sdf.parse("15-Jul-2003").getTime(), 155); 92 series1.add(sdf.parse("16-Jul-2003").getTime(), 160); 93 series1.add(sdf.parse("17-Jul-2003").getTime(), 165); 94 series1.add(sdf.parse("18-Jul-2003").getTime(), 170); 95 96 series2.add(sdf.parse("30-Jun-2003").getTime(), 50); 97 series2.add(sdf.parse("01-Jul-2003").getTime(), 60); 98 series2.add(sdf.parse("02-Jul-2003").getTime(), 70); 99 series2.add(sdf.parse("03-Jul-2003").getTime(), 80); 100 series2.add(sdf.parse("04-Jul-2003").getTime(), 90); 101 series2.add(sdf.parse("07-Jul-2003").getTime(), 100); 102 series2.add(sdf.parse("08-Jul-2003").getTime(), 110); 103 series2.add(sdf.parse("09-Jul-2003").getTime(), 120); 104 series2.add(sdf.parse("10-Jul-2003").getTime(), 130); 105 series2.add(sdf.parse("11-Jul-2003").getTime(), 140); 106 series2.add(sdf.parse("14-Jul-2003").getTime(), 150); 107 series2.add(sdf.parse("15-Jul-2003").getTime(), 160); 108 series2.add(sdf.parse("16-Jul-2003").getTime(), 170); 109 series2.add(sdf.parse("17-Jul-2003").getTime(), 180); 110 series2.add(sdf.parse("18-Jul-2003").getTime(), 190); 111 112 series3.add(sdf.parse("30-Jun-2003").getTime(), 100); 113 series3.add(sdf.parse("01-Jul-2003").getTime(), 120); 114 series3.add(sdf.parse("02-Jul-2003").getTime(), 110); 115 series3.add(sdf.parse("03-Jul-2003").getTime(), 120); 116 series3.add(sdf.parse("04-Jul-2003").getTime(), 130); 117 series3.add(sdf.parse("07-Jul-2003").getTime(), 135); 118 series3.add(sdf.parse("08-Jul-2003").getTime(), 140); 119 series3.add(sdf.parse("09-Jul-2003").getTime(), 155); 120 series3.add(sdf.parse("10-Jul-2003").getTime(), 130); 121 series3.add(sdf.parse("11-Jul-2003").getTime(), 135); 122 series3.add(sdf.parse("14-Jul-2003").getTime(), 140); 123 series3.add(sdf.parse("15-Jul-2003").getTime(), 165); 124 series3.add(sdf.parse("16-Jul-2003").getTime(), 170); 125 series3.add(sdf.parse("17-Jul-2003").getTime(), 165); 126 series3.add(sdf.parse("18-Jul-2003").getTime(), 140); 127 } catch (ParseException e) { 128 e.printStackTrace(); 130 } 131 DefaultTableXYDataset dataset = new DefaultTableXYDataset(series1); 132 dataset.addSeries(series2); 133 dataset.addSeries(series3); 134 135 JFreeChart chart = createChart(dataset); 136 137 ChartPanel chartPanel = new ChartPanel(chart); 138 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 139 setContentPane(chartPanel); 140 141 } 142 143 154 161 private JFreeChart createChart(TableXYDataset dataset) { 162 163 SimpleDateFormat sdf = new SimpleDateFormat ("dd-MMM-yyyy", Locale.UK); 164 TimeSeriesToolTipGenerator ttg = new TimeSeriesToolTipGenerator( 165 sdf, NumberFormat.getInstance()); 166 DateAxis xAxis = new DateAxis("Domain (X)"); 167 xAxis.setLowerMargin(0.0); 168 xAxis.setUpperMargin(0.0); 169 170 NumberAxis yAxis = new NumberAxis("Range (Y)"); 171 yAxis.setAutoRangeIncludesZero(true); 172 StackedAreaXYRenderer renderer = 173 new StackedAreaXYRenderer(AreaXYRenderer.AREA_AND_SHAPES, ttg, null); 174 renderer.setOutline(true); 175 renderer.setSeriesPaint(0, new Color (255, 255, 206)); 176 renderer.setSeriesPaint(1, new Color (206, 230, 255)); 177 renderer.setSeriesPaint(2, new Color (255, 230, 230)); 178 renderer.setShapePaint(Color.gray); 179 renderer.setShapeStroke(new BasicStroke (0.5f)); 180 renderer.setShape(new Ellipse2D.Double (-3, -3, 6, 6)); 181 XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer); 182 183 yAxis.configure(); 185 186 JFreeChart chart = new JFreeChart(null, JFreeChart.DEFAULT_TITLE_FONT, plot, true); 187 188 return chart; 189 } 190 191 196 public static void main(String [] args) { 197 StackedAreaXYChartDemo demo = new StackedAreaXYChartDemo("Stacked Area XY Chart Demo"); 198 demo.pack(); 199 RefineryUtilities.centerFrameOnScreen(demo); 200 demo.setVisible(true); 201 } 202 203 } 204 | Popular Tags |