[Solved] Inserting a chart with OO Basic

Java, C++, C#, Delphi... - Using the UNO bridges
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

[Solved] Inserting a chart with OO Basic

Post by saleem145 »

Hello,

I have written ("recorded") a simple OO Basic Macro to insert a chart. See code below. I would like to make the following enhancements --

1. Set the chart type
2. Set the main and sub title
3. Set the x and y axis
4. Insert some text at the bottom
5. Instead of specifying a range to plot, I would like to create a 2D array of data and pass in the data.

Are these possible?? (Record macro does not provide any insights)

Thanks,

Saleem

Code: Select all

sub Chart

dim document   as object
dim dispatcher as object

document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = "$B$4"

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())

dim args2(0) as new com.sun.star.beans.PropertyValue
args2(0).Name = "ToPoint"
args2(0).Value = "$B$4:$C$14"

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args2())
dispatcher.executeDispatch(document, ".uno:InsertObjectChart", "", 0, Array())

end sub
Last edited by Hagar Delest on Sat Jul 28, 2012 9:11 am, edited 3 times in total.
Reason: tagged [Solved].
OpenOffice 3.4.0
Mac OS X 10.5.8
User avatar
Villeroy
Volunteer
Posts: 31345
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Inserting a chart with OO Basic

Post by Villeroy »

You must not try to record macros. This is a dead end.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Inserting a chart with OO Basic

Post by Charlie Young »

Villeroy wrote:You must not try to record macros. This is a dead end.
+1 (and then some)

I should probably scold you further since we've gone as far as c++ add-ins, but I'll let it slide for now. ;)
saleem145 wrote:Hello,

I have written ("recorded") a simple OO Basic Macro to insert a chart. .... I would like to make the following enhancements --

1. Set the chart type
2. Set the main and sub title
3. Set the x and y axis
4. Insert some text at the bottom
5. Instead of specifying a range to plot, I would like to create a 2D array of data and pass in the data.

Are these possible?? (Record macro does not provide any insights)

Thanks,

Saleem
Everything you ask is possible, using the API. I'll concentrate on the trickiest thing, which is #5. I'll do an xy chart.

The only way I know to add a chart via the API, is with addNewByName, which requires, among other things, a CellRangeAddress, to get around this, just pass an empty one, with all the elements = 0, then you can remove all the dataseries with a subroutine:

Code: Select all

Sub ClearChart(Chart As Object)

	Dim Doc As Object
	Dim Sheet As Object
	Dim ChartSheet As Object
	Dim SetupSheet As Object
	Dim oChart
	Dim oFirstDiagram
	Dim oDataSeries
	Dim oCoords
	Dim oCoord
	Dim oChartTypes
	Dim oChartType
	Dim i As Long
	
	Doc = ThisComponent
		
	oChart = Chart.getEmbeddedObject()
	oFirstDiagram = oChart.getFirstDiagram()
  	
  ' get coordinates from the diagram
  	oCoords = oFirstDiagram.getCoordinateSystems()
  	oCoord = oCoords(0)

  ' get chart type
  	oChartTypes = oCoord.getChartTypes()
  	oChartType = oChartTypes(0)
  	
  	oDataSeries = oChartType.getDataSeries()
  	i = UBound(oDataSeries)
  	  	
  	do while i >= 0 
  		oChartType.removeDataSeries(oDataSeries(i))
  		i = i - 1
  	loop
  	
End Sub
Then you need a dataarray, say

Code: Select all

  	Dim oDataArray( 0 To 1000,0 To 1)
You can do multiple series by making the second dimension larger (0 to x), but I'll stick with one for now.

I believe this is limited to 32,767 rows, which I hope isn't a problem.

oDataArray(i, 0) are the x values, and oDataArray(i, 1) the y values, so you can do something like

Code: Select all


  	Dim x As Double
  	Dim i As Long
  	
  	for i = 0 to 1000
  		if i <> 0 then
  			x = 2 * pi * i / 1000
  		else
  			x = 0
  		endif
  		
  		oDataArray( i, 0) = x
  		oDataArray( i, 1) = Sin(x)
  	next i
Then oDataArray is assigned to the chart by

Code: Select all


 	oData = oChartDoc.getData()
 	
  ' call setData() method of XChartDataArray to apply the data
  	oData.setData(oDataArray())
  	
