Creating a Chart OLE Object in Draw and Impress

From Apache OpenOffice Wiki
Jump to: navigation, search

The alternative is to create an OLE shape and insert it into a draw page. Writer, Spreadsheet documents and Draw/Impress documents have access to a draw page. The shape is told to be a chart by setting the unique class-id.

Documentation note.png The unique Class-Id string for charts is "12dcae26-281f-416f-a234-c3086127382e".

A draw page collection is obtained from the of a draw or presentation document. To retrieve a single draw page, use

A spreadsheet document is also a that provides draw pages for all sheets, that is, the draw page for the third sheet is obtained by calling getByIndex(2) on the interface of the container, returned by

A spreadsheet draw page can be acquired directly at the corresponding sheet object. A single sheet supports the service that supplies a single page,, where the page is acquired using the method getDrawPage().

The Writer currently does not support the creation of OLE Charts or Charts based on a text table in a Writer document using the API.

The document model is required once a chart is inserted. In charts inserted as OLE2Shape, the document model is available at the property Model of the OLE2Shape after setting the CLSID.

Documentation note.png Note, that the mechanism described for OLE objects seems to work in Writer, that is, you can see the OLE-Chart inside the Text document after executing the API calls described, but it is not treated as a real OLE object by the Writer. Thus, you can not activate it by double-clicking, because it puts the document into an inconsistent state.

The following example assumes a valid drawing document in the variable aDrawDoc and creates a chart in a Draw document. See Drawing Documents and Presentation Documents for an example of how to create a drawing document.

  final String msChartClassID = "12dcae26-281f-416f-a234-c3086127382e"; aDrawDoc;
  // get a draw document into aDrawDoc
  // ...
  // this will become the resulting chart
  XChartDocument aChartDoc; aSupplier = (
      UnoRuntime.queryInterface(, aDrawDoc); aPage = null;
  try {
      // get first page
      aPage = ( aSupplier.getDrawPages().getByIndex(0);
  } catch ( ex) {
      System.out.println("Document has no pages: " + ex);
  if (aPage != null) {
      // the document should be a factory that can create shapes
      XMultiServiceFactory aFact = (XMultiServiceFactory) UnoRuntime.queryInterface(
          XMultiServiceFactory.class, aDrawDoc);
      if (aFact != null) {
          try {
              // create an OLE 2 shape
     aShape = (
              // insert the shape into the page
              aShape.setPosition(new, 1000));
              aShape.setSize(new, 9271));
              // make the OLE shape a chart
              XPropertySet aShapeProp = (XPropertySet) UnoRuntime.queryInterface(
                  XPropertySet.class, aShape );
              // set the class id for charts
              aShapeProp.setPropertyValue("CLSID", msChartClassID);
              // retrieve the chart document as model of the OLE shape
              aChartDoc = (XChartDocument) UnoRuntime.queryInterface(
          } catch( ex) {
              System.out.println("Couldn't change the OLE shape into a chart: " + ex);
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages