帮我写AS读取传感器数据后用jfreechart库画图的Java代码
时间: 2024-05-02 14:19:44 浏览: 93
好的,以下是示例代码:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
public class SensorDataChart extends ApplicationFrame {
private static final long serialVersionUID = 1L;
public SensorDataChart(String applicationTitle, String chartTitle) {
super(applicationTitle);
JFreeChart lineChart = ChartFactory.createLineChart(
chartTitle,
"Sensor Reading",
"Value",
createDataset(),
PlotOrientation.VERTICAL,
true,true,false);
ChartPanel chartPanel = new ChartPanel(lineChart);
chartPanel.setPreferredSize(new java.awt.Dimension(560 , 367));
chartPanel.setBackground(Color.WHITE);
setContentPane(chartPanel);
}
private DefaultCategoryDataset createDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
// Add sensor readings here
dataset.addValue(1.0, "Sensor 1", "Reading 1");
dataset.addValue(1.2, "Sensor 1", "Reading 2");
dataset.addValue(1.5, "Sensor 1", "Reading 3");
dataset.addValue(1.8, "Sensor 1", "Reading 4");
dataset.addValue(2.1, "Sensor 1", "Reading 5");
dataset.addValue(2.5, "Sensor 1", "Reading 6");
return dataset;
}
public static void main(String[] args) {
SensorDataChart chart = new SensorDataChart("Sensor Data Chart", "Sensor Readings");
chart.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
chart.pack();
chart.setVisible(true);
}
}
```
希望对您有所帮助,有其他问题请随时提出。
阅读全文