基于网络爬虫技术的新闻分析系统的设计与实现的数据展示模块使用jfreechart的代码怎么写
时间: 2024-02-03 14:15:08 浏览: 30
下面是一个简单的使用 JFreeChart 进行数据展示的示例代码:
```java
import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class MyChart extends ApplicationFrame {
public MyChart(String title) {
super(title);
}
public void createChart(ArrayList<Double> data, String xLabel, String yLabel) {
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series = new XYSeries("Data");
for (int i = 0; i < data.size(); i++) {
series.add(i + 1, data.get(i));
}
dataset.addSeries(series);
JFreeChart chart = ChartFactory.createXYLineChart(
"Data Chart",
xLabel,
yLabel,
dataset,
PlotOrientation.VERTICAL,
true,
true,
false
);
chart.getTitle().setFont(new Font("Arial", Font.BOLD, 18));
chart.setBackgroundPaint(Color.WHITE);
XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.WHITE);
plot.setDomainGridlinePaint(Color.BLACK);
plot.setRangeGridlinePaint(Color.BLACK);
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesPaint(0, Color.BLUE);
renderer.setSeriesStroke(0, new BasicStroke(2.0f));
plot.setRenderer(renderer);
setContentPane(new ChartPanel(chart));
pack();
RefineryUtilities.centerFrameOnScreen(this);
setVisible(true);
}
}
```
使用示例:
```java
import java.util.ArrayList;
import java.util.Random;
public class Main {
public static void main(String[] args) {
ArrayList<Double> data = new ArrayList<>();
Random rand = new Random();
for (int i = 0; i < 10; i++) {
data.add(rand.nextDouble() * 100);
}
MyChart chart = new MyChart("Data Chart");
chart.createChart(data, "X-Axis Label", "Y-Axis Label");
}
}
```
这个例子展示了如何通过 JFreeChart 创建一个简单的数据曲线图。你可以将 `data` 替换为你自己的数据,`xLabel` 和 `yLabel` 分别表示 X 轴和 Y 轴的标签。请注意,这只是一个基本的示例。你可以根据自己的需求进行修改和扩展。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)