Page 1 of 1

Adding Charts

PostPosted: Fri Apr 22, 2011 3:29 am
by compactMetric
I tried to add a chart to a presentation via:

Object theChart = presentationMLPackage
.getParts().getParts()
.get(new PartName("/ppt/charts/chart1.xml"));

However, after this line, theChart is null.

Here is the full code:
Code: Select all
package com.ama.pptgen;
import org.docx4j.openpackaging.packages.PresentationMLPackage;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.PresentationML.MainPresentationPart;
import org.docx4j.openpackaging.parts.PresentationML.SlideLayoutPart;
import org.docx4j.openpackaging.parts.PresentationML.SlidePart;


public class PptGenerator
{
   public static void main(String[] args)
   {
      try
      {
         make();
      }
      catch (Exception e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }

   public static void make() throws Exception
   {
      // Where will we save our new .ppxt?
      String outputfilepath = System.getProperty("user.dir")
            + "/ppt_chart_test3.pptx";

      // Create skeletal package
      PresentationMLPackage presentationMLPackage = PresentationMLPackage
            .createPackage();

      // Create and add a new slidepart to the presentation package
      // First get Main Presentation Part and add new slide to it
      MainPresentationPart mpPart = (MainPresentationPart) presentationMLPackage
            .getParts().getParts()
            .get(new PartName("/ppt/presentation.xml"));

      SlidePart slidePart = new SlidePart(new PartName(
            "/ppt/slides/slide1.xml"));
      mpPart.addSlideIdListEntry(slidePart);
      slidePart.setJaxbElement(SlidePart.createSld());

      // Get and set slide layout part target
      SlideLayoutPart layoutPart = (SlideLayoutPart) presentationMLPackage
            .getParts().getParts()
            .get(new PartName("/ppt/slideLayouts/slideLayout1.xml"));
      slidePart.addTargetPart(layoutPart);

      //add chart
      Object theChart =  presentationMLPackage
         .getParts().getParts()
         .get(new PartName("/ppt/charts/chart1.xml"));
      System.out.println("theChart is of type " + theChart.getClass().toString());
      
      // All done: save it
      presentationMLPackage.save(new java.io.File(outputfilepath));

      System.out.println("\n\n done .. saved " + outputfilepath);
   }
}


If you run it, you should get a null pointer exception. Is there a work-around?

Re: Adding Charts

PostPosted: Fri Apr 22, 2011 7:38 pm
by jason
Create a pptx in Powerpoint and add a chart to it. Run that pptx through PartsList (using latest docx4j nightly), and you'll see it contains:

Code: Select all
        Part /ppt/slides/slide1.xml [org.docx4j.openpackaging.parts.PresentationML.SlidePart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide containing JaxbElement:org.pptx4j.pml.Sld

            Part /ppt/charts/chart1.xml [org.docx4j.openpackaging.parts.DrawingML.Chart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart containing JaxbElement:org.docx4j.dml.chart.CTChartSpace

                Part /ppt/embeddings/Microsoft_Office_Excel_Worksheet1.xlsx [org.docx4j.openpackaging.parts.WordprocessingML.EmbeddedPackagePart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/package


The code:

Code: Select all
      Object theChart =  presentationMLPackage
         .getParts().getParts()
         .get(new PartName("/ppt/charts/chart1.xml"));


doesn't actually create a new Part named "/ppt/charts/chart1.xml".

The way to do that is:

Code: Select all
      org.docx4j.openpackaging.parts.DrawingML.Chart chartPart
         = new org.docx4j.openpackaging.parts.DrawingML.Chart(new PartName("/ppt/charts/chart1.xml"));


Then you need to add it to your slide:

Code: Select all
      slidePart.addTargetPart(chartPart);


You'll also need to add content to your new chartPart, and probably add a spreadsheet to it (an EmbeddedPackagePart according to the PartsList output above).

Your slide will also need to contain a reference to the chart part:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
           <c:chart xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:id="rId3"/>
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


The value to use for @r:id is returned by the addTargetPart call.

Re: Adding Charts

PostPosted: Tue Dec 06, 2011 12:21 pm
by dpravin
Hi,

How do we edit the data from the spreadsheet that is used to render the Chart?

Re: Adding Charts

PostPosted: Tue Jan 31, 2012 10:10 am
by ikola
Hi ,

Any update on how to inject values in the spreadsheet that is used to render the Chart

I am planning to use docx4j for this .. Any samples or help is appreciated ..

Isaac