1 37 38 package org.jfree.chart.demo; 39 40 import java.awt.BorderLayout ; 41 import java.awt.FlowLayout ; 42 import java.awt.event.ActionEvent ; 43 import java.awt.event.ActionListener ; 44 45 import javax.swing.JButton ; 46 import javax.swing.JPanel ; 47 import javax.swing.Timer ; 48 49 import org.jfree.chart.ChartFactory; 50 import org.jfree.chart.ChartPanel; 51 import org.jfree.chart.JFreeChart; 52 import org.jfree.chart.axis.CyclicNumberAxis; 53 import org.jfree.chart.axis.NumberAxis; 54 import org.jfree.chart.plot.PlotOrientation; 55 import org.jfree.chart.plot.XYPlot; 56 import org.jfree.chart.renderer.CyclicXYItemRenderer; 57 import org.jfree.data.XYSeries; 58 import org.jfree.data.XYSeriesCollection; 59 import org.jfree.ui.ApplicationFrame; 60 import org.jfree.ui.RefineryUtilities; 61 62 67 public class CyclicXYPlotDemo extends ApplicationFrame implements ActionListener { 68 69 XYSeries series; 70 long x = 0; 71 double y = 50; 72 Timer timer; 73 74 79 public CyclicXYPlotDemo(String title) { 80 81 super(title); 82 83 series = new XYSeries("Random Data"); 84 series.setMaximumItemCount(50); XYSeriesCollection data = new XYSeriesCollection(series); 86 87 JFreeChart chart = ChartFactory.createXYLineChart( 88 "Cyclic XY Plot Demo", 89 "X", 90 "Y", 91 data, 92 PlotOrientation.VERTICAL, 93 true, 94 true, 95 false 96 ); 97 98 XYPlot plot = chart.getXYPlot(); 99 plot.setDomainAxis(new CyclicNumberAxis(10,0)); 100 plot.setRenderer(new CyclicXYItemRenderer()); 101 102 NumberAxis axis = (NumberAxis) plot.getRangeAxis(); 103 axis.setAutoRangeIncludesZero(false); 104 axis.setAutoRangeMinimumSize(1.0); 105 106 ChartPanel chartPanel = new ChartPanel(chart); 107 chartPanel.setPreferredSize(new java.awt.Dimension (400, 300)); 108 JPanel content = new JPanel (new BorderLayout ()); 109 content.add(chartPanel,BorderLayout.CENTER); 110 111 JButton button1 = new JButton ("Start"); 112 button1.addActionListener(new ActionListener () { 113 public void actionPerformed(ActionEvent e) { 114 timer.start(); 115 } 116 }); 117 118 JButton button2 = new JButton ("Stop"); 119 button2.addActionListener(new ActionListener () { 120 public void actionPerformed(ActionEvent e) { 121 timer.stop(); 122 } 123 }); 124 125 JButton button3 = new JButton ("Step by step"); 126 button3.addActionListener(new ActionListener () { 127 public void actionPerformed(ActionEvent e) { 128 CyclicXYPlotDemo.this.actionPerformed(null); 129 } 130 }); 131 132 JPanel buttonPanel = new JPanel (new FlowLayout ()); 133 buttonPanel.add(button1); 134 buttonPanel.add(button2); 135 buttonPanel.add(button3); 136 137 content.add(buttonPanel, BorderLayout.SOUTH); 138 setContentPane(content); 139 140 timer = new Timer (200, this); 141 } 142 143 148 public static void main(String [] args) { 149 150 CyclicXYPlotDemo demo = new CyclicXYPlotDemo("Cyclic XY Plot Demo"); 151 demo.pack(); 152 RefineryUtilities.centerFrameOnScreen(demo); 153 demo.setVisible(true); 154 155 } 157 158 161 public void actionPerformed(ActionEvent e) { 162 double delta = Math.random()*10 - 5; 163 if (delta==-5.0) delta = 0; y += delta; 165 series.add(x++ / 4.0, y); 166 } 167 168 } 169 | Popular Tags |