1 37 38 package org.jfree.chart.demo; 39 40 import java.awt.Color ; 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 62 public class DifferenceChartDemo extends ApplicationFrame { 63 64 69 public DifferenceChartDemo(String 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 (500, 270)); 94 setContentPane(chartPanel); 95 96 } 97 98 105 private JFreeChart createChart(XYDataset dataset) { 106 JFreeChart chart = ChartFactory.createTimeSeriesChart( 107 "Difference Chart Demo", 108 "Time", "Value", 109 dataset, 110 true, true, false ); 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 136 public static void main(String [] 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 |