リーダーライン付きの円グラフの作成
Aspose.Cells for Python via .NET APIを使用してリーダー線付きの円グラフを作成する方法を示すために、まず新しい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") |
作成された円グラフ |
---|
![]() |