How to change Writer diagram range by macro

Test this, but add the colors to the aColors for more Columns.
diagram-ve-writeru-pokus1.odt (23.9 kB)

source: Apache OpenOffice Community Forum - Add multiple series in calc chart using macros - (View topic)

Perfect !!! Super !!! I am so happy ! … It does exactly the job !

Solution !!!

Thank you so much ! … we did not find it because we were looking for “sequences”, not for “series” !!! and for Writer solution !

And thanks to all of you for your attention and help !!! Finally KamilLanda got the solution !!

Philippe

Hello KamilLanda,

You really can do a lot with macros . For example you can find a bill system built on openOffice Calc.

What I got comes from PYS ! Thank to him !

Data is, as far as I understand, just a view of DataSequences. I did not find documentation on how to manage diagram structure with macro, but the code below shows that if you make a change in Data structure, DataSequences is updated.

	' get first diagram
	oDocument = ThisComponent.CurrentController.Frame
	o = oDocument.controller.model.embeddedObjects.getByIndex(0)
	oExt = o.ExtendedControlOverEmbeddedObject
	oDiag = oExt.component	
	
	' get data structure (oDiag.Data)
	oData = oDiag.getData
	
	' get data array
	d = oData.getData
	
	' change any value
	d(0)(1) = -1
	
	' update data structure
	oData.setData( d)
	
	' get sequence structure
	oSeq = oDiag.DataSequences(2)
	
	' show value updated in DataSequences structure
	msgBox oSeq(0).Values.Data(0)

Through “Data” we can manage DataSequences which seems to be where the diagram get what it needs to update its range configuration.

The big trouble left is : it does not allow to change columns number ! And that exactly what we are trying to find now !

Any way, thank you for your attention,
Philippe

Only small note, I suppose you will make the code optimalization :-).
For example the array p() starts from index 0 but it is null, because the loop starts from 1: for numColonne=1…

Nice … I had already seen usedData … it contains o copy of DataSequences where we can use redim !!! … I do not imagine the purpose of those views of DataSequences.

I checked that redim is allowed on UsedData.DataSequences ! Then I ran MiseAJourDiagrammes (update Diagrams) and it had no effect at all !

I can’t see any method to update DataSequences from UsedData ! :frowning:

Found the solution for column A ! … I used my version of MiseAJourColonne to update DataSequence(0)

Complete code … this way the code preserve the diagram configuration … just change the data range.
Column A needs to be treated separatly, not considered as data !

Sub A1D3
	ModifSourceDiagramme(array("B", "C", "D"), 3)
End Sub


Sub ModifSourceDiagramme(colonnes,  derniereLigne as integer)
	'COLORS FOR THE CHART
	dim aColors() : aColors=array(RGB(0,69,134), RGB(255,66,14), RGB(255,211,32), RGB(87,157,28), RGB(126,0,33), RGB(131,202,255))
	dim o,  oExt,  document,  oDiag,  oDataProvider,  oDataSequences, oDiagram, oCooSys, oCoods, oChartTypes, oChartType, oDataSeriesList, oDataSeries
	dim dimDataSequences as integer,  numColonne as integer,  col as string,  nseq as integer

	dim uColonnes as integer : uColonnes=ubound(colonnes)

	' récupère le diagramme (unique dans le document)
	document=ThisComponent.CurrentController.Frame
	o=document.controller.model.embeddedObjects.getByIndex(0)
	oExt=o.ExtendedControlOverEmbeddedObject
	oDiag=oExt.component

	oDataProvider=oDiag.getDataProvider()
	oDataSequences=oDiag.DataSequences
	oDiagram=oDiag.getFirstDiagram()
	oCooSys=oDiagram.getCoordinateSystems()
	oCoods=oCooSys(0) ' this chart has only a coordinate system
	oChartTypes=oCoods.getChartTypes() ' chart type one by one
	oChartType=oChartTypes(0)


	MiseAJourColonneA( oDataProvider, oDataSequences, 0, _
		"Donnees." & "A1:" & "A1", _
		"Donnees." & "A2:" & col & cstr(derniereLigne) )
		


	' DataSeries actuel	
	oDataSeries = oChartType.getDataSeries()
	redim preserve oDataSeries(uColonnes)

	for numColonne=0 to uColonnes
		col=colonnes(numColonne)
		
		MiseAJourColonne( oDataProvider, oDataSeries, numColonne, _
			"Donnees." & col & "1:" & col & "1", _
			"Donnees." & col & "2:" & col & cstr(derniereLigne) )
			
	next numColonne
	
	' mettre à jour DataSeries
	oChartType.setDataSeries(oDataSeries)
	
	' mettre à jour le diagramme
	MiseAJourDiagrammes()
End Sub


Sub MiseAJourColonne(oDataProvider as object, oDataSeries, numColonne as integer, sLabel as string,  sValeurs as string) as object
	dim oValues as object,  oData as object,  oLabel as object, oSeq as object
	
	oData=CreateUnoService("com.sun.star.chart2.data.LabeledDataSequence")
	
	oSeq=CreateDataSequence(oDataProvider, sValeurs, "values-y")
	oData.setValues(oSeq)
	
	oLabel=CreateDataSequence(oDataProvider, sLabel, "")
	oData.setLabel(oLabel) ' label is used as name
	
	oDataSeries(numColonne).setData(array(oData))
	
End sub


Sub **MiseAJourColonneA**(oDataProvider as object, oDataSequences as object, nseq as integer, sLabel as string, sValeurs as string)

dim oSeq as object, oValues as object
dim newrepr as string
dim newvalues as object
dim newseqValues as object
dim newseqLabel as object
dim oLabel as object
dim newlabel as object

oSeq = oDataSequences(nseq)

     oValues = oSeq.Values
    newrepr = sValeurs
    newvalues = oDataProvider.createDataSequenceByRangeRepresentation(newrepr)
    newvalues.Role = oValues.Role
    newseqValues = CreateUnoService("com.sun.star.chart2.data.LabeledDataSequence")
    newseqValues.Values = newvalues

    oLabel = oSeq.Label
    newrepr = sLabel
    newLabel = oDataProvider.createDataSequenceByRangeRepresentation(newrepr)
    ' newLabel.Role = oLabel.Role --> pour Label, toujours = ""
   newseqLabel = CreateUnoService("com.sun.star.chart2.data.LabeledDataSequence")
   newseqLabel.Label = newLabel

   with oSeq
       .setLabel(newseqLabel.label)
      .setValues(newseqValues.values)
    end with

end Sub

and now … implemented your solution in my aplication … crashed L.O. !!! :slightly_smiling_face: … that’s the next challenge ! :slightly_smiling_face:

Please consider to post code as code using the tool Preformatted text (Ctrl+E)?

sorry!
reply mail … thought it was private !!! :slight_smile:

Test this, columns A that is for Labels is updated via DataSequences, and then there is adding new DataSeries without Column A.
version3.odt (24.3 kB)