Creazione e personalizzazione di grafici

Possibili Scenari di Utilizzo

Un grafico è una visualizzazione visuale delle informazioni. Aspose.Cells consente agli sviluppatori di visualizzare le informazioni nei grafici proprio come fa Microsoft Excel. Presentare informazioni nei grafici è sempre utile per i decision maker per prendere decisioni rapide e tempestive. È più facile vedere rapidamente confronti, pattern e tendenze nei dati con i grafici piuttosto che con i numeri grezzi. La creazione di grafici a tempo di esecuzione, basati sui dati in un foglio di calcolo, è una delle utili funzionalità di Aspose.Cells. Aspose.Cells supporta la creazione di grafici standard e personalizzati. Di seguito, mostreremo alcuni esempi con file di esempio su come creare alcuni tipi comuni di grafici MS-Excel utilizzando l’API di Aspose.Cells.

Grafico a piramide

Quando il codice di esempio viene eseguito, viene aggiunto un grafico a piramide al foglio di lavoro. Si prega di consultare il file Excel di output del seguente codice di esempio.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
Aspose::Cells::Startup();
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of output excel file
U16String outputChartTypePyramid = outDir + u"outputChartTypePyramid.xlsx";
// Create a new workbook
Workbook workbook;
// Get first worksheet which is created by default
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Adding sample values to cells
worksheet.GetCells().Get(u"A1").PutValue(50);
worksheet.GetCells().Get(u"A2").PutValue(100);
worksheet.GetCells().Get(u"A3").PutValue(150);
worksheet.GetCells().Get(u"B1").PutValue(4);
worksheet.GetCells().Get(u"B2").PutValue(20);
worksheet.GetCells().Get(u"B3").PutValue(50);
// Adding a chart to the worksheet
int chartIndex = worksheet.GetCharts().Add(Aspose::Cells::Charts::ChartType::Pyramid, 5, 0, 20, 8);
// 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 "B3"
chart.GetNSeries().Add(u"A1:B3", true);
// Saving the Excel file
workbook.Save(outputChartTypePyramid);
Aspose::Cells::Cleanup();

Grafico a linee

Nell’esempio precedente, cambiare semplicemente ChartType in ChartType::Line crea un grafico a linee. Di seguito viene fornito il codice completo. Quando il codice viene eseguito, viene aggiunto un grafico a linee al foglio di lavoro. Si prega di consultare il file Excel di output del seguente codice di esempio.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
Aspose::Cells::Startup();
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of output excel file
U16String outputChartTypeLine = testPath + u"outputChartTypeLine.xlsx";
// Create a new workbook
Workbook workbook;
// Get first worksheet which is created by default
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Adding sample values to cells
worksheet.GetCells().Get(u"A1").PutValue(50);
worksheet.GetCells().Get(u"A2").PutValue(100);
worksheet.GetCells().Get(u"A3").PutValue(150);
worksheet.GetCells().Get(u"B1").PutValue(4);
worksheet.GetCells().Get(u"B2").PutValue(20);
worksheet.GetCells().Get(u"B3").PutValue(50);
// Adding a chart to the worksheet
int chartIndex = worksheet.GetCharts().Add(Aspose::Cells::Charts::ChartType::Line, 5, 0, 20, 8);
// 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 "B3"
chart.GetNSeries().Add(u"A1:B3", true);
// Saving the Excel file
workbook.Save(outputChartTypeLine);
Aspose::Cells::Cleanup();

Grafico a bolle

Per creare un grafico a bolle, ChartType deve essere impostato su ChartType_Bubble e alcune proprietà aggiuntive come SetBubbleSizes e SetXValues devono essere impostate di conseguenza. Dopo aver eseguito il seguente codice, viene aggiunto un grafico a bolle al foglio di lavoro. Si prega di consultare il file Excel di output del seguente codice di esempio.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
Aspose::Cells::Startup();
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of output excel file
U16String outputChartTypeBubble = outDir + u"outputChartTypeBubble.xlsx";
// Create a new workbook
Workbook workbook;
// Get first worksheet which is created by default
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Fill in data for chart's series
// Y Values
worksheet.GetCells().Get(0, 0).PutValue(u"Y Values");
worksheet.GetCells().Get(0, 1).PutValue(2);
worksheet.GetCells().Get(0, 2).PutValue(4);
worksheet.GetCells().Get(0, 3).PutValue(6);
// Bubble Size
worksheet.GetCells().Get(1, 0).PutValue(u"Bubble Size");
worksheet.GetCells().Get(1, 1).PutValue(2);
worksheet.GetCells().Get(1, 2).PutValue(3);
worksheet.GetCells().Get(1, 3).PutValue(1);
// X Values
worksheet.GetCells().Get(2, 0).PutValue(u"X Values");
worksheet.GetCells().Get(2, 1).PutValue(1);
worksheet.GetCells().Get(2, 2).PutValue(2);
worksheet.GetCells().Get(2, 3).PutValue(3);
// Set first column width
worksheet.GetCells().SetColumnWidth(0, 12);
// Adding a chart to the worksheet
int chartIndex = worksheet.GetCharts().Add(Aspose::Cells::Charts::ChartType::Bubble, 5, 0, 20, 8);
// Accessing the instance of the newly added chart
Chart chart = worksheet.GetCharts().Get(chartIndex);
// Adding SeriesCollection (chart data source) to the chart ranging from B1 to D1
chart.GetNSeries().Add(u"B1:D1", true);
// Set bubble sizes
chart.GetNSeries().Get(0).SetBubbleSizes(u"B2:D2");
// Set X axis values
chart.GetNSeries().Get(0).SetXValues(u"B3:D3");
// Set Y axis values
chart.GetNSeries().Get(0).SetValues(u"B1:D1");
// Saving the Excel file
workbook.Save(outputChartTypeBubble);
Aspose::Cells::Cleanup();

Creazione di grafici personalizzati

Finora, quando abbiamo discusso dei grafici, abbiamo esaminato grafici standard che hanno le proprie impostazioni di formattazione standard. Definiamo solo la fonte dei dati, impostiamo alcune proprietà e il grafico viene creato con le sue impostazioni di formato predefinite. Ma le API Aspose.Cells supportano anche la creazione di grafici personalizzati che consentono ai programmatori di creare grafici con le proprie impostazioni di formato. I programmatori possono creare grafici personalizzati durante l’esecuzione utilizzando Aspose.Cells.

Un grafico è composto da una serie di dati. Nella creazione di un grafico personalizzato, i programmatori hanno la libertà di utilizzare diversi tipi di grafici per diverse serie di dati.

Il seguente codice di esempio mostra come creare grafici personalizzati. In questo esempio, useremo un grafico a colonne per la prima serie di dati e un grafico a linee per la seconda serie. Il risultato è che aggiungiamo un grafico a colonne, combinato con un grafico a linee, al foglio di lavoro. Si prega di consultare il file Excel di output del seguente codice di esempio.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
Aspose::Cells::Startup();
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Path of output excel file
U16String outputChartTypeCustom = outDir + u"outputChartTypeCustom.xlsx";
// Create a new workbook
Workbook workbook;
// Get first worksheet which is created by default
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Adding sample values to cells
worksheet.GetCells().Get(u"A1").PutValue(50);
worksheet.GetCells().Get(u"A2").PutValue(100);
worksheet.GetCells().Get(u"A3").PutValue(150);
worksheet.GetCells().Get(u"A4").PutValue(110);
worksheet.GetCells().Get(u"B1").PutValue(260);
worksheet.GetCells().Get(u"B2").PutValue(12);
worksheet.GetCells().Get(u"B3").PutValue(50);
worksheet.GetCells().Get(u"B4").PutValue(100);
// Adding a chart to the worksheet
int chartIndex = worksheet.GetCharts().Add(Aspose::Cells::Charts::ChartType::Column, 5, 0, 20, 8);
// 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 to B4
chart.GetNSeries().Add(u"A1:B4", true);
// Setting the chart type of 2nd NSeries to display as line chart
chart.GetNSeries().Get(1).SetType(Aspose::Cells::Charts::ChartType::Line);
// Saving the Excel file
workbook.Save(outputChartTypeCustom);
Aspose::Cells::Cleanup();