Paramétrer l apparence du graphique
Réglage de l’apparence du graphique
Dans ‘Comment créer un graphique’, nous avons donné une brève introduction aux types de graphiques et aux objets de graphique proposés par Aspose.Cells pour Python via .NET, et décrit comment en créer un. Cet article explique comment personnaliser l’apparence des graphiques en réglant leurs propriétés :
- Définir la zone du graphique.
- Définition des lignes du graphique.
- Application de thèmes.
- Définition des titres des graphiques et des axes.
- Travailler avec des lignes de repère.
Définition de la zone du graphique
Il existe différents types de zones dans un graphique et Aspose.Cells pour Python via .NET offre la flexibilité pour modifier l’apparence de chaque zone. Les développeurs peuvent appliquer différents réglages de mise en forme en changeant leur couleur de premier plan, leur couleur d’arrière-plan et leur format de remplissage, etc.
Dans l’exemple ci-dessous, nous avons appliqué différents paramètres de mise en forme sur différents types de zones d’un graphique. Ces zones comprennent :
- Zone de traçage
- Zone du graphique Zone de SeriesCollection
- Zone d’un point unique dans une SeriesCollection
Le code suivant montre comment définir la zone du graphique.
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") |
Définition des lignes du graphique
Les développeurs peuvent également appliquer différents styles aux lignes ou aux marqueurs de données de la SeriesCollection. Le code suivant illustre comment définir les lignes du graphique en utilisant l’API Aspose.Cells pour Python via .NET.
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") |
Application des thèmes Microsoft Excel 2007/2010 aux graphiques
Les développeurs peuvent appliquer différents thèmes/couleurs Microsoft Excel à la SeriesCollection ou à d’autres objets de graphique, comme illustré dans l’exemple ci-dessous.
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") |
Configuration des titres des graphiques ou des axes
Vous pouvez utiliser Microsoft Excel pour définir les titres d’un graphique et de ses axes dans un environnement WYSIWYG. Aspose.Cells pour Python via .NET permet également aux développeurs de définir les titres d’un graphique et de ses axes en temps réel. Tous les graphiques et leurs axes contiennent une propriété Chart.title qui peut être utilisée pour définir leurs titres comme dans l’exemple ci-dessous.
Le snippet de code suivant démontre comment définir des titres pour les graphiques et les axes.
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") |
Travailler avec les grandes lignes de la grille
Il est possible de personnaliser l’apparence des grandes lignes de la grille. Masquer ou afficher les lignes de la grille, ou définir leur couleur et d’autres paramètres. Ci-dessous, nous montrons comment masquer les lignes de la grille et comment changer leur couleur.
Masquer les grandes lignes de la grille
Les développeurs peuvent contrôler la visibilité des lignes de grille principales en réglant la propriété is_visible de l’objet Line sur true ou false.
Le code suivant montre comment masquer les grandes lignes de la grille. Après avoir masqué les grandes lignes de la grille, un graphique en colonnes sera ajouté à la feuille de calcul sans lignes de grille.
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") |
Changer les paramètres des grandes lignes de la grille
Les développeurs peuvent non seulement contrôler la visibilité des grandes lignes de la grille, mais aussi d’autres propriétés comme sa couleur, etc.
Le code suivant montre comment changer la couleur des grandes lignes de la grille. Après avoir défini la couleur des grandes lignes de la grille, un graphique en colonnes sera ajouté à la feuille de calcul avec des lignes de grille modifiées.
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") |