向图表系列的数据点添加自定义标签
Contents
[
Hide
]
您可以在图表系列的数据点中添加自定义标签。Aspose.Cells for Python via .NET提供ChartPoint.data_labels.text属性来添加这些自定义标签。本文将介绍如何使用此属性为图表中的数据点添加自定义标签。
以下代码创建了通过线连接的数据标记散点图,然后在图表的系列中为数据点添加了自定义标签。每个自定义标签显示系列名称和点名称。您可以使用任何其他文本代替它。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.cells import FileFormatType, SaveFormat, Workbook | |
from aspose.cells.charts import ChartType | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
workbook = Workbook(FileFormatType.XLSX) | |
sheet = workbook.worksheets[0] | |
# Put data | |
sheet.cells.get(0, 0).put_value(1) | |
sheet.cells.get(0, 1).put_value(2) | |
sheet.cells.get(0, 2).put_value(3) | |
sheet.cells.get(1, 0).put_value(4) | |
sheet.cells.get(1, 1).put_value(5) | |
sheet.cells.get(1, 2).put_value(6) | |
sheet.cells.get(2, 0).put_value(7) | |
sheet.cells.get(2, 1).put_value(8) | |
sheet.cells.get(2, 2).put_value(9) | |
# Generate the chart | |
chartIndex = sheet.charts.add(ChartType.SCATTER_CONNECTED_BY_LINES_WITH_DATA_MARKER, 5, 1, 24, 10) | |
chart = sheet.charts[chartIndex] | |
chart.title.text = "Test" | |
chart.category_axis.title.text = "X-Axis" | |
chart.value_axis.title.text = "Y-Axis" | |
chart.n_series.category_data = "A1:C1" | |
# Insert series | |
chart.n_series.add("A2:C2", False) | |
series = chart.n_series[0] | |
pointCount = series.points.count | |
for i in range(pointCount): | |
pointIndex = series.points[i] | |
pointIndex.data_labels.text = "Series 1" + "\n" + "Point " + str(i) | |
# Insert series | |
chart.n_series.add("A3:C3", False) | |
series = chart.n_series[1] | |
pointCount = series.points.count | |
for i in range(pointCount): | |
pointIndex = series.points[i] | |
pointIndex.data_labels.text = "Series 2" + "\n" + "Point " + str(i) | |
workbook.save(dataDir + "output_out.xlsx", SaveFormat.XLSX) |