Swing中用JFreeChart生成时序图的例子

Jfreechart时序图(折线图)

package com.testchart;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Paint;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

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;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

import com.dataForm.ParameterForm;

public class RealTimeChart extends ChartPanel implements Runnable {

private static TimeSeries timeSeries;
private static TimeSeries timeSeries1;
private static TimeSeries timeSeries2;
private static ParameterForm pf;

private static final long serialVersionUID = 1L;

public RealTimeChart(String chartContent,String title,String yaxisName,ParameterForm pf)
{
super(createChart(chartContent,title,yaxisName));
//设置显示区域的尺寸
this.setPreferredSize(new Dimension(500,200));

this.pf = pf;
}

private static JFreeChart createChart(String chartContent,String title,String yaxisName){

//创建时序图对象
//第一条折线
timeSeries = new TimeSeries("调整后的直径",Millisecond.class);

//第二条折线
timeSeries1 = new TimeSeries("目标直径",Millisecond.class);

//第三条折线
timeSeries2 = new TimeSeries("调整之前的直径",Millisecond.class);

TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeSeries);
timeseriescollection.addSeries(timeSeries1);
timeseriescollection.addSeries(timeSeries2);

JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(
"直径变化时序图",
"时间(秒)",
yaxisName,
timeseriescollection,
true,
true,
false);

jfreechart.getTitle().setFont(new Font("宋体",12,12));
jfreechart.getLegend().setItemFont(new Font("宋体",12,12));
jfreechart.setBackgroundPaint(Color.GREEN);

XYPlot xyplot = jfreechart.getXYPlot();

//XY轴上的字体
xyplot.getRangeAxis().setLabelFont(new Font("宋体", 12, 12));
xyplot.getDomainAxis().setLabelFont(new Font("宋体", 12, 12));
//坐标轴
xyplot.setBackgroundPaint(Color.lightGray);
xyplot.setDomainGridlinePaint(Color.white);
xyplot.setRangeGridlinePaint(Color.white);
//xyplot.setAxisOffset(new Spacer(1, 4D, 4D, 4D, 4D));
ValueAxis valueaxis = xyplot.getDomainAxis();

valueaxis.setAutoRange(true);

valueaxis.setFixedAutoRange(10000D);

valueaxis = xyplot.getRangeAxis();

return jfreechart;

}

public void run() {
// TODO Auto-generated method stub
while(true){
try{
System.out.println(pf.getZhijing());
timeSeries.add(new Millisecond(), pf.getZhijing());//调整之后的直径
timeSeries1.add(new Millisecond(), pf.getMubiao_zhijing());//目标直径
timeSeries2.add(new Millisecond(), pf.getMubiao_zhijing()+(Math.random()*2-1));//调整之后的直径
Thread.sleep(300);
}catch (InterruptedException e) { }
}
}

private long randomNum(float mubiao_zhijing){

System.out.println("调整之前的直径="+(mubiao_zhijing+(Math.random()*2-1)));
return (long) (mubiao_zhijing+(Math.random()*2-1));
}
public static void main(String[] args){

JFrame frame=new JFrame("Test Chart");
RealTimeChart rtcp=new RealTimeChart("Random Data","随机数","数值",pf);
frame.getContentPane().add(rtcp,new BorderLayout().CENTER);
frame.pack();
frame.setVisible(true);
(new Thread(rtcp)).start();
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent windowevent){
System.exit(0);
}
});
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值