Page 1 of 1

Add Chart

PostPosted: Fri Sep 30, 2011 2:41 am
by joviedo
Hi,
I want to add a chart in a document word, but I don't do and I haven' t found example.

Please, you help me with a example.

Thanks.

Re: Add Chart

PostPosted: Sun Oct 02, 2011 2:34 pm
by jason
Hi,

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:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
      <w:r>
        <w:drawing>
          <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"/>
            <wp:cNvGraphicFramePr/>
            <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
              <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/chart">
                <c:chart xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:id="rId4"/>
              </a:graphicData>
            </a:graphic>
          </wp:inline>
        </w:drawing>
      </w:r>
 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


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

Re: Add Chart

PostPosted: Wed Oct 05, 2011 9:07 pm
by joviedo
Thanks for your response. But I would like to know the methods for work with charts.

Re: Add Chart

PostPosted: Thu Aug 07, 2014 9:22 pm
by Oleksandr
Hi Jason,

I did what you suggested in your previous post:
1. Create docx with a chart
2. Unpack docx
3. Find chart1.xml
4. Delete <c:externalData r:id="rId1"/>
5. Zip folder
6. Change file extension to docx
7. Try open file

MS Word 2010 says that there is an error: Some parts are missing or invalid.

Could you, please help me to solve this error.
P.S.
I just want to create several pie charts and I stuck on this problem.

Re: Add Chart

PostPosted: Fri Aug 08, 2014 10:34 am
by jason
I may be able to help if you attach your code as a single complete class, and input docx, so I can reproduce what you are doing

Re: Add Chart

PostPosted: Tue Feb 24, 2015 2:14 am
by sekhar-rangam
Programmatically creating MS Office-compatible charts

If you have a web application that has chart visualizations and customer requests a downloadable copy of the report that can be viewed in PowerPoint, How do you go about it?

see more at: http://bridgei2i.com/blog/programmatica ... le-charts/