A Combined Chart Using OpenOffice.org Chart2 API

This Post describes a method to programatically combine different OpenOffice chart types in same coordinate system. This will be done using OpenOffice.org chart2 API as the old chart1 API is not flexible enough to allow combining multiple OpenOffice.org chart-types in same Graph. ( see my previous topic : OpenOffice Chart2 Not yet Ready! )

This Tutorial suppose you are already familiar with OpenOffice UNO API development in Java. If you are new to this field you can get started by taking a look to the Openoffice.org API developer guide.
From my own experience I found the Netbeans OpenOffice API plugin to be an Excellent Tool that could simplify the somewhat complicated tasks of java UNO API development. Kay Kroll from Netbeans.org has an article on Netbeans OpenOffice integration. He mentioned that we could extend OpenOffice.org’s charting capabilities by using the Netbeans OpenOffice Integration tool.

Now let’s walk through the steps to produce a combined chart using the chart2 API and the Java programming language. Again a warning about using com.sun.star.chart2 API; It is not provided for external usage yet. It is an internal API that will change further. When the API evolves your code will not work with future versions. If you nevertheless want to use the chart2 API on your own risk do the following:

1-Query your chart document for interface com.sun.star.chart2.XChartDocument

2-Call methods getFirstDiagram() and getDataProvider() on the returned Object from step one

3-Query the received diagram for interface com.sun.star.chart2.XCoordinateSystemContainer

4-Receive the first and only coordinate system from the container

5-Query the coordinate system for interface com.sun.star.chart2.XChartTypeContainer

The Java code snippet below shows the implementation for steps 1 to 5:

 public com.sun.star.chart2.XChartTypeContainer getChartTypeContainer() {
    /* let the Calc document create a data provider, set it at the chart */
    com.sun.star.chart2.data.XDataProvider oDataProv = xChartDoc.getDataProvider();
    com.sun.star.chart2.XDiagram  oDiagram  = xChartDoc.getFirstDiagram();
    // insert a coordinate system into the diagram
com.sun.star.chart2.XCoordinateSystemContainer oCoordSysCnt =(com.sun.star.chart2.XCoordinateSystemContainer) UnoRuntime.queryInterface(
                com.sun.star.chart2.XCoordinateSystemContainer.class, oDiagram);
 com.sun.star.chart2.XCoordinateSystem[] oCoordSys = oCoordSysCnt.getCoordinateSystems();
 // Query the coordinate system for interface com.sun.star.chart2.XChartTypeContainer
   xChartTypeContainer = (com.sun.star.chart2.XChartTypeContainer) UnoRuntime.queryInterface(
    com.sun.star.chart2.XChartTypeContainer.class, oCoordSys[0]);
        return xChartTypeContainer;

6-create an instance of the line chart type :

public com.sun.star.chart2.XChartType getChartType(String type) throws Exception {
        Object  object    = xMCF.createInstanceWithContext(type, xContext);
        com.sun.star.chart2.XChartType chartType =(com.sun.star.chart2.XChartType)
        UnoRuntime.queryInterface(com.sun.star.chart2.XChartType.class, object);
        return chartType;

7-add the line chart type to chart type container:

com.sun.star.chart2.XChartType lineChartType =chart.getChartType("com.sun.star.chart2.LineChartType");
  // add line chart type

8-Query the new chart type for interface com.sun.star.chart2.XDataSeriesContainer:

com.sun.star.chart2.XDataSeriesContainer dataSeriesCnt=(com.sun.star.chart2.XDataSeriesContainer)UnoRuntime.queryInterface(com.sun.star.chart2.XDataSeriesContainer.class,lineChartType);

9-Create a new data series ( create an instance of service com.sun.star.chart2.DataSeries) and add it to the container:

public com.sun.star.chart2.XDataSeries getDataSeries() throws Exception {
Object object = xMCF.createInstanceWithContext("com.sun.star.chart2.DataSeries",xContext);
com.sun.star.chart2.XDataSeries oSeries =(com.sun.star.chart2.XDataSeries)
UnoRuntime.queryInterface(com.sun.star.chart2.XDataSeries.class, object);
return oSeries;


To connect data to the data series do the following:

10- Query the series for interface com.sun.star.chart2.data.XDataSink:

com.sun.star.chart2.data.XDataSink dataSink=(com.sun.star.chart2.data.XDataSink)UnoRuntime.queryInterface(com.sun.star.chart2.data.XDataSink.class,oSeries);

11- Use method createDataSequenceByRangeRepresentation() at the formerly received data provider to create a data sequences from the cell ranges of your choice:

com.sun.star.chart2.data.XDataSequence oSequence=oDataProv.createDataSequenceByRangeRepresentation("$Sheet1.$A$70:$C$777");

12-Set the property ‘Role’ at the data sequence to ‘values-y’:

XPropertySet cProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oSequence);
cProp.setPropertyValue("Role", "values-y");

13-Create an instance of UNO service ‘com.sun.star.chart2.LabeledDataSequence’ and use method setValues() to attach the formerly created sequence:

Object object =xMCF.createInstanceWithContext("com.sun.star.chart2.data.LabeledDataSequence", xContext);
com.sun.star.chart2.data.XLabeledDataSequence oLabeledSequence=(com.sun.star.chart2.data.XLabeledDataSequence)UnoRuntime.queryInterface(com.sun.star.chart2.data.XLabeledDataSequence.class, object);

14-Use method ‘setData()’ at the formerly received interface XDataSink to attach the labeled data sequences to the series:

com.sun.star.chart2.data.XLabeledDataSequence[] aLabeledSequence={ dLabeledSequence,oLabeledSequence,hLabeledSequence, lLabeledSequence, cLabeledSequence };

Using the Netbeans OpenOffice.org plugin i created a OpenOffice Addon that implements the above 14 steps to draw a combined candle chart and 2 line charts super-imposed in the same Graph coordinate system.
the results are shown in Image below :

combined candle and line charts

(Graph programatically drawn using openoffice.org chart2 API)

As you can see this method gives quit satisfactory results. However, No doubt the Openoffice Charting component is not yet mature and is by no way a robust charting API for Java developers. SUN OpenOffice.org charting developer team admit these facts and say there is still lot of ongoing work.

Waiting for the official OpenOffice Chart2 API release , i Hope this Tutorial could be useful for some Java UNO API developers willing to start playing with the unofficial OpenOffice chart2 API.

You can check the source code demo of this project at Github.



4 comments on “A Combined Chart Using OpenOffice.org Chart2 API

  1. Fantastic blog! I definitely love how it is easy on my eyes and also the data are well written. I am wondering how I could be notified whenever a new post has been made. I have subscribed to your rss feed which really should do the trick! Have a nice day!

  2. Great post, I always like to read this type of informative post,plz keep posting to upgrade my knowledge.

  3. Thanks for the well-thought article. I’m actually at work right now! So I need to go off without reading all I’d like. But, I put your site on my msn feed so that I can read even more.

  4. This is very moving work you have written for us. Some people need to know that these things can ensue to anyone. You have given me a better position now

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s