我正在通过python uno脚本创建图表。我添加了一个范围,类别,并设法为y轴设置了最小值-最大值。
我唯一不能做的就是更改“名称范围”。
现在,我的图表在第一个数据系列中显示“列AP”,在第二个数据系列中显示“列AQ”。图表看起来像https://imgur.com/AFNHWw5,但我希望它看起来像https://imgur.com/XUrMQ8e。我想在第一个数据系列中将图例更改为“已购买”,而在第二个数据系列中将其更改为“现在”。
我尝试按照https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=8991#p46467中的描述创建数据系列,但是无法使其在python中工作。 https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=8991#p46467似乎有些矫kill过正,因为除了“名称范围”之外,我拥有所有其他功能。
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.$A$551:$A$808"
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
我的图表显示了两条线。我希望图例在第一行显示“已购买”,在第二行显示“现在”。
答案 0 :(得分:1)
以下代码是从您的问题链接中改编而成的。不确定这是否是您所说的“过度杀伤”,但这似乎是设置标签的正确方法。
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)