Creating Pie Chart with Leader Lines
To demonstrate the usage of Aspose.Cells for Python via .NET API to create a pie chart with leader lines, we will first create a new Workbook and input some data that will serve as the series data source. Once the data is in place, we will add a Chart of type ChartType.PIE to the collection of charts and set its different aspects to get the desired chart view.
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 |
So far we have created a pie chart and set its different aspects. Now we are going to turn on the leader lines for the chart. Please note, to show the leader lines, we have to move the data labels a little.
The following piece of code turns on the leader lines, refresh the chart, and then calculates the data labels' positions to move them accordingly.
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 |
Finally, the following code saves the chart in image format and the workbook in XLSX format.
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") |
Resultant Pie Chart |
---|
![]() |
Advance topics
- Custom Slice or Sector Colors in Pie Chart
- Find if Data Points are in the Second Pie or Bar on a Pie of Pie or Bar of Pie Chart