org.apache.poi.xssf.usermodel.XSSFTable Java Examples

The following examples show how to use org.apache.poi.xssf.usermodel.XSSFTable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ChartRenderTest.java    From poi-tl with Apache License 2.0 4 votes vote down vote up
public void testChart() throws Exception {
    try (XWPFDocument doc = new XWPFDocument()) {

        XWPFChart chart = doc.createChart(5143500, 2495550);
        XSSFTable newTable = chart.getWorkbook().getSheetAt(0).createTable(null);
        CTTableColumns addNewTableColumns = newTable.getCTTable().addNewTableColumns();
        addNewTableColumns.setCount(1);
        CTTableColumn addNewTableColumn = addNewTableColumns.addNewTableColumn();
        addNewTableColumn.setId(0);
        chart.getWorkbook().getSheetAt(0).getTables().add(newTable);

        // XDDFChartLegend legend = chart.getOrAddLegend();
        // legend.setPosition(LegendPosition.TOP_RIGHT);

        String[] series = new String[] { "countries", "speakers", "language" };
        String[] categories = new String[] { "中文", "English", "日本語", "português" };
        Double[] values1 = new Double[] { 58.0, 40.0, 38.0, 118.0 };
        // Double[] values2 = new Double[] {315.0,243.0,699.0,378.0};
        // XDDFDataSource<String> cat =
        // XDDFDataSourcesFactory.fromStringCellRange(sheet,
        // new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
        // XDDFNumericalDataSource<Double> val =
        // XDDFDataSourcesFactory.fromNumericCellRange(sheet,
        // new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
        final int numOfPoints = categories.length;
        final String categoryDataRange = chart
                .formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));
        final String valuesDataRange = chart
                .formatRange(new CellRangeAddress(1, numOfPoints, 1, 1));
        // final String valuesDataRange2 = chart.formatRange(new
        // CellRangeAddress(1, numOfPoints, 2, 2));
        final XDDFDataSource<?> categoriesData = XDDFDataSourcesFactory.fromArray(categories,
                categoryDataRange, 0);
        final XDDFNumericalDataSource<? extends Number> valuesData = XDDFDataSourcesFactory
                .fromArray(values1, valuesDataRange, 1);
        // values1[6] = 16.0; // if you ever want to change the underlying
        // data
        // final XDDFNumericalDataSource<? extends Number> valuesData2 =
        // XDDFDataSourcesFactory.fromArray(values2, valuesDataRange2, 2);

        // XDDFChartData data = new
        // XDDFPieChartData(chart.getCTChart().getPlotArea().addNewPieChart());
        XDDFCategoryAxis categoryAxis = chart.createCategoryAxis(AxisPosition.LEFT);
        XDDFValueAxis createValueAxis = chart.createValueAxis(AxisPosition.BOTTOM);
        XDDFChartData data = chart.createData(ChartTypes.BAR, categoryAxis, createValueAxis);
        // XDDFChartData data = new
        // XDDFBarChartData(chart.getCTChart().getPlotArea().addNewBarChart(),
        // null, null);
        // data.setVaryColors(true);
        Series addSeries = data.addSeries(categoriesData, valuesData);
        addSeries.setTitle(series[0], chart.setSheetTitle(series[0], 0));
        // Series series2 = data.addSeries(categoriesData, valuesData2);
        // series2.setTitle(series[1], chart.setSheetTitle(series[1], 1));
        chart.plot(data);
        chart.setTitleText("My Bar Chart");
        chart.setTitleOverlay(false);

        chart.getWorkbook().getSheetAt(0).removeTable(newTable);

        try (FileOutputStream out = new FileOutputStream("out_chart_demo.docx")) {
            doc.write(out);
        }
    }
}