Data Access

From Apache OpenOffice Wiki
Jump to: navigation, search

Data can only be accessed for reading when a chart resides inside a spreadsheet document and was inserted as a table chart, that is, the table chart obtains its data from cell ranges of spreadsheets. To change the underlying data, modify the content of the spreadsheet cells. For OLE charts, that is, charts that were inserted as OLE2Shape objects, modify the data.

The data of a chart is acquired from the interface of the chart document model using the method The current implementation of charts provides a interface, derived from and supports the service

Documentation note.png Note that the interface definition of does not require XChartDocument.getData() to return an XChartDataArray, but XChartData, so there may be implementations that do not offer access to the values.

The methods of XChartDataArray are:

  sequence <sequence < double > > getData()
  void setData( [in] sequence <sequence < double > > aData)
  sequence < string > getRowDescriptions ()
  void setRowDescriptions(sequence < string aRowDescriptions >)
  sequence < string > getColumnDescriptions()
  void setColumnDescriptions(sequence < string aColumnDescriptions >)

Included are the following methods from XChartData:

  void addChartDataChangeEventListener( 
      [in] com::sun::star::chart::XChartDataChangeEventListener aListener)
  void removeChartDataChangeEventListener (
      [in] com::sun::star::chart::XChartDataChangeEventListener aListener)
  double getNotANumber()
  boolean isNotANumber( [in] double nNumber)

The interface offers several methods to access data. A sequence of sequences is obtained containing double values by calling getData(). With setData(), such an array of values can be applied to the XChartDataArray.

The arrays are a nested array, not two-dimensional. Java has only nested arrays, but in Basic a nested array must be used instead of a multidimensional array. The following example shows how to apply values to a chart in Basic:

  ' create data with dimensions 2 x 3
  ' 2 is called outer dimension and 3 inner dimension
  ' assume that oChartDocument contains a valid 
  Dim oData As Object
  Dim oDataArray( 0 To 1 ) As Object
  Dim oSeries1( 0 To 2 ) As Double
  Dim oSeries2( 0 To 2 ) As Double
  oSeries1( 0 ) = 3.141
  oSeries1( 1 ) = 2.718
  oSeries1( 2 ) = 1.0
  oSeries2( 0 ) = 17.0
  oSeries2( 1 ) = 23.0
  oSeries2( 2 ) = 42.0
  oDataArray( 0 ) = oSeries1()
  oDataArray( 1 ) = oSeries2()
  ' call getData() method of XChartDocument to get the XChartDataArray
  oData = oChartDocument.Data
  ' call setData() method of XChartDataArray to apply the data
  oData.Data = oDataArray()
  ' Note: to use the series arrays here as values for the series in the chart
  '       you have to set the DataRowSource of the XDiagram object to
  ' (default is COLUMNS)
Documentation note.png The Data obtained is a sequence that contains one sequence for each row. If you want to interpret the inner sequences as data for the series, set the DataRowSource of your XDiagram to, otherwise, the data for the nth series is in the nth element of each inner series.

With the methods of the XChartData interface, check if a number from the chart has to be interpreted as non-existent, that is, the number is not a number (NaN).

Documentation caution.png In the current implementation of Chart, the value of NaN is not the standard ISO value for NaN of the C++ double type, but instead DBL_MIN which is 2.2250738585072014-308.

Additionally, the XChartData interface is used to connect a component as a listener on data changes. For example, to use a spreadsheet as the source of the data of a chart that resides inside a presentation. It can also be used in the opposite direction: the spreadsheet could display the data from a chart that resides in a presentation document. To achieve this, the service also points to the XChartData interface, so that a listener can be attached to update the chart OLE object.

The following class ListenAtCalcRangeInDraw demonstrates how to synchronize the data of a spreadsheet range with a chart residing in another document. Here the chart is placed into a drawing document.

The class ListenAtCalcRangeInDraw in the example below implements a to get notified when the spreadsheet document or drawing document are closed.

It also implements a that listens for changes in the underlying XchartData. In this case, it is the range in the spreadsheet.

  // implement an XEventListener for listening to the disposing
  // of documents. Also implement XChartDataChangeEventListener
  // to get informed about changes of data in a chart
  public class ListenAtCalcRangeInDraw implements XChartDataChangeEventListener {
      public ListenAtCalcRangeInDraw(String args[]) {
          // create a spreadsheet document in maSheetDoc
          // create a drawing document in maDrawDoc
          // put a chart into the drawing document
          // and store it in maChartDocument
          // ...
          // assign a range from the spreadsheet to aRange
          // ...
          // attach the data coming from the cell range to the chart
          maChartData = (XChartData) UnoRuntime.queryInterface(XChartData.class, aRange);
      public void run() { 
          try {
              // show a sub title to inform about last update
              ((XPropertySet) UnoRuntime.queryInterface( 
                  XPropertySet.class, maChartDocument)).setPropertyValue(
                      "HasSubTitle", new Boolean(true));
              // start listening for death of spreadsheet
              ((XComponent) UnoRuntime.queryInterface(XComponent.class,
              // start listening for death of chart
              ((XComponent) UnoRuntime.queryInterface(XComponent.class,
              //start listening for change of data
          } catch ( ex) {
              System.out.println("Oops: " + ex);
          // call listener once for initialization
          ChartDataChangeEvent aEvent = new ChartDataChangeEvent();
          aEvent.Type = ChartDataChangeType.ALL;
      // XEventListener (base interface of XChartDataChangeEventListener)
      public void disposing( aSourceObj)
          // test if the Source object is a chart document
          if( UnoRuntime.queryInterface(XChartDocument.class, aSourceObj.Source) != null)
              System.out.println("Disconnecting Listener because Chart was shut down");
          // test if the Source object is a spreadsheet document
          if (UnoRuntime.queryInterface(XSpreadsheetDocument.class, aSourceObj.Source) != null)
              System.out.println("Disconnecting Listener because Spreadsheet was shut down");
          // remove data change listener
          // remove dispose listeners
          ((XComponent) UnoRuntime.queryInterface(XComponent.class,
          ((XComponent) UnoRuntime.queryInterface(XComponent.class,
          // this program cannot do anything any more
      // XChartDataChangeEventListener
      public void chartDataChanged(ChartDataChangeEvent aEvent)
          // update subtitle with current date
          String aTitle = new String("Last Update: " + new java.util.Date(System.currentTimeMillis()));
          try {
              ((XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
                      "String", aTitle);
          } catch(Exception ex) {
              System.out.println("Oops: " + ex);
          System.out.println("Data has changed");
      // private
      private maSheetDoc;
      private maDrawDoc;
      private maChartDocument;
      private maChartData;
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages