Manipular la Posición, Tamaño y Diseño del Gráfico
Posición y Tamaño del Gráfico
A veces, desea cambiar la posición o tamaño del gráfico nuevo o existente dentro de la hoja de cálculo. Aspose.Cells proporciona la propiedad Chart.ChartObject para lograr esto. Puede utilizar sus subpropiedades para cambiar el tamaño del gráfico con una nueva altura y ancho o reposicionarlo 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 el uso de las API anteriores. Carga el libro existente que contiene un gráfico en su primera hoja de trabajo. Luego redimensiona y reposiciona el gráfico utilizando Aspose.Cells.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
Workbook workbook = new Workbook(dataDir + "chart.xls"); | |
Worksheet worksheet = workbook.Worksheets[1]; | |
// Load the chart from source worksheet | |
Chart chart = worksheet.Charts[0]; | |
// Resize the chart | |
chart.ChartObject.Width = 400; | |
chart.ChartObject.Height = 300; | |
// Reposition the chart | |
chart.ChartObject.X = 250; | |
chart.ChartObject.Y = 150; | |
// Output the file | |
workbook.Save(dataDir + "chart.out.xls"); |
Manipulación de gráficos de diseñador
Hay momentos en los que necesitas manipular o modificar gráficos en archivos de plantillas de diseñador. Aspose.Cells es compatible con la manipulación de contenidos y elementos de gráficos de diseñador. Los datos, los contenidos del gráfico, la imagen de fondo y los formatos se pueden preservar 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.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Excel object | |
int sheetIndex = workbook.Worksheets.Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[sheetIndex]; | |
// Adding sample values to cells | |
worksheet.Cells["A1"].PutValue(50); | |
worksheet.Cells["A2"].PutValue(100); | |
worksheet.Cells["A3"].PutValue(150); | |
worksheet.Cells["B1"].PutValue(4); | |
worksheet.Cells["B2"].PutValue(20); | |
worksheet.Cells["B3"].PutValue(50); | |
// Adding a chart to the worksheet | |
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Pyramid, 5, 0, 15, 5); | |
// Accessing the instance of the newly added chart | |
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex]; | |
// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3" | |
chart.NSeries.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’.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Open the existing file. | |
Workbook workbook = new Workbook(dataDir + "piechart.xls"); | |
// Get the designer chart in the second sheet. | |
Worksheet sheet = workbook.Worksheets[1]; | |
Aspose.Cells.Charts.Chart chart = sheet.Charts[0]; | |
// Get the data labels in the data series of the third data point. | |
Aspose.Cells.Charts.DataLabels datalabels = chart.NSeries[0].Points[2].DataLabels; | |
// 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.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Open the existing file. | |
Workbook workbook = new Workbook(dataDir + "Book1.xlsx"); | |
// Get the designer chart in the first worksheet. | |
Aspose.Cells.Charts.Chart chart = workbook.Worksheets[0].Charts[0]; | |
// Add the third data series to it. | |
chart.NSeries.Add("{60, 80, 10}", true); | |
// Add another data series (fourth) to it. | |
chart.NSeries.Add("{0.3, 0.7, 1.2}", true); | |
// Plot the fourth data series on the second axis. | |
chart.NSeries[3].PlotOnSecondAxis = true; | |
// Change the Border color of the second data series. | |
chart.NSeries[1].Border.Color = Color.Green; | |
// Change the Border color of the third data series. | |
chart.NSeries[2].Border.Color = Color.Red; | |
// Make the second value axis visible. | |
chart.SecondValueAxis.IsVisible = true; | |
// Save the excel file. | |
workbook.Save(dataDir + "output.xls"); |