It is not at all obvious how to give a title to the series, and I'll refer you to the attached document for that and other details.

Run Main to regenerate the chart.

I'm not sure what you want for text at the bottom. If you want more than the x axis title, that's something else.
Attachments
MakeChart.ods
Make xy chart with dataarray
(45.86 KiB) Downloaded 857 times
Apache OpenOffice 4.1.1
Windows XP
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Inserting a chart with OO Basic

Post by saleem145 »

Fantastic -- exactly what I wanted to see!! Very helpful indeed.

One enhancement will clarify the other set of my questions. When I run the macro I want a dialog box to pop up where I can enter amplitude, frequency and phase. Once these quantities are entered and the user clicks ok, it should make the plot....

Saleem
OpenOffice 3.4.0
Mac OS X 10.5.8
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Inserting a chart with OO Basic

Post by Charlie Young »

saleem145 wrote:Fantastic -- exactly what I wanted to see!! Very helpful indeed.

One enhancement will clarify the other set of my questions. When I run the macro I want a dialog box to pop up where I can enter amplitude, frequency and phase. Once these quantities are entered and the user clicks ok, it should make the plot....

Saleem
I did the basics, I will leave it as an exercise to pretty-up the dialog and such.

Other than the dialog, the major change is

Code: Select all

oDataArray( i, 1) = w.Amplitude * Sin(w.Frequency*(x - w.Phase))
where w is a "Wave" defined by

Code: Select all

Type Wave
	Amplitude As Double
	Frequency As Double
	Phase As Double
End Type


and the dialog, in effect, returns one of those.

There is a link to run the macro (still Main) in M2.
Attachments
WavyChart.ods
Version 2
(48.8 KiB) Downloaded 540 times
Apache OpenOffice 4.1.1
Windows XP
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Inserting a chart with OO Basic

Post by saleem145 »

Fantastic!! It should get me started with what I need to do -- doesn't look to complicated!! Might have questions later, but good for now!!

Thanks a lot for your help,

Saleem
OpenOffice 3.4.0
Mac OS X 10.5.8
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Inserting a chart with OO Basic

Post by saleem145 »

One follow up question I had is what is the recommended programming language for this kind of add-on. It is equally easy to do in C++ or should I do this in Python. My understanding is I cannot implement an add-in in OO Basic -- I can only write macro's that are part of a spreadsheet??
OpenOffice 3.4.0
Mac OS X 10.5.8
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Inserting a chart with OO Basic

Post by Charlie Young »

saleem145 wrote:One follow up question I had is what is the recommended programming language for this kind of add-on. It is equally easy to do in C++ or should I do this in Python. My understanding is I cannot implement an add-in in OO Basic -- I can only write macro's that are part of a spreadsheet??
If you want to do an actual "add-on," that is a program added to the Tools menu, then probably c++ is the best choice, simply because if it's well written it tends to be faster than anything else (short of assembly language). I haven't actually written such an add-on, a-la MRI, though I would like to learn how. I do add-in functions in c++ for the same reason, but I have done them with Python just to see how it works. In this particular case, which isn't an "add-in" as such, and I don't see why it would need to be what I'm calling an "add-on" here; it could be macro such as I've been doing in this thread, which needn't be embedded in a spreadsheet, it could also be done with automation, using c++ or Java (or Delphi or Visual Basic). It could therefore be written in ooBasic, but as Villeroy would surely remind us, that is stinky since we do have alternatives like JavaScript and Python which are much more modern and powerful programming languages (I haven't looked at BeanShell at al). I don't really know if Python is the better choice over JScript, others are invited to add their opinions. I know Python has a very rich variety of features, but I've been playing with JScript and since it incorporates a lot of Java, it is full of goodies too, though the successive queryInterface calls might drive one batty (c++ has the same problem).

I'll make a deal; you do the above graph program in Python, and I'll do the JavaScript, then we'll choose. :D
Apache OpenOffice 4.1.1
Windows XP
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Inserting a chart with OO Basic

Post by saleem145 »

I will trying to get RCalc to work. I would like the same dialog box, computations done in R and then plotted in OO. I do not know either Java or Python but I if were to take you up on your offer, I am sure you will end up writing both the python and java version of this extension ;)
OpenOffice 3.4.0
Mac OS X 10.5.8
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Inserting a chart with OO Basic

Post by Charlie Young »

