Come creare un grafico waterfall con C++
Contents
[
Hide
]
Un grafico a cascata è un tipo speciale di grafico che viene normalmente utilizzato per mostrare come la posizione iniziale aumenta o diminuisce. Microsoft Excel ha molti tipi di grafici predefiniti, tra cui colonne, linee, torte, barre, radar, ecc. ma il grafico a cascata va oltre i grafici di base e può essere creato utilizzando i tipi di grafico esistenti con una personalizzazione minima o maggiore.
Le API Aspose.Cells consentono di creare un grafico a cascata con l’aiuto di un grafico a linee. L’API consente anche di personalizzare l’aspetto del grafico per dargli la forma della cascata impostando le proprietà Series.GetUpBars() e Series.GetDownBars().
Il seguente esempio di codice dimostra l’uso dell’API Aspose.Cells for C++ per creare un grafico waterfall da zero.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Charts;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create an instance of Workbook
Workbook workbook;
// Retrieve the first Worksheet in Workbook
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Retrieve the Cells of the first Worksheet
Cells cells = worksheet.GetCells();
// Input some data which chart will use as source
cells.Get(U16String(u"A1")).PutValue(U16String(u"Previous Year"));
cells.Get(U16String(u"A2")).PutValue(U16String(u"January"));
cells.Get(U16String(u"A3")).PutValue(U16String(u"March"));
cells.Get(U16String(u"A4")).PutValue(U16String(u"August"));
cells.Get(U16String(u"A5")).PutValue(U16String(u"October"));
cells.Get(U16String(u"A6")).PutValue(U16String(u"Current Year"));
cells.Get(U16String(u"B1")).PutValue(8.5);
cells.Get(U16String(u"B2")).PutValue(1.5);
cells.Get(U16String(u"B3")).PutValue(7.5);
cells.Get(U16String(u"B4")).PutValue(7.5);
cells.Get(U16String(u"B5")).PutValue(8.5);
cells.Get(U16String(u"B6")).PutValue(3.5);
cells.Get(U16String(u"C1")).PutValue(1.5);
cells.Get(U16String(u"C2")).PutValue(4.5);
cells.Get(U16String(u"C3")).PutValue(3.5);
cells.Get(U16String(u"C4")).PutValue(9.5);
cells.Get(U16String(u"C5")).PutValue(7.5);
cells.Get(U16String(u"C6")).PutValue(9.5);
// Add a Chart of type Waterfall in same worksheet as of data
int idx = worksheet.GetCharts().Add(ChartType::Waterfall, 4, 4, 25, 13);
// Retrieve the Chart object
Chart chart = worksheet.GetCharts().Get(idx);
// Add Series
chart.GetNSeries().Add(U16String(u"$B$1:$C$6"), true);
// Add Category Data
chart.GetNSeries().SetCategoryData(U16String(u"$A$1:$A$6"));
// Series has Up Down Bars
chart.GetNSeries().Get(0).SetHasUpDownBars(true);
// Set the colors of Up and Down Bars
chart.GetNSeries().Get(0).GetUpBars().GetArea().SetForegroundColor(Color::Green());
chart.GetNSeries().Get(0).GetDownBars().GetArea().SetForegroundColor(Color::Red());
// Make both Series Lines invisible
chart.GetNSeries().Get(0).GetBorder().SetIsVisible(false);
chart.GetNSeries().Get(1).GetBorder().SetIsVisible(false);
// Set the Plot Area Formatting Automatic
chart.GetPlotArea().GetArea().SetFormatting(FormattingType::Automatic);
// Delete the Legend
chart.GetLegend().GetLegendEntries().Get(0).SetIsDeleted(true);
chart.GetLegend().GetLegendEntries().Get(1).SetIsDeleted(true);
// Save the workbook
workbook.Save(outDir + u"output_out.xlsx");
Aspose::Cells::Cleanup();
}