Diagrammdarstellung einstellen
Diagrammaussehen festlegen
Beim Erstellen eines Diagramms geben wir eine kurze Einführung in die Arten der von Aspose.Cells für Python via .NET angebotenen Diagramme und Diagrammobjekte und beschreiben, wie man eines erstellt. Dieser Artikel behandelt, wie man das Erscheinungsbild von Diagrammen anpasst, indem man ihre Eigenschaften festlegt:
- Festlegen des Diagrammbereichs.
- Festlegen von Diagrammlinien.
- Anwenden von Designs.
- Titel für Diagramme und Achsen festlegen.
- Arbeiten mit Gitterlinien.
Diagrammbereich einstellen
Es gibt verschiedene Bereiche in einem Diagramm, und Aspose.Cells für Python via .NET bietet die Flexibilität, das Erscheinungsbild jedes Bereichs anzupassen. Entwickler können verschiedene Formatierungseinstellungen auf einen Bereich anwenden, indem sie seine Vordergrundfarbe, Hintergrundfarbe und Füllformat ändern.
Im untenstehenden Beispiel haben wir verschiedene Formatierungseinstellungen auf verschiedene Arten von Bereichen eines Diagramms angewendet. Diese Bereiche umfassen:
- Plot-Bereich
- Diagrammbereich
- SeriesCollection Bereich
- Fläche eines einzelnen Punktes in einer SeriesCollection
Der folgende Codeschnipsel zeigt, wie der Diagrammbereich festgelegt wird.
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") |
Einstellen von Diagramm Linien
Entwickler können auch verschiedene Stile auf Linien oder Datenmarkierungen der SeriesCollection anwenden. Das folgende Codebeispiel zeigt, wie man Diagrammlinien mit Aspose.Cells für Python via .NET festlegt.
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") |
Anwendung von Microsoft Excel 2007/2010-Themen auf Diagramme
Entwickler können verschiedene Microsoft Excel-Themen/Farben auf SeriesCollection oder andere Diagrammobjekte anwenden, wie im Beispiel gezeigt.
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") |
Einstellen der Titel von Diagrammen oder Achsen
Sie können Microsoft Excel verwenden, um die Titel eines Diagramms und seiner Achsen in einer WYSIWYG-Umgebung festzulegen. Aspose.Cells für Python via .NET erlaubt Entwicklern auch, die Titel eines Diagramms und seiner Achsen zur Laufzeit festzulegen. Alle Diagramme und ihre Achsen enthalten eine Chart.title-Eigenschaft, mit der ihre Titel eingestellt werden können, wie im Beispiel gezeigt.
Der folgende Codeausschnitt zeigt, wie Titel für Diagramme und Achsen festgelegt werden können.
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") |
Arbeiten mit Haupt-Gitterlinien
Es ist möglich, das Aussehen der Haupt-Gitterlinien anzupassen. Gitterlinien ausblenden oder anzeigen, oder ihre Farbe und andere Einstellungen definieren. Nachfolgend zeigen wir, wie die Gitterlinien ausgeblendet und wie ihre Farbe geändert wird.
Ausblenden von Haupt-Gitterlinien
Entwickler können die Sichtbarkeit der Haupt-Gitterlinien steuern, indem sie die Eigenschaft is_visible des Line-Objekts auf true oder false setzen.
Der folgende Codeschnipsel zeigt, wie Haupt-Gitterlinien verborgen werden. Nach dem Ausblenden der Haupt-Gitterlinien wird dem Arbeitsblatt ein Säulendiagramm hinzugefügt, das keine Gitterlinien hat.
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") |
Ändern der Einstellungen für Haupt-Gitterlinien
Entwickler können nicht nur die Sichtbarkeit der Haupt-Gitterlinien, sondern auch andere Eigenschaften wie deren Farbe usw. steuern.
Der folgende Codeschnipsel zeigt, wie die Farbe der Haupt-Gitterlinien geändert wird. Nachdem die Farbe der Haupt-Gitterlinien festgelegt wurde, wird dem Arbeitsblatt ein Säulendiagramm mit geänderten Gitterlinien hinzugefügt.
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") |