ExcelChartDefault implements ExcelChart { @Override public Workbook createWorkbook() { final ExcelChartSheet excelChartSheet = new ExcelChartSheetDefault(workbook, type.toString()); final Chart chart = excelChartSheet.createChartAndLegend(); addCategories(excelChartSheet); addValues(excelChartSheet); final CustomChartDataFactory customChartDataFactory = new CustomChartDataFactoryDefault(); final CustomChartData chartData = customChartDataFactory.createChartData(type, title); final ChartDataSource<?> categoryDataSource = excelChartSheet.getCategoryChartDataSource(); final List<ChartDataSource<Number>> valuesDataSource = excelChartSheet.getValuesChartDataSource(); for (int i = 0; i < valuesDataSource.size(); i++) { final ChartDataSource<Number> valueDataSource = valuesDataSource.get(i); if (seriesTitle.isEmpty()) { chartData.addSeries(categoryDataSource, valueDataSource); } else { chartData.addSeries(seriesTitle.get(i), categoryDataSource, valueDataSource); } } if (type.equals(ChartType.pie)) { chart.plot(chartData); } else { final ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM); final ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); chart.plot(chartData, bottomAxis, leftAxis); } return workbook; } ExcelChartDefault(final String title, final ChartType type, final List<?> categories,
            final List<List<? extends Number>> values); @Override Workbook createWorkbook(); @Override void configureSeriesTitle(final List<String> seriesTitle);  }
@Test public void createWorkbook() throws Exception { final ExcelChart excelChart = new ExcelChartDefault("line chart", ChartType.line, CATEGORIES, VALUES); excelChart.configureSeriesTitle(Arrays.asList("foo", "bar")); final Workbook workbook = excelChart.createWorkbook(); Assert.assertNotNull(workbook); final Sheet sheet = workbook.getSheet(ChartType.line.toString()); Assert.assertNotNull(sheet); final XSSFDrawing drawing = (XSSFDrawing) sheet.getDrawingPatriarch(); final List<XSSFChart> charts = drawing.getCharts(); Assert.assertEquals("number of charts", 1, charts.size()); final XSSFChart chart = charts.get(0); Assert.assertEquals("chart title", "line chart", chart.getTitleText().getString()); final CTChart ctChart = chart.getCTChart(); Assert.assertEquals("We should not have any area chart", 0, ctChart.getPlotArea().getAreaChartArray().length); Assert.assertEquals("Check if we have 1 line chart", 1, ctChart.getPlotArea().getLineChartArray().length); Assert.assertEquals("Check that we have a legend and that it's position is bottom", STLegendPos.B, ctChart.getLegend().getLegendPos().getVal()); final CTLineChart ctLineChart = ctChart.getPlotArea().getLineChartArray()[0]; final CTLineSer[] ctLineSer = ctLineChart.getSerArray(); Assert.assertEquals("Check number of CTLineSer", 2, ctLineSer.length); Assert.assertEquals("check first series title", "foo", ctLineSer[0].getTx().getV()); Assert.assertEquals("check second series title", "bar", ctLineSer[1].getTx().getV()); final CTAxDataSource cat1 = ctLineSer[0].getCat(); Assert.assertEquals("check first category", "cat 1", cat1.getStrRef().getStrCache().getPtArray()[0].getV()); Assert.assertEquals("check last category", "cat 5", cat1.getStrRef().getStrCache().getPtArray()[4].getV()); final CTAxDataSource cat2 = ctLineSer[1].getCat(); Assert.assertEquals("check first category", "cat 1", cat2.getStrRef().getStrCache().getPtArray()[0].getV()); Assert.assertEquals("check last category", "cat 5", cat2.getStrRef().getStrCache().getPtArray()[4].getV()); final CTNumDataSource val1 = ctLineSer[0].getVal(); Assert.assertEquals("check first value", "5.0", val1.getNumRef().getNumCache().getPtArray()[0].getV()); Assert.assertEquals("check last value", "6.0", val1.getNumRef().getNumCache().getPtArray()[4].getV()); final CTNumDataSource val2 = ctLineSer[1].getVal(); Assert.assertEquals("check first value", "20.0", val2.getNumRef().getNumCache().getPtArray()[0].getV()); Assert.assertEquals("check last value", "14.0", val2.getNumRef().getNumCache().getPtArray()[4].getV()); final List<? extends XSSFChartAxis> axis = chart.getAxis(); Assert.assertEquals("number of axis", 2, axis.size()); Assert.assertTrue("category axis", axis.get(0) instanceof XSSFCategoryAxis); Assert.assertTrue("value axis", axis.get(1) instanceof XSSFValueAxis); }