1 37 38 package org.jfree.chart.demo; 39 40 import java.awt.BorderLayout ; 41 import java.awt.event.ActionEvent ; 42 import java.awt.event.ActionListener ; 43 44 import javax.swing.JButton ; 45 import javax.swing.JPanel ; 46 47 import org.jfree.chart.ChartFactory; 48 import org.jfree.chart.ChartPanel; 49 import org.jfree.chart.JFreeChart; 50 import org.jfree.chart.axis.ValueAxis; 51 import org.jfree.chart.plot.XYPlot; 52 import org.jfree.data.time.Millisecond; 53 import org.jfree.data.time.TimeSeries; 54 import org.jfree.data.time.TimeSeriesCollection; 55 import org.jfree.ui.ApplicationFrame; 56 import org.jfree.ui.RefineryUtilities; 57 58 64 public class DynamicDataDemo extends ApplicationFrame implements ActionListener { 65 66 67 private TimeSeries series; 68 69 70 private double lastValue = 100.0; 71 72 77 public DynamicDataDemo(String title) { 78 79 super(title); 80 this.series = new TimeSeries("Random Data", Millisecond.class); 81 TimeSeriesCollection dataset = new TimeSeriesCollection(series); 82 JFreeChart chart = ChartFactory.createTimeSeriesChart( 83 "Dynamic Data Demo", 84 "Time", 85 "Value", 86 dataset, 87 true, 88 true, 89 false 90 ); 91 XYPlot plot = chart.getXYPlot(); 92 ValueAxis axis = plot.getDomainAxis(); 93 axis.setAutoRange(true); 94 axis.setFixedAutoRange(60000.0); 96 axis = plot.getRangeAxis(); 97 axis.setRange(0.0, 200.0); 98 99 JPanel content = new JPanel (new BorderLayout ()); 100 101 ChartPanel chartPanel = new ChartPanel(chart); 102 JButton button = new JButton ("Add New Data Item"); 103 button.setActionCommand("ADD_DATA"); 104 button.addActionListener(this); 105 content.add(chartPanel); 106 content.add(button, BorderLayout.SOUTH); 107 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 108 setContentPane(content); 109 110 } 111 112 123 128 public void actionPerformed(ActionEvent e) { 129 if (e.getActionCommand().equals("ADD_DATA")) { 130 double factor = 0.90 + 0.2 * Math.random(); 131 this.lastValue = lastValue * factor; 132 Millisecond now = new Millisecond(); 133 System.out.println("Now = " + now.toString()); 134 this.series.add(new Millisecond(), lastValue); 135 } 136 } 137 138 143 public static void main(String [] args) { 144 145 DynamicDataDemo demo = new DynamicDataDemo("Dynamic Data Demo"); 146 demo.pack(); 147 RefineryUtilities.centerFrameOnScreen(demo); 148 demo.setVisible(true); 149 150 } 151 152 } 153 154 155 156 157 158 159 160 | Popular Tags |