Search in sources :

Example 6 with CTNumDataSource

use of org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource in project poi by apache.

the class TestXSLFChart method testFillChartTemplate.

/**
     * a modified version from POI-examples
     */
@Test
public void testFillChartTemplate() throws IOException {
    // first line is chart title
    String chartTitle = "Apache POI";
    XMLSlideShow pptx = XSLFTestDataSamples.openSampleDocument("pie-chart.pptx");
    XSLFSlide slide = pptx.getSlides().get(0);
    // find chart in the slide
    XSLFChart chart = null;
    for (POIXMLDocumentPart part : slide.getRelations()) {
        if (part instanceof XSLFChart) {
            chart = (XSLFChart) part;
            break;
        }
    }
    if (chart == null)
        throw new IllegalStateException("chart not found in the template");
    // embedded Excel workbook that holds the chart data
    POIXMLDocumentPart xlsPart = chart.getRelations().get(0);
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sheet = wb.createSheet();
    CTChart ctChart = chart.getCTChart();
    CTPlotArea plotArea = ctChart.getPlotArea();
    CTPieChart pieChart = plotArea.getPieChartArray(0);
    //Pie Chart Series
    CTPieSer ser = pieChart.getSerArray(0);
    // Series Text
    CTSerTx tx = ser.getTx();
    tx.getStrRef().getStrCache().getPtArray(0).setV(chartTitle);
    sheet.createRow(0).createCell(1).setCellValue(chartTitle);
    String titleRef = new CellReference(sheet.getSheetName(), 0, 1, true, true).formatAsString();
    tx.getStrRef().setF(titleRef);
    // Category Axis Data
    CTAxDataSource cat = ser.getCat();
    CTStrData strData = cat.getStrRef().getStrCache();
    // Values
    CTNumDataSource valSrc = ser.getVal();
    CTNumData numData = valSrc.getNumRef().getNumCache();
    // unset old axis text
    strData.setPtArray(null);
    // unset old values
    numData.setPtArray(null);
    Map<String, Double> pieModel = new LinkedHashMap<String, Double>();
    pieModel.put("First", 1.0);
    pieModel.put("Second", 3.0);
    pieModel.put("Third", 4.0);
    // set model
    int idx = 0;
    int rownum = 1;
    for (String key : pieModel.keySet()) {
        double val = pieModel.get(key);
        CTNumVal numVal = numData.addNewPt();
        numVal.setIdx(idx);
        numVal.setV("" + val);
        CTStrVal sVal = strData.addNewPt();
        sVal.setIdx(idx);
        sVal.setV(key);
        idx++;
        XSSFRow row = sheet.createRow(rownum++);
        row.createCell(0).setCellValue(key);
        row.createCell(1).setCellValue(val);
    }
    numData.getPtCount().setVal(idx);
    strData.getPtCount().setVal(idx);
    String numDataRange = new CellRangeAddress(1, rownum - 1, 1, 1).formatAsString(sheet.getSheetName(), true);
    valSrc.getNumRef().setF(numDataRange);
    String axisDataRange = new CellRangeAddress(1, rownum - 1, 0, 0).formatAsString(sheet.getSheetName(), true);
    cat.getStrRef().setF(axisDataRange);
    // updated the embedded workbook with the data
    OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream();
    wb.write(xlsOut);
    xlsOut.close();
    wb.close();
}
Also used : CTStrData(org.openxmlformats.schemas.drawingml.x2006.chart.CTStrData) CTNumDataSource(org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) OutputStream(java.io.OutputStream) CTPlotArea(org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea) CTSerTx(org.openxmlformats.schemas.drawingml.x2006.chart.CTSerTx) CellReference(org.apache.poi.ss.util.CellReference) CTAxDataSource(org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource) LinkedHashMap(java.util.LinkedHashMap) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) CTChart(org.openxmlformats.schemas.drawingml.x2006.chart.CTChart) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) CTNumData(org.openxmlformats.schemas.drawingml.x2006.chart.CTNumData) CTPieChart(org.openxmlformats.schemas.drawingml.x2006.chart.CTPieChart) CTPieSer(org.openxmlformats.schemas.drawingml.x2006.chart.CTPieSer) CTStrVal(org.openxmlformats.schemas.drawingml.x2006.chart.CTStrVal) CTNumVal(org.openxmlformats.schemas.drawingml.x2006.chart.CTNumVal) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Test(org.junit.Test)

Example 7 with CTNumDataSource

use of org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource in project ocvn by devgateway.

the class XSSFBarChartData method createNewSerie.

