0
  • My current java code generates a stacked-bar chart using Apache POI 4.0.1 as shown in following diagram.
  • I don't want to print a line for future dates (for example: In following chart, I don't want those zeros from 04/04 date); but I want those dates below X-axis.
  • My question is how to remove/disable line drawing for Zero ?


Code sample

// line chart

// axis must be there but must not be visible
bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
bottomAxis.setVisible(false);
leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setVisible(false);

// set correct cross axis
bottomAxis.crossAxis(leftAxis);
leftAxis.crossAxis(bottomAxis);

data = chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);
XDDFLineChartData.Series series4 = (XDDFLineChartData.Series)data.addSeries(date, category);
series4.setTitle("Total", null);
series4.setSmooth(false);
series4.setMarkerStyle(MarkerStyle.STAR);
chart.plot(data);

// correct the id and order, must not start 0 again because there are three bar series already
chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(0).getIdx().setVal(3);
chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(0).getOrder().setVal(3);

solidLineSeries(data, 0, PresetColor.YELLOW);

// add data labels
chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(0).addNewDLbls();
chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(0).getDLbls()
 .addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[]{(byte)255,(byte)255,0});
chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(0).getDLbls()
 .addNewDLblPos().setVal(org.openxmlformats.schemas.drawingml.x2006.chart.STDLblPos.CTR);
chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(0).getDLbls().addNewShowVal().setVal(true);
chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(0).getDLbls().addNewShowLegendKey().setVal(false);
chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(0).getDLbls().addNewShowCatName().setVal(false);
chart.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(0).getDLbls().addNewShowSerName().setVal(false);
Andreas
  • 2,455
  • 10
  • 21
  • 24

1 Answers1

0
  • First you've to write following lines of code before chart.plot(data);.

    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);    
  • Imports
import org.openxmlformats.schemas.drawingml.x2006.chart.CTDispBlanksAs;    
import org.openxmlformats.schemas.drawingml.x2006.chart.STDispBlanksAs;
  • Then after Drawing code and outside loop, write a code to set those zeros as BLANK.
int rownum = 0;      
// loop start     
Row rowT = sheetTestcasenBurndown.getRow(rownum);    
for (int cellnumStart = 1; cellnumStart <= cellnumEnd; cellnumStart++) {     
    Cell cell = rowT.getCell(cellnumStart);       
    cell.setCellType(CellType.BLANK);    
}    
// loop end     
rownum++;

Output