1 37 38 package org.jfree.chart.demo; 39 40 import java.awt.BorderLayout ; 41 import java.awt.Color ; 42 import java.awt.FlowLayout ; 43 import java.awt.event.ActionEvent ; 44 import java.awt.event.ActionListener ; 45 46 import javax.swing.JButton ; 47 import javax.swing.JPanel ; 48 49 import org.jfree.chart.ChartFactory; 50 import org.jfree.chart.ChartPanel; 51 import org.jfree.chart.JFreeChart; 52 import org.jfree.chart.Spacer; 53 import org.jfree.chart.axis.NumberAxis; 54 import org.jfree.chart.axis.ValueAxis; 55 import org.jfree.chart.plot.XYPlot; 56 import org.jfree.chart.renderer.DefaultXYItemRenderer; 57 import org.jfree.data.time.Millisecond; 58 import org.jfree.data.time.TimeSeries; 59 import org.jfree.data.time.TimeSeriesCollection; 60 import org.jfree.ui.ApplicationFrame; 61 import org.jfree.ui.RefineryUtilities; 62 63 69 public class DynamicDataDemo2 extends ApplicationFrame implements ActionListener { 70 71 72 private TimeSeries series1; 73 74 75 private TimeSeries series2; 76 77 78 private double lastValue1 = 100.0; 79 80 81 private double lastValue2 = 500.0; 82 83 88 public DynamicDataDemo2(String title) { 89 90 super(title); 91 this.series1 = new TimeSeries("Random 1", Millisecond.class); 92 this.series2 = new TimeSeries("Random 2", Millisecond.class); 93 TimeSeriesCollection dataset1 = new TimeSeriesCollection(this.series1); 94 TimeSeriesCollection dataset2 = new TimeSeriesCollection(this.series2); 95 JFreeChart chart = ChartFactory.createTimeSeriesChart( 96 "Dynamic Data Demo 2", "Time", "Value", dataset1, true, true, false 97 ); 98 chart.setBackgroundPaint(Color.white); 99 100 XYPlot plot = chart.getXYPlot(); 101 plot.setBackgroundPaint(Color.lightGray); 102 plot.setDomainGridlinePaint(Color.white); 103 plot.setRangeGridlinePaint(Color.white); 104 plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 4, 4, 4, 4)); 105 ValueAxis axis = plot.getDomainAxis(); 106 axis.setAutoRange(true); 107 axis.setFixedAutoRange(60000.0); 109 plot.setSecondaryDataset(0, dataset2); 110 NumberAxis rangeAxis2 = new NumberAxis("Range Axis 2"); 111 rangeAxis2.setAutoRangeIncludesZero(false); 112 plot.setSecondaryRenderer(0, new DefaultXYItemRenderer()); 113 plot.setSecondaryRangeAxis(0, rangeAxis2); 114 plot.mapSecondaryDatasetToRangeAxis(0, new Integer (0)); 115 116 JPanel content = new JPanel (new BorderLayout ()); 117 118 ChartPanel chartPanel = new ChartPanel(chart); 119 content.add(chartPanel); 120 121 JButton button1 = new JButton ("Add To Series 1"); 122 button1.setActionCommand("ADD_DATA_1"); 123 button1.addActionListener(this); 124 125 JButton button2 = new JButton ("Add To Series 2"); 126 button2.setActionCommand("ADD_DATA_2"); 127 button2.addActionListener(this); 128 129 JButton button3 = new JButton ("Add To Both"); 130 button3.setActionCommand("ADD_BOTH"); 131 button3.addActionListener(this); 132 133 JPanel buttonPanel = new JPanel (new FlowLayout ()); 134 buttonPanel.add(button1); 135 buttonPanel.add(button2); 136 buttonPanel.add(button3); 137 138 content.add(buttonPanel, BorderLayout.SOUTH); 139 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 140 setContentPane(content); 141 142 } 143 144 155 160 public void actionPerformed(ActionEvent e) { 161 boolean add1 = false; 162 boolean add2 = false; 163 if (e.getActionCommand().equals("ADD_DATA_1")) { 164 add1 = true; 165 } 166 else if (e.getActionCommand().equals("ADD_DATA_2")) { 167 add2 = true; 168 } 169 else if (e.getActionCommand().equals("ADD_BOTH")) { 170 add1 = true; 171 add2 = true; 172 } 173 if (add1) { 174 double factor = 0.90 + 0.2 * Math.random(); 175 this.lastValue1 = lastValue1 * factor; 176 Millisecond now = new Millisecond(); 177 System.out.println("Now = " + now.toString()); 178 this.series1.add(new Millisecond(), lastValue1); 179 } 180 if (add2) { 181 double factor = 0.90 + 0.2 * Math.random(); 182 this.lastValue2 = lastValue2 * factor; 183 Millisecond now = new Millisecond(); 184 System.out.println("Now = " + now.toString()); 185 this.series2.add(new Millisecond(), lastValue2); 186 } 187 } 188 189 194 public static void main(String [] args) { 195 196 DynamicDataDemo2 demo = new DynamicDataDemo2("Dynamic Data Demo 2"); 197 demo.pack(); 198 RefineryUtilities.centerFrameOnScreen(demo); 199 demo.setVisible(true); 200 201 } 202 203 } 204 | Popular Tags |