@Override
protected CustomChartSeries createNewSerie(final int id, final int order, final ChartDataSource<?> categories, final ChartDataSource<? extends Number> values) {
    return new AbstractSeries(id, order, categories, values) {

        @Override
        public void addToChart(final XmlObject ctChart) {
            final CTBarChart ctBarChart = (CTBarChart) ctChart;
            final CTBarSer ctBarSer = ctBarChart.addNewSer();
            ctBarSer.addNewIdx().setVal(this.id);
            ctBarSer.addNewOrder().setVal(this.order);
            final CTAxDataSource catDS = ctBarSer.addNewCat();
            XSSFChartUtil.buildAxDataSource(catDS, this.categories);
            final CTNumDataSource valueDS = ctBarSer.addNewVal();
            XSSFChartUtil.buildNumDataSource(valueDS, this.values);
            if (isTitleSet()) {
                ctBarSer.setTx(getCTSerTx());
            }
        }
    };
}
Also used : CTNumDataSource(org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource) CTBarSer(org.openxmlformats.schemas.drawingml.x2006.chart.CTBarSer) XmlObject(org.apache.xmlbeans.XmlObject) CTBarChart(org.openxmlformats.schemas.drawingml.x2006.chart.CTBarChart) CTAxDataSource(org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource)

Example 8 with CTNumDataSource

use of org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource in project ocvn by devgateway.

the class XSSFScatterChartData method createNewSerie.

@Override
protected CustomChartSeries createNewSerie(final int id, final int order, final ChartDataSource<?> categories, final ChartDataSource<? extends Number> values) {
    return new AbstractSeries(id, order, categories, values) {

        @Override
        public void addToChart(final XmlObject ctChart) {
            final CTScatterChart ctScatterChart = (CTScatterChart) ctChart;
            final CTScatterSer scatterSer = ctScatterChart.addNewSer();
            scatterSer.addNewIdx().setVal(this.id);
            scatterSer.addNewOrder().setVal(this.order);
            final CTAxDataSource catDS = scatterSer.addNewXVal();
            XSSFChartUtil.buildAxDataSource(catDS, this.categories);
            final CTNumDataSource valueDS = scatterSer.addNewYVal();
            XSSFChartUtil.buildNumDataSource(valueDS, this.values);
            if (isTitleSet()) {
                scatterSer.setTx(getCTSerTx());
            }
        }
    };
}
Also used : CTScatterChart(org.openxmlformats.schemas.drawingml.x2006.chart.CTScatterChart) CTNumDataSource(org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource) CTScatterSer(org.openxmlformats.schemas.drawingml.x2006.chart.CTScatterSer) XmlObject(org.apache.xmlbeans.XmlObject) CTAxDataSource(org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource)

Example 9 with CTNumDataSource

use of org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource in project ocvn by devgateway.

the class ExcelChartDefaultTest method createWorkbook.

@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.getTitle().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());
    // check the actual chart data
    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);
}
Also used : CTNumDataSource(org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource) XSSFCategoryAxis(org.apache.poi.xssf.usermodel.charts.XSSFCategoryAxis) XSSFValueAxis(org.apache.poi.xssf.usermodel.charts.XSSFValueAxis) Workbook(org.apache.poi.ss.usermodel.Workbook) CTAxDataSource(org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource) XSSFChart(org.apache.poi.xssf.usermodel.XSSFChart) CTLineChart(org.openxmlformats.schemas.drawingml.x2006.chart.CTLineChart) CTChart(org.openxmlformats.schemas.drawingml.x2006.chart.CTChart) CTLineSer(org.openxmlformats.schemas.drawingml.x2006.chart.CTLineSer) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFDrawing(org.apache.poi.xssf.usermodel.XSSFDrawing) Test(org.junit.Test)

Aggregations

CTAxDataSource (org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource)9 CTNumDataSource (org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource)9 XmlObject (org.apache.xmlbeans.XmlObject)6 CTChart (org.openxmlformats.schemas.drawingml.x2006.chart.CTChart)3 CTPieChart (org.openxmlformats.schemas.drawingml.x2006.chart.CTPieChart)3 CTPieSer (org.openxmlformats.schemas.drawingml.x2006.chart.CTPieSer)3 OutputStream (java.io.OutputStream)2 POIXMLDocumentPart (org.apache.poi.POIXMLDocumentPart)2 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)2 CellReference (org.apache.poi.ss.util.CellReference)2 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)2 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)2 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)2 Test (org.junit.Test)2 CTLineChart (org.openxmlformats.schemas.drawingml.x2006.chart.CTLineChart)2 CTLineSer (org.openxmlformats.schemas.drawingml.x2006.chart.CTLineSer)2 CTNumData (org.openxmlformats.schemas.drawingml.x2006.chart.CTNumData)2 CTNumVal (org.openxmlformats.schemas.drawingml.x2006.chart.CTNumVal)2 CTPlotArea (org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea)2 CTSerTx (org.openxmlformats.schemas.drawingml.x2006.chart.CTSerTx)2