package ss;
//导入java2d包
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.PrintStream;
//导入jfreechart包(chart)
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
//导入jfreechart包(data)
import org.jfree.data.time.*;
import org.jfree.data.xy.XYDataset;
//导入jfreechart包(ui)
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;public class TimeSeriesDemo1 extends JFrame implements Runnable,ActionListener{
//时序图数据集
private TimeSeries timeseries;
//Value坐标轴初始值
private double lastValue;
static Class class$org$jfree$data$time$Millisecond;
static Thread thread1; public static void main(String[] args){
TimeSeriesDemo1 TimeSeriesDemo1 = new TimeSeriesDemo1();
TimeSeriesDemo1.pack();
RefineryUtilities.centerFrameOnScreen(TimeSeriesDemo1);
TimeSeriesDemo1.setVisible(true);
startThread();
} public void run(){
while(true){
try{
//根据实际需要在此处加入需要执行的代码
double d = 0.9D + 0.2D * Math.random();
lastValue = lastValue * d;
Millisecond millisecond = new Millisecond();
System.out.println("Now=" + millisecond.toString());
timeseries.add(millisecond, lastValue);
Thread.sleep(300);
}catch(InterruptedException e){}
}
} public static void startThread(){
thread1.start();
} public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("EXIT")){
thread1.destroy();
System.exit(0);
}
}
public TimeSeriesDemo1(){
//super(new BorderLayout());
thread1 = new Thread(this);
lastValue = 100D;
//创建时序图对象
timeseries = new TimeSeries("Random Data",TimeSeriesDemo1.class$org$jfree$data$time$Millisecond != null ? TimeSeriesDemo1.class$org$jfree$data$time$Millisecond : (TimeSeriesDemo1.class$org$jfree$data$time$Millisecond = TimeSeriesDemo1.getClass("org.jfree.data.time.Millisecond")));
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeseries);
//创建图表面板
ChartPanel chartpanel = new ChartPanel(createChart(timeseriescollection));
chartpanel.setPreferredSize(new Dimension(500,270)); JPanel jpanel = new JPanel();
jpanel.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));//边距为4
JButton jbutton = new JButton("退出");
jbutton.setActionCommand("EXIT");
jbutton.addActionListener(this);
jpanel.add(jbutton); getContentPane().add(chartpanel);
getContentPane().add(jpanel,"South");
} private JFreeChart createChart(XYDataset xydataset){
JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("时序图例子","时间","温度值",xydataset,true,true,false);
XYPlot xyplot = jfreechart.getXYPlot();
//纵坐标设定
ValueAxis valueaxis = xyplot.getDomainAxis();
valueaxis.setAutoRange(true);
valueaxis.setFixedAutoRange(60000D); valueaxis = xyplot.getRangeAxis();
valueaxis.setRange(0.0D,200D); return jfreechart;
} static Class getClass(String s){
Class cls = null;
try{
cls = Class.forName(s);
}catch(ClassNotFoundException cnfe){
throw new NoClassDefFoundError(cnfe.getMessage());
}
return cls;
}}这个例子实现了实时曲线,但是在swing中调用jfreechart,现在我想在SWT中实现它,请大侠帮帮忙,谢谢!
//导入java2d包
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.PrintStream;
//导入jfreechart包(chart)
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
//导入jfreechart包(data)
import org.jfree.data.time.*;
import org.jfree.data.xy.XYDataset;
//导入jfreechart包(ui)
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;public class TimeSeriesDemo1 extends JFrame implements Runnable,ActionListener{
//时序图数据集
private TimeSeries timeseries;
//Value坐标轴初始值
private double lastValue;
static Class class$org$jfree$data$time$Millisecond;
static Thread thread1; public static void main(String[] args){
TimeSeriesDemo1 TimeSeriesDemo1 = new TimeSeriesDemo1();
TimeSeriesDemo1.pack();
RefineryUtilities.centerFrameOnScreen(TimeSeriesDemo1);
TimeSeriesDemo1.setVisible(true);
startThread();
} public void run(){
while(true){
try{
//根据实际需要在此处加入需要执行的代码
double d = 0.9D + 0.2D * Math.random();
lastValue = lastValue * d;
Millisecond millisecond = new Millisecond();
System.out.println("Now=" + millisecond.toString());
timeseries.add(millisecond, lastValue);
Thread.sleep(300);
}catch(InterruptedException e){}
}
} public static void startThread(){
thread1.start();
} public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("EXIT")){
thread1.destroy();
System.exit(0);
}
}
public TimeSeriesDemo1(){
//super(new BorderLayout());
thread1 = new Thread(this);
lastValue = 100D;
//创建时序图对象
timeseries = new TimeSeries("Random Data",TimeSeriesDemo1.class$org$jfree$data$time$Millisecond != null ? TimeSeriesDemo1.class$org$jfree$data$time$Millisecond : (TimeSeriesDemo1.class$org$jfree$data$time$Millisecond = TimeSeriesDemo1.getClass("org.jfree.data.time.Millisecond")));
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeseries);
//创建图表面板
ChartPanel chartpanel = new ChartPanel(createChart(timeseriescollection));
chartpanel.setPreferredSize(new Dimension(500,270)); JPanel jpanel = new JPanel();
jpanel.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));//边距为4
JButton jbutton = new JButton("退出");
jbutton.setActionCommand("EXIT");
jbutton.addActionListener(this);
jpanel.add(jbutton); getContentPane().add(chartpanel);
getContentPane().add(jpanel,"South");
} private JFreeChart createChart(XYDataset xydataset){
JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("时序图例子","时间","温度值",xydataset,true,true,false);
XYPlot xyplot = jfreechart.getXYPlot();
//纵坐标设定
ValueAxis valueaxis = xyplot.getDomainAxis();
valueaxis.setAutoRange(true);
valueaxis.setFixedAutoRange(60000D); valueaxis = xyplot.getRangeAxis();
valueaxis.setRange(0.0D,200D); return jfreechart;
} static Class getClass(String s){
Class cls = null;
try{
cls = Class.forName(s);
}catch(ClassNotFoundException cnfe){
throw new NoClassDefFoundError(cnfe.getMessage());
}
return cls;
}}这个例子实现了实时曲线,但是在swing中调用jfreechart,现在我想在SWT中实现它,请大侠帮帮忙,谢谢!
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.experimental.chart.swt.ChartComposite;public class TimeSeriesDemo1 implements Runnable {
// 时序图数据集
private TimeSeries timeseries; // Value坐标轴初始值
private double lastValue; static Class class$org$jfree$data$time$Millisecond; static Thread thread1; private Display disp; private static boolean runflag=true;
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setSize(600, 300);
shell.setLayout(new FillLayout());
shell.setText("Test for jfreechart running with SWT");
new TimeSeriesDemo1(shell);
TimeSeriesDemo1.startThread();
shell.open();
shell.addDisposeListener(new DisposeListener(){
public void widgetDisposed(DisposeEvent e) {
runflag=false;
}
});
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
} public void run() {
while (runflag) {
try {
// 根据实际需要在此处加入需要执行的代码
double d = 0.9D + 0.2D * Math.random();
lastValue = lastValue * d;
//System.out.println("Now=" + millisecond.toString());
disp.asyncExec(new Runnable() {
public void run() { timeseries.add(new Millisecond(), lastValue);
}
});
Thread.sleep(300);
} catch (InterruptedException e) {
}
}
} public static void startThread() {
thread1.start();
} public TimeSeriesDemo1(Shell shell) {
// super(new BorderLayout());
thread1 = new Thread(this);
lastValue = 100D;
// 创建时序图对象
timeseries = new TimeSeries(
"Random Data",
TimeSeriesDemo1.class$org$jfree$data$time$Millisecond != null ? TimeSeriesDemo1.class$org$jfree$data$time$Millisecond
: (TimeSeriesDemo1.class$org$jfree$data$time$Millisecond = TimeSeriesDemo1
.getClass("org.jfree.data.time.Millisecond")));
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(
timeseries);
// 创建图表面板
disp = shell.getDisplay(); final ChartComposite frame = new ChartComposite(shell, SWT.NONE,
createChart(timeseriescollection), true); frame.pack(); } private JFreeChart createChart(XYDataset xydataset) {
JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("时序图例子",
"时间", "温度值", xydataset, true, true, false);
XYPlot xyplot = jfreechart.getXYPlot();
// 纵坐标设定
ValueAxis valueaxis = xyplot.getDomainAxis();
valueaxis.setAutoRange(true);
valueaxis.setFixedAutoRange(60000D); valueaxis = xyplot.getRangeAxis();
valueaxis.setRange(0.0D, 200D); return jfreechart;
} static Class getClass(String s) {
Class cls = null;
try {
cls = Class.forName(s);
} catch (ClassNotFoundException cnfe) {
throw new NoClassDefFoundError(cnfe.getMessage());
}
return cls;
}}