Gestionar los ejes de gráficos de Excel con C++
En los gráficos de Excel, hay 3 tipos de ejes:
- Eje Horizontal (Categoría): Eje X
- Eje Vertical (Valor): Eje Y
- Eje de Profundidad (Serie): Eje Z
Opciones del Eje
Aspose.Cells también permite gestionar los ejes del gráfico en tiempo de ejecución. Con el objeto Axis puedes cambiar todas las opciones del eje, como en Excel.
||
Administrar Ejes X e Y
En los gráficos de Excel, los ejes horizontal y vertical son los dos ejes más populares para usar.
El siguiente fragmento de código demuestra cómo establecer las opciones de los ejes X e Y.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Charts;
int main()
{
Aspose::Cells::Startup();
// Instantiating a Workbook object
Workbook workbook;
// Adding a new worksheet to the Workbook object
int sheetIndex = workbook.GetWorksheets().Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.GetWorksheets().Get(sheetIndex);
// Adding sample values to cells
worksheet.GetCells().Get(u"A1").PutValue(u"Series1");
worksheet.GetCells().Get(u"A2").PutValue(50);
worksheet.GetCells().Get(u"A3").PutValue(100);
worksheet.GetCells().Get(u"A4").PutValue(150);
worksheet.GetCells().Get(u"B1").PutValue(u"Series2");
worksheet.GetCells().Get(u"B2").PutValue(60);
worksheet.GetCells().Get(u"B3").PutValue(32);
worksheet.GetCells().Get(u"B4").PutValue(50);
// Adding a chart to the worksheet
int chartIndex = worksheet.GetCharts().Add(ChartType::Column, 5, 0, 15, 5);
// Accessing the instance of the newly added chart
Chart chart = worksheet.GetCharts().Get(chartIndex);
// Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B4"
chart.SetChartDataRange(u"A1:B4", true);
// Hiding X axis
chart.GetCategoryAxis().SetIsVisible(false);
// Setting max value of Y axis
chart.GetValueAxis().SetMaxValue(200);
// Setting major unit
chart.GetValueAxis().SetMajorUnit(50);
// Save the file
workbook.Save(u"chart_axes.xlsx");
Aspose::Cells::Cleanup();
}