Chart Macro Questions

general Chart macro questions:

  1. How to select a chart, the problem being name vs object #, where do I find the object number?
  2. How not to default to Column (Bar), the problem is that the programming is not working
  3. How to enter specific columns instead of A1:D1, how about A1:A20 & D1:D20 ???
  1. I’m assuming that when you say “select,” you actually mean “instantiate.”

When you add a chart programmatically, you always set its name, so you can easily access it using that name. The chart’s index is simply the order in which they are inserted.

    doc = XSCRIPTCONTEXT.getDocument()
    sheet = doc.CurrentController.ActiveSheet
    charts = sheet.Charts

    for chart in charts:
        print(chart.Name)

    for i in range(charts.Count):
        chart = charts[i]
        print(chart.Name)

  1. How are you doing it?
    By specifying the chart type, it should work. For example, for a bar chart.
    TYPE_CHART = 'com.sun.star.chart.BarDiagram'
    chart.setDiagram(chart.createInstance(TYPE_CHART))
  1. Just specify the sequence of ranges you need in the aRanges argument. Keep in mind that depending on the chart type, you may need different ranges.
    LibreOffice: XTableCharts Interface Reference

Hi,
LO 25.6.5.2

  1. if “Instantiate” means create then it’s doing that ok

  2. The error msg says “not selected” like when you touch the chart with the mouse and you get the eight dots around it.

  3. I’m getting columns and some wierd script because I have text in one of the columns. (I don’t want to reposition the columns because I read them)
    Which is why one of my needs is to Grab column data not by A5:H5 but individually by A1:A5 & D1:D5 etc.

  4. “Just specify the sequence of ranges you need in the aRanges argument. Keep in mind that depending on the chart type, you may need different ranges.”
    YES, but I need an example of how to do that
    thankyou

It goes something like this

Option Explicit
Sub CreateProjChart()
	GlobalScope.BasicLibraries.loadLibrary("MRILib")
	Dim Sheet As Object, Data_Range As Object, Chart As Object
	Dim Charts
	Dim rangos(1)
	Dim pos_size As New com.sun.star.awt.Rectangle
	On Error GoTo Erro
	Sheet = ThisComponent.CurrentController.ActiveSheet
	rangos(0) = Sheet.getCellRangeByName("A1:B6").RangeAddress
	rangos(1) = Sheet.getCellRangeByName("D1:E9").RangeAddress
	Charts = Sheet.Charts
	If Charts.hasByName("ProjChart") Then
        MsgBox "Chart exists"
        Exit Sub
    End If	
	With pos_size
    	.X = 12500
        .Y = 0           
        .Width = 20000
        .Height = 10000 
    End With
    Charts.addNewByName("ProjChart", pos_size, rangos, True, True)
    Chart = Charts(0).EmbeddedObject
    Chart.setDiagram(Chart.createInstance("com.sun.star.chart.LineDiagram"))
    Chart.HasLegend = True
	Exit Sub
	Erro: MsgBox "ERRO " & Error & Chr(10) & "na linha " & Erl
End Sub

No relation between Countries Population and the Years line
 

Ok now yer tawkin’

  1. I moved my Date range next to my data and still didn’t get a linediagram
  2. Then I used your ranges to input the chart … VERY GOOD, the X is categories, and the Y is your "Rangos() BUT … still not getting .linediagram!
  3. Also I’m hung on GlobalScope.BasicLibraries.loadLibrary(“MRILib”). I’m using LO25.2.5.2 and see no info on where a separate add on “Macro recording interface” would not be already included.
  4. Can you point me in the right direction on that exception
  5. thankyou very very

Forget this. This is an Object Inspector Software. Delete this line.

Please upload a simplified Spreadsheet.
 
D130:D200G130:G200
LineDiagram with non-contiguous columns = 2 ranges:
DoubleColumns.ods (19.6 KB)

oops a corrupted page I removed it

Yes… I don’t know :thinking:
It is ignoring .setDiagram(
For simplified ranges D4:D8H4:H8

Sub CreateProjChart()
	'GlobalScope.BasicLibraries.loadLibrary("MRILib")
	Dim rangos(1)
	Dim pos_size As New com.sun.star.awt.Rectangle
	sheet = ThisComponent.CurrentController.ActiveSheet
	rangos(0) = sheet.getCellRangeByName("D4:D8").RangeAddress
	rangos(1) = sheet.getCellRangeByName("H4:H8").RangeAddress
	charts = sheet.Charts	
	chart_name = "ProjChart"
    If charts.hasByName(chart_name) Then
        MsgBox "Chart exists"
        Exit Sub
    End If
	print "check: routine working..."
	With pos_size
    	.X = 90000
        .Y = 3000           
        .Width = 25000
        .Height = 15000 
    End With
    charts.addNewByName("ProjChart", pos_size, rangos, True, True)
    chart = charts(0).EmbeddedObject
    'chart.setDiagram(chart.createInstance("com.sun.star.chart.LineDiagram"))
    chart.HasLegend = True
End Sub

DefaultChart

  1. The page was corrupted somehow so I replaced the page and now it saves ok.

  2. The chart works (with .lineDiagram) if no other charts are on the page. Maybe there’s a problem with chart indexing?

  3. Type: com.sun.star.uno.RuntimeException when I try to create a second named chart with another macro

Now I need to format everything else;
add 2nd Y
color the line
adjust the X axis
Inserting Title
label text orientation 90deg
naming Series
plot options continue line

I’ll create a new topic

 
:flushed:
There are 5 charts, and this is chart index 4 :exclamation:
But indexes in a Project changes when objects are added or removed… so :thinking:
Char4
CharCount

lol, have to find a way around that, may just using chart.names
I put up a new topic for the formatting but it disappeared ???

End With
charts.addNewByName(chart_name, pos_size, rangos, True, True)
chart = charts.getByName(chart_name).EmbeddedObject
chart.setDiagram(chart.createInstance("com.sun.star.chart.LineDiagram"))

Yes I’ve changed to this, is working now
thankyou

how about adding a 2nd Y axis and assigning a range to it?

Do you mean this topic? You can see all of your posting activity here.

ahh, there it is

tnx
eddie