//RealTimeChart .java
import java.awt.BorderLayout;
import java.awt.Font;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
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;
public class RealTimeChart extends ChartPanel implements Runnable {
private static TimeSeries timeSeries;
private long value = 0;
String dataPath=null;
int samplingRate=100;
public RealTimeChart(String chartContent, String title, String yaxisName,String dataPath) {
super(createChart(chartContent, title, yaxisName));
this.dataPath=dataPath;
}
public static double[] getDataFromFile(String fileURL) {
FileReader reader;
List<Double> list = new ArrayList<Double>();
StringBuffer sb = new StringBuffer();
try {
reader = new FileReader(fileURL);
int b;
while ((b = reader.read()) != -1) {
sb.append((char) b);
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
String dataString = null;
if (sb != null) {
dataString = sb.toString().trim();
}
// 将读到的数据存入字符串数组中
String dataArray[] = dataString.split(" ");
// 去除数组中的数据前后空格
for (int i = 0; i < dataArray.length; i++) {
dataArray[i] = dataArray[i].trim();
}
// 解析字符串数组,得到对应的数字
for (int i = 0; i < dataArray.length; i++) {
// 数据不为"" ," ",null才作解析
if (!"".equals(dataArray[i]) && !" ".equals(dataArray[i])
&& !dataArray[i].equals(null)) {
double d = Double.parseDouble(dataArray[i]);
list.add(new Double(d));
}
}
double dataArrayD[] = new double[list.size()];
for (int i = 0; i < list.size(); i++) {
double dtemp=list.get(i).doubleValue();
dtemp=(int)(dtemp*10000)/10000;
dataArrayD[i] = dtemp;
}
return dataArrayD;
}
@SuppressWarnings("deprecation")
private static JFreeChart createChart(String chartContent, String title,
String yaxisName) {
// --------------------
StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); // 设置标题字体
standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20)); // 设置图例的字体
standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 15)); // 设置轴向的字体
standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15)); // 应用主题样式
ChartFactory.setChartTheme(standardChartTheme);
// --------------------
// 创建时序图对象
timeSeries = new TimeSeries(chartContent, Millisecond.class);
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(
timeSeries);
JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(title,
"时间(秒)", yaxisName, timeseriescollection, false, true, false);
XYPlot xyplot = jfreechart.getXYPlot();
// 纵坐标设定,getDomainAxis()得到横轴
ValueAxis valueaxis = xyplot.getDomainAxis();
// 自动设置数据轴数据范围
valueaxis.setAutoRange(true);
// 数据轴固定数据范围 30s
valueaxis.setFixedAutoRange(80000D);
//getRangeAxis()得到纵轴
valueaxis = xyplot.getRangeAxis();
// valueaxis.setRange(0.0D,200D);//这句设置纵坐标范围,没有则默认纵坐标自动变换
return jfreechart;
}
public void run() {
while (true) {
try {
for (double i = 0; i < 2 * Math.PI; i = i + 0.1) {
timeSeries.add(new Millisecond(),
getDataFromFile(dataPath)[getDataFromFile(dataPath).length-1]);
Thread.sleep(200);//
}
} catch (InterruptedException e) {
}
}
}
public static void main(String[] args) {
String dataPath="C:\\Users\\Mao\\Desktop\\chanel0-index0.txt";
JFrame frame = new JFrame("Test Chart");
RealTimeChart rtcp = new RealTimeChart("随机数", "随机数", "数值",dataPath);
frame.getContentPane().add(rtcp, new BorderLayout().CENTER);
frame.pack();
frame.setVisible(true);
(new Thread(rtcp)).start();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
JFreeChart绘制时序图示例
最新推荐文章于 2019-04-27 13:30:09 发布