saleem145 wrote:I will trying to get RCalc to work. I would like the same dialog box, computations done in R and then plotted in OO. I do not know either Java or Python but I if were to take you up on your offer, I am sure you will end up writing both the python and java version of this extension ;)
I have started the JavaScript, no promises about when it will happen though. I haven't tried RCalc, and I only vaguely remember seeing references to it.
Apache OpenOffice 4.1.1
Windows XP
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Inserting a chart with OO Basic

Post by saleem145 »

Ok. But how about doing the python version instead??
OpenOffice 3.4.0
Mac OS X 10.5.8
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Inserting a chart with OO Basic

Post by saleem145 »

Charlie,

One quick question -- does each of these languages have their own dialog box designer?? I now understand how to put these together in OO Basic. On windows environment, Visual Studio has these as well -- but not sure what the options are for Mac and C++ or python....

Saleem
OpenOffice 3.4.0
Mac OS X 10.5.8
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Inserting a chart with OO Basic

Post by Charlie Young »

saleem145 wrote:Charlie,

One quick question -- does each of these languages have their own dialog box designer?? I now understand how to put these together in OO Basic. On windows environment, Visual Studio has these as well -- but not sure what the options are for Mac and C++ or python....

Saleem
The dialog box itself is the same for all the languages (with one possible exception I'll discuss later), the programming to use it is quite different however, and this is what I've been wrestling with. It is simplified in Basic by the createUNODialog function. I've finally gotten it to work in both Python and JavaScript, thanks to some posts by hanya and Bernard Marcelly on the other forum. The Python was bad enough, but the JavaScript was a real bear -- I think I've finally got it though.

I'll have more on this once I get the graphs working. That should be straightforward compared to what I've been dealing with about the dialogs!
Apache OpenOffice 4.1.1
Windows XP
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Inserting a chart with OO Basic

Post by Charlie Young »

There are no differences with the dialog boxes among the three languages under consideration. I thought there might be because of a misunderstanding I had about the OK/Cancel buttons, but I'm glad I learned about that.

As Saleem surmised, I wound up doing both the Python and JavaScript -- both versions (and the Basic) are in the attached document. I added a second series, the cosine, even though it is just a phase-shifted sine. The dialog, which could still be prettied-up I think, asks for independent wave characteristics for the two series.

Since the Python is embedded, it can't be accessed without unzipping the document, so here it is. Our resident Pythonians are welcome to have at it.

Code: Select all

import uno
import os
import unohelper
import math
from com.sun.star.lang import Locale
from com.sun.star.awt import Rectangle
from com.sun.star.table import CellRangeAddress
from com.sun.star.chart.ChartAxisPosition import ZERO
from com.sun.star.awt.FontWeight import BOLD

class Wave:
    def __init__(self, Amplitude, Frequency, Phase):
        self.Amplitude = Amplitude
        self.Frequency = Frequency
        self.Phase = Phase
		
def MakeChart(*dummy):
    oDoc = XSCRIPTCONTEXT.getDocument()
    w = []
    r = RunDialog(w)
    if r:
        GenerateChart(0,0,"com.sun.star.chart.XYDiagram",w)
		
def RunDialog(w):
   
    oDoc = XSCRIPTCONTEXT.getDocument()
    ctx = uno.getComponentContext()
    oDialog = create_dialog(ctx,oDoc,"Standard","Dialog1")
    DlgField = [[0,0],[0,0],[0,0]]
    
    DlgField[0][0] = oDialog.getControl("Amplitude")
    DlgField[1][0] = oDialog.getControl("Frequency")
    DlgField[2][0] = oDialog.getControl("Phase")
    DlgField[0][1] = oDialog.getControl("CosAmplitude")
    DlgField[1][1] = oDialog.getControl("CosFrequency")
    DlgField[2][1] = oDialog.getControl("CosPhase")
    
    r = oDialog.execute()
    w.append(Wave(DlgField[0][0].Value,DlgField[1][0].Value,DlgField[2][0].Value))
    w.append(Wave(DlgField[0][1].Value,DlgField[1][1].Value,DlgField[2][1].Value))
    return r == 1
    
def create_dialog(ctx, doc, lib_name, dialog_name):
    
    dialog_lib = doc.DialogLibraries
    if not dialog_lib.isLibraryLoaded(lib_name):
        dialog_lib.loadLibrary(lib_name)
    lib = dialog_lib.getByName(lib_name)
    input_provider = lib.getByName(dialog_name)
    
    return ctx.getServiceManager().createInstanceWithArgumentsAndContext(
                "com.sun.star.awt.DialogProvider",
                (None, input_provider.createInputStream(), lib, None),
                ctx).createDialog("")

def removeChart(ChartName, SheetNo):
    oDoc = XSCRIPTCONTEXT.getDocument()
    oSheet = oDoc.Sheets.getByIndex(SheetNo)
    charts = oSheet.Charts
    i = 0
    while i < charts.getCount():
        if charts.getByIndex(i).getName() == ChartName:
            charts.removeByName(ChartName)
            break

        i += 1

def getFormat(f):
      
    oDoc = XSCRIPTCONTEXT.getDocument()
    Loc = Locale()
    Loc.Language = "en"
    Loc.Country = "US"

    NumberFormats = oDoc.NumberFormats

    formatId = NumberFormats.queryKey(f, Loc, False)
    if formatId == -1:
        formatId = NumberFormats.addNew(f, Loc)
    
    return formatId

def ClearChart(Chart):
	
    oChart = Chart.getEmbeddedObject()
    oFirstDiagram = oChart.getFirstDiagram()
  	
    #get coordinates from the diagram
    oCoords = oFirstDiagram.getCoordinateSystems()
    oCoord = oCoords[0]

    #get chart type
    oChartTypes = oCoord.getChartTypes()
    oChartType = oChartTypes[0]

    oDataSeries = oChartType.getDataSeries()
    i = len(oDataSeries)

    while i > 0: 
        oChartType.removeDataSeries(oDataSeries[i - 1])
        i -= 1

def GenerateChart(SheetNo, ChartNo, ChartType, w):

    oDataArray = []
    for i in range(1000):
        x = 2 * math.pi * i / 1000
        l = []
        l.append(x)
        l.append(w[0].Amplitude * math.sin(w[0].Frequency * (x - w[0].Phase)))
        l.append(w[1].Amplitude * math.cos(w[1].Frequency * (x - w[1].Phase)))
        oDataArray.append(tuple(l))
 
    oDoc = XSCRIPTCONTEXT.getDocument()
	
    oSheet = oDoc.Sheets.getByIndex(SheetNo)
    charts = oSheet.getCharts()
	
    #Position and dimension
    Rect = Rectangle()
    Rect.Width, Rect.Height, Rect.X, Rect.Y = 25000, 12500, 100, 500

    RangeAddress = []
    RangeAddress.append(CellRangeAddress())

    RangeAddress[0].Sheet = oSheet.RangeAddress.Sheet
    RangeAddress[0].StartRow = 0
    RangeAddress[0].StartColumn = 0
    RangeAddress[0].EndRow = 0
    RangeAddress[0].EndColumn = 0
    	  	
    removeChart("chart" + str(ChartNo), SheetNo) #Delete chart if present
    charts.addNewByName("chart" + str(ChartNo), Rect , tuple(RangeAddress), False, False)
    oChart = oSheet.Charts.getByName( "chart" + str(ChartNo) )
    oChartDoc = oChart.getEmbeddedObject()

   #Change to ChartType
    oChartDoc.createInstance(ChartType)
 
    ClearChart(oChart)
 	
    oData = oChartDoc.getData()
 	
    oData.setData(tuple(oDataArray))
    
    oDesc = list(oChartDoc.Data.getColumnDescriptions())
    oDesc[1] = "Sin x"
    oDesc[2] = "Cos x"
    oChartDoc.Data.setColumnDescriptions(tuple(oDesc))
    
    oDesc = list(oChartDoc.Data.getComplexColumnDescriptions())
    oDesc[1] = list(oDesc[1])
    oDesc[2] = list(oDesc[2])
    oDesc[1][0] = "Sin x"
    oDesc[2][0] = "Cos x"
    oDesc[1] = tuple(oDesc[1])
    oDesc[2] = tuple(oDesc[2])
    oChartDoc.Data.setComplexColumnDescriptions(tuple(oDesc))
   
    oFirstDiagram = oChartDoc.getFirstDiagram()
    oDiagram = oChartDoc.getDiagram()
    #get coordinates from the diagram
    oCoords = oFirstDiagram.getCoordinateSystems()
    oCoord = oCoords[0]
    #get Chart Type
    oChartTypes = oCoord.getChartTypes()
    oChartType = oChartTypes[0]
    oDataSeries = oChartType.getDataSeries()
    nSeries = len(oDataSeries)

    for i in range(nSeries):
        oSeries = oDataSeries[i]
        oSymbol = oSeries.Symbol
        oSymbol.Style = 0
        oSeries.Symbol = oSymbol
        oSeries.LineWidth = 1

    oChartDoc.HasMainTitle = True
    oChartDoc.Title.String = "Sine x, Cosine x"
    oChartDoc.Title.CharWeight = BOLD
    oChartDoc.Title.CharHeight = 20
    oChartDoc.Title.CharFontName = "Times New Roman"
    oChartDoc.HasSubTitle = True
    oChartDoc.SubTitle.String = "0 to 2" + unichr(960)
    oChartDoc.SubTitle.CharFontName = "Times New Roman"
    oxAxis = oDiagram.XAxis
    oxAxis.NumberFormat = getFormat("0.00")
    oxAxis.AxisTitle.String = "x"
    oxAxis.CrossoverPosition = ZERO
    oxAxis.Min = 0.0
    oxAxis.Max = 2 * math.pi
    oyAxis = oDiagram.YAxis
    oyAxis.NumberFormat = getFormat("0.00")
    oyAxis.AxisTitle.String = "y"
 
g_exportedScripts = MakeChart, 


I have links in column M for running each of the three languages. A couple of lessons: 1) It is easier to translate from Basic to Python than from Basic to JavaScript. 2) Both the JavaScript and Python are distinctly faster than the Basic, though in the case of the JavaScript, the first time it is run it is a bit slow, since the first run loads the library.
Attachments
NewWaveChart.ods
Basic, JavaScript, and Python
(61.12 KiB) Downloaded 538 times
Apache OpenOffice 4.1.1
Windows XP
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Inserting a chart with OO Basic

