创建带引导线的饼图
Contents
[
Hide
]
本文介绍了如何在使用 Aspose.Cells for Python via .NET API 时,从头开始创建带引线的饼图。在 Excel 中,默认启用“显示引线”选项,因此在Excel中创建饼图时会显示引线。然而,在使用 Aspose.Cells for Python via .NET API 创建类似图表时,需显式设置 Series.has_leader_lines 属性。
为了演示如何使用 Aspose.Cells for Python via .NET API 创建带引线的饼图,我们将首先创建一个新的 Workbook 并输入一些作为系列数据源的数据。一旦数据准备就绪,我们将添加一个类型为 ChartType.PIE 的 Chart 图表到集合中,并设置其不同方面以达到预期的图表效果。
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, Workbook | |
from aspose.cells.charts import ChartType, DataLabelsSeparatorType, LabelPositionType | |
# 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(".") | |
# Create an instance of Workbook in XLSX format | |
workbook = Workbook(FileFormatType.XLSX) | |
# Access the first worksheet | |
worksheet = workbook.worksheets[0] | |
# Add two columns of data | |
worksheet.cells.get("A1").put_value("Retail") | |
worksheet.cells.get("A2").put_value("Services") | |
worksheet.cells.get("A3").put_value("Info & Communication") | |
worksheet.cells.get("A4").put_value("Transport Equip") | |
worksheet.cells.get("A5").put_value("Construction") | |
worksheet.cells.get("A6").put_value("Other Products") | |
worksheet.cells.get("A7").put_value("Wholesale") | |
worksheet.cells.get("A8").put_value("Land Transport") | |
worksheet.cells.get("A9").put_value("Air Transport") | |
worksheet.cells.get("A10").put_value("Electric Appliances") | |
worksheet.cells.get("A11").put_value("Securities") | |
worksheet.cells.get("A12").put_value("Textiles & Apparel") | |
worksheet.cells.get("A13").put_value("Machinery") | |
worksheet.cells.get("A14").put_value("Metal Products") | |
worksheet.cells.get("A15").put_value("Cash") | |
worksheet.cells.get("A16").put_value("Banks") | |
worksheet.cells.get("B1").put_value(10.4) | |
worksheet.cells.get("B2").put_value(5.2) | |
worksheet.cells.get("B3").put_value(6.4) | |
worksheet.cells.get("B4").put_value(10.4) | |
worksheet.cells.get("B5").put_value(7.9) | |
worksheet.cells.get("B6").put_value(4.1) | |
worksheet.cells.get("B7").put_value(3.5) | |
worksheet.cells.get("B8").put_value(5.7) | |
worksheet.cells.get("B9").put_value(3) | |
worksheet.cells.get("B10").put_value(14.7) | |
worksheet.cells.get("B11").put_value(3.6) | |
worksheet.cells.get("B12").put_value(2.8) | |
worksheet.cells.get("B13").put_value(7.8) | |
worksheet.cells.get("B14").put_value(2.4) | |
worksheet.cells.get("B15").put_value(1.8) | |
worksheet.cells.get("B16").put_value(10.1) | |
# Create a pie chart and add it to the collection of charts | |
id = worksheet.charts.add(ChartType.PIE, 3, 3, 23, 13) | |
# Access newly created Chart instance | |
chart = worksheet.charts[id] | |
# Set series data range | |
chart.n_series.add("B1:B16", True) | |
# Set category data range | |
chart.n_series.category_data = "A1:A16" | |
# Turn off legend | |
chart.show_legend = False | |
# Access data labels | |
dataLabels = chart.n_series[0].data_labels | |
# Turn on category names | |
dataLabels.show_category_name = True | |
# Turn on percentage format | |
dataLabels.show_percentage = True | |
# Set position | |
dataLabels.position = LabelPositionType.OUTSIDE_END | |
# Set separator | |
dataLabels.separator_type = DataLabelsSeparatorType.COMMA |
到目前为止,我们已经创建了饼图并设置了其不同的方面。现在我们将为图表打开引导线。请注意,为了显示引导线,我们必须稍微移动数据标签的位置。
以下代码片段打开了引导线,刷新了图表,然后计算数据标签的位置,以相应地移动它们。
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, Workbook | |
from aspose.cells.charts import ChartType, DataLabelsSeparatorType, LabelPositionType | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Create an instance of Workbook in XLSX format | |
workbook = Workbook(FileFormatType.XLSX) | |
# Access the first worksheet | |
worksheet = workbook.worksheets[0] | |
# Add two columns of data | |
worksheet.cells.get("A1").put_value("Retail") | |
worksheet.cells.get("A2").put_value("Services") | |
worksheet.cells.get("A3").put_value("Info & Communication") | |
worksheet.cells.get("A4").put_value("Transport Equip") | |
worksheet.cells.get("A5").put_value("Construction") | |
worksheet.cells.get("A6").put_value("Other Products") | |
worksheet.cells.get("A7").put_value("Wholesale") | |
worksheet.cells.get("A8").put_value("Land Transport") | |
worksheet.cells.get("A9").put_value("Air Transport") | |
worksheet.cells.get("A10").put_value("Electric Appliances") | |
worksheet.cells.get("A11").put_value("Securities") | |
worksheet.cells.get("A12").put_value("Textiles & Apparel") | |
worksheet.cells.get("A13").put_value("Machinery") | |
worksheet.cells.get("A14").put_value("Metal Products") | |
worksheet.cells.get("A15").put_value("Cash") | |
worksheet.cells.get("A16").put_value("Banks") | |
worksheet.cells.get("B1").put_value(10.4) | |
worksheet.cells.get("B2").put_value(5.2) | |
worksheet.cells.get("B3").put_value(6.4) | |
worksheet.cells.get("B4").put_value(10.4) | |
worksheet.cells.get("B5").put_value(7.9) | |
worksheet.cells.get("B6").put_value(4.1) | |
worksheet.cells.get("B7").put_value(3.5) | |
worksheet.cells.get("B8").put_value(5.7) | |
worksheet.cells.get("B9").put_value(3) | |
worksheet.cells.get("B10").put_value(14.7) | |
worksheet.cells.get("B11").put_value(3.6) | |
worksheet.cells.get("B12").put_value(2.8) | |
worksheet.cells.get("B13").put_value(7.8) | |
worksheet.cells.get("B14").put_value(2.4) | |
worksheet.cells.get("B15").put_value(1.8) | |
worksheet.cells.get("B16").put_value(10.1) | |
# Create a pie chart and add it to the collection of charts | |
id = worksheet.charts.add(ChartType.PIE, 3, 3, 23, 13) | |
# Access newly created Chart instance | |
chart = worksheet.charts[id] | |
# Set series data range | |
chart.n_series.add("B1:B16", True) | |
# Set category data range | |
chart.n_series.category_data = "A1:A16" | |
# Turn off legend | |
chart.show_legend = False | |
# Access data labels | |
dataLabels = chart.n_series[0].data_labels | |
# Turn on category names | |
dataLabels.show_category_name = True | |
# Turn on percentage format | |
dataLabels.show_percentage = True | |
# Set position | |
dataLabels.position = LabelPositionType.OUTSIDE_END | |
# Set separator | |
dataLabels.separator_type = DataLabelsSeparatorType.COMMA | |
# Turn on leader lines | |
chart.n_series[0].has_leader_lines = True | |
# Calculete chart | |
chart.calculate() | |
# You need to move DataLabels a little leftward or rightward depending on their position to show leader lines | |
DELTA = 100 | |
for i in range(chart.n_series[0].points.count): | |
X = chart.n_series[0].points[i].data_labels.x | |
# If it is greater than 2000, then move the X position a little right otherwise move the X position a little left | |
if X > 2000: | |
chart.n_series[0].points[i].data_labels.x = X + DELTA | |
else: | |
chart.n_series[0].points[i].data_labels.x = X - DELTA |
最后,以下代码将图表保存为图像格式,将工作簿保存为XLSX格式。
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, Workbook | |
from aspose.cells.charts import ChartType, DataLabelsSeparatorType, LabelPositionType | |
from aspose.cells.drawing import ImageType | |
from aspose.cells.rendering import ImageOrPrintOptions | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Create an instance of Workbook in XLSX format | |
workbook = Workbook(FileFormatType.XLSX) | |
# Access the first worksheet | |
worksheet = workbook.worksheets[0] | |
# Add two columns of data | |
worksheet.cells.get("A1").put_value("Retail") | |
worksheet.cells.get("A2").put_value("Services") | |
worksheet.cells.get("A3").put_value("Info & Communication") | |
worksheet.cells.get("A4").put_value("Transport Equip") | |
worksheet.cells.get("A5").put_value("Construction") | |
worksheet.cells.get("A6").put_value("Other Products") | |
worksheet.cells.get("A7").put_value("Wholesale") | |
worksheet.cells.get("A8").put_value("Land Transport") | |
worksheet.cells.get("A9").put_value("Air Transport") | |
worksheet.cells.get("A10").put_value("Electric Appliances") | |
worksheet.cells.get("A11").put_value("Securities") | |
worksheet.cells.get("A12").put_value("Textiles & Apparel") | |
worksheet.cells.get("A13").put_value("Machinery") | |
worksheet.cells.get("A14").put_value("Metal Products") | |
worksheet.cells.get("A15").put_value("Cash") | |
worksheet.cells.get("A16").put_value("Banks") | |
worksheet.cells.get("B1").put_value(10.4) | |
worksheet.cells.get("B2").put_value(5.2) | |
worksheet.cells.get("B3").put_value(6.4) | |
worksheet.cells.get("B4").put_value(10.4) | |
worksheet.cells.get("B5").put_value(7.9) | |
worksheet.cells.get("B6").put_value(4.1) | |
worksheet.cells.get("B7").put_value(3.5) | |
worksheet.cells.get("B8").put_value(5.7) | |
worksheet.cells.get("B9").put_value(3) | |
worksheet.cells.get("B10").put_value(14.7) | |
worksheet.cells.get("B11").put_value(3.6) | |
worksheet.cells.get("B12").put_value(2.8) | |
worksheet.cells.get("B13").put_value(7.8) | |
worksheet.cells.get("B14").put_value(2.4) | |
worksheet.cells.get("B15").put_value(1.8) | |
worksheet.cells.get("B16").put_value(10.1) | |
# Create a pie chart and add it to the collection of charts | |
id = worksheet.charts.add(ChartType.PIE, 3, 3, 23, 13) | |
# Access newly created Chart instance | |
chart = worksheet.charts[id] | |
# Set series data range | |
chart.n_series.add("B1:B16", True) | |
# Set category data range | |
chart.n_series.category_data = "A1:A16" | |
# Turn off legend | |
chart.show_legend = False | |
# Access data labels | |
dataLabels = chart.n_series[0].data_labels | |
# Turn on category names | |
dataLabels.show_category_name = True | |
# Turn on percentage format | |
dataLabels.show_percentage = True | |
# Set position | |
dataLabels.position = LabelPositionType.OUTSIDE_END | |
# Set separator | |
dataLabels.separator_type = DataLabelsSeparatorType.COMMA | |
# In order to save the chart image, create an instance of ImageOrPrintOptions | |
anOption = ImageOrPrintOptions() | |
# Set image format | |
anOption.image_type = ImageType.PNG | |
# Set resolution | |
anOption.horizontal_resolution = 200 | |
anOption.vertical_resolution = 200 | |
# Render chart to image | |
chart.to_image(dataDir + "output_out.png", anOption) | |
# Save the workbook to see chart inside the Excel | |
workbook.save(dataDir + "output_out.xlsx") |
结果饼图 |
---|
![]() |