Ask Your Question

Graph in calc with star basic

asked 2017-09-06 15:19:06 +0200

Fede gravatar image

updated 2017-09-06 15:48:23 +0200

Ratslinger gravatar image

Hello, I have problem with this code: the graph "Example_01" is created, but I don't see it

Can You help me?

Thank you


S.O. Windows7

  Sub CreateCalcWithSimpleChart

Dim oSheet 'Sheet containing the chart
Dim oRect 'How big is the chart
Dim oCharts 'Charts in the sheet
Dim oChart 'Created chart
Dim oAddress 'Address of data to plot
Dim sName$ 'Chart name
Dim oChartDoc 'Embedded chart object
Dim oTitle 'Chart title object
Dim oDiagram 'Inserted diagram (data).
Dim sDataRng$ 'Where is the data
Dim oCalcDoc

oCalcDoc = CreateCalcForChart()

    sName = "Example_01"
    sDataRng = "W71:AD120"
    oSheets = ThisComponent.getSheets()
    oSheet  = oSheets.getbyIndex(2)
    oAddress = oSheet.getCellRangeByName( sDataRng ).getRangeAddress()
    oCharts = oSheet.getCharts()

If NOT oCharts.hasByName(sName) Then
    oRect = createObject("")
    oRect.X = 10000
    oRect.Y = 10000
    oRect.width = 200000
    oRect.Height= 100000
    ' The rectangle identifies the dimensions in 1/100 mm.
    ' The address is the location of the data.
    ' True indicates that column headings should be used.
    ' False indicates that Row headings should not be used.
    oCharts.addNewByName(sName, oRect, Array(oAddress), False, True)
End If

oChart = oCharts.getByName( sName )
oChartDoc = oChart.getEmbeddedObject()
oTitle = oChartDoc.getTitle()
oTitle.String = "Speed - Torque"

' Create a diagram.
oDiagram = oChartDoc.createInstance( "" )
oChartDoc.setDiagram( oDiagram )
oDiagram = oChartDoc.getDiagram()
'   oDiagram.DataCaption =
oDiagram.DataRowSource =

end sub
edit retag flag offensive close merge delete


@Fede Question edited for code clarity.

Ratslinger gravatar imageRatslinger ( 2017-09-06 15:49:13 +0200 )edit

1 Answer

Sort by » oldest newest most voted

answered 2017-09-06 16:07:59 +0200

Ratslinger gravatar image

updated 2017-09-06 16:10:25 +0200

Have tried your the code which works. The resulting graph was extremely large so for display purposes, reduced size by 90%:

image description

The code indicates it to appear on the third sheet and get the necessary data from there.

Make sure data placement and sheet used are correct.

Ran this using LO v5.4.1.2 on Mint 18.2

edit flag offensive delete link more


Thank you Mark, Can you help me to understand how to insert the legend?

Fede gravatar imageFede ( 2017-09-06 16:41:27 +0200 )edit


Add line = oChartDoc.HasLegend = 1 after oTitle.String = "Speed - Torque"

Ratslinger gravatar imageRatslinger ( 2017-09-06 16:58:52 +0200 )edit

Sorry @Ratslinger. Thank you

There is something that I don't undestand... the variable "sdatarng" contains the y-variables, if i would like to define the x-axis, instead?

Fede gravatar imageFede ( 2017-09-07 09:04:29 +0200 )edit

@Fede Sorry for delayed response. Sleep caught up with me. First, let me explain I am no expert with charts. I get by. My interest was to create charts in LO Base (which I did after about a two year on/off relation - see this post). Most of how I got there was from documentation found here which may also assist you. Continued...

Ratslinger gravatar imageRatslinger ( 2017-09-07 18:33:16 +0200 )edit

Now as to your question, I believe the way data is presented is dependent upon how it is arranged in columns vs rows. This may be one solution. Another is to simply flip the x-y axis in the macro:

oFirstDiagram = oChartDoc.getFirstDiagram()
oCoordinateSystems = oFirstDiagram.getCoordinateSystems()
oXCoordinateSystem = oCoordinateSystems(0)
oXCoordinateSystem.SwapXAndYAxis = 1

Just place at very end of your sub. Hopefully this is what is wanted.

Ratslinger gravatar imageRatslinger ( 2017-09-07 18:37:44 +0200 )edit

A question, I would like to create a graph in "sheet 1" with data that are in "sheet 2"... Is it possible?

Thank you for all!

Fede gravatar imageFede ( 2017-09-21 11:06:05 +0200 )edit

@Fede Data can come from any sheet. This is done when setting the data range which specifies not only the cells where the data resides but the sheet. Please see LO docs (click here) Chapter 3.

Questions are always welcome. However, the forum is set up for Q & A. Ask a question and get an answer for that question. If you have further questions, they should be asked as new questions.

Ratslinger gravatar imageRatslinger ( 2017-09-21 15:27:04 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-09-06 15:19:06 +0200

Seen: 247 times

Last updated: Sep 06 '17