如何做一个实时动态Swing折线图呢?
由于脑海中构想了如下图这样一个画面,所以经过查资料写代码,实现了视觉化的图景。分享之。。。
public class JFSwingDynamicChart extends JFrame implements ActionListener {
private TimeSeries series;
private double lastValue = 100.0;
/**
* 构造
*/
public JFSwingDynamicChart() {
getContentPane().setBackground(Color.green);
}
/**
* 创建应用程序界面
*/
public void createUI() {
this.series = new TimeSeries("Math.random()-随机数据", Millisecond.class);
TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
ChartPanel chartPanel = new ChartPanel(createChart(dataset));
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
add(chartPanel);
add(buttonPanel, BorderLayout.SOUTH);
}
/**
* 根据结果集构造JFreechart报表对象
*
* @param dataset
* @return
*/
private JFreeChart createChart(XYDataset dataset) {
JFreeChart result = ChartFactory.createTimeSeriesChart("Swing动态折线图", "系统当前时间",
"动态数值变化", dataset, true, true, false);
XYPlot plot = (XYPlot) result.getPlot();
ValueAxis axis = plot.getDomainAxis();
axis.setAutoRange(true);
axis.setFixedAutoRange(60000.0);
axis = plot.getRangeAxis();
axis.setRange(0.0, 200.0);
return result;
}
public void actionPerformed(ActionEvent e) {
}
/**
* 动态运行
*/
public void dynamicRun() {
while (true) {
double factor = 0.90 + 0.2 * Math.random();
this.lastValue = this.lastValue * factor;
Millisecond now = new Millisecond();
this.series.add(new Millisecond(), this.lastValue);
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// 主函数入口
public static void main(String[] args) {
JFSwingDynamicChart jsdChart = new JFSwingDynamicChart();
jsdChart.setTitle("Swing动态折线图");
jsdChart.createUI();
jsdChart.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jsdChart.setBounds(100, 100, 900, 600);
jsdChart.setVisible(true);
// Color c=new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));
jsdChart.dynamicRun();
}
}
-------------------------
Thread.currentThread().sleep(100); //线程交替,给绘制线程以时间可以解决此异常
Exception in thread "main" org.jfree.data.general.SeriesException: You are attempting to add an observation for the time period Wed Jul 07 11:36:28 CST 2010 but the series already contains an observation for that time period. Duplicates are not permitted. Try using the addOrUpdate() method.
at org.jfree.data.time.TimeSeries.add(TimeSeries.java:529)
at org.jfree.data.time.TimeSeries.add(TimeSeries.java:572)
at org.jfree.data.time.TimeSeries.add(TimeSeries.java:558)
at chapter09.JFSwingDynamicChart.dynamicRun(JFSwingDynamicChart.java:76)
at chapter09.JFSwingDynamicChart.main(JFSwingDynamicChart.java:96)
- 大小: 103.7 KB
分享到:
相关推荐
python库,解压后可用。 资源全名:big_sleep-0.0.1-py3-none-any.whl
sleep-stage-identification-master_故障诊断_ELM.zip
资源来自pypi官网,解压后可用。 资源全名:big_sleep-0.8.5-py3-none-any.whl
资源分类:Python库 所属语言:Python 资源全名:big_sleep-0.4.7-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
sleep-stage-identification-master_故障诊断_ELM_源码.zip
资源来自pypi官网。 资源全名:big_sleep-0.0.1-py3-none-any.whl
移动端在浏览H5网页时,经过一段时候后系统会黑屏,手机进入休眠模式。NoSleep插件旨在让任何Android ,iOS浏览器进入休眠状态,保持屏幕常亮。
TC10 Wake-up and Sleep Specification for Automotive Ethernet_11-2017.pdfTC10 Wake-up and Sleep Specification for Automotive Ethernet_11-2017.pdfTC10 Wake-up and Sleep Specification for Automotive ...
python库。 资源全名:big-sleep-0.2.7.tar.gz
T03_Sleep_Yield_Join.java
$ pip install big-sleep 用法 $ dream " a pyramid made of ice " 图像将保存到调用命令的任何位置 先进的 您可以使用以下代码在代码中调用它 from big_sleep import Imagine dream = Imagine ( text = "fire
NoSleep-1.4.0.dmg安装后mac合上后也不断网,解决了很多人mac合屏后断网的情况。
python库。 资源全名:big-sleep-0.5.1.tar.gz
linux nanosleep纳秒级sleep的实现 - 更精确的延迟.zip
npm install sleep-ms --save; 使用CLI # sleep for 1 second sleepms 1000; 建议您使用npm install sleep-ms -g;进行全局安装npm install sleep-ms -g; JS API 在最新版本中不推荐使用JS API。
基于MATLAB的人脸眼睛嘴巴检测,内涵说明书,摘要,代码等,同时含有自制神经网络训练集的代码(用户运行代码做出表情代码自动归类训练集合),主代码通过先检测脸部特征,后综合检测眼睛张开关闭+嘴巴张开关闭判定...
动态生成樱花.py --python有趣案例 import turtle as T import random import time # 画樱花的躯干(60,t) def Tree(branch, t): time.sleep(0.0005)