I want to make line chart in Excel with apache poi. I want to cut or make 0 those parts of graph, which are negative. I have this example from apache poi examples. Is there any solution to this?
我想用apache poi在Excel中制作折线图。我想剪切或制作0图形的那些部分,它们都是负数。我从apache poi示例中得到了这个例子。这有什么解决方案吗?
My code.
我的代码。
try (Workbook wb = new XSSFWorkbook()) {
Sheet sheet = wb.createSheet("linechart");
final int NUM_OF_ROWS = 2;
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 < 1; rowIndex++) {
row = sheet.createRow((short) rowIndex);
for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
cell = row.createCell((short) colIndex);
cell.setCellValue(4*colIndex * (rowIndex + 1));
}
}
for (int rowIndex = 1; 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(ThreadLocalRandom.current().nextInt(3));
}
}
Drawing<?> drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 0, 20, 15);
Chart chart = drawing.createChart(anchor);
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);
bottomAxis.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));
LineChartSeries chartSerie = data.addSeries(xs, ys1);
chartSerie.setTitle("My Title");
chart.plot(data, bottomAxis, leftAxis);
// Write the output to a file
try (FileOutputStream fileOut = new FileOutputStream("ooxml-line-chart.xlsx")) {
wb.write(fileOut);
}
}
This is what I have now:
这就是我现在拥有的:
我的折线图
这就是我要的
1 个解决方案
#1
0
As one can find in apache poi
's API docs ValueAxis extends ChartAxis
and ChartAxis
has a method ChartAxis.setMinimum.
正如人们可以在apache poi的API文档中找到的,ValueAxis扩展了ChartAxis,ChartAxis有一个方法ChartAxis.setMinimum。
So
所以
...
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
leftAxis.setMinimum(0);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
...
#1
0
As one can find in apache poi
's API docs ValueAxis extends ChartAxis
and ChartAxis
has a method ChartAxis.setMinimum.
正如人们可以在apache poi的API文档中找到的,ValueAxis扩展了ChartAxis,ChartAxis有一个方法ChartAxis.setMinimum。
So
所以
...
ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
leftAxis.setMinimum(0);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
...