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 com.sun.star.chart.XChartDocument interface of the chart document model using the method com.sun.star.chart.XChartDocument:getData(). The current implementation of OpenOffice.org charts provides a com.sun.star.chart.XChartDataArray interface, derived from com.sun.star.chart.XChartData and supports the service com.sun.star.chart.ChartDataArray.

Documentation note.png Note that the interface definition of com.sun.star.chart.XChartDocument 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 com.sun.star.chart.XChartDataArray 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 
  ' com.sun.star.chart.XChartDocument
 
  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
  '       com.sun.star.chart.ChartDataRowSource.ROWS (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 com.sun.star.chart.ChartDataRowSource.ROWS, 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 OpenOffice.org 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 com.sun.star.table.CellRange 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 com.sun.star.lang.XEventListener to get notified when the spreadsheet document or drawing document are closed.

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

  import com.sun.star.uno.UnoRuntime;
  import com.sun.star.lang.XEventListener;
  import com.sun.star.beans.XPropertySet;
  import com.sun.star.lang.XComponent;
 
  import com.sun.star.chart.*;
  import com.sun.star.sheet.XSpreadsheetDocument;
 
  // 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
          // ...
 
          com.sun.star.table.XCellRange aRange;
          // 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);
          maChartDocument.attachData(maChartData);
      }
 
      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,
                  maSheetDoc)).addEventListener(this);
 
              // start listening for death of chart
              ((XComponent) UnoRuntime.queryInterface(XComponent.class,
                  maChartDocument)).addEventListener(this);
 
              //start listening for change of data
              maChartData.addChartDataChangeEventListener(this);
          } catch (com.sun.star.uno.Exception ex) {
              System.out.println("Oops: " + ex);
          }
 
          // call listener once for initialization
          ChartDataChangeEvent aEvent = new ChartDataChangeEvent();
          aEvent.Type = ChartDataChangeType.ALL;
          chartDataChanged(aEvent);
      }
 
      // XEventListener (base interface of XChartDataChangeEventListener)
      public void disposing(com.sun.star.lang.EventObject 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
          maChartData.removeChartDataChangeEventListener(this);
 
          // remove dispose listeners
          ((XComponent) UnoRuntime.queryInterface(XComponent.class,
              maSheetDoc)).removeEventListener(this);
          ((XComponent) UnoRuntime.queryInterface(XComponent.class,
              maChartDocument)).removeEventListener(this);
          // this program cannot do anything any more
          System.exit(0);
      }
 
      // 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,
                  maChartDocument.getSubTitle())).setPropertyValue(
                      "String", aTitle);
 
              maChartDocument.attachData(maChartData);
          } catch(Exception ex) {
              System.out.println("Oops: " + ex);
          }
 
          System.out.println("Data has changed");
      }
 
      // private
      private com.sun.star.sheet.XSpreadsheetDocument maSheetDoc;
      private com.sun.star.frame.XModel maDrawDoc;
      private com.sun.star.chart.XChartDocument maChartDocument;
      private com.sun.star.chart.XChartData maChartData;
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages