チャートの外観を設定する

グラフの外観設定

「チャートの作成方法」では、Aspose.Cells for Python via .NETが提供するチャートタイプの概要と、それらを作成・カスタマイズしてワークシートを強化する方法について説明しました。この記事では、これらのプロパティを設定してチャートの外観をカスタマイズする方法について詳述します。

  • チャートエリアの設定。
  • チャートラインの設定。
  • テーマの適用。
  • チャートと軸のタイトルの設定。
  • グリッド線の操作。

チャートエリアの設定

チャートにはさまざまな種類があり、Aspose.Cells for Python via .NETは各種の外観を変更する柔軟性を提供します。開発者は、前景色、背景色、塗りつぶし形式などを変更することで、それぞれのエリアの書式設定を適用できます。

以下の例では、チャートの異なる種類のエリアに異なる書式設定を適用しました。

  • プロットエリア
  • チャートエリア
  • SeriesCollectionエリア
  • SeriesCollection内の単一のポイントのエリア

以下のコードスニペットは、チャートエリアを設定する方法を示しています。

from aspose.cells import Workbook
from aspose.cells.charts import ChartType
from aspose.cells.drawing import GradientStyleType
from aspose.pydrawing import Color
from os import os, path
# 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 directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Workbook object
sheetIndex = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[sheetIndex]
# Adding sample values to cells
worksheet.cells.get("A1").put_value(50)
worksheet.cells.get("A2").put_value(100)
worksheet.cells.get("A3").put_value(150)
worksheet.cells.get("B1").put_value(60)
worksheet.cells.get("B2").put_value(32)
worksheet.cells.get("B3").put_value(50)
# Adding a chart to the worksheet
chartIndex = worksheet.charts.add(ChartType.COLUMN, 5, 0, 15, 5)
# Accessing the instance of the newly added chart
chart = worksheet.charts[chartIndex]
# Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.n_series.add("A1:B3", True)
# Setting the foreground color of the plot area
chart.plot_area.area.foreground_color = Color.blue
# Setting the foreground color of the chart area
chart.chart_area.area.foreground_color = Color.yellow
# Setting the foreground color of the 1st SeriesCollection area
chart.n_series[0].area.foreground_color = Color.red
# Setting the foreground color of the area of the 1st SeriesCollection point
chart.n_series[0].points[0].area.foreground_color = Color.cyan
# Filling the area of the 2nd SeriesCollection with a gradient
chart.n_series[1].area.fill_format.set_one_color_gradient(Color.lime, 1, GradientStyleType.HORIZONTAL, 1)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

チャートラインの設定

開発者はまた、SeriesCollectionの線やデータマーカーに異なるスタイルを適用することも可能です。以下のコードスニペットは、Aspose.Cells for Python via .NET APIを使用してチャートの線を設定する方法を示しています。

from aspose.cells import Workbook
from aspose.cells.charts import ChartMarkerType, ChartType
from aspose.cells.drawing import GradientStyleType, LineType, WeightType
from aspose.pydrawing import Color
from os import os, path
# 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 directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Workbook object
sheetIndex = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[sheetIndex]
# Adding sample values to cells
worksheet.cells.get("A1").put_value(50)
worksheet.cells.get("A2").put_value(100)
worksheet.cells.get("A3").put_value(150)
worksheet.cells.get("B1").put_value(60)
worksheet.cells.get("B2").put_value(32)
worksheet.cells.get("B3").put_value(50)
# Adding a chart to the worksheet
chartIndex = worksheet.charts.add(ChartType.COLUMN, 5, 0, 15, 5)
# Accessing the instance of the newly added chart
chart = worksheet.charts[chartIndex]
# Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.n_series.add("A1:B3", True)
# Setting the foreground color of the plot area
chart.plot_area.area.foreground_color = Color.blue
# Setting the foreground color of the chart area
chart.chart_area.area.foreground_color = Color.yellow
# Setting the foreground color of the 1st SeriesCollection area
chart.n_series[0].area.foreground_color = Color.red
# Setting the foreground color of the area of the 1st SeriesCollection point
chart.n_series[0].points[0].area.foreground_color = Color.cyan
# Filling the area of the 2nd SeriesCollection with a gradient
chart.n_series[1].area.fill_format.set_one_color_gradient(Color.lime, 1, GradientStyleType.HORIZONTAL, 1)
# Applying a dotted line style on the lines of a SeriesCollection
chart.n_series[0].border.style = LineType.DOT
# Applying a triangular marker style on the data markers of a SeriesCollection
chart.n_series[0].marker.marker_style = ChartMarkerType.TRIANGLE
# Setting the weight of all lines in a SeriesCollection to medium
chart.n_series[1].border.weight = WeightType.MEDIUM_LINE
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

