Create a new docx in Word, and insert a chart in it.
You can then inspect the result to see what you need to mimic.
I did this, and ran it through the docx4j PartsList sample.
That told me document.xml has a rel to:
- Code: Select all
Part /word/charts/chart1.xml [org.docx4j.openpackaging.parts.DrawingML.Chart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart containing JaxbElement:org.docx4j.dml.chart.CTChartSpace
which in turn has a rel to the spreadsheet containing the original data (duplicated in chart1.xml):
- Code: Select all
Part /word/embeddings/Microsoft_Office_Excel_Worksheet1.xlsx [org.docx4j.openpackaging.parts.WordprocessingML.EmbeddedPackagePart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/package
document.xml also has content which points to chart1 via a w:drawing element:
Using xml Syntax Highlighting
<wp:inline distT="0" distB="0" distL="0" distR="0">
<wp:extent cx="5486400" cy="3200400"/>
<wp:effectExtent l="19050" t="0" r="19050" b="0"/>
<wp:docPr id="1" name="Chart 1"/>
<c:chart xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:id="rId4"/>
Parsed in 0.002 seconds, using GeSHi
The chart1.xml part uses <c:externalData r:id="rId1"/> to point to the spreadsheet.
That is actually optional. I deleted that line and the embedded spreadsheet from my docx, and Word 2007 happily opened it and rendered my chart1.xml data
So, if you aren't starting with an Excel spreadsheet, I don't think you need to create one and embed it.
If you already have a spreadsheet, its better to embed it, since without it, you can't right click in Word to edit the data (assuming you care!).
In summary, it is pretty straightforward (as long as you don't embed the spreadsheet):
- create/populate the Chart part, and add it to your package (you can read the required XML from a file or string if you want) using addTargetPart
- add the w:drawing element to your document.xml, using the relId returned by addTargetPart.
Hope this helps .. Jason