إنشاء رسم بياني دائري مع خطوط الريادة

لعرض كيفية استخدام API الخاص بـ Aspose.Cells للبايثون via .NET لإنشاء مخطط دائري مع خطوط قيادية، سنبدأ بإنشاء Workbook جديد وإدخال بعض البيانات التي ستعمل كمصدر بيانات السلسلة. بمجرد وضع البيانات، سنضيف Chart من نوع ChartType.PIE إلى مجموعة الرسوم البيانية ونقوم بضبط خصائصها المختلفة للحصول على العرض المطلوب للرسم.

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

حتى الآن قمنا بإنشاء رسم بياني دائري وضبط جوانبه المختلفة. الآن سنقوم بتشغيل خطوط الريادة للرسم البياني. يرجى ملاحظة أنه لعرض خطوط الريادة، علينا نقل تسميات البيانات قليلاً.

يقوم الكود التالي بتشغيل خطوط الريادة، يحدث الرسم البياني، ومن ثم يحسب مواقع تسميات البيانات لنقلها وفقًا لذلك.

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.

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")
الرسم البياني الناتج
todo:image_alt_text

مواضيع متقدمة

مقالات ذات صلة