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);
}
});
}
}
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);
}
});
}
}