mirror of https://github.com/apache/poi.git
Retrieve chart from graphical frame
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1871012 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
4703c18b83
commit
bb2ad49a2f
|
@ -21,6 +21,7 @@ package org.apache.poi.xslf.usermodel;
|
||||||
|
|
||||||
import java.awt.geom.Rectangle2D;
|
import java.awt.geom.Rectangle2D;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
@ -95,6 +96,20 @@ public class ChartFromScratch {
|
||||||
ppt.write(out);
|
ppt.write(out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
try (FileInputStream is = new FileInputStream("chart-from-scratch.pptx")) {
|
||||||
|
try (XMLSlideShow ppt = new XMLSlideShow(is)) {
|
||||||
|
for (XSLFSlide slide : ppt.getSlides()) {
|
||||||
|
for (XSLFShape shape : slide.getShapes()) {
|
||||||
|
if (shape instanceof XSLFGraphicFrame) {
|
||||||
|
XSLFGraphicFrame frame = (XSLFGraphicFrame) shape;
|
||||||
|
if (frame.hasChart()) {
|
||||||
|
System.out.println(frame.getChart().getTitleShape().getText());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
System.out.println("Done");
|
System.out.println("Done");
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,6 +47,7 @@ import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
|
||||||
|
|
||||||
@Beta
|
@Beta
|
||||||
public class XSLFGraphicFrame extends XSLFShape implements GraphicalFrame<XSLFShape, XSLFTextParagraph> {
|
public class XSLFGraphicFrame extends XSLFShape implements GraphicalFrame<XSLFShape, XSLFTextParagraph> {
|
||||||
|
private static final String DRAWINGML_CHART_URI = "http://schemas.openxmlformats.org/drawingml/2006/chart";
|
||||||
private static final POILogger LOG = POILogFactory.getLogger(XSLFGraphicFrame.class);
|
private static final POILogger LOG = POILogFactory.getLogger(XSLFGraphicFrame.class);
|
||||||
|
|
||||||
/*package*/ XSLFGraphicFrame(CTGraphicalObjectFrame shape, XSLFSheet sheet){
|
/*package*/ XSLFGraphicFrame(CTGraphicalObjectFrame shape, XSLFSheet sheet){
|
||||||
|
@ -97,7 +98,7 @@ public class XSLFGraphicFrame extends XSLFShape implements GraphicalFrame<XSLFSh
|
||||||
return new XSLFGraphicFrame(shape, sheet);
|
return new XSLFGraphicFrame(shape, sheet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getUri(CTGraphicalObjectFrame shape) {
|
private static String getUri(CTGraphicalObjectFrame shape) {
|
||||||
final CTGraphicalObject g = shape.getGraphic();
|
final CTGraphicalObject g = shape.getGraphic();
|
||||||
if (g == null) {
|
if (g == null) {
|
||||||
|
@ -160,15 +161,45 @@ public class XSLFGraphicFrame extends XSLFShape implements GraphicalFrame<XSLFSh
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean hasChart() {
|
||||||
|
String uri = getGraphicalData().getUri();
|
||||||
|
return uri.equals(DRAWINGML_CHART_URI);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CTGraphicalObjectData getGraphicalData() {
|
||||||
|
return ((CTGraphicalObjectFrame)getXmlObject()).getGraphic().getGraphicData();
|
||||||
|
}
|
||||||
|
|
||||||
|
public XSLFChart getChart() {
|
||||||
|
if (hasChart()) {
|
||||||
|
String id = null;
|
||||||
|
String xpath = "declare namespace c='" + DRAWINGML_CHART_URI + "' c:chart";
|
||||||
|
XmlObject[] obj = getGraphicalData().selectPath(xpath);
|
||||||
|
if (obj != null && obj.length == 1) {
|
||||||
|
XmlCursor c = obj[0].newCursor();
|
||||||
|
QName idQualifiedName = new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "id");
|
||||||
|
id = c.getAttributeText(idQualifiedName);
|
||||||
|
c.dispose();
|
||||||
|
}
|
||||||
|
if (id == null) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return (XSLFChart) getSheet().getRelationById(id);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void copy(XSLFShape sh){
|
void copy(XSLFShape sh){
|
||||||
super.copy(sh);
|
super.copy(sh);
|
||||||
|
|
||||||
CTGraphicalObjectData data = ((CTGraphicalObjectFrame)getXmlObject()).getGraphic().getGraphicData();
|
CTGraphicalObjectData data = getGraphicalData();
|
||||||
String uri = data.getUri();
|
String uri = data.getUri();
|
||||||
if(uri.equals("http://schemas.openxmlformats.org/drawingml/2006/diagram")){
|
if(uri.equals("http://schemas.openxmlformats.org/drawingml/2006/diagram")){
|
||||||
copyDiagram(data, (XSLFGraphicFrame)sh);
|
copyDiagram(data, (XSLFGraphicFrame)sh);
|
||||||
} if(uri.equals("http://schemas.openxmlformats.org/drawingml/2006/chart")){
|
} if(uri.equals(DRAWINGML_CHART_URI)){
|
||||||
copyChart(data, (XSLFGraphicFrame)sh);
|
copyChart(data, (XSLFGraphicFrame)sh);
|
||||||
} else {
|
} else {
|
||||||
// TODO support other types of objects
|
// TODO support other types of objects
|
||||||
|
@ -179,7 +210,7 @@ public class XSLFGraphicFrame extends XSLFShape implements GraphicalFrame<XSLFSh
|
||||||
private void copyChart(CTGraphicalObjectData objData, XSLFGraphicFrame srcShape) {
|
private void copyChart(CTGraphicalObjectData objData, XSLFGraphicFrame srcShape) {
|
||||||
XSLFSlide slide = (XSLFSlide) getSheet();
|
XSLFSlide slide = (XSLFSlide) getSheet();
|
||||||
XSLFSheet src = srcShape.getSheet();
|
XSLFSheet src = srcShape.getSheet();
|
||||||
String xpath = "declare namespace c='http://schemas.openxmlformats.org/drawingml/2006/chart' c:chart";
|
String xpath = "declare namespace c='" + DRAWINGML_CHART_URI + "' c:chart";
|
||||||
XmlObject[] obj = objData.selectPath(xpath);
|
XmlObject[] obj = objData.selectPath(xpath);
|
||||||
if (obj != null && obj.length == 1) {
|
if (obj != null && obj.length == 1) {
|
||||||
XmlCursor c = obj[0].newCursor();
|
XmlCursor c = obj[0].newCursor();
|
||||||
|
|
Loading…
Reference in New Issue