[Solved] Adding a regression line using the Chart2 API

Creating a macro - Writing a Script - Using the API

[Solved] Adding a regression line using the Chart2 API

Postby AndrewDavison2557 » Sun Oct 05, 2014 9:47 pm

Dear All,

I trying to write Java code which adds a linear regression line (trend line) to a scatter chart. The code is using the Chart2 API, not the older Chart modules.

I've been trying two different approaches with no luck:

1. Create a XRegressionCurve instance and add it to a XRegressionCurveContainer linked to the XDataSeries object associated with the scatter chart points. The problem is that I can't find a property that sets the curve's trend type (e.g. to linear or polynomial).

2. Access the XChartDocument as a XShape, and send it a .uno:InsertTrendLine dispatch. This does nothing to the chart unfortunately and, even if it did, I still don't know how to set the trend type.

I realize many people are not Java programmers, so I'd be happy for ideas coded in any language.

Many thanks in advance.

- Andrew
Last edited by AndrewDavison2557 on Mon Oct 06, 2014 10:47 am, edited 1 time in total.
OpenOffice 4.1.1 on Windows 7
AndrewDavison2557
 
Posts: 46
Joined: Sun Oct 05, 2014 7:42 pm

Re: Adding a regression line using the Chart2 API

Postby FJCC » Mon Oct 06, 2014 1:23 am

This Basic code adds a linear regression curve to an existing chart.
Code: Select all   Expand viewCollapse view
  oSheets = ThisComponent.getSheets()
  oObj1 = oSheets.getByIndex(0)
  oCharts = oObj1.getCharts()
 
  oObj2 = oCharts.getByIndex(0)
  oEmbeddedObject = oObj2.getEmbeddedObject()
  oDiagram = oEmbeddedObject.getDiagram()
  oDiagram.RegressionCurves = com.sun.star.chart.ChartRegressionCurveType.LINEAR


I don't do Java but I recorded the Java version of all the steps except setting the value of oDiagram.RegressionCurves with MRI:
Code: Select all   Expand viewCollapse view
import com.sun.star.chart.XChartDocument;
import com.sun.star.chart.XDiagram;
import com.sun.star.container.XIndexAccess;
import com.sun.star.document.XEmbeddedObjectSupplier;
import com.sun.star.lang.IndexOutOfBoundsException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.lang.XComponent;
import com.sun.star.sheet.XSpreadsheet;
import com.sun.star.sheet.XSpreadsheetDocument;
import com.sun.star.sheet.XSpreadsheets;
import com.sun.star.table.XTableChart;
import com.sun.star.table.XTableCharts;
import com.sun.star.table.XTableChartsSupplier;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public static void snippet(XComponentContext xComponentContext, Object oInitialTarget)
{
   try
   {
      XSpreadsheetDocument xSpreadsheetDocument = UnoRuntime.queryInterface(
         XSpreadsheetDocument.class, oInitialTarget);
      XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
      
      XIndexAccess xIndexAccess = UnoRuntime.queryInterface(
         XIndexAccess.class, xSpreadsheets);
      XSpreadsheet xSpreadsheet = UnoRuntime.queryInterface(
         XSpreadsheet.class, xIndexAccess.getByIndex(0));
      
      XTableChartsSupplier xTableChartsSupplier = UnoRuntime.queryInterface(
         XTableChartsSupplier.class, xSpreadsheet);
      
      XTableCharts xTableCharts = xTableChartsSupplier.getCharts();
      
      XIndexAccess xIndexAccess2 = UnoRuntime.queryInterface(
         XIndexAccess.class, xTableCharts);
      XTableChart xTableChart = UnoRuntime.queryInterface(
         XTableChart.class, xIndexAccess2.getByIndex(0));
      
      XEmbeddedObjectSupplier xEmbeddedObjectSupplier = UnoRuntime.queryInterface(
         XEmbeddedObjectSupplier.class, xTableChart);
      XComponent xComponent = xEmbeddedObjectSupplier.getEmbeddedObject();
      
      XChartDocument xChartDocument = UnoRuntime.queryInterface(
         XChartDocument.class, xComponent);
      XDiagram xDiagram = xChartDocument.getDiagram();
      
   }
   catch (WrappedTargetException e1)
   {
      // getByIndex
      e1.printStackTrace();
   }
   catch (IndexOutOfBoundsException e2)
   {
      // getByIndex
      e2.printStackTrace();
   }
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 8306
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Adding a regression line using the Chart2 API

Postby AndrewDavison2557 » Mon Oct 06, 2014 8:49 am

This doesn't work for me since I'm using the Chart2 API.

ChartRegressionCurveType is part of the older Chart module, as are the XChartDocument and XDiagram interfaces that the generated Java code is using.

The Chart2 XChartDocument accesses an XDiagram using the getFirstDiagram() method. I've checked through the Chart2 XDiagram properties, and there's no RegressionCurves property (although there is in the Chart XDiagram!)

- Andrew
OpenOffice 4.1.1 on Windows 7
AndrewDavison2557
 
Posts: 46
Joined: Sun Oct 05, 2014 7:42 pm

Re: Adding a regression line using the Chart2 API

Postby AndrewDavison2557 » Mon Oct 06, 2014 9:52 am

I solved the problem myself. Referring back to approach no (1) in my first post: I wasn't instantiating the right service and I wasn't adding the newly created XRegressionCurve to the container. Here's a fragment of my working code, which uses some of my own support functions:
Code: Select all   Expand viewCollapse view
    XDataSeries[] dataSeriesArr = getDataSeries(chartDoc);

    XRegressionCurveContainer rcCon = UnoRuntime.queryInterface(
                         XRegressionCurveContainer.class, dataSeriesArr[0]);

    XRegressionCurve[] curves = rcCon.getRegressionCurves();

    XRegressionCurve curve = Lo.createInstanceMCF(XRegressionCurve.class,
                                                        "com.sun.star.chart2.LinearRegressionCurve");   // for a linear regression curve
    rcCon.addRegressionCurve(curve);
OpenOffice 4.1.1 on Windows 7
AndrewDavison2557
 
Posts: 46
Joined: Sun Oct 05, 2014 7:42 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 7 guests