import java.io.File; import java.io.IOException; import java.util.Date;

import org.apache.poi.ss.usermodel.; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.jfree.chart.ChartFactory; 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.time.; import org.jfree.data.xy.DefaultXYDataset;

public class ExcelChartExample06 { public static void main(String[] args) { try { // 读取Excel文件 Workbook workbook = WorkbookFactory.create(new File('input.xlsx')); Sheet sheet = workbook.getSheet('P1');

        // 创建时间序列数据集
        TimeSeriesCollection dataset = new TimeSeriesCollection();

        // 获取水平轴数据
        Row horizontalAxisRow = sheet.getRow(1);
        for (int i = 1; i <= 50; i++) {
            Cell cell = horizontalAxisRow.getCell(i);
            Date date = cell.getDateCellValue();
            TimeSeries series = new TimeSeries('Series ' + i);
            series.add(new Day(date), 0);
            dataset.addSeries(series);
        }

        // 创建垂直轴数据集
        DefaultXYDataset verticalAxisDataset = new DefaultXYDataset();
        DefaultXYDataset secondaryVerticalAxisDataset = new DefaultXYDataset();

        // 获取垂直轴和次要垂直轴数据
        for (int i = 1; i <= 50; i++) {
            Row row = sheet.getRow(i + 1);
            Cell verticalAxisCell = row.getCell(1);
            Cell secondaryVerticalAxisCell = row.getCell(2);

            double[][] verticalAxisData = {{i, verticalAxisCell.getNumericCellValue()}};
            double[][] secondaryVerticalAxisData = {{i, secondaryVerticalAxisCell.getNumericCellValue()}};

            verticalAxisDataset.addSeries('Series ' + i, verticalAxisData);
            secondaryVerticalAxisDataset.addSeries('Series ' + i, secondaryVerticalAxisData);
        }

        // 创建水平轴
        DateAxis horizontalAxis = new DateAxis('时间');

        // 创建垂直轴
        NumberAxis verticalAxis = new NumberAxis('垂直轴');
        verticalAxis.setAutoRangeIncludesZero(false);

        // 创建次要垂直轴
        NumberAxis secondaryVerticalAxis = new NumberAxis('次要垂直轴');
        secondaryVerticalAxis.setAutoRangeIncludesZero(false);

        // 创建折线图
        XYPlot plot = new XYPlot(verticalAxisDataset, horizontalAxis, verticalAxis, null);
        plot.setOrientation(PlotOrientation.VERTICAL);
        plot.setRangeAxis(1, secondaryVerticalAxis);

        // 添加次要垂直轴数据
        plot.setDataset(1, secondaryVerticalAxisDataset);
        plot.mapDatasetToRangeAxis(1, 1);

        // 创建组合图表
        CombinedDomainXYPlot combinedPlot = new CombinedDomainXYPlot(horizontalAxis);
        combinedPlot.add(plot);

        // 创建图表
        JFreeChart chart = new JFreeChart('折线图', JFreeChart.DEFAULT_TITLE_FONT, combinedPlot, true);

        // 保存图表为图片
        ChartUtilities.saveChartAsJPEG(new File('output.jpg'), chart, 800, 600);

        // 关闭工作簿
        workbook.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

Java Excel 折线图生成示例 - 使用 JFreeChart 从 Excel 文件创建多轴折线图

原文地址: https://www.cveoy.top/t/topic/fSY7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录