Code: Select all
REM ***** BASIC *****
Option Explicit
Sub Main
Dim SheetName As String
SheetName = "Cell"
GenerateChart(SheetName,0,"com.sun.star.chart.XYDiagram")
End Sub
Sub removeChart(ChartName As String, SheetName as String)
Dim oDoc As Object, oSheet As Object, oShapes As Object, oShape As Object
Dim i As Integer
oDoc = ThisComponent
oSheet = oDoc.Sheets.getByName(SheetName)
oShapes = oSheet.DrawPage
i = 0
do while i < oShapes.getCount()
oShape = oShapes(i)
if oShape.Name = ChartName then
oShapes.remove(oShape)
exit do
endif
i = i + 1
loop
End Sub
Sub GenerateChart(SheetName as string, ChartNo as integer, ChartType As String)
Dim oDoc As Object, oSheet As Object, Charts As Object, Chart As Object, oChartDoc As Object, oChart As Object
Dim Rect As new com.sun.star.awt.Rectangle
Dim RangeAddress(0) As New com.sun.star.table.CellRangeAddress
Dim oData As Object
Dim oDataArray( 0 To 1000,0 To 1)
Dim oFirstDiagram As Object, oDiagram As Object
Dim oCoords
Dim oCoord
Dim oChartTypes
Dim oChartType
Dim oDataSeries
Dim oSeries
Dim oSymbol
Dim oDesc
Dim oxAxis, oyAxis
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
oDoc = ThisComponent
oSheet = oDoc.Sheets.getByName(SheetName)
charts= oSheet.Charts
'Position and dimension
with Rect
.Width = 25000
.Height = 12500
.X = 100
.Y = 500
end with
with RangeAddress(0)
.Sheet = oSheet.RangeAddress.Sheet
.StartRow = 0
.StartColumn = 0
.EndRow = 0
end with
removeChart("chart" & ChartNo, SheetName) ' Delete chart if present
charts.addNewByName("chart" & ChartNo, Rect ,RangeAddress(), false, false)
oChart = oSheet.Charts.getByName( "chart" & ChartNo )
oChartDoc = oChart.getEmbeddedObject()
'Change to ChartType
with oChartDoc
.createInstance(ChartType)
end with
ClearChart(oChart)
oData = oChartDoc.getData()
' call setData() method of XChartDataArray to apply the data
oData.setData(oDataArray())
oDesc = oChartDoc.Data.ColumnDescriptions
oDesc(0) = "Sin x"
oDesc(1) = "Sin x"
oChartDoc.Data.ColumnDescriptions = oDesc
oDesc = oChartDoc.Data.ComplexColumnDescriptions
oDesc(0)(0) = "Sin x"
oDesc(1)(0) = "Sin x"
oChartDoc.Data.ComplexColumnDescriptions = 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()
oSeries = oDataSeries(0)
oSymbol = oSeries.Symbol
oSymbol.Style = 0
oSeries.Symbol = oSymbol
oSeries.LineWidth = 1
oChartDoc.hasMainTitle = True
oChartDoc.Title.String = "Sine x"
oChartDoc.Title.CharWeight = com.sun.star.awt.FontWeight.BOLD
oChartDoc.Title.CharHeight = 20
oChartDoc.Title.CharFontName = "Times New Roman"
oChartDoc.hasSubTitle = True
oChartDoc.SubTitle.String = "0 to 2" & Chr(960)
oChartDoc.SubTitle.CharFontName = "Times New Roman"
oxAxis = oDiagram.XAxis
oxAxis.NumberFormat = getFormat("0.00")
oxAxis.AxisTitle.String = "x"
oxAxis.CrossoverPosition = com.sun.star.chart.ChartAxisPosition.ZERO
oyAxis = oDiagram.yAxis
oyAxis.NumberFormat = getFormat("0.00")
oyAxis.AxisTitle.String = "y"
End Sub
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
Function getformat(f As String) As Long
Dim oDoc As Object
Dim NumberFormats As Object
Dim Loc as New com.sun.star.lang.Locale
Dim formatID As Long
oDoc = ThisComponent
Loc.Language = "en"
Loc.Country = "US"
NumberFormats = oDoc.NumberFormats
formatId = NumberFormats.queryKey(f, Loc, False)
If formatId = -1 Then
formatId = NumberFormats.addNew(f, Loc)
End If
getformat = formatID
End Function
BASIC runtime error. An Exception occured Type: com.sun.star.uni.RuntimeExceptionMessage: .
with the following line highlighted:
charts.addNewByName("chart" & ChartNo, Rect ,RangeAddress(), false, false)
anybody has an idea what the problem is?