如何为数据系列中的数据范围命名?

how do I give a name to a data range in a data series?

我正在通过 python uno 脚本创建图表。我添加了范围、类别,并设法为 y 轴设置了最小值和最大值。

我唯一无法做到的是更改 'range for name'。

现在我的图表显示第一个数据系列 'Column AP',第二个数据系列 'Column AQ'。图表看起来像 https://imgur.com/AFNHWw5 but I want it to look like https://imgur.com/XUrMQ8e。我想将第一个数据系列的图例更改为 'Bought',将第二个数据系列的图例更改为 'Now'。

我尝试创建一个数据系列,如 https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=8991#p46467 but can't get it to work in python. https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=8991#p46467 中所述,这似乎有些过分,因为我拥有除了 'range for name' 之外的所有内容。

def create_data_sequence(data_provider, ran, rol):
    data_sequence = data_provider.createDataSequenceByRangeRepresentation(ran)
    if data_sequence:
        data_sequence.Role = rol
    return data_sequence

# TODO: move to end when done
# https://forum.openoffice.org/en/forum/viewtopic.php?t=53750
# https://forum.openoffice.org/en/forum/viewtopic.php?f=44&t=55115
dashboard_sheet = sheets.getByName('DASHBOARD')
values_sheet = sheets.getByName('Values') # TODO: remove, because it's referenced later
charts = dashboard_sheet.getCharts()

rect = Rectangle()
rect.Width, rect.Height, rect.X, rect.Y = 22000, 12000, 1000, 9200

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

range_address[0].Sheet = values_sheet.RangeAddress.Sheet
range_address[0].StartColumn = 41
range_address[0].StartRow = 550
range_address[0].EndColumn = 42
range_address[0].EndRow = 807

charts.addNewByName("Total Value Over Time", rect , tuple(range_address), False, False)
chart = charts.getByName("Total Value Over Time").getEmbeddedObject()
chart.createInstance("com.sun.star.chart.LineDiagram")

data_provider = chart.getDataProvider()
categories_sequence = smgr.createInstanceWithContext(\
                                        "com.sun.star.chart2.data.LabeledDataSequence", ctx)
categories_range = "$Values.$A1:$A8"
categories_sequence.setValues(create_data_sequence(data_provider,\
                                                   categories_range, "categories"))

coordinate_system = chart.getFirstDiagram().getCoordinateSystems()[0]

x_axis = coordinate_system.getAxisByDimension(0, 0) # TODO : why chart.getFirstDiagram()?
y_axis = chart.getDiagram().YAxis                   # TODO : why chart.getDiagram()? diff?
scale_x_data = x_axis.getScaleData()
scale_x_data.Categories = categories_sequence
x_axis.setScaleData(scale_x_data)
y_axis.Min = 55000
y_axis.Max = 125000
y_axis.NumberFormat = number_format_id

chart.HasMainTitle = True
chart.HasLegend = True
chart.Title.String = "Total Value Over Time"
chart.Title.CharHeight = 24
chart.HasSubTitle = True
chart.SubTitle.String = "Funds and Structured Products"
chart.SubTitle.CharHeight = 12

我的图表显示两条线。我希望图例在第一行显示 'Bought',在第二行显示 'Now'。

以下代码改编自您问题中的link。不确定 "overkill" 是否是您的意思,但这似乎是设置标签的正确方法。

set_sequence_label(chart, 0, "bought")
set_sequence_label(chart, 1, "now")

def set_sequence_label(chart, series_index, label):
    data_series = chart.FirstDiagram.getCoordinateSystems()[0].getChartTypes(
        )[0].getDataSeries()[series_index]
    data_sequence = data_series.getDataSequences()[0]
    oSequenceLabel = (
        chart.getDataProvider().createDataSequenceByRangeRepresentation(label))
    data_sequence.setLabel(oSequenceLabel)

文档:XLabeledDataSequence.setLabel().