Post by saleem145 »

Thanks for your help Charlie!! I had to sink deep into another project and so wasn't checking this board. But I will download and try the new stuff tonight!! Thanks again for your help!

Saleem
OpenOffice 3.4.0
Mac OS X 10.5.8
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Inserting a chart with OO Basic

Post by Charlie Young »

saleem145 wrote:Thanks for your help Charlie!! I had to sink deep into another project and so wasn't checking this board. But I will download and try the new stuff tonight!! Thanks again for your help!

Saleem
Welcome back.

That last attachment, while it's probably worth understanding, isn't actually the best way to do this. As I said at first, Charts.addNewByName() was the only way I knew how to do this, but I suspected then that there was some other way, and there is.

The attached example uses an OLE2Shape, with CLSID "12DCAE26-281F-416F-a234-c3086127382e." This method doesn't require the null CellRange to be specified and the DataSeries to be removed. It can also be adapted for use in Impress or Draw, or in Writer by using a TextEmbeddedObject instead of an OLE2Shape.

I got bored with the trig functions, so the attached file also includes Basic, JavaScript, and Python to play with a cycloid. See the link for reasonable parameters to use. I also added a rotation angle, just for kicks.
Attachments
OleCharts.ods
Charts by OLE2Shape
(69.8 KiB) Downloaded 535 times
Apache OpenOffice 4.1.1
Windows XP
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Inserting a chart with OO Basic

