Establecer fuente de datos para el gráfico

En nuestros temas anteriores, ya hemos proporcionado muchos ejemplos para demostrar cómo puede establecer una fuente de datos para su gráfico, pero en este tema, vamos a proporcionar más detalles sobre los tipos de datos que se pueden establecer para un gráfico.

Establecer Datos del Gráfico

Hay dos tipos de datos con los que trabajar al crear gráficos usando Aspose.Cells para Python via .NET:

  • Datos del gráfico.
  • Datos de categoría.

Datos del Gráfico

Los datos del gráfico son los datos que usamos como fuente de datos para construir nuestros gráficos. Podemos agregar un rango de celdas (que contienen datos del gráfico) llamando al método add del objeto SeriesCollection.

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(170)
worksheet.cells.get("A4").put_value(300)
worksheet.cells.get("B1").put_value(160)
worksheet.cells.get("B2").put_value(32)
worksheet.cells.get("B3").put_value(50)
worksheet.cells.get("B4").put_value(40)
# Adding sample values to cells as category data
worksheet.cells.get("C1").put_value("Q1")
worksheet.cells.get("C2").put_value("Q2")
worksheet.cells.get("C3").put_value("Y1")
worksheet.cells.get("C4").put_value("Y2")
# 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 "B4"
chart.n_series.add("A1:B4", True)
# Saving the Excel file
workbook.save(dataDir + "output.xls")

Datos de Categoría

Los datos de categoría se utilizan para la etiquetación de los datos del gráfico y se pueden agregar a SeriesCollection mediante su propiedad category_data. A continuación se muestra un ejemplo completo para demostrar el uso de datos del gráfico y de categoría. Después de ejecutar el código de ejemplo anterior, se añadirá un gráfico de columnas a la hoja de cálculo como se muestra a continuación.

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(10)
worksheet.cells.get("A2").put_value(100)
worksheet.cells.get("A3").put_value(170)
worksheet.cells.get("A4").put_value(200)
worksheet.cells.get("B1").put_value(120)
worksheet.cells.get("B2").put_value(320)
worksheet.cells.get("B3").put_value(50)
worksheet.cells.get("B4").put_value(40)
# Adding sample values to cells as category data
worksheet.cells.get("C1").put_value("Q1")
worksheet.cells.get("C2").put_value("Q2")
worksheet.cells.get("C3").put_value("Y1")
worksheet.cells.get("C4").put_value("Y2")
# 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 "B4"
chart.n_series.add("A1:B4", True)
# Setting the data source for the category data of SeriesCollection
chart.n_series.category_data = "C1:C4"
# Saving the Excel file
workbook.save(dataDir + "output.xls")

Temas avanzados