gpt4 book ai didi

java - 如何更改 apache poi 生成的图表以不使用平滑线并将空单元格显示为间隙?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:25:06 25 4
gpt4 key购买 nike

我使用的是 POI 3.12-beta1,并且有代码可以创建一个包含多个数据集并在图例中命名系列的折线图。但是,poi 中折线图的默认设置会生成一条在数据点上平滑的线。空值也被绘制为 0,但我们希望线条停在有空单元格的第一列。

在 xlsx 文件中呈现图表后,我可以进入图表属性并更改这些设置,但我们需要使用这些设置呈现 xlsx。我在可用的 API 中找不到任何内容来更改这些设置。

我使用这个示例类作为我下面代码的起点 http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/LineChart.java

        Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 17, 18, 30);
Chart chart = drawing.createChart(anchor);
ChartLegend legend = chart.getOrCreateLegend();
legend.setPosition(LegendPosition.RIGHT);
LineChartData data = chart.getChartDataFactory().createLineChartData();
ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

int row = 2;
int startCol = 3;
int endCol = 17;
boolean abs = false;
ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(row, row, startCol, endCol));

row = 10;
int seriesCol = 0;
ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(row, row, startCol, endCol));
LineChartSerie ser1 = data.addSerie(xs, ys1);
ser1.setTitle(new CellReference(sheet.getSheetName(), row, seriesCol, abs, abs));

row = 11;
ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(row, row, startCol, endCol));
LineChartSerie ser2 = data.addSerie(xs, ys2);
ser2.setTitle(new CellReference(sheet.getSheetName(), row, seriesCol, abs, abs));

row = 12;
ChartDataSource<Number> ys3 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(row, row, startCol, endCol));
LineChartSerie ser3 = data.addSerie(xs, ys3);
ser3.setTitle(new CellReference(sheet.getSheetName(), row, seriesCol, abs, abs));

chart.plot(data, new ChartAxis[] { bottomAxis, leftAxis });

最佳答案

感谢 Etienne 提供将空白设置为间隙的代码。我从 POI 开发人员那里得到了帮助,这里是解决原始问题中提到的两个问题的解决方案。

    XSSFChart chart = (XSSFChart)drawing.createChart(anchor);

// this will set blank values as gaps in the chart so you
// can accurately plot data series of different lengths
CTDispBlanksAs disp = CTDispBlanksAs.Factory.newInstance();
disp.setVal(STDispBlanksAs.GAP);
chart.getCTChart().setDispBlanksAs(disp);


// setup chart, axes, data series, etc


chart.plot(data, new ChartAxis[] { bottomAxis, leftAxis });

// this must occur after the call to chart.plot above
CTPlotArea plotArea = chart.getCTChart().getPlotArea();
for (CTLineChart ch : plotArea.getLineChartList()) {
for (CTLineSer ser : ch.getSerList()) {
CTBoolean ctBool = CTBoolean.Factory.newInstance();
ctBool.setVal(false);
ser.setSmooth(ctBool);
}
}

关于java - 如何更改 apache poi 生成的图表以不使用平滑线并将空单元格显示为间隙?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29014848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com