Manipular la Posición, Tamaño y Diseño del Gráfico
Posición y Tamaño del Gráfico
A veces, quieres cambiar la posición o tamaño del gráfico nuevo o existente dentro de la hoja de cálculo. Aspose.Cells para Python via .NET proporciona la propiedad Chart.chart_object para lograr esto. Puedes usar sus subpropiedades para redimensionar el gráfico con una nueva altura y anchura o reubicarlo con nuevas coordenadas X y Y.
Controlar la Posición y Tamaño del Gráfico
Para cambiar la posición (coordenadas X, Y) o el tamaño (altura, ancho) del gráfico, use estas propiedades:
El siguiente ejemplo explica cómo usar las APIs anteriores, carga el libro existente que contiene un gráfico en su primera hoja y luego lo redimensiona y reubica usando Aspose.Cells para Python via .NET.
from aspose.cells import Workbook | |
# 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(".") | |
workbook = Workbook(dataDir + "chart.xls") | |
worksheet = workbook.worksheets[1] | |
# Load the chart from source worksheet | |
chart = worksheet.charts[0] | |
# Resize the chart | |
chart.chart_object.width = 400 | |
chart.chart_object.height = 300 | |
# Reposition the chart | |
chart.chart_object.x = 250 | |
chart.chart_object.y = 150 | |
# Output the file | |
workbook.save(dataDir + "chart.out.xls") |
Manipulación de gráficos de diseñador
Hay momentos en que necesitas manipular o modificar gráficos en archivos de plantilla de diseño. Aspose.Cells para Python via .NET soporta completamente la manipulación de contenidos y elementos de gráficos de diseño. Los datos, contenidos del gráfico, imagen de fondo y formatos pueden ser preservados con precisión.
Manipulación de gráficos de diseñador en archivos de plantillas
Para manipular gráficos de diseñador en archivos de plantillas, utiliza la API relacionada con los gráficos. Por ejemplo, puedes usar la propiedad Worksheet.Charts para obtener la colección de gráficos existentes en el archivo de plantilla.
Creación de un gráfico
El siguiente ejemplo muestra cómo crear un gráfico de pirámide. Más adelante manipularemos este gráfico.
from aspose.cells import Workbook | |
from aspose.cells.charts import ChartType | |
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 Excel 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(4) | |
worksheet.cells.get("B2").put_value(20) | |
worksheet.cells.get("B3").put_value(50) | |
# Adding a chart to the worksheet | |
chartIndex = worksheet.charts.add(ChartType.PYRAMID, 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) | |
# Saving the Excel file | |
workbook.save(dataDir + "book1.out.xls") |
Manipulación del gráfico
El siguiente ejemplo muestra cómo manipular el gráfico existente. En este ejemplo, modificamos el gráfico creado anteriormente. En la salida generada, se observa que la etiqueta de fecha de un punto de datos se ha establecido en ‘Reino Unido, 30K’.
from aspose.cells import Workbook | |
# 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(".") | |
# Open the existing file. | |
workbook = Workbook(dataDir + "piechart.xls") | |
# Get the designer chart in the second sheet. | |
sheet = workbook.worksheets[1] | |
chart = sheet.charts[0] | |
# Get the data labels in the data series of the third data point. | |
datalabels = chart.n_series[0].points[2].data_labels | |
# Change the text of the label. | |
datalabels.text = "Unided Kingdom, 400K " | |
# Save the excel file. | |
workbook.save(dataDir + "output.xls") |
Manipulación de un gráfico de líneas en la plantilla de diseñador
En este ejemplo, manipularemos un gráfico de líneas. Agregaremos algunas series de datos al gráfico existente y cambiaremos sus colores de línea.
from aspose.cells import Workbook | |
from aspose.pydrawing import Color | |
# 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(".") | |
# Open the existing file. | |
workbook = Workbook(dataDir + "Book1.xlsx") | |
# Get the designer chart in the first worksheet. | |
chart = workbook.worksheets[0].charts[0] | |
# Add the third data series to it. | |
chart.n_series.add("{60, 80, 10}", True) | |
# Add another data series (fourth) to it. | |
chart.n_series.add("{0.3, 0.7, 1.2}", True) | |
# Plot the fourth data series on the second axis. | |
chart.n_series[3].plot_on_second_axis = True | |
# Change the Border color of the second data series. | |
chart.n_series[1].border.color = Color.green | |
# Change the Border color of the third data series. | |
chart.n_series[2].border.color = Color.red | |
# Make the second value axis visible. | |
chart.second_value_axis.is_visible = True | |
# Save the excel file. | |
workbook.save(dataDir + "output.xls") |