使用JFreeChart库创建双Y轴时间序列图表
使用JFreeChart库创建双Y轴时间序列图表
本教程将指导您使用JFreeChart库,从Excel文件'input.xlsx'的'P1'工作表中读取数据,创建一个包含时间格式水平轴和双Y轴的图表。
步骤 1:导入必要的库
首先,将以下JFreeChart和Apache POI库导入到您的Java项目中:
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.apache.poi.ss.usermodel.*;
import java.io.File;
步骤 2:从Excel文件读取数据
使用Apache POI库读取Excel文件'input.xlsx'并获取'P1'工作表。然后,创建三个数据集:一个用于水平轴的时间序列数据集,一个用于主垂直轴的数据集,以及一个用于次要垂直轴的数据集。
// 读取Excel文件
Workbook workbook = WorkbookFactory.create(new File('input.xlsx'));
// 获取P1工作表
Sheet sheet = workbook.getSheet('P1');
// 创建时间序列数据集
TimeSeriesCollection dataset = new TimeSeriesCollection();
// 读取A2-A50的数据作为时间序列的水平轴
Row xRow = sheet.getRow(1);
for (int i = 1; i <= 49; i++) {
Cell cell = xRow.getCell(i);
Date date = cell.getDateCellValue();
TimeSeries series = new TimeSeries('Series ' + i);
series.add(new Day(date), 0);
dataset.addSeries(series);
}
// 创建垂直轴数据集
DefaultCategoryDataset verticalDataset = new DefaultCategoryDataset();
// 读取B2-B50的数据作为垂直轴
Row yRow = sheet.getRow(1);
for (int i = 1; i <= 49; i++) {
Cell cell = yRow.getCell(i);
double value = cell.getNumericCellValue();
verticalDataset.addValue(value, 'Vertical Axis', 'Series ' + i);
}
// 创建次要垂直轴数据集
DefaultCategoryDataset secondaryVerticalDataset = new DefaultCategoryDataset();
// 读取C2-C50的数据作为次要垂直轴
Row y2Row = sheet.getRow(2);
for (int i = 1; i <= 49; i++) {
Cell cell = y2Row.getCell(i);
double value = cell.getNumericCellValue();
secondaryVerticalDataset.addValue(value, 'Secondary Vertical Axis', 'Series ' + i);
}
步骤 3:创建图表并显示
创建水平轴、主垂直轴和次要垂直轴对象。然后,使用这些轴对象创建两个绘图区域:一个用于主垂直轴,另一个用于次要垂直轴。将这两个绘图区域添加到组合绘图区域中,并使用该组合绘图区域创建图表对象。最后,创建一个图表框架对象,并将图表添加到该框架中。
// 创建水平轴对象
DateAxis xAxis = new DateAxis('Time');
// 创建垂直轴对象
NumberAxis yAxis = new NumberAxis('Vertical Axis');
// 创建次要垂直轴对象
NumberAxis secondaryYAxis = new NumberAxis('Secondary Vertical Axis');
// 创建水平轴绘图区域对象
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, null);
// 创建次要垂直轴绘图区域对象
XYPlot secondaryPlot = new XYPlot(secondaryVerticalDataset, xAxis, secondaryYAxis, null);
// 创建组合绘图区域对象
CombinedDomainXYPlot combinedPlot = new CombinedDomainXYPlot(xAxis);
// 将水平轴绘图区域对象和次要垂直轴绘图区域对象添加到组合绘图区域对象中
combinedPlot.add(plot);
combinedPlot.add(secondaryPlot);
// 创建图表对象
JFreeChart chart = new JFreeChart('Chart Title', JFreeChart.DEFAULT_TITLE_FONT, combinedPlot, true);
// 创建图表框架对象
ChartFrame frame = new ChartFrame('Chart', chart);
// 设置图表框架的大小
frame.setSize(800, 600);
// 显示图表框架
frame.setVisible(true);
完成上述步骤后,您将获得一个显示从Excel文件读取的数据的双Y轴时间序列图表。该图表将使用A2-A50单元格中的值作为时间格式的水平轴,使用B2-B50单元格中的值作为主垂直轴,并使用C2-C50单元格中的值作为次要垂直轴。
原文地址: https://www.cveoy.top/t/topic/lajJ 著作权归作者所有。请勿转载和采集!