Post by saleem145 »

Thanks Charlie!! Its all very helpful. As far as I see it it should be one of the examples distributed with OpenOffice so others can benefit for it as well.

One question for you....I am trying to design my own dialog box with a ComboBox field. In properties how do I enter information for the "List Entries" field. I tried "USA, UK, Italy" and {"USA, US, Italy"} but it thinks of them as one entry as opposed to three seperate entries.

Thanks,

Saleem

BTW: Something closer to the app I am trying to develop is I have data with n columns, each column is a Series....Series1, Series2,.....

I want the user to be able to pick Series-x and Series-y and have it plot Series-x vs Series-y.

I will take a look at the new example. Thanks.
OpenOffice 3.4.0
Mac OS X 10.5.8
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Inserting a chart with OO Basic

Post by Charlie Young »

saleem145 wrote:Thanks Charlie!! Its all very helpful. As far as I see it it should be one of the examples distributed with OpenOffice so others can benefit for it as well.

One question for you....I am trying to design my own dialog box with a ComboBox field. In properties how do I enter information for the "List Entries" field. I tried "USA, UK, Italy" and {"USA, US, Italy"} but it thinks of them as one entry as opposed to three seperate entries.

Thanks,

Saleem

BTW: Something closer to the app I am trying to develop is I have data with n columns, each column is a Series....Series1, Series2,.....

I want the user to be able to pick Series-x and Series-y and have it plot Series-x vs Series-y.

I will take a look at the new example. Thanks.
The ComboBox is populated by

Code: Select all

DialogField = oDialog.getControl("ComboBox1")
DialogField.addItems(Array("USA", "US", "Italy"),0)
That's in Basic, which I am trying to discourage the use of by providing JavaScript and Python examples, but it should be easy enough to do it in those languages once you have the idea.


For the series, just set up the ChartDataArray with the desired data. I've been using various functions for this, but it could just as well be a database query, or an array.
Apache OpenOffice 4.1.1
Windows XP
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Inserting a chart with OO Basic

Post by saleem145 »

Sorry I am new to python. So this is probably a silly question. I have added

Code: Select all

import array

Code: Select all

def RunDialog(w):

    oDoc = XSCRIPTCONTEXT.getDocument()
    ctx = uno.getComponentContext()
    oDialog = create_dialog(ctx,oDoc,"Standard","Dialog1")
    DlgField = [[0,0],[0,0],[0,0],[0,0]]

    DlgField[0][0] = oDialog.getControl("Amplitude")
    DlgField[1][0] = oDialog.getControl("Frequency")
    DlgField[2][0] = oDialog.getControl("Phase")
    DlgField[3][0] = oDialog.getControl("SignalBox")

    DlgField[0][1] = oDialog.getControl("CosAmplitude")
    DlgField[1][1] = oDialog.getControl("CosFrequency")
    DlgField[2][1] = oDialog.getControl("CosPhase")
    DlgField[3][1] = oDialog.getControl("TargetBox")

    a = array.array('i',[1,2,3,5])

    DlgField[3][0].addItems(a,0)

    r = oDialog.execute()
    w.append(Wave(DlgField[0][0].Value,DlgField[1][0].Value,DlgField[2][0].Value))
    w.append(Wave(DlgField[0][1].Value,DlgField[1][1].Value,DlgField[2][1].Value))
    return r == 1

Its complaining array.array has not attribute getTypes. Thanks.
OpenOffice 3.4.0
Mac OS X 10.5.8
User avatar
RoryOF
Moderator
Posts: 35066
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Inserting a chart with OO Basic

Post by RoryOF »

A good intro to Python is "A Byte of Python" at
http://www.swaroopch.org/notes/Python

Note there are different editions for Python 2 and Python 3. As far as I know OpenOffice uses Python 2.x.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.5 LTS
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Inserting a chart with OO Basic

Post by saleem145 »

Got something to work actually!!

Code: Select all

   DlgField[3][0].addItem("Saleem",0)
Did the trick.

Saleem
OpenOffice 3.4.0
Mac OS X 10.5.8
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Inserting a chart with OO Basic

Post by Charlie Young »

saleem145 wrote:Sorry I am new to python. So this is probably a silly question. I have added

Code: Select all

import array

Code: Select all

def RunDialog(w):

    oDoc = XSCRIPTCONTEXT.getDocument()
    ctx = uno.getComponentContext()
    oDialog = create_dialog(ctx,oDoc,"Standard","Dialog1")
    DlgField = [[0,0],[0,0],[0,0],[0,0]]

    DlgField[0][0] = oDialog.getControl("Amplitude")
    DlgField[1][0] = oDialog.getControl("Frequency")
    DlgField[2][0] = oDialog.getControl("Phase")
    DlgField[3][0] = oDialog.getControl("SignalBox")

    DlgField[0][1] = oDialog.getControl("CosAmplitude")
    DlgField[1][1] = oDialog.getControl("CosFrequency")
    DlgField[2][1] = oDialog.getControl("CosPhase")
    DlgField[3][1] = oDialog.getControl("TargetBox")

    a = array.array('i',[1,2,3,5])

    DlgField[3][0].addItems(a,0)

    r = oDialog.execute()
    w.append(Wave(DlgField[0][0].Value,DlgField[1][0].Value,DlgField[2][0].Value))
    w.append(Wave(DlgField[0][1].Value,DlgField[1][1].Value,DlgField[2][1].Value))
    return r == 1

