Add static data to chart2 with python

Hey Guys, I am writing a python script where I am trying to populate com.sun.star.chart2 with data. The chart is created on LO Base form and should be populated with data which I first query from the database and apply some logic to it.

Chart object is created in one place as follows:

	chartObject = doc.createInstance( "com.sun.star.text.TextEmbeddedObject" )
	chartObject.setPropertyValue("CLSID","12dcae26-281f-416f-a234-c3086127382e")

And then I want to pass it to the following function to populate with data:

def set_chart(chart_doc, raw_labels_list, raw_values_list):

    ctx = uno.getComponentContext()
    smgr = ctx.ServiceManager
    
    chart_model = chart_doc.Model
    diagram = chart_model.getFirstDiagram()
    coord_sys = diagram.getCoordinateSystems()[0]

    line_chart_type = smgr.createInstanceWithContext("com.sun.star.chart2.LineChartType", ctx)
    data_series		= smgr.createInstanceWithContext("com.sun.star.chart2.DataSeries", ctx)
    labeled_cat_seq = smgr.createInstanceWithContext("com.sun.star.chart2.data.LabeledDataSequence", ctx)
    labeled_val_seq = smgr.createInstanceWithContext("com.sun.star.chart2.data.LabeledDataSequence", ctx)
    val_seq			= smgr.createInstanceWithContext("com.sun.star.chart2.data.DataSequence", ctx)
    cat_seq			= smgr.createInstanceWithContext("com.sun.star.chart2.data.DataSequence", ctx)
    
    val_seq.Role = "values-y"
    cat_seq.Role = "categories"    

	# This does not work:
    cat_seq.setData(raw_labels_list)
    val_seq.setData(raw_values_list)

	# Those approaches also do not work:

    # cat_seq.setTextualData(tuple(str(x) for x in raw_labels_list))
	# val_seq.setNumericalData(tuple(float(x) for x in raw_values_list))

    # for index, value in enumerate(raw_labels_list):
    #     cat_seq.replaceByIndex(index, value)
    
    labeled_cat_seq.setValues(cat_seq)
    labeled_val_seq.setValues(val_seq)

    coord_sys.addChartType(line_chart_type)
    coord_sys.getAxisByDimension(0, 0).getScaleData().Categories = labeled_cat_seq
    
    data_series.setData(tuple([labeled_val_seq]))

    series_tuple = tuple([data_series])
    line_chart_type.setDataSeries(series_tuple)

The interesting part is that if I comment out the attemp to “setData” everything else seems to work. Does anybody know, how to set the data for a DataSequence object? I am able to getData() but of course the result is empty.

Thanks!

If you want to get things done easily, create a database range in Calc linked to your query and link the chart to that database range.

Here is something in Basic: https://forum.openoffice.org/en/forum/viewtopic.php?t=88567