Creating a Chart OLE Object in Draw and Impress

From Apache OpenOffice Wiki
< Documentation‎ | DevGuide
Revision as of 16:26, 4 July 2018 by Sancho (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 com.sun.star.drawing.XDrawPagesSupplier of a draw or presentation document. To retrieve a single draw page, use com.sun.star.container.XIndexAccess.

A spreadsheet document is also a com.sun.star.drawing.XDrawPagesSupplier 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 com.sun.star.container.XIndexAccess of the container, returned by com.sun.star.drawing.XDrawPagesSupplier:getDrawPages().

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

The OpenOffice.org 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";
  com.sun.star.frame.XModel aDrawDoc;
 
  // get a draw document into aDrawDoc
  // ...
 
  // this will become the resulting chart
  XChartDocument aChartDoc;
 
  com.sun.star.drawing.XDrawPagesSupplier aSupplier = (com.sun.star.drawing.XDrawPagesSupplier)
      UnoRuntime.queryInterface(com.sun.star.drawing.XDrawPagesSupplier.class, aDrawDoc);
 
  com.sun.star.drawing.XShapes aPage = null;
  try {
      // get first page
      aPage = (com.sun.star.drawing.XShapes) aSupplier.getDrawPages().getByIndex(0);
  } catch (com.sun.star.lang.IndexOutOfBoundsException 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
              com.sun.star.drawing.XShape aShape = (com.sun.star.drawing.XShape)
                  UnoRuntime.queryInterface(
                      com.sun.star.drawing.XShape.class,
                          aFact.createInstance("com.sun.star.drawing.OLE2Shape"));
 
              // insert the shape into the page
              aPage.add(aShape);
              aShape.setPosition(new com.sun.star.awt.Point(1000, 1000));
              aShape.setSize(new com.sun.star.awt.Size(15000, 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(
                  XChartDocument.class,
                  aShapeProp.getPropertyValue("Model"));
          } catch(com.sun.star.uno.Exception 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