You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openoffice.apache.org by Semen Gubarev <mn...@gmail.com> on 2014/12/01 11:52:20 UTC
Creating Chart "com.sun.star.chart.XYDiagram"
Hello Everyone!
I read a lot of documentation about creation charts using macro BASIC
And I still can't create chart in Writer (almost all documentation describe
how to create chart in Calc). The problem with inserting series XY data
into chart (separate X values and separate Y values)
First of all I create a chart object like this:
'Prepare embedded object
Dim oEmbeddedObject
oEmbeddedObject =
oDoc.CreateInstance("com.sun.star.text.TextEmbeddedObject")
oEmbeddedObject.SetPropertyValue("CLSID",
"12dcae26-281f-416f-a234-c3086127382e")
oEmbeddedObject.Name = "ChartID"
'Insert chart in document
oCursor.GetText().InsertTextContent(oCursor, oEmbeddedObject, False)
'The first approach inserting data like this:
oDocChart.Diagram.DataRowSource = com.sun.star.chart.ChartDataRowSource.ROWS
Dim aData(1, 3)
aData(0, 0) = 10
aData(0, 1) = 20
aData(0, 2) = 30
aData(1, 0) = 10
aData(1, 1) = 40
aData(1, 2) = 90
Dim oData as Object
oData = oDocChart.Data
oData.Data = aData
for this case I get 2 series Y data, but I need 1 series with data X-Y
'The second approach inserting data using Writer table as datasource
'I'will miss a lot of code
Dim oChartType
oChartType =
oDocChart.FirstDiagram.GetCoordinateSystems()(0).GetChartTypes()(0)
Dim oDataProv
oDataProv = oDoc.createInstance( "com.sun.star.chart2.data.DataProvider" )
Dim oDataSeries
oDataSeries = CreateDataSeries_XYDiagram(oDataProv, "table1.A1:A10",
"table1.B1:B10")
Dim oNewDataSeriesList(0) as Object 'new data series
oNewDataSeriesList(0) = oDataSeries
'Correspond with last approach I get good chart, but after reopening the
document Y values become to X values
The question is, how to right create a X-Y chart?
Re: Creating Chart "com.sun.star.chart.XYDiagram"
Posted by Fernand Vanrie <so...@pmgroup.be>.
Semen,
I use a hidden calc doc to produce charts
i hope this can help::
greetz
Fernand
Sub AreaChart(ChartDataArray(), LabelsArray(), CName as String, CSheet
as String, Optional YPos as Integer,bOrders as boolean)
Dim Length as Integer
Dim oCharts
Dim oRect as new com.sun.star.awt.Rectangle
Dim loadArgs(0) as new com.sun.star.beans.PropertyValue
loadArgs(0).Name = "Hidden"
loadArgs(0).Value = true
oDoc =
StarDesktop.LoadComponentFromUrl("private:factory/scalc","_default",0,loadArgs())
oSheet = oDoc.Sheets.getByIndex(0)
oCharts = osheet.charts
If IsMissing(ChartNum) Then ChartNum=0
With oRect
.X = 0
.Y = YPos
.Height= 12000
.width = 12000/4 * ubound(LabelsArray())
End With
' Remove the CName
'chart if it exists
If oSheet.getCharts().hasByName(CName) Then
oSheet.getCharts().removeByName(CName)
endif
' Make Chart
oCharts.addNewByName(CName,oRect,Array(),TRUE, TRUE)
oChart = oCharts.getByName(CName).embeddedObject
oChart.diagram =
oChart.createInstance("com.sun.star.chart.AreaDiagram")
oDiagram = ochart.getFirstDiagram()
oCoords = oDiagram.getCoordinateSystems()
oCoord = oCoords(0)
oChartTypes = oCoord.getChartTypes()
oChartType = oChartTypes(0)
REM Wanneer Array Dataseries leeg is (Bij LO 4.3), Voeg deze dan toe
If uBound(oChartType.getDataSeries()) = -1 Then
oNewDataSeries = CreateUnoService("com.sun.star.chart2.DataSeries")
oChartType.addDataSeries(oNewDataSeries)
oNewDataSeries = CreateUnoService("com.sun.star.chart2.DataSeries")
oChartType.addDataSeries(oNewDataSeries)
EndIf
oDataSeriesList = oChartType.getDataSeries()
oDataSerieOFFERTES = oDataSeriesList(0)'=' first series
oDataSerieORDERS = oDataSeriesList(1)'1=' 2nd series
oDataSerieOFFERTES.color = rgb(0,184,255)
if bOrders then
oDataSerieOFFERTES.Transparency = 80
else
oDataSerieOFFERTES.Transparency = 0
endif
oDataSerieORDERS.color = rgb(220,35,0)
'oDataSerieORDERS.Transparency = 50
oXaxis = oCoord.getAxisByDimension(0, 0)
oXaxis.TextRotation = 90
oChart.lockControllers()
' Data
oChart.Data.setData(ChartDataArray())
oChart.Data.setrowDescriptions(LabelsArray())
dim columnLabelsArray(1)
ss = "Gezien & Herinnerd voor " & sfirma & " verschenen in "
& smagnaam
columnLabelsArray(0) = "OFFERTES"
columnLabelsArray(1) = "ORDERS"
oChart.Data.setColumnDescriptions(columnLabelsArray())
' Title
oChart.HasMainTitle=True
oChart.Title.string = ss
' Labels
oChart.DataSourceLabelsInFirstColumn = true
oChart.DataSourceLabelsInFirstRow= false
' Legend
oSheet.getCharts.getByName(CName).getEmbeddedObject.hasLegend = true
' Percentages
' Last DrawPage is oSheet.getDrawPage().getCount()-1
oSheet.drawpage.getByIndex(oSheet.getDrawPage().getCount()-1).model.diagram.setPropertyValue("DataCaption",0)
'PERCENT = 2 and TEXT = 4
oChart.unlockControllers()
End Sub
On 1/12/2014 11:52, Semen Gubarev wrote:
> Hello Everyone!
> I read a lot of documentation about creation charts using macro BASIC
> And I still can't create chart in Writer (almost all documentation describe
> how to create chart in Calc). The problem with inserting series XY data
> into chart (separate X values and separate Y values)
>
> First of all I create a chart object like this:
>
> 'Prepare embedded object
> Dim oEmbeddedObject
> oEmbeddedObject =
> oDoc.CreateInstance("com.sun.star.text.TextEmbeddedObject")
> oEmbeddedObject.SetPropertyValue("CLSID",
> "12dcae26-281f-416f-a234-c3086127382e")
> oEmbeddedObject.Name = "ChartID"
>
> 'Insert chart in document
> oCursor.GetText().InsertTextContent(oCursor, oEmbeddedObject, False)
>
> 'The first approach inserting data like this:
> oDocChart.Diagram.DataRowSource = com.sun.star.chart.ChartDataRowSource.ROWS
> Dim aData(1, 3)
> aData(0, 0) = 10
> aData(0, 1) = 20
> aData(0, 2) = 30
> aData(1, 0) = 10
> aData(1, 1) = 40
> aData(1, 2) = 90
> Dim oData as Object
> oData = oDocChart.Data
> oData.Data = aData
> for this case I get 2 series Y data, but I need 1 series with data X-Y
>
> 'The second approach inserting data using Writer table as datasource
> 'I'will miss a lot of code
>
> Dim oChartType
> oChartType =
> oDocChart.FirstDiagram.GetCoordinateSystems()(0).GetChartTypes()(0)
>
> Dim oDataProv
> oDataProv = oDoc.createInstance( "com.sun.star.chart2.data.DataProvider" )
>
> Dim oDataSeries
> oDataSeries = CreateDataSeries_XYDiagram(oDataProv, "table1.A1:A10",
> "table1.B1:B10")
>
> Dim oNewDataSeriesList(0) as Object 'new data series
> oNewDataSeriesList(0) = oDataSeries
>
> 'Correspond with last approach I get good chart, but after reopening the
> document Y values become to X values
>
> The question is, how to right create a X-Y chart?
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@openoffice.apache.org
For additional commands, e-mail: dev-help@openoffice.apache.org
Re: Creating Chart "com.sun.star.chart.XYDiagram"
Posted by "F C. Costero" <fj...@gmail.com>.
On Mon, Dec 1, 2014 at 3:52 AM, Semen Gubarev <mn...@gmail.com> wrote:
> Hello Everyone!
> I read a lot of documentation about creation charts using macro BASIC
> And I still can't create chart in Writer (almost all documentation describe
> how to create chart in Calc). The problem with inserting series XY data
> into chart (separate X values and separate Y values)
>
> First of all I create a chart object like this:
>
> 'Prepare embedded object
> Dim oEmbeddedObject
> oEmbeddedObject =
> oDoc.CreateInstance("com.sun.star.text.TextEmbeddedObject")
> oEmbeddedObject.SetPropertyValue("CLSID",
> "12dcae26-281f-416f-a234-c3086127382e")
> oEmbeddedObject.Name = "ChartID"
>
> 'Insert chart in document
> oCursor.GetText().InsertTextContent(oCursor, oEmbeddedObject, False)
>
> 'The first approach inserting data like this:
> oDocChart.Diagram.DataRowSource =
> com.sun.star.chart.ChartDataRowSource.ROWS
> Dim aData(1, 3)
> aData(0, 0) = 10
> aData(0, 1) = 20
> aData(0, 2) = 30
> aData(1, 0) = 10
> aData(1, 1) = 40
> aData(1, 2) = 90
> Dim oData as Object
> oData = oDocChart.Data
> oData.Data = aData
> for this case I get 2 series Y data, but I need 1 series with data X-Y
>
> 'The second approach inserting data using Writer table as datasource
> 'I'will miss a lot of code
>
> Dim oChartType
> oChartType =
> oDocChart.FirstDiagram.GetCoordinateSystems()(0).GetChartTypes()(0)
>
> Dim oDataProv
> oDataProv = oDoc.createInstance( "com.sun.star.chart2.data.DataProvider" )
>
> Dim oDataSeries
> oDataSeries = CreateDataSeries_XYDiagram(oDataProv, "table1.A1:A10",
> "table1.B1:B10")
>
> Dim oNewDataSeriesList(0) as Object 'new data series
> oNewDataSeriesList(0) = oDataSeries
>
> 'Correspond with last approach I get good chart, but after reopening the
> document Y values become to X values
>
> The question is, how to right create a X-Y chart?
>
Hello Semen,
I find that the code below, a slight modification of yours, works for me.
--------------------------------
oDoc = ThisComponent
oCursor = oDoc.Text.createTextCursor()
oTextObject = oDoc.CreateInstance("com.sun.star.text.TextEmbeddedObject")
oTextObject.SetPropertyValue("CLSID",
"12dcae26-281f-416f-a234-c3086127382e")
oTextObject.Name = "ChartID"
'Insert chart in document
oCursor.GetText().InsertTextContent(oCursor, oTextObject, False)
oDocChart = oTextObject.EmbeddedObject.Component
'The first approach inserting data like this:
oDocChart.Diagram = oDocChart.createInstance("com.sun.star.chart.XYDiagram")
oDocChart.Diagram.DataRowSource = com.sun.star.chart.ChartDataRowSource.ROWS
Dim aData(1, 2)
aData(0, 0) = 10
aData(0, 1) = 20
aData(0, 2) = 30
aData(1, 0) = 10
aData(1, 1) = 40
aData(1, 2) = 90
oDocChart.Data.Data = aData
oDocChart.DataSourceLabelsInFirstRow = False
------------------------------------
I hope that helps.
Best regards,
Francis