【问题标题】:Changing the shape of chart generated by apache poi for excel sheet更改 apache poi 为 Excel 工作表生成的图表的形状
【发布时间】:2017-05-30 06:57:43
【问题描述】:

我有一个生成包含大量数据的 Excel 表的 java 文件。为了更好的用户体验,我们决定简单地从数据中生成一个折线图。

生成图表并不困难,但要使其成为我们需要的确切形状是一个问题。目前,图表边框如下所示:

如您所见,边框在拐角处是弯曲的。我希望它看起来像这样:

所以,到目前为止,我还没有找到一种方法来做到这一点。文档对此不是很详细。

【问题讨论】:

    标签: java excel charts apache-poi


    【解决方案1】:

    所以在您看来圆角已经过时了?我也是这个观点,但似乎Microsoft 不是。因为如果在CTChartSpace 中没有设置RoundedCorners,则默认为true

    不过还好我们可以根据需要使用以下方法进行设置:

    private static void setRoundedCorners(XSSFChart chart, boolean setVal) {
        if (chart.getCTChartSpace().getRoundedCorners() == null) chart.getCTChartSpace().addNewRoundedCorners();
        chart.getCTChartSpace().getRoundedCorners().setVal(setVal);
    }
    

    完整示例:

    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Chart;
    import org.apache.poi.ss.usermodel.ClientAnchor;
    import org.apache.poi.ss.usermodel.Drawing;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.usermodel.charts.AxisCrosses;
    import org.apache.poi.ss.usermodel.charts.AxisPosition;
    import org.apache.poi.ss.usermodel.charts.ChartAxis;
    import org.apache.poi.ss.usermodel.charts.ChartDataSource;
    import org.apache.poi.ss.usermodel.charts.ChartLegend;
    import org.apache.poi.ss.usermodel.charts.DataSources;
    import org.apache.poi.ss.usermodel.charts.LegendPosition;
    import org.apache.poi.ss.usermodel.charts.LineChartData;
    import org.apache.poi.ss.usermodel.charts.ValueAxis;
    import org.apache.poi.ss.util.CellRangeAddress;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.apache.poi.xssf.usermodel.XSSFChart;
    
    /**
     * Line chart example.
     */
    public class LineChart {
    
        private static void setRoundedCorners(XSSFChart chart, boolean setVal) {
            if (chart.getCTChartSpace().getRoundedCorners() == null) chart.getCTChartSpace().addNewRoundedCorners();
            chart.getCTChartSpace().getRoundedCorners().setVal(setVal);
        }
    
        public static void main(String[] args) throws IOException {
            Workbook wb = new XSSFWorkbook();
            Sheet sheet = wb.createSheet("linechart");
            final int NUM_OF_ROWS = 3;
            final int NUM_OF_COLUMNS = 10;
    
            // Create a row and put some cells in it. Rows are 0 based.
            Row row;
            Cell cell;
            for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
                row = sheet.createRow((short) rowIndex);
                for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
                    cell = row.createCell((short) colIndex);
                    cell.setCellValue(colIndex * (rowIndex + 1));
                }
            }
    
            Drawing<?> drawing = sheet.createDrawingPatriarch();
            ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
    
            Chart chart = drawing.createChart(anchor);
    
            setRoundedCorners((XSSFChart)chart, false);
    
            ChartLegend legend = chart.getOrCreateLegend();
            legend.setPosition(LegendPosition.TOP_RIGHT);
    
            LineChartData data = chart.getChartDataFactory().createLineChartData();
    
            // Use a category axis for the bottom axis.
            ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
            ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
            leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
    
            ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
            ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
            ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
    
    
            data.addSeries(xs, ys1);
            data.addSeries(xs, ys2);
    
            chart.plot(data, bottomAxis, leftAxis);
    
            // Write the output to a file
            FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx");
            wb.write(fileOut);
            fileOut.close();
            wb.close();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      • 1970-01-01
      • 2013-03-22
      • 2014-07-02
      • 1970-01-01
      • 2022-12-21
      • 1970-01-01
      相关资源
      最近更新 更多