Ask Your Question
1

macro basic XYDiagramm with regression line

asked 2018-03-21 07:26:02 +0200

olbra gravatar image

My script makes only points with line. Instead of this standard line I want to have points with regression line.

`

ChartNo = 1
sName = "test_" + str(ChartNo)
sDataRng = "A10:B20"
SheetForRectangle = ThisComponent.Sheets.getByName("Rectangle")
ChartsForTest = SheetForRectangle.getCharts()
AddressForCells = SheetForRectangle.getCellRangeByName( sDataRng ).getRangeAddress()

If ChartsForTest.hasByName(sName) Then
    ChartsForTest.removeByName(sName)
End If

If NOT ChartsForTest.hasByName(sName) Then
    RectForTest = createObject("com.sun.star.awt.Rectangle")
    RectForTest.X = 10000
    RectForTest.Y = 10000
    RectForTest.width = 20000
    RectForTest.Height= 10000
    ChartsForTest.addNewByName(sName, RectForTest, Array(AddressForCells), False, True)
End If

ChartForTest = ChartsForTest.getByName( sName )  
ChartForTest.setRanges(Array(AddressForCells))
oChartDoc = ChartForTest.getEmbeddedObject()
oTitle = oChartDoc.getTitle()  
oTitle.String = "Some title"

oChartDoc.HasLegend = 1
LegendForTest = oChartDoc.Legend
oChartDoc.Legend.Alignment = com.sun.star.chart.ChartLegendPosition.RIGHT 
oChartDoc.Legend.FillStyle = com.sun.star.drawing.FillStyle.SOLID
oChartDoc.Legend.FillColor = RGB(210, 210, 210)
oChartDoc.Legend.LineColor = RGB(50, 0, 0)
oChartDoc.Legend.CharHeight = 7

oDiagram = oChartDoc.createInstance( "com.sun.star.chart.XYDiagram" )
oChartDoc.setDiagram(oDiagram)
oDiagram = oChartDoc.getDiagram()

oDiagram.Lines = True
oDiagram.SymbolType = -10

oDiagram.HasXAxisTitle = True
oDiagram.XAxisTitle.String ="Some X-Axis"

oDiagram.HasYAxisTitle = True
oDiagram.YAxisTitle.String ="Some Y-Axis"

oDiagram.RegressionCurves = com.sun.star.chart.ChartRegressionCurveType.LINEAR
oDiagram.DataRowSource = com.sun.star.chart.ChartDataRowSource.COLUMNS

`

What I do wrong? Why it is going without regression line?

edit retag flag offensive close merge delete

Comments

I cannot tell. But there must be a misunderstanding: An X-Y-Diagram can contain many DataSeries, and for each one the TrendLine can be set and formatted independently. There must be a property of the chart containing respective information for the many series one by one. The .RegressionCurves cannot do that due to its type.
Probably the XRay tool or the MRT tool can help.

Lupp gravatar imageLupp ( 2018-03-21 13:22:46 +0200 )edit

Thank you. Below is the answer from Jim K.

olbra gravatar imageolbra ( 2018-03-21 19:50:10 +0200 )edit

1 Answer

Sort by » oldest newest most voted
3

answered 2018-03-21 19:17:01 +0200

Jim K gravatar image

updated 2018-03-21 19:18:41 +0200

Regression curves must be added to a specific data series.

Sub AddRegressionCurve
  oCharts = ThisComponent.getSheets().getByIndex(0).getCharts()
  oEmbeddedObject = oCharts.getByIndex(0).getEmbeddedObject()
  oDataSeries = oEmbeddedObject.FirstDiagram.CoordinateSystems(0).ChartTypes(0).DataSeries(0)
  curves = oDataSeries.getRegressionCurves()
  curve = CreateUnoService("com.sun.star.chart2.LinearRegressionCurve")
  oDataSeries.addRegressionCurve(curve)
End Sub
edit flag offensive delete link more

Comments

Thank you very much for answer and links :)

olbra gravatar imageolbra ( 2018-03-21 19:48:13 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-03-21 07:26:02 +0200

Seen: 81 times

Last updated: Mar 21 '18