Page 1 of 1

Need help to create chart in PPT slide

PostPosted: Thu Sep 12, 2013 9:29 pm
by praveenbnsm
Hi,

I'm trying to insert a chart into PPT by unmarshalling an existing chart XML.

I've created ChartSpace by unmarshalling chart XML. created Chartobject and added to slide by using the following code.

>>>
Chart chart = new org.docx4j.openpackaging.parts.DrawingML.Chart(new PartName("/ppt/charts/chart1.xml"));
CTChartSpace cs = (CTChartSpace)XmlUtils.unmarshalString(chartXML);
chart.setJaxbElement(cs);
sp.addTargetPart(chart);
<<<

When i tried opening the presentation, it's says "Powerpoint found a problem with the content and it can attempt to repair". The moment, I click on repair, it says 'Removed unreadable content' and blank slide is getting displayed.

Could anyone please provide help in fixing this issue ? I'm attaching the generated PPT for reference

-Thanks,
Praveen

Could you please help me in figuring out the actual issue ? I'm attaching the generated PPTX file here.

-Thanks,
Praveenb

Re: Need help to create chart in PPT slide

PostPosted: Fri Sep 13, 2013 1:35 am
by praveenbnsm
Hi,

After going through the steps which I've performed, came to know that i didn't add the graphicFrame with the chart reference to the slide. After doing that, chart is getting displayed in the slide.

But some how, bottom axis is not getting displayed. Please tell me if I'm doing something wrong. I'm attaching the latest PPT that has been generated.

And also, I'm encountering the following WARNING during PPT generation.

WARN org.docx4j.jaxb.JaxbValidationEventHandler.handleEvent line 90 - [ERROR] : unexpected element (uri:"http://schemas.openxmlformats.org/drawingml/2006/chart", local:"shape"). Expected elements are


-Thanks,
Praveen

Re: Need help to create chart in PPT slide

PostPosted: Fri Nov 22, 2013 6:43 am
by rickgarcia
After going through the steps which I've performed, came to know that i didn't add the graphicFrame with the chart reference to the slide. After doing that, chart is getting displayed in the slide.


Hi. I'm trying to add a chart to a slide as you just did.

Would you mind posting the code you used to add the chart reference to the graphic frame? I've searched everywhere on how to do this, but it seems I can't find an answer, and your post is the top hit on Google search.

Thanks.

Re: Need help to create chart in PPT slide

PostPosted: Fri Nov 22, 2013 8:52 am
by rickgarcia
Nevermind, I figured it out.

With the webapp located at http://webapp.docx4java.org/ , I managed to obtain the following code:

Method 1: via ObjectFactory

Code: Select all
mport javax.xml.bind.JAXBElement;
import org.docx4j.dml.GraphicData;
import org.docx4j.dml.chart.CTRelId;


public class Foo {
public GraphicData createIt() {

org.docx4j.dml.ObjectFactory dmlObjectFactory = new org.docx4j.dml.ObjectFactory();

GraphicData graphicdata = dmlObjectFactory.createGraphicData();
    graphicdata.setUri( "http://schemas.openxmlformats.org/drawingml/2006/chart");
org.docx4j.dml.chart.ObjectFactory dmlchartObjectFactory = new org.docx4j.dml.chart.ObjectFactory();
    // Create object for chart (wrapped in JAXBElement)
    CTRelId relid = dmlchartObjectFactory.createCTRelId();
    JAXBElement<org.docx4j.dml.chart.CTRelId> relidWrapped = dmlchartObjectFactory.createChart(relid);
    graphicdata.getAny().add( relidWrapped);
        relid.setId( "rId2");

return graphicdata;
}
}


Method 2:

Code: Select all
String openXML = "<a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/chart\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:c=\"http://schemas.openxmlformats.org/drawingml/2006/chart\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
            + "<c:chart r:id=\"rId2\"/>"
        +"</a:graphicData>";
GraphicData graphicdata = (GraphicData)XmlUtils.unmarshalString(openXML);


But instead of using "rId2", we should use rel.getId(), where rel is the result of sp.addTargetPart(part), part is the chart that we have previously created, and sp is our SlidePart object.

By the way, your original code for unmarshalling the chart xml didn't work with version 2.8.1. Here's the code that I used:

Code: Select all
CTChartSpace el = ((JAXBElement<CTChartSpace>) XmlUtils.unmarshalString(chartXML)).getValue();
org.docx4j.openpackaging.parts.DrawingML.Chart chart = new org.docx4j.openpackaging.parts.DrawingML.Chart(new PartName("/ppt/charts/chart1.xml"));
chart.setJaxbElement(el);

Re: Need help to create chart in PPT slide

PostPosted: Tue Mar 11, 2014 8:29 am
by badari
Hi All,
My use case is very similar, I need to export a dynamic number of charts into a ppt. I am able to edit a template ppt to create a bar chart & pie chart.
But when I try to create a new ppt and add the chart in slide 1, the ppt is created but doesn't open up successfully. The chart.xml is created properly but there is no worksheet containing the data for the chart. Can someone point out what I'm doing wrong? (code snippet below)

Code: Select all
PresentationMLPackage presentationMLPackage = PresentationMLPackage.createPackage();

        MainPresentationPart pp = (MainPresentationPart)presentationMLPackage.getParts().getParts().get(
                new PartName("/ppt/presentation.xml"));

        SlideLayoutPart layoutPart = (SlideLayoutPart)presentationMLPackage.getParts().getParts().get(
                new PartName("/ppt/slideLayouts/slideLayout1.xml"));


        SlidePart slidePart = presentationMLPackage.createSlidePart(pp, layoutPart,
                new PartName("/ppt/slides/slide1.xml"));

        Chart chart = getBarChartPart();

        Relationship relationship = slidePart.addTargetPart(chart);
        CTGraphicalObjectFrame ctGraphicalObjectFrame = createChartFrame(relationship);

        slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add( ctGraphicalObjectFrame );

Re: Need help to create chart in PPT slide

PostPosted: Mon Feb 23, 2015 11:57 pm
by sekhar-rangam
I created a sample code with blog that explains **"Programmatically creating MS Office-compatible charts"** using docx4j library

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

Re: Need help to create chart in PPT slide

PostPosted: Sat Oct 17, 2015 5:42 am
by agashen
Hi Sekhar,

I referred to your code, it is really helpful. thanks!

I am trying to change the chart type to line chart, for that i changed chart-data.xml file and thought that when i generate the chart it will generate the line chart but it still keeps generating bar chart.
could you please help me with it..? please see my chart-data file in attachments.

Regards,
Narendra

Re: Need help to create chart in PPT slide

PostPosted: Fri Jul 28, 2017 8:11 pm
by rishi
Hi,
I am creating a pptx using dox4j and trying to create chart from it refering to below link
http://bridgei2i.com/blog/programmatica ... le-charts/

However a blank pptx is getting generated.
Could someone please assist? I have gone through this post and tried the snippets here but no luck.

Here is my code. May be I am doing some minor mistake. Kindly help.
I am using the chart.xml from below link
https://github.com/sekhar-rangam/docx4j ... t_data.xml

package default;

import org.docx4j.XmlUtils;
import org.docx4j.dml.chart.CTChartSpace;
import org.docx4j.openpackaging.exceptions.Docx4JException;
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;
import org.pptx4j.Pptx4jException;
import org.pptx4j.jaxb.Context;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import java.io.*;

public class CreateChartDox4j {
public static void main(String args[]) throws IOException, JAXBException, Docx4JException, Pptx4jException {

String outputfilepath = System.getProperty("user.dir") + "/pptx-test1.pptx";

InputStream in = null;
BufferedReader br = null;
Reader fr =null;
String line = "";

PresentationMLPackage presentationMLPackage = PresentationMLPackage.createPackage();
MainPresentationPart pp = (MainPresentationPart) presentationMLPackage.getParts().getParts().get(
new PartName("/ppt/presentation.xml"));
SlideLayoutPart layoutPart = (SlideLayoutPart) presentationMLPackage.getParts().getParts().get(
new PartName("/ppt/slideLayouts/slideLayout1.xml"));

// OK, now we can create a slide
SlidePart slidePart = new SlidePart(new PartName("/ppt/slides/slide1.xml"));
slidePart.setContents(SlidePart.createSld());
pp.addSlide(0, slidePart);

// Slide layout part
slidePart.addTargetPart(layoutPart);

int slideIndex =1;
org.docx4j.openpackaging.parts.DrawingML.Chart chartPart = new org.docx4j.openpackaging.parts.DrawingML.Chart(new PartName("/ppt/charts/chart" + slideIndex + ".xml"));
StringBuffer chartXMLBuffer = new StringBuffer();
String chartDataXmlFile = "/temp/chart.xml";
in = Docx4jChart.class.getResourceAsStream(chartDataXmlFile);
fr = new InputStreamReader(in, "utf-8");
br = new BufferedReader(fr);
while ((line = br.readLine()) != null) {
chartXMLBuffer.append(line);
chartXMLBuffer.append("");
}
//CTChartSpace chartSpace = (CTChartSpace) XmlUtils.unmarshalString(chartXMLBuffer.toString(), Context.jcPML,CTChartSpace.class);
CTChartSpace chartSpace = ((JAXBElement<CTChartSpace>) XmlUtils.unmarshalString(chartXMLBuffer.toString())).getValue();
chartPart.setJaxbElement(chartSpace );
slidePart.addTargetPart(chartPart);

presentationMLPackage.save(new java.io.File(outputfilepath));
System.out.println("Powerpoint Office document is created in the following path: "+outputfilepath);


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

}
}

Re: Need help to create chart in PPT slide

PostPosted: Fri Jul 28, 2017 11:20 pm
by rishi
@Jason could you please address. That would be really gratefull

Re: Need help to create chart in PPT slide

PostPosted: Mon Jul 31, 2017 3:23 pm
by rishi
Hi Jason, could you please assist here. Do we need to purchase license version for this?

Re: Need help to create chart in PPT slide

PostPosted: Wed Nov 28, 2018 2:10 am
by mgriffin629
rishi

Did you manage to figure out why your chart did not display in your powerpoint slide?
I am having a similiar issue.