--- /dev/null
+/*\r
+ * ====================================================================\r
+ * Licensed to the Apache Software Foundation (ASF) under one or more\r
+ * contributor license agreements. See the NOTICE file distributed with\r
+ * this work for additional information regarding copyright ownership.\r
+ * The ASF licenses this file to You under the Apache License, Version 2.0\r
+ * (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ====================================================================\r
+ */\r
+\r
+package org.apache.poi.xslf.usermodel;\r
+\r
+import org.apache.poi.POIXMLDocumentPart;\r
+import org.apache.poi.ss.util.CellRangeAddress;\r
+import org.apache.poi.ss.util.CellReference;\r
+import org.apache.poi.xssf.usermodel.XSSFRow;\r
+import org.apache.poi.xssf.usermodel.XSSFSheet;\r
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumData;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumVal;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieChart;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieSer;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTSerTx;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrData;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrVal;\r
+\r
+import java.io.BufferedReader;\r
+import java.io.FileInputStream;\r
+import java.io.FileOutputStream;\r
+import java.io.FileReader;\r
+import java.io.OutputStream;\r
+\r
+/**\r
+ * Build a pie chart from a template pptx\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class PieChartDemo {\r
+ private static void usage(){\r
+ System.out.println("Usage: PieChartDemo <pie-chart-template.pptx> <pie-chart-data.txt>");\r
+ System.out.println(" pie-chart-template.pptx template with a pie chart");\r
+ System.out.println(" pie-chart-data.txt the model to set. First line is chart title, " +\r
+ "then go pairs {axis-label value}");\r
+ }\r
+\r
+ public static void main(String[] args) throws Exception {\r
+ if(args.length < 2) {\r
+ usage();\r
+ return;\r
+ }\r
+\r
+ BufferedReader modelReader = new BufferedReader(new FileReader(args[1]));\r
+\r
+ String chartTitle = modelReader.readLine(); // first line is chart title\r
+\r
+ XMLSlideShow pptx = new XMLSlideShow(new FileInputStream(args[0]));\r
+ XSLFSlide slide = pptx.getSlides()[0];\r
+\r
+ // find chart in the slide\r
+ XSLFChart chart = null;\r
+ for(POIXMLDocumentPart part : slide.getRelations()){\r
+ if(part instanceof XSLFChart){\r
+ chart = (XSLFChart) part;\r
+ break;\r
+ }\r
+ }\r
+\r
+ if(chart == null) throw new IllegalStateException("chart not found in the template");\r
+\r
+ // embedded Excel workbook that holds the chart data\r
+ POIXMLDocumentPart xlsPart = chart.getRelations().get(0);\r
+ XSSFWorkbook wb = new XSSFWorkbook();\r
+ XSSFSheet sheet = wb.createSheet();\r
+\r
+ CTChart ctChart = chart.getCTChart();\r
+ CTPlotArea plotArea = ctChart.getPlotArea();\r
+\r
+ CTPieChart pieChart = plotArea.getPieChartArray(0);\r
+ //Pie Chart Series\r
+ CTPieSer ser = pieChart.getSerArray(0);\r
+\r
+ // Series Text\r
+ CTSerTx tx = ser.getTx();\r
+ tx.getStrRef().getStrCache().getPtArray(0).setV(chartTitle);\r
+ sheet.createRow(0).createCell(1).setCellValue(chartTitle);\r
+ String titleRef = new CellReference(sheet.getSheetName(), 0, 1, true, true).formatAsString();\r
+ tx.getStrRef().setF(titleRef);\r
+\r
+\r
+ // Category Axis Data\r
+ CTAxDataSource cat = ser.getCat();\r
+ CTStrData strData = cat.getStrRef().getStrCache();\r
+\r
+ // Values\r
+ CTNumDataSource val = ser.getVal();\r
+ CTNumData numData = val.getNumRef().getNumCache();\r
+\r
+ strData.setPtArray(null); // unset old axis text\r
+ numData.setPtArray(null); // unset old values\r
+\r
+\r
+ // set model\r
+ int idx = 0;\r
+ int rownum = 1;\r
+ String ln;\r
+ while((ln = modelReader.readLine()) != null){\r
+ String[] vals = ln.split("\\s+");\r
+ CTNumVal numVal = numData.addNewPt();\r
+ numVal.setIdx(idx);\r
+ numVal.setV(vals[1]);\r
+\r
+ CTStrVal sVal = strData.addNewPt();\r
+ sVal.setIdx(idx);\r
+ sVal.setV(vals[0]);\r
+\r
+ idx++;\r
+ XSSFRow row = sheet.createRow(rownum++);\r
+ row.createCell(0).setCellValue(vals[0]);\r
+ row.createCell(1).setCellValue(Double.valueOf(vals[1]));\r
+ }\r
+ numData.getPtCount().setVal(idx);\r
+ strData.getPtCount().setVal(idx);\r
+\r
+ String numDataRange = new CellRangeAddress(1, rownum-1, 1, 1).formatAsString(sheet.getSheetName(), true);\r
+ val.getNumRef().setF(numDataRange);\r
+ String axisDataRange = new CellRangeAddress(1, rownum-1, 0, 0).formatAsString(sheet.getSheetName(), true);\r
+ cat.getStrRef().setF(axisDataRange);\r
+\r
+ // updated the embedded workbook with the data\r
+ OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream();\r
+ wb.write(xlsOut);\r
+ xlsOut.close();\r
+\r
+ // save the result\r
+ FileOutputStream out = new FileOutputStream("pie-chart-demo-output.pptx");\r
+ pptx.write(out);\r
+ out.close();\r
+ }\r
+}\r
--- /dev/null
+My Chart\r
+First 1.0\r
+Second 3.0\r
+Third 4.0
\ No newline at end of file
--- /dev/null
+/*\r
+ * ====================================================================\r
+ * Licensed to the Apache Software Foundation (ASF) under one or more\r
+ * contributor license agreements. See the NOTICE file distributed with\r
+ * this work for additional information regarding copyright ownership.\r
+ * The ASF licenses this file to You under the Apache License, Version 2.0\r
+ * (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ====================================================================\r
+ */\r
+package org.apache.poi.xslf.usermodel;\r
+\r
+import junit.framework.TestCase;\r
+import org.apache.poi.POIXMLDocumentPart;\r
+import org.apache.poi.ss.util.CellRangeAddress;\r
+import org.apache.poi.ss.util.CellReference;\r
+import org.apache.poi.xslf.XSLFTestDataSamples;\r
+import org.apache.poi.xssf.usermodel.XSSFRow;\r
+import org.apache.poi.xssf.usermodel.XSSFSheet;\r
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
+import org.openxmlformats.schemas.drawingml.x2006.chart.*;\r
+import org.openxmlformats.schemas.drawingml.x2006.main.*;\r
+import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector;\r
+\r
+import java.awt.*;\r
+import java.io.*;\r
+import java.util.LinkedHashMap;\r
+import java.util.Map;\r
+\r
+/**\r
+ * @author Yegor Kozlov\r
+ */\r
+public class TestXSLFChart extends TestCase {\r
+\r
+ /**\r
+ * a modified version from POI-examples\r
+ */\r
+ public void testFillChartTemplate() throws Exception {\r
+\r
+ String chartTitle = "Apache POI"; // first line is chart title\r
+\r
+ XMLSlideShow pptx = XSLFTestDataSamples.openSampleDocument("pie-chart.pptx");\r
+ XSLFSlide slide = pptx.getSlides()[0];\r
+\r
+ // find chart in the slide\r
+ XSLFChart chart = null;\r
+ for(POIXMLDocumentPart part : slide.getRelations()){\r
+ if(part instanceof XSLFChart){\r
+ chart = (XSLFChart) part;\r
+ break;\r
+ }\r
+ }\r
+\r
+ if(chart == null) throw new IllegalStateException("chart not found in the template");\r
+\r
+ // embedded Excel workbook that holds the chart data\r
+ POIXMLDocumentPart xlsPart = chart.getRelations().get(0);\r
+ XSSFWorkbook wb = new XSSFWorkbook();\r
+ XSSFSheet sheet = wb.createSheet();\r
+\r
+ CTChart ctChart = chart.getCTChart();\r
+ CTPlotArea plotArea = ctChart.getPlotArea();\r
+\r
+ CTPieChart pieChart = plotArea.getPieChartArray(0);\r
+ //Pie Chart Series\r
+ CTPieSer ser = pieChart.getSerArray(0);\r
+\r
+ // Series Text\r
+ CTSerTx tx = ser.getTx();\r
+ tx.getStrRef().getStrCache().getPtArray(0).setV(chartTitle);\r
+ sheet.createRow(0).createCell(1).setCellValue(chartTitle);\r
+ String titleRef = new CellReference(sheet.getSheetName(), 0, 1, true, true).formatAsString();\r
+ tx.getStrRef().setF(titleRef);\r
+\r
+\r
+ // Category Axis Data\r
+ CTAxDataSource cat = ser.getCat();\r
+ CTStrData strData = cat.getStrRef().getStrCache();\r
+\r
+ // Values\r
+ CTNumDataSource valSrc = ser.getVal();\r
+ CTNumData numData = valSrc.getNumRef().getNumCache();\r
+\r
+ strData.setPtArray(null); // unset old axis text\r
+ numData.setPtArray(null); // unset old values\r
+\r
+ Map<String, Double> pieModel = new LinkedHashMap<String, Double>();\r
+ pieModel.put("First", 1.0);\r
+ pieModel.put("Second", 3.0);\r
+ pieModel.put("Third", 4.0);\r
+\r
+ // set model\r
+ int idx = 0;\r
+ int rownum = 1;\r
+ for(String key : pieModel.keySet()){\r
+ double val = pieModel.get(key);\r
+\r
+ CTNumVal numVal = numData.addNewPt();\r
+ numVal.setIdx(idx);\r
+ numVal.setV("" + val);\r
+\r
+ CTStrVal sVal = strData.addNewPt();\r
+ sVal.setIdx(idx);\r
+ sVal.setV(key);\r
+\r
+ idx++;\r
+ XSSFRow row = sheet.createRow(rownum++);\r
+ row.createCell(0).setCellValue(key);\r
+ row.createCell(1).setCellValue(val);\r
+ }\r
+ numData.getPtCount().setVal(idx);\r
+ strData.getPtCount().setVal(idx);\r
+\r
+ String numDataRange = new CellRangeAddress(1, rownum-1, 1, 1).formatAsString(sheet.getSheetName(), true);\r
+ valSrc.getNumRef().setF(numDataRange);\r
+ String axisDataRange = new CellRangeAddress(1, rownum-1, 0, 0).formatAsString(sheet.getSheetName(), true);\r
+ cat.getStrRef().setF(axisDataRange);\r
+\r
+ // updated the embedded workbook with the data\r
+ OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream();\r
+ wb.write(xlsOut);\r
+ xlsOut.close();\r
+\r
+ }\r
+\r
+}
\ No newline at end of file
package org.apache.poi.xslf.usermodel;\r
\r
import junit.framework.TestCase;\r
-import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndLength;\r
-import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndType;\r
-import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndWidth;\r
+import org.openxmlformats.schemas.drawingml.x2006.main.*;\r
+import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector;\r
+\r
+import java.awt.*;\r
\r
/**\r
* @author Yegor Kozlov\r
\r
}\r
\r
+ public void testAddConnector(){\r
+ XMLSlideShow pptx = new XMLSlideShow();\r
+ XSLFSlide slide = pptx.createSlide();\r
+\r
+ XSLFAutoShape rect1 = slide.createAutoShape();\r
+ rect1.setShapeType(XSLFShapeType.RECT);\r
+ rect1.setAnchor(new Rectangle(100, 100, 100, 100));\r
+ rect1.setFillColor(Color.blue);\r
+\r
+ XSLFAutoShape rect2 = slide.createAutoShape();\r
+ rect2.setShapeType(XSLFShapeType.RECT);\r
+ rect2.setAnchor(new Rectangle(300, 300, 100, 100));\r
+ rect2.setFillColor(Color.red);\r
+\r
+\r
+ XSLFConnectorShape connector1 = slide.createConnector();\r
+ connector1.setAnchor(new Rectangle(200, 150, 100, 200));\r
+\r
+ CTConnector ctConnector = (CTConnector)connector1.getXmlObject();\r
+ ctConnector.getSpPr().getPrstGeom().setPrst(STShapeType.BENT_CONNECTOR_3);\r
+ CTNonVisualConnectorProperties cx = ctConnector.getNvCxnSpPr().getCNvCxnSpPr();\r
+ // connection start\r
+ CTConnection stCxn = cx.addNewStCxn();\r
+ stCxn.setId(rect1.getShapeId());\r
+ // side of the rectangle to attach the connector: left=1, bottom=2,right=3, top=4\r
+ stCxn.setIdx(2);\r
+\r
+ CTConnection end = cx.addNewEndCxn();\r
+ end.setId(rect2.getShapeId());\r
+ // side of the rectangle to attach the connector: left=1, bottom=2,right=3, top=4\r
+ end.setIdx(3);\r
+ }\r
+\r
}
\ No newline at end of file