Libreoffice crashed when I tried to add 'regression lines' to the XY-plot with basic macro

    oCharts.addNewByName(PlotName, Rect, Source(), False, False)
    oChart=oCharts.getByName(PlotName).EmbeddedObject
    oDataSeries=oChart.FirstDiagram.CoordinateSystems(0).ChartTypes(0).DataSeries(0)
    oChart.Diagram=oChart.createInstance("com.sun.star.chart.XYDiagram")
    oCurve=oChart.createInstance ("com.sun.star.chart2.LinearRegressionCurve")
    oDataSeries.addRegressionCurve(oCurve)

The last commend seems to crash the Libreoffice.

When starting the libreoffice, below messages appear on the shell terminal.

Gtk-Message: 19:33:54.208: GtkDialog mapped without a transient parent. This is discouraged.
Gtk-Message: 19:33:59.165: GtkDialog mapped without a transient parent. This is discouraged.
Gtk-Message: 19:40:25.365: GtkDialog mapped without a transient parent. This is discouraged.

The macro should be inside the below sheet:
XY_PLOT.ods (12.6 KB)

I think the latest version didn’t crash, but regression curve is not coming out.

REM  *****  BASIC  *****
REM XY PLOT for cell range B2:C11 filled with data as below (C=(B^2)/4 parabola)
REM PLOT APPEARS in cellrange F2:R46
REM B2:C2=1, 0.25
REM B3:C3=2, 1
REM ...
REM B10:C10=9, 20.25
REM B11:C11=10, 25

Sub PLOTXY

Dim Doc as Object
Dim oSheet as Object
Dim Flags as Long
Dim oTarget as Object
Dim DescriptX, DescriptY, Cell, oCharts, oChart As Object
Dim Rect As new com.sun.star.awt.Rectangle
Dim Source(1) As New com.sun.star.table.CellRangeAddress
Dim oLabelAll as new com.sun.star.chart2.DataPointLabel
Dim oDataSeries As Object
Dim oCurves, Ocurve as Object

Doc=ThisComponent
oSheet=Doc.CurrentController.ActiveSheet
xSheet=oSheet.getRangeAddress().Sheet
oLabelAll.ShowCategoryName = True   
oLabelAll.ShowLegendSymbol = False   
oLabelAll.ShowNumber = False   
oLabelAll.ShowNumberInPercent = False

oCharts=oSheet.Charts

Rect.Width = 30000
Rect.Height = 20000

  xCol=1
  yCol=2
  nRow=10
  LTX=5
  LTY=1

    Rect.X = oSheet.getCellByPosition(LTX,LTY).Position.X
    Rect.Y = oSheet.getCellByPosition(LTX,LTY).Position.Y

    Source(0).Sheet = xSheet
    Source(0).StartColumn = xCol
    Source(0).StartRow = 1
    Source(0).EndColumn = xCol
    Source(0).EndRow = nRow

    Source(1).Sheet = xSheet
    Source(1).StartColumn = yCol
    Source(1).StartRow = 1
    Source(1).EndColumn = yCol
    Source(1).EndRow = nRow 

    PlotName=oSheet.getCellByPosition(xCol,0).String+".Sheet"+Trim(Str(xSheet))+".0"
    
    oCharts.addNewByName(PlotName, Rect, Source(), False, False)
    oChart=oCharts.getByName(PlotName).EmbeddedObject
    oDataSeries=oChart.FirstDiagram.CoordinateSystems(0).ChartTypes(0).DataSeries(0)
    oChart.Diagram=oChart.createInstance("com.sun.star.chart.XYDiagram")
    REM The regression curve below seems hard case to get it working
    oCurves=oDataSeries.getRegressionCurves()
    oCurve=oChart.createInstance ("com.sun.star.chart2.LinearRegressionCurve")
    oCurves.addRegressionCurve(oCurve) 

End Sub

Please upload an ODF type sample file and the full embedded macro code.

It was uploaded…

This is python but regression curves have been implemented in OOO Develepment Tools (OooDev). It might be helpful to see how it works.

There is the create_curve() method that creats the curve.
The draw_regression_curve() that draws the curve. And the calc_regressions() that prints info to the console.
There is a scatter_chart() example that uses the static classes of OooDev and a second scatter_chart example that uses more modern aspects of OooDev.

Here is a similar question:

And here is a modified code:

Sub PLOTXY

Dim Doc as Object
Dim oSheet as Object
Dim Flags as Long
Dim oTarget as Object
Dim DescriptX, DescriptY, Cell, oCharts, oChart As Object
Dim Rect As new com.sun.star.awt.Rectangle
Dim Source(1) As New com.sun.star.table.CellRangeAddress
Dim oLabelAll as new com.sun.star.chart2.DataPointLabel
Dim oDataSeries As Object
Dim oCurves, Ocurve as Object

Doc=ThisComponent
oSheet=Doc.CurrentController.ActiveSheet
xSheet=oSheet.getRangeAddress().Sheet
oLabelAll.ShowCategoryName = True   
oLabelAll.ShowLegendSymbol = False   
oLabelAll.ShowNumber = False   
oLabelAll.ShowNumberInPercent = False

oCharts=oSheet.Charts

Rect.Width = 30000
Rect.Height = 20000

  xCol=1
  yCol=2
  nRow=10
  LTX=5
  LTY=1

    Rect.X = oSheet.getCellByPosition(LTX,LTY).Position.X
    Rect.Y = oSheet.getCellByPosition(LTX,LTY).Position.Y

    Source(0).Sheet = xSheet
    Source(0).StartColumn = xCol
    Source(0).StartRow = 1
    Source(0).EndColumn = xCol
    Source(0).EndRow = nRow

    Source(1).Sheet = xSheet
    Source(1).StartColumn = yCol
    Source(1).StartRow = 1
    Source(1).EndColumn = yCol
    Source(1).EndRow = nRow 

    PlotName=oSheet.getCellByPosition(xCol,0).String+".Sheet"+Trim(Str(xSheet))+".0"
    
    oCharts.addNewByName(PlotName, Rect, Source(), False, False)
    oChart=oCharts.getByName(PlotName).EmbeddedObject
    oDataSeries=oChart.FirstDiagram.CoordinateSystems(0).ChartTypes(0).DataSeries(0)
    oChart.Diagram=oChart.createInstance("com.sun.star.chart.XYDiagram")
    
    
    
    REM The regression curve below seems hard case to get it working
    REM oCurves=oDataSeries.getRegressionCurves()
    REM oCurve=oChart.createInstance ("com.sun.star.chart2.LinearRegressionCurve")
    REM oCurves.addRegressionCurve(oCurve) 
    
    
    
    oCharts = ThisComponent.getSheets().getByIndex(0).getCharts()
	oEmbeddedObject = oCharts.getByIndex(0).getEmbeddedObject()
	oDataSeries = oEmbeddedObject.FirstDiagram.CoordinateSystems(0).ChartTypes(0).DataSeries(0)
	oCurves = oDataSeries.getRegressionCurves()
	oCurve = CreateUnoService("com.sun.star.chart2.LinearRegressionCurve")
	oDataSeries.addRegressionCurve(oCurve)

End Sub

It looks like replacing line:

oCurve=oChart.createInstance (“com.sun.star.chart2.LinearRegressionCurve”)

with

oCurve = CreateUnoService(“com.sun.star.chart2.LinearRegressionCurve”)

makes it work.
But is it possible to make this work without using the “UnoService” ?

whats wrong with the »UnoService« ??