Its complaining array.array has not attribute getTypes. Thanks.
That complaint usually means you're supplying a list where it wants a tuple, and you can fix it by just using a tuple() constructor. You didn't indicate the offending line, but maybe you want

Code: Select all

a = [1,2,3,5]
DlgField[3][0].addItems(tuple(a),0)
and it looks like you don't even need the list, so you could just start with the tuple

Code: Select all

a = (1,2,3,5)
DlgField[3][0].addItems(a,0)
though I'm not sure what you're trying to do there.
Apache OpenOffice 4.1.1
Windows XP
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Inserting a chart with OO Basic

Post by saleem145 »

Yes I have that one sorted out...Another issue

sinAmplitude = DlgField[3][0].getSelectedItemPos()
cosAmplitude = DlgField[3][1].getSelectedItemPos()

It complains on these two

I am also trying

DlgField[3][0].selectItemPos(0,True)
DlgField[3][0].selectItemPos(0,True)

Complains on this too.
OpenOffice 3.4.0
Mac OS X 10.5.8
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Inserting a chart with OO Basic

Post by saleem145 »

This is a ListBox vs ComboBox issue....I have a ComboBox but was trying to use ListBox interface....resolved..
OpenOffice 3.4.0
Mac OS X 10.5.8
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Inserting a chart with OO Basic

Post by saleem145 »

How can I turn off the legend using python -- I removed all the code that was setting the series names...the names are gone...but the lines and colors are still there. Thanks.
OpenOffice 3.4.0
Mac OS X 10.5.8
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Inserting a chart with OO Basic

Post by Charlie Young »

saleem145 wrote:How can I turn off the legend using python -- I removed all the code that was setting the series names...the names are gone...but the lines and colors are still there. Thanks.
In my examples, oChartDoc refers to XChartDocument, and HasLegend is a boolean property of it.

Code: Select all

oChartDoc.HasLegend = False
Apache OpenOffice 4.1.1
Windows XP
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: Inserting a chart with OO Basic

Post by saleem145 »

Thanks. I just figured it out!! I am almost have it working....
OpenOffice 3.4.0
Mac OS X 10.5.8
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: [Solved] Inserting a chart with OO Basic

Post by saleem145 »

Ok now one question -- how can I make the dialog a part of an add-in as opposed to a spreadsheet.

In my add-in I created a Dialogs folder which contains a dialog-lc.xml and a Standard subfolder. Standard subfolder contains Dialog1.xml dialog-lb.xml. All these files are copied over from the spreadsheet we have been working with.

I edited the manifest.xml of my addin to include the following lines of code

<manifest:file-entry manifest:media-type="text/xml" manifest:full-path=Dialogs/dialog-lc.xml>
<manifest:file-entry manifest:media-type="text/xml" manifest:full-path=Dialogs/Standard/dialog-lb.xml>
<manifest:file-entry manifest:media-type="text/xml" manifest:full-path=Dialogs/Standard/Dialog1.xml>

I installed the add-in. But when I go to Tools->Organize Dialogs the dialog box is nowhere to be found....

Saleem
OpenOffice 3.4.0
Mac OS X 10.5.8
saleem145
Posts: 130
Joined: Mon Jul 02, 2012 4:47 pm

Re: [Solved] Inserting a chart with OO Basic

Post by saleem145 »

One more question -- how are the buttons / links in the spreadsheet created?? I am trying to embed the python code in my add-in as well and then try to call it from a spreadsheet.

Thanks,

Saleem
OpenOffice 3.4.0
Mac OS X 10.5.8
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: [Solved] Inserting a chart with OO Basic

Post by Charlie Young »

saleem145 wrote:One more question -- how are the buttons / links in the spreadsheet created?? I am trying to embed the python code in my add-in as well and then try to call it from a spreadsheet.

Thanks,

Saleem
The links are added to cell text using Insert > Hyperlink. This may be done via macro as well, though that gets tricky.
link.jpg
The link for the embedded Python looks like

Code: Select all

vnd.sun.star.script:OLECharts.py$MakeWaveChart?language=Python&location=document
I'm not sure what your problem is with the dialogs. You're trying to put these in an .OXT file?
Apache OpenOffice 4.1.1
Windows XP
Post Reply