Microsoft Excel 2007/2010のテーマをチャートに適用

開発者は、SeriesCollectionや他のチャートオブジェクトにMicrosoft Excelのテーマや色を適用できます。以下の例に示す通りです。

from aspose.cells import ThemeColor, ThemeColorType, Workbook
from aspose.cells.drawing import FillType
# 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(".")
# Instantiate the workbook to open the file that contains a chart
workbook = Workbook(dataDir + "book1.xlsx")
# Get the first worksheet
worksheet = workbook.worksheets[1]
# Get the first chart in the sheet
chart = worksheet.charts[0]
# Specify the FilFormat's type to Solid Fill of the first series
chart.n_series[0].area.fill_format.fill_type = FillType.SOLID
# Get the CellsColor of SolidFill
cc = chart.n_series[0].area.fill_format.solid_fill.cells_color
# Create a theme in Accent style
cc.theme_color = ThemeColor(ThemeColorType.ACCENT6, 0.6)
# Apply the them to the series
chart.n_series[0].area.fill_format.solid_fill.cells_color = cc
# Save the Excel file
workbook.save(dataDir + "output.out.xlsx")

チャートや軸のタイトルの設定

Microsoft Excelを使って、チャートや軸のタイトルをWYSIWYG環境で設定できます。Aspose.Cells for Python via .NETでは、開発者が実行時にチャートや軸のタイトルを設定することも可能です。すべてのチャートと軸は Chart.title プロパティを持ち、それを使用してタイトルを設定できます。例を次に示します。

次のコードスニペットは、チャートや軸にタイトルを設定する方法を示しています。

from aspose.cells import Workbook
from aspose.cells.charts import ChartType
from aspose.cells.drawing import GradientStyleType
from aspose.pydrawing import Color
from os import os, path
# 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 directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Workbook object
sheetIndex = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[sheetIndex]
# Adding sample values to cells
worksheet.cells.get("A1").put_value(50)
worksheet.cells.get("A2").put_value(100)
worksheet.cells.get("A3").put_value(150)
worksheet.cells.get("B1").put_value(60)
worksheet.cells.get("B2").put_value(32)
worksheet.cells.get("B3").put_value(50)
# Adding a chart to the worksheet
chartIndex = worksheet.charts.add(ChartType.COLUMN, 5, 0, 15, 5)
# Accessing the instance of the newly added chart
chart = worksheet.charts[chartIndex]
# Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.n_series.add("A1:B3", True)
# Setting the foreground color of the plot area
chart.plot_area.area.foreground_color = Color.blue
# Setting the foreground color of the chart area
chart.chart_area.area.foreground_color = Color.yellow
# Setting the foreground color of the 1st SeriesCollection area
chart.n_series[0].area.foreground_color = Color.red
# Setting the foreground color of the area of the 1st SeriesCollection point
chart.n_series[0].points[0].area.foreground_color = Color.cyan
# Filling the area of the 2nd SeriesCollection with a gradient
chart.n_series[1].area.fill_format.set_one_color_gradient(Color.lime, 1, GradientStyleType.HORIZONTAL, 1)
# Setting the title of a chart
chart.title.text = "Title"
# Setting the font color of the chart title to blue
chart.title.font.color = Color.blue
# Setting the title of category axis of the chart
chart.category_axis.title.text = "Category"
# Setting the title of value axis of the chart
chart.value_axis.title.text = "Value"
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

主な目盛りの操作

主な目盛りの外観をカスタマイズできます。目盛りを非表示にしたり表示したり、その色やその他の設定を定義できます。以下では、目盛りを非表示にする方法と色を変更する方法を説明します。

メジャーグリッドラインの非表示

開発者は、Line オブジェクトの is_visible プロパティを true または false に設定して、主要なグリッド線の表示/非表示を制御できます。

次のコードスニペットは、メジャーグリッド線を非表示にする方法を示しています。メジャーグリッド線を非表示にした後、ワークシートに列のチャートが追加され、グリッド線がありません。

from aspose.cells import Workbook
from aspose.cells.charts import ChartType
from aspose.cells.drawing import GradientStyleType
from aspose.pydrawing import Color
from os import os, path
# 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 directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Workbook object
sheetIndex = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[sheetIndex]
# Adding sample values to cells
worksheet.cells.get("A1").put_value(50)
worksheet.cells.get("A2").put_value(100)
worksheet.cells.get("A3").put_value(150)
worksheet.cells.get("B1").put_value(60)
worksheet.cells.get("B2").put_value(32)
worksheet.cells.get("B3").put_value(50)
# Adding a chart to the worksheet
chartIndex = worksheet.charts.add(ChartType.COLUMN, 5, 0, 15, 5)
# Accessing the instance of the newly added chart
chart = worksheet.charts[chartIndex]
# Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.n_series.add("A1:B3", True)
# Setting the foreground color of the plot area
chart.plot_area.area.foreground_color = Color.blue
# Setting the foreground color of the chart area
chart.chart_area.area.foreground_color = Color.yellow
# Setting the foreground color of the 1st SeriesCollection area
chart.n_series[0].area.foreground_color = Color.red
# Setting the foreground color of the area of the 1st SeriesCollection point
chart.n_series[0].points[0].area.foreground_color = Color.cyan
# Filling the area of the 2nd SeriesCollection with a gradient
chart.n_series[1].area.fill_format.set_one_color_gradient(Color.lime, 1, GradientStyleType.HORIZONTAL, 1)
# Hiding the major gridlines of Category Axis
chart.category_axis.major_grid_lines.is_visible = False
# Hiding the major gridlines of Value Axis
chart.value_axis.major_grid_lines.is_visible = False
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

メジャーグリッドラインの設定変更

開発者は、メジャーグリッド線の可視性だけでなく、色などの他のプロパティも制御できます。

次のコードスニペットは、メジャーグリッド線の色を変更する方法を示しています。メジャーグリッド線の色を設定した後、ワークシートに変更されたグリッド線を持つ列チャートが追加されます。

from aspose.cells import Workbook
from aspose.cells.charts import ChartType
from aspose.cells.drawing import GradientStyleType
from aspose.pydrawing import Color
from os import os, path
# 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 directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Workbook object
sheetIndex = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[sheetIndex]
# Adding sample values to cells
worksheet.cells.get("A1").put_value(50)
worksheet.cells.get("A2").put_value(100)
worksheet.cells.get("A3").put_value(150)
worksheet.cells.get("B1").put_value(60)
worksheet.cells.get("B2").put_value(32)
worksheet.cells.get("B3").put_value(50)
# Adding a chart to the worksheet
chartIndex = worksheet.charts.add(ChartType.COLUMN, 5, 0, 15, 5)
# Accessing the instance of the newly added chart
chart = worksheet.charts[chartIndex]
# Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.n_series.add("A1:B3", True)
# Setting the foreground color of the plot area
chart.plot_area.area.foreground_color = Color.blue
# Setting the foreground color of the chart area
chart.chart_area.area.foreground_color = Color.yellow
# Setting the foreground color of the 1st SeriesCollection area
chart.n_series[0].area.foreground_color = Color.red
# Setting the foreground color of the area of the 1st SeriesCollection point
chart.n_series[0].points[0].area.foreground_color = Color.cyan
# Filling the area of the 2nd SeriesCollection with a gradient
chart.n_series[1].area.fill_format.set_one_color_gradient(Color.lime, 1, GradientStyleType.HORIZONTAL, 1)
# Setting the color of Category Axis' major gridlines to silver
chart.category_axis.major_grid_lines.color = Color.silver
# Setting the color of Value Axis' major gridlines to red
chart.value_axis.major_grid_lines.color = Color.red
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

高度なトピック