Java – how to change the chart generated by Apache POI to not use smooth lines and display empty cells as gaps?
I use poi 3.12-beta 1, and the code can create a line chart containing multiple data sets and named Series in the legend However, the default setting of POI polyline chart will generate a smooth line on the data point Null values are also plotted as 0, but we want these lines to stop in the first column with an empty cell
Once I render and change these settings in the xlsx file, I can enter the chart properties, but we need to render xlsx with these settings I can't find anything to change these settings in the available APIs
I use this sample class as a starting point for the following code 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,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,endCol)); LineChartSerie ser1 = data.addSerie(xs,ys1); ser1.setTitle(new CellReference(sheet.getSheetName(),seriesCol,abs,abs)); row = 11; ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet,endCol)); LineChartSerie ser2 = data.addSerie(xs,ys2); ser2.setTitle(new CellReference(sheet.getSheetName(),abs)); row = 12; ChartDataSource<Number> ys3 = DataSources.fromNumericCellRange(sheet,endCol)); LineChartSerie ser3 = data.addSerie(xs,ys3); ser3.setTitle(new CellReference(sheet.getSheetName(),abs)); chart.plot(data,new ChartAxis[] { bottomAxis,leftAxis });
Solution
Thanks to Etienne's code for setting the blank to the gap I got help from POI developers, which is the solution to the two problems mentioned in the original problem
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,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); } }