Creazione di un Grafico a Torta con Linee di Guida

Per dimostrare l’uso dell’API Aspose.Cells per Python via .NET per creare un grafico a torta con linee di collegamento, prima creeremo un nuovo Workbook e inseriremo alcuni dati che serviranno come fonte della serie di dati. Una volta che i dati sono in posizione, aggiungeremo un Chart di tipo ChartType.PIE alla collezione di grafici e imposteremo i suoi aspetti diversi per ottenere la visualizzazione desiderata del grafico.

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

Finora abbiamo creato un grafico a torta e ne abbiamo impostato gli aspetti differenti. Ora stiamo per attivare i capi di linea per il grafico. Si noti, per mostrare i capi di linea, dobbiamo spostare leggermente le etichette dei dati.

Il seguente pezzo di codice attiva i capi di linea, aggiorna il grafico e quindi calcola le posizioni delle etichette dei dati per spostarle di conseguenza.

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

Infine, il seguente codice salva il grafico in formato immagine e il workbook in formato 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")
Grafico a torta risultante
todo:image_alt_text

Argomenti avanzati

Articoli correlati