[Solved] Create Chart Using Data From Rows

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
flyer48
Posts: 14
Joined: Fri Jun 18, 2021 5:19 pm

[Solved] Create Chart Using Data From Rows

Post by flyer48 »

Hello,

I am trying to create a macro that produces a series of charts on a large dataset. I am not very experienced so I tried adapting the sample code from https://wiki.openoffice.org/w/images/e/ ... o3.0.0.pdf which is

Code: Select all

Dim Doc As Object
Dim Charts As Object
Dim Chart as Object
Dim Rect As New com.sun.star.awt.Rectangle
Dim RangeAddress(0) As New com.sun.star.table.CellRangeAddress

Doc = StarDesktop.CurrentComponent
Charts = Doc.Sheets(0).Charts

Rect.X = 8000
Rect.Y = 1000
Rect.Width = 10000
Rect.Height = 7000
RangeAddress(0).Sheet = 0
RangeAddress(0).StartColumn = 0
RangeAddress(0).StartRow = 0
RangeAddress(0).EndColumn = 8
RangeAddress(0).EndRow = 1

Charts.addNewByName("MyChart", Rect, RangeAddress(0), True, True)
Chart = Charts.getByName("MyChart").embeddedObject
Chart.Diagram = Chart.createInstance("com.sun.star.chart.LineDiagram")
Chart.Diagram.Wall.FillColor = RGB(255,255,255)
Chart.Diagram.HasXAxisGrid = True
Chart.Diagram.HasYAxisGrid = True
Chart.Diagram.YAxis.Min = 0
Chart.Diagram.YAxis.Max = 100

Chart.Diagram.HasXAxisTitle = True
Chart.diagram.XAxisTitle.String = "Tests"
and I think this can get me what I need pretty much everything I need, except that the data is brought in from the columns instead of the rows, producing a bunch of short lines on the graph instead of one long one. I cannot change the format of the dataset because that data is copy/pasted in by multiple people. I have tried reading the IDL reference and my attempts at implementation failed.

Thanks in advance for your help.
Last edited by flyer48 on Fri Jun 18, 2021 6:33 pm, edited 1 time in total.
OpenOffice 4.1.10 Windows 10
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Create Chart Using Data From Rows

Post by FJCC »

Here is an old example I have.

Code: Select all

Sub Main    
Dim Doc As Object
Dim Sheet as Object
Dim RangeAddress(0) As New com.sun.star.table.CellRangeAddress
Dim Charts As Object
Dim Chart as Object
Dim Rect As New com.sun.star.awt.Rectangle
Dim Diagram as Object

Doc = ThisComponent
Rect.X = 100 
Rect.Y = 200 
Rect.Width = 18000
Rect.Height = 9000
RangeAddress(0).Sheet = 0
RangeAddress(0).StartColumn = 2
RangeAddress(0).StartRow = 1
RangeAddress(0).EndColumn = 8
RangeAddress(0).EndRow = 2
Charts = Doc.Sheets(0).Charts
Charts.addNewByName("ChartName", Rect, RangeAddress(), False, False)
Chart = Charts.getByName("ChartName").EmbeddedObject

Chart.Diagram.DataRowSource = com.sun.star.chart.ChartDataRowSource.ROWS

Chart.Diagram = Chart.createInstance("com.sun.star.chart.LineDiagram")
Chart.HasMainTitle = True
Chart.Title.String = "ChartTitle"   
Chart.HasLegend = True
End Sub
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Post Reply