java怎么实现折线图
时间: 2023-05-13 10:06:31 浏览: 174
Java可以使用开源的图表库JFreeChart来实现折线图。JFreeChart提供了丰富的API和示例代码,可以轻松地创建各种类型的图表,包括折线图、柱状图、饼图等。以下是一个简单的折线图的示例代码:
```
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.DefaultXYDataset;
public class LineChartExample {
public static void main(String[] args) {
// 创建数据集
DefaultXYDataset dataset = new DefaultXYDataset();
double[][] data = {{1, 2, 3, 4, 5}, {10, 20, 30, 40, 50}};
dataset.addSeries("Series 1", data);
// 创建折线图
JFreeChart chart = ChartFactory.createXYLineChart(
"Line Chart Example", // 图表标题
"X", // X轴标签
"Y", // Y轴标签
dataset // 数据集
);
// 显示图表
ChartFrame frame = new ChartFrame("Line Chart Example", chart);
frame.pack();
frame.setVisible(true);
}
}
```
这段代码创建了一个包含一个数据系列的折线图,数据系列包含5个点,X轴的值为1到5,Y轴的值为10到50。运行这段代码可以看到一个简单的折线图窗口。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)