Crea o aggiorna grafici delle presentazioni PowerPoint in .NET
Panoramica
Questo articolo fornisce una guida completa su come creare e personalizzare i grafici utilizzando Aspose.Slides per .NET. Imparerai come aggiungere programmaticamente un grafico a una diapositiva, popolarlo con i dati e applicare varie opzioni di formattazione per soddisfare i requisiti di progettazione specifici. In tutto l’articolo, esempi di codice dettagliati illustrano ogni passaggio, dall’inizializzazione della presentazione e dell’oggetto chart alla configurazione delle serie, degli assi e delle legende. Seguendo questa guida, otterrai una solida comprensione di come integrare la generazione dinamica di grafici nelle tue applicazioni .NET, semplificando il processo di creazione di presentazioni basate sui dati.
Creare un grafico
I grafici aiutano le persone a visualizzare rapidamente i dati e a ottenere approfondimenti che potrebbero non essere immediatamente evidenti da una tabella o da un foglio di calcolo.
Perché creare grafici?
- aggregare, condensare o riepilogare grandi quantità di dati su una singola diapositiva in una presentazione;
- rivelare modelli e tendenze nei dati;
- dedurre la direzione e lo slancio dei dati nel tempo o rispetto a una specifica unità di misura;
- identificare valori anomali, aberrazioni, deviazioni, errori e dati insensati;
- comunicare o presentare dati complessi.
Nella PowerPoint, è possibile creare grafici tramite la funzione Inserisci, che fornisce modelli per progettare molti tipi di grafico. Utilizzando Aspose.Slides, è possibile creare sia grafici regolari (basati su tipi di grafico popolari) sia grafici personalizzati.
Note
Utilizza l’enumerazione ChartType nel namespace Aspose.Slides.Charts. I valori di questa enumerazione corrispondono a diversi tipi di grafico.Creare grafici a colonne raggruppate
L’esempio utilizza AddChart per creare un grafico a colonne raggruppate che confronta due serie su tre categorie. Le serie utilizzano riempimenti rosso e verde, e le etichette individuali mostrano i nomi delle categorie, i nomi delle serie e i valori. L’indice del foglio di lavoro 0 si riferisce al primo foglio nella cartella di lavoro del grafico incorporato.
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.ClusteredColumn, 20, 20, 500, 300);
chart.ChartTitle.AddTextFrameForOverriding("Sample Title");
chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
chart.ChartTitle.Height = 20;
chart.HasTitle = true;
var workbook = chart.ChartData.ChartDataWorkbook;
var worksheetIndex = 0;
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
var seriesNameCell = workbook.GetCell(worksheetIndex, 0, 1, "Series 1");
chart.ChartData.Series.Add(seriesNameCell, chart.Type);
seriesNameCell = workbook.GetCell(worksheetIndex, 0, 2, "Series 2");
chart.ChartData.Series.Add(seriesNameCell, chart.Type);
var categoryCell = workbook.GetCell(worksheetIndex, 1, 0, "Category 1");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(worksheetIndex, 2, 0, "Category 2");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(worksheetIndex, 3, 0, "Category 3");
chart.ChartData.Categories.Add(categoryCell);
var series = chart.ChartData.Series[0];
var valueCell = workbook.GetCell(worksheetIndex, 1, 1, 20);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 2, 1, 50);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 3, 1, 30);
series.DataPoints.AddDataPointForBarSeries(valueCell);
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.Red;
series = chart.ChartData.Series[1];
valueCell = workbook.GetCell(worksheetIndex, 1, 2, 30);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 2, 2, 10);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 3, 2, 60);
series.DataPoints.AddDataPointForBarSeries(valueCell);
series.Format.Fill.FillType = FillType.Solid;
series.Format.Fill.SolidFillColor.Color = Color.Green;
var label = series.DataPoints[0].Label;
label.DataLabelFormat.ShowCategoryName = true;
label = series.DataPoints[1].Label;
label.DataLabelFormat.ShowSeriesName = true;
label = series.DataPoints[2].Label;
label.DataLabelFormat.ShowValue = true;
label.DataLabelFormat.ShowSeriesName = true;
label.DataLabelFormat.Separator = "/";
presentation.Save("AsposeChart.pptx", SaveFormat.Pptx);
Il risultato:

Creare grafici a dispersione
I grafici a dispersione (noti anche come scatter plot o grafici x-y) sono spesso utilizzati per verificare la presenza di pattern o dimostrare correlazioni tra due variabili.
Utilizza un grafico a dispersione quando:
- Hai dati numerici accoppiati.
- Hai due variabili che si abbinano bene tra loro.
- Vuoi determinare se le due variabili sono correlate.
- Hai una variabile indipendente che ha più valori per una variabile dipendente.
L’esempio traccia due serie di valori x-y. La prima serie utilizza linee rette con marcatori a stella, mentre la seconda utilizza linee fluide con marcatori a cerchio; entrambe usano una Size di 10. La proprietà Symbol controlla la forma del marcatore.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.ScatterWithSmoothLines, 20, 20, 500, 300);
var workbook = chart.ChartData.ChartDataWorkbook;
var worksheetIndex = 0;
chart.ChartData.Series.Clear();
var seriesNameCell = workbook.GetCell(worksheetIndex, 1, 1, "Series 1");
chart.ChartData.Series.Add(seriesNameCell, chart.Type);
seriesNameCell = workbook.GetCell(worksheetIndex, 1, 3, "Series 2");
chart.ChartData.Series.Add(seriesNameCell, chart.Type);
var series = chart.ChartData.Series[0];
var xValueCell = workbook.GetCell(worksheetIndex, 2, 1, 1);
var yValueCell = workbook.GetCell(worksheetIndex, 2, 2, 3);
series.DataPoints.AddDataPointForScatterSeries(xValueCell, yValueCell);
xValueCell = workbook.GetCell(worksheetIndex, 3, 1, 2);
yValueCell = workbook.GetCell(worksheetIndex, 3, 2, 10);
series.DataPoints.AddDataPointForScatterSeries(xValueCell, yValueCell);
series.Type = ChartType.ScatterWithStraightLinesAndMarkers;
series.Marker.Size = 10;
series.Marker.Symbol = MarkerStyleType.Star;
series = chart.ChartData.Series[1];
xValueCell = workbook.GetCell(worksheetIndex, 2, 3, 5);
yValueCell = workbook.GetCell(worksheetIndex, 2, 4, 2);
series.DataPoints.AddDataPointForScatterSeries(xValueCell, yValueCell);
xValueCell = workbook.GetCell(worksheetIndex, 3, 3, 3);
yValueCell = workbook.GetCell(worksheetIndex, 3, 4, 1);
series.DataPoints.AddDataPointForScatterSeries(xValueCell, yValueCell);
xValueCell = workbook.GetCell(worksheetIndex, 4, 3, 2);
yValueCell = workbook.GetCell(worksheetIndex, 4, 4, 2);
series.DataPoints.AddDataPointForScatterSeries(xValueCell, yValueCell);
xValueCell = workbook.GetCell(worksheetIndex, 5, 3, 5);
yValueCell = workbook.GetCell(worksheetIndex, 5, 4, 1);
series.DataPoints.AddDataPointForScatterSeries(xValueCell, yValueCell);
series.Marker.Size = 10;
series.Marker.Symbol = MarkerStyleType.Circle;
presentation.Save("AsposeChart.pptx", SaveFormat.Pptx);
Il risultato:

Creare grafici a torta
I grafici a torta sono più adatti per mostrare la relazione parte-tutto nei dati, soprattutto quando i dati contengono etichette categoriali con valori numerici. Tuttavia, se i tuoi dati contengono molte parti o etichette, potresti considerare l’uso di un grafico a barre invece.
L’esempio crea un grafico a torta con tre valori trimestrali. Ogni fetta ha un riempimento e un bordo personalizzati, e le etichette mostrano diverse combinazioni di valori, percentuali e informazioni sulla serie. La proprietà FirstSliceAngle imposta la prima fetta a partire da 180 gradi.
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.Pie, 20, 20, 500, 300);
chart.ChartTitle.AddTextFrameForOverriding("Sample Title");
chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True;
chart.ChartTitle.Height = 20;
chart.HasTitle = true;
chart.ChartData.Series[0].Labels.DefaultDataLabelFormat.ShowValue = true;
var workbook = chart.ChartData.ChartDataWorkbook;
var worksheetIndex = 0;
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
var categoryCell = workbook.GetCell(0, 1, 0, "1st Qtr");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, 2, 0, "2nd Qtr");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, 3, 0, "3rd Qtr");
chart.ChartData.Categories.Add(categoryCell);
var seriesNameCell = workbook.GetCell(0, 0, 1, "Series 1");
var series = chart.ChartData.Series.Add(seriesNameCell, chart.Type);
var valueCell = workbook.GetCell(worksheetIndex, 1, 1, 20);
series.DataPoints.AddDataPointForPieSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 2, 1, 50);
series.DataPoints.AddDataPointForPieSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 3, 1, 30);
series.DataPoints.AddDataPointForPieSeries(valueCell);
chart.ChartData.SeriesGroups[0].IsColorVaried = true;
var firstSlice = series.DataPoints[0];
firstSlice.Format.Fill.FillType = FillType.Solid;
firstSlice.Format.Fill.SolidFillColor.Color = Color.Cyan;
firstSlice.Format.Line.FillFormat.FillType = FillType.Solid;
firstSlice.Format.Line.FillFormat.SolidFillColor.Color = Color.Gray;
firstSlice.Format.Line.Width = 3.0;
firstSlice.Format.Line.Style = LineStyle.ThinThick;
firstSlice.Format.Line.DashStyle = LineDashStyle.LargeDash;
var secondSlice = series.DataPoints[1];
secondSlice.Format.Fill.FillType = FillType.Solid;
secondSlice.Format.Fill.SolidFillColor.Color = Color.Brown;
secondSlice.Format.Line.FillFormat.FillType = FillType.Solid;
secondSlice.Format.Line.FillFormat.SolidFillColor.Color = Color.Blue;
secondSlice.Format.Line.Width = 3.0;
secondSlice.Format.Line.Style = LineStyle.Single;
secondSlice.Format.Line.DashStyle = LineDashStyle.LargeDashDot;
var thirdSlice = series.DataPoints[2];
thirdSlice.Format.Fill.FillType = FillType.Solid;
thirdSlice.Format.Fill.SolidFillColor.Color = Color.Coral;
thirdSlice.Format.Line.FillFormat.FillType = FillType.Solid;
thirdSlice.Format.Line.FillFormat.SolidFillColor.Color = Color.Red;
thirdSlice.Format.Line.Width = 2.0;
thirdSlice.Format.Line.Style = LineStyle.ThinThin;
thirdSlice.Format.Line.DashStyle = LineDashStyle.LargeDashDotDot;
var firstLabel = series.DataPoints[0].Label;
firstLabel.DataLabelFormat.ShowValue = true;
var secondLabel = series.DataPoints[1].Label;
secondLabel.DataLabelFormat.ShowValue = true;
secondLabel.DataLabelFormat.ShowLegendKey = true;
secondLabel.DataLabelFormat.ShowPercentage = true;
var thirdLabel = series.DataPoints[2].Label;
thirdLabel.DataLabelFormat.ShowSeriesName = true;
thirdLabel.DataLabelFormat.ShowPercentage = true;
series.Labels.DefaultDataLabelFormat.ShowLeaderLines = true;
chart.ChartData.SeriesGroups[0].FirstSliceAngle = 180;
presentation.Save("PieChart.pptx", SaveFormat.Pptx);
Il risultato:

Per migliorare la leggibilità delle etichette fuori dalla torta, vedi Format Leader Lines per un esempio che posiziona le etichette e personalizza il colore, la larghezza, la trasparenza e lo stile tratteggiato delle linee.
Creare grafici a linee
I grafici a linee (noti anche come grafici a linee) sono più indicati in situazioni in cui si desidera mostrare variazioni di valore nel tempo. Utilizzando un grafico a linee, è possibile confrontare una grande quantità di dati contemporaneamente, monitorare cambiamenti e tendenze nel tempo, evidenziare anomalie nelle serie di dati e altro.
L’esempio utilizza AddChart per creare un grafico a linee con le serie e le categorie predefinite, quindi salva la presentazione come PPTX.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var lineChart = slide.Shapes.AddChart(ChartType.Line, 20, 20, 500, 300);
presentation.Save("lineChart.pptx", SaveFormat.Pptx);
Per impostazione predefinita, i punti su un grafico a linee sono collegati da linee continue e rette. Se desideri che i punti siano collegati da linee tratteggiate, imposta DashStyle a un valore di LineDashStyle:
using Aspose.Slides;
using Aspose.Slides.Charts;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var lineChart = slide.Shapes.AddChart(ChartType.Line, 20, 20, 500, 300);
foreach (var series in lineChart.ChartData.Series)
{
series.Format.Line.DashStyle = LineDashStyle.Dash;
}
Il risultato:

Creare grafici a mappa ad albero
I grafici a mappa ad albero sono più indicati per i dati di vendita quando si desidera mostrare la dimensione relativa delle categorie di dati e attirare rapidamente l’attenzione sugli elementi che sono grandi contributori all’interno di ciascuna categoria.
L’esempio crea una mappa ad albero con otto foglie raggruppate in quattro tronchi e due rami. Il metodo SetGroupingItem assegna i tronchi al livello di raggruppamento 1 e i rami al livello 2. I valori determinano le dimensioni relative dei rettangoli, mentre le etichette di categoria e le etichette genitore sovrapposte identificano la gerarchia.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.Treemap, 20, 20, 500, 300);
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
// Ramo 1
var categoryCell = workbook.GetCell(0, "C1", "Leaf1");
var leaf = chart.ChartData.Categories.Add(categoryCell);
leaf.GroupingLevels.SetGroupingItem(1, "Stem1");
leaf.GroupingLevels.SetGroupingItem(2, "Branch1");
categoryCell = workbook.GetCell(0, "C2", "Leaf2");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "C3", "Leaf3");
leaf = chart.ChartData.Categories.Add(categoryCell);
leaf.GroupingLevels.SetGroupingItem(1, "Stem2");
categoryCell = workbook.GetCell(0, "C4", "Leaf4");
chart.ChartData.Categories.Add(categoryCell);
// Ramo 2
categoryCell = workbook.GetCell(0, "C5", "Leaf5");
leaf = chart.ChartData.Categories.Add(categoryCell);
leaf.GroupingLevels.SetGroupingItem(1, "Stem3");
leaf.GroupingLevels.SetGroupingItem(2, "Branch2");
categoryCell = workbook.GetCell(0, "C6", "Leaf6");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "C7", "Leaf7");
leaf = chart.ChartData.Categories.Add(categoryCell);
leaf.GroupingLevels.SetGroupingItem(1, "Stem4");
categoryCell = workbook.GetCell(0, "C8", "Leaf8");
chart.ChartData.Categories.Add(categoryCell);
var series = chart.ChartData.Series.Add(ChartType.Treemap);
series.Labels.DefaultDataLabelFormat.ShowCategoryName = true;
var valueCell = workbook.GetCell(0, "D1", 4);
series.DataPoints.AddDataPointForTreemapSeries(valueCell);
valueCell = workbook.GetCell(0, "D2", 5);
series.DataPoints.AddDataPointForTreemapSeries(valueCell);
valueCell = workbook.GetCell(0, "D3", 3);
series.DataPoints.AddDataPointForTreemapSeries(valueCell);
valueCell = workbook.GetCell(0, "D4", 6);
series.DataPoints.AddDataPointForTreemapSeries(valueCell);
valueCell = workbook.GetCell(0, "D5", 9);
series.DataPoints.AddDataPointForTreemapSeries(valueCell);
valueCell = workbook.GetCell(0, "D6", 9);
series.DataPoints.AddDataPointForTreemapSeries(valueCell);
valueCell = workbook.GetCell(0, "D7", 4);
series.DataPoints.AddDataPointForTreemapSeries(valueCell);
valueCell = workbook.GetCell(0, "D8", 3);
series.DataPoints.AddDataPointForTreemapSeries(valueCell);
series.ParentLabelLayout = ParentLabelLayoutType.Overlapping;
presentation.Save("Treemap.pptx", SaveFormat.Pptx);
Il risultato:

Creare grafici a cascata
Un grafico a cascata mostra come le variazioni positive e negative contribuiscano a un valore finale. Ad esempio, una ripartizione del profitto può iniziare con il fatturato, sottrarre i costi e aggiungere altri redditi. Le colonne di variazione si spostano tra il valore corrente precedente e quello nuovo, mentre le colonne totali si estendono da zero al valore accumulato.
L’esempio seguente utilizza importi in migliaia di dollari statunitensi:
| Categoria | Valore cartella di lavoro | Tipo colonna |
|---|---|---|
| Ricavi | 1000 | Aumento |
| Costo delle vendite | -400 | Diminuzione |
| Utile lordo | 600 | Totale intermedio |
| Spese operative | -200 | Diminuzione |
| Altri ricavi | 50 | Aumento |
| Utile netto | 450 | Totale finale |
Per contrassegnare un punto come totale intermedio o finale, imposta la sua proprietà SetAsTotal su true. Fornisci l’importo reale accumulato nella cella del foglio di lavoro di quel punto: 600 per l’utile lordo (1000 - 400) e 450 per l’utile netto (600 - 200 + 50). La proprietà segna la colonna come totale; non calcola né popola il valore della cella. Non utilizzare zero come segnaposto o fornire un’altra variazione per un punto totale. Una colonna totale visualizza il valore accumulato senza aggiungerlo nuovamente al valore corrente.
Questo esempio completo in C# crea un grafico utilizzando il membro Waterfall di ChartType, cancella i dati predefiniti e riempie la cartella di lavoro incorporata. L’indice del foglio di lavoro 0 seleziona il primo foglio; la colonna A contiene i nomi delle categorie e la colonna B contiene i valori. Ogni cella valore viene aggiunta con AddDataPointForWaterfallSeries. Gli indici dei punti basati su zero 2 e 5 identificano i totali intermedi e finali. Abilitando ShowValue vengono visualizzati gli importi sulle colonne. Non è necessaria alcuna presentazione o cartella di lavoro di input.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.Waterfall, 20, 20, 680, 200);
chart.HasTitle = true;
chart.ChartTitle.AddTextFrameForOverriding("Profit breakdown (USD thousands)");
chart.ChartTitle.Overlay = false;
chart.HasLegend = false;
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
var worksheetIndex = 0;
workbook.Clear(worksheetIndex);
var seriesNameCell = workbook.GetCell(worksheetIndex, 0, 1, "Profit");
var series = chart.ChartData.Series.Add(seriesNameCell, ChartType.Waterfall);
var categories = new[] { "Revenue", "Cost of sales", "Gross profit", "Operating expenses", "Other income", "Net profit" };
var values = new double[] { 1000, -400, 600, -200, 50, 450 };
for (var i = 0; i < categories.Length; i++)
{
var categoryCell = workbook.GetCell(worksheetIndex, i + 1, 0, categories[i]);
chart.ChartData.Categories.Add(categoryCell);
var valueCell = workbook.GetCell(worksheetIndex, i + 1, 1, values[i]);
series.DataPoints.AddDataPointForWaterfallSeries(valueCell);
}
// Il terzo e il sesto punto rappresentano totali, non cambiamenti aggiuntivi.
series.DataPoints[2].SetAsTotal = true;
series.DataPoints[5].SetAsTotal = true;
series.ShowConnectorLines = true;
series.Labels.DefaultDataLabelFormat.ShowValue = true;
presentation.Save("WaterfallChart.pptx", SaveFormat.Pptx);
Imposta la proprietà di livello serie ShowConnectorLines su true per mostrare le linee che collegano le colonne consecutive e rendere più facile seguire il valore corrente. Per nascondere queste linee, sostituisci l’assegnazione nell’esempio con series.ShowConnectorLines = false; prima di salvare. Questo cambia solo la visibilità della linea, non i dati o i totali.
La presentazione salvata mostra un utile lordo di 600 e un utile netto di 450, con entrambi i totali disegnati da zero:

Creare grafici azionari
I grafici azionari sono usati per visualizzare dati finanziari come prezzi di apertura, massimo, minimo e chiusura, aiutando ad analizzare le tendenze di mercato e la volatilità. Offrono informazioni essenziali sulle performance delle azioni, supportando investitori e analisti nel prendere decisioni informate.
L’esempio crea un grafico open-high-low-close con quattro serie in quest’ordine e tre categorie. La proprietà HasUpDownBars abilita le barre su/giù, e HiLowLinesFormat controlla le linee alto-basso che mostrano le fasce di prezzo, mentre le linee delle serie individuali e le linee di griglia orizzontali minori sono nascoste.
Il gestore UpDownBars espone gli oggetti separati UpBarsFormat e DownBarsFormat. Usa le proprietà Fill e Line di ciascun oggetto per impostare riempimento e contorno in modo indipendente. Questo esempio utilizza barre verdi con contorni verde scuro per gli intervalli in rialzo e barre arancione-rosse con contorni rosso scuro per gli intervalli in ribasso.
Fornisci valori numerici per tutte e quattro le serie in ciascuna categoria, con Alto e Basso che racchiudono Apertura e Chiusura. Le barre confrontano la Chiusura con l’Apertura all’interno della stessa categoria, non con la categoria precedente: A scende da 72 a 25, mentre B sale da 25 a 38 e C sale da 38 a 50. Valori di Apertura e Chiusura uguali non producono alcun intervallo riempito visibile. Le barre su/giù si estendono da Apertura a Chiusura; le linee alto-basso si estendono da Basso a Alto. Queste barre appartengono al gruppo di serie e non a serie di grafico a barre ordinarie.
Questa formattazione funziona anche con i grafici Line and LineWithMarkers contenenti almeno due serie nello stesso gruppo e valori numerici corrispondenti per ciascuna categoria. Per questi grafici a linee non impilati, le barre su/giù confrontano la prima e l’ultima serie: un valore finale più alto produce una barra su, e un valore finale più basso produce una barra giù. Invertire quelle serie inverte la classificazione. L’esempio sotto utilizza OpenHighLowClose; non presumere che altre varianti di stock, linee impilate o 3D usino la stessa disposizione dei dati o interpretazione dell’intervallo.
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.OpenHighLowClose, 20, 20, 500, 150, false);
var workbook = chart.ChartData.ChartDataWorkbook;
var categoryCell = workbook.GetCell(0, 1, 0, "A");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, 2, 0, "B");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, 3, 0, "C");
chart.ChartData.Categories.Add(categoryCell);
var seriesNameCell = workbook.GetCell(0, 0, 1, "Open");
chart.ChartData.Series.Add(seriesNameCell, ChartType.OpenHighLowClose);
seriesNameCell = workbook.GetCell(0, 0, 2, "High");
chart.ChartData.Series.Add(seriesNameCell, ChartType.OpenHighLowClose);
seriesNameCell = workbook.GetCell(0, 0, 3, "Low");
chart.ChartData.Series.Add(seriesNameCell, ChartType.OpenHighLowClose);
seriesNameCell = workbook.GetCell(0, 0, 4, "Close");
chart.ChartData.Series.Add(seriesNameCell, ChartType.OpenHighLowClose);
var series = chart.ChartData.Series[0];
var valueCell = workbook.GetCell(0, 1, 1, 72);
series.DataPoints.AddDataPointForStockSeries(valueCell);
valueCell = workbook.GetCell(0, 2, 1, 25);
series.DataPoints.AddDataPointForStockSeries(valueCell);
valueCell = workbook.GetCell(0, 3, 1, 38);
series.DataPoints.AddDataPointForStockSeries(valueCell);
series = chart.ChartData.Series[1];
valueCell = workbook.GetCell(0, 1, 2, 172);
series.DataPoints.AddDataPointForStockSeries(valueCell);
valueCell = workbook.GetCell(0, 2, 2, 57);
series.DataPoints.AddDataPointForStockSeries(valueCell);
valueCell = workbook.GetCell(0, 3, 2, 57);
series.DataPoints.AddDataPointForStockSeries(valueCell);
series = chart.ChartData.Series[2];
valueCell = workbook.GetCell(0, 1, 3, 12);
series.DataPoints.AddDataPointForStockSeries(valueCell);
valueCell = workbook.GetCell(0, 2, 3, 12);
series.DataPoints.AddDataPointForStockSeries(valueCell);
valueCell = workbook.GetCell(0, 3, 3, 13);
series.DataPoints.AddDataPointForStockSeries(valueCell);
series = chart.ChartData.Series[3];
valueCell = workbook.GetCell(0, 1, 4, 25);
series.DataPoints.AddDataPointForStockSeries(valueCell);
valueCell = workbook.GetCell(0, 2, 4, 38);
series.DataPoints.AddDataPointForStockSeries(valueCell);
valueCell = workbook.GetCell(0, 3, 4, 50);
series.DataPoints.AddDataPointForStockSeries(valueCell);
var upDownBars = chart.ChartData.SeriesGroups[0].UpDownBars;
upDownBars.HasUpDownBars = true;
upDownBars.UpBarsFormat.Fill.FillType = FillType.Solid;
upDownBars.UpBarsFormat.Fill.SolidFillColor.Color = Color.LimeGreen;
upDownBars.UpBarsFormat.Line.FillFormat.FillType = FillType.Solid;
upDownBars.UpBarsFormat.Line.FillFormat.SolidFillColor.Color = Color.DarkGreen;
upDownBars.UpBarsFormat.Line.Width = 2;
upDownBars.DownBarsFormat.Fill.FillType = FillType.Solid;
upDownBars.DownBarsFormat.Fill.SolidFillColor.Color = Color.OrangeRed;
upDownBars.DownBarsFormat.Line.FillFormat.FillType = FillType.Solid;
upDownBars.DownBarsFormat.Line.FillFormat.SolidFillColor.Color = Color.DarkRed;
upDownBars.DownBarsFormat.Line.Width = 2;
chart.ChartData.SeriesGroups[0].HiLowLinesFormat.Line.FillFormat.FillType = FillType.Solid;
foreach (var stockSeries in chart.ChartData.Series)
{
stockSeries.Format.Line.FillFormat.FillType = FillType.NoFill;
}
chart.Axes.VerticalAxis.MinorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
presentation.Save("Stock-chart.pptx", SaveFormat.Pptx);
Il risultato, esportato da PowerPoint:

Creare grafici a scatola e baffi
I grafici a scatola e baffi sono usati per visualizzare la distribuzione dei dati riassumendo misure statistiche chiave, come la mediana, i quartili e i possibili valori anomali. Sono particolarmente utili nell’analisi esplorativa dei dati e negli studi statistici per comprendere rapidamente la variabilità dei dati e identificare eventuali anomalie.
L’esempio crea un grafico a scatola e baffi da sei valori. La proprietà QuartileMethod seleziona il calcolo quartile esclusivo. L’esempio abilita anche linee medie, marcatori mediani, punti interni e punti anomali.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.BoxAndWhisker, 20, 20, 500, 300);
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
var categoryCell = workbook.GetCell(0, "A1", "Category 1");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "A2", "Category 2");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "A3", "Category 3");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "A4", "Category 4");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "A5", "Category 5");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "A6", "Category 6");
chart.ChartData.Categories.Add(categoryCell);
var series = chart.ChartData.Series.Add(ChartType.BoxAndWhisker);
series.QuartileMethod = QuartileMethodType.Exclusive;
series.ShowMeanLine = true;
series.ShowMeanMarkers = true;
series.ShowInnerPoints = true;
series.ShowOutlierPoints = true;
var valueCell = workbook.GetCell(0, "B1", 15);
series.DataPoints.AddDataPointForBoxAndWhiskerSeries(valueCell);
valueCell = workbook.GetCell(0, "B2", 41);
series.DataPoints.AddDataPointForBoxAndWhiskerSeries(valueCell);
valueCell = workbook.GetCell(0, "B3", 16);
series.DataPoints.AddDataPointForBoxAndWhiskerSeries(valueCell);
valueCell = workbook.GetCell(0, "B4", 10);
series.DataPoints.AddDataPointForBoxAndWhiskerSeries(valueCell);
valueCell = workbook.GetCell(0, "B5", 23);
series.DataPoints.AddDataPointForBoxAndWhiskerSeries(valueCell);
valueCell = workbook.GetCell(0, "B6", 16);
series.DataPoints.AddDataPointForBoxAndWhiskerSeries(valueCell);
presentation.Save("BoxAndWhisker.pptx", SaveFormat.Pptx);
Creare grafici a imbuto
I grafici a imbuto sono usati per visualizzare processi che coinvolgono fasi sequenziali, dove il volume dei dati diminuisce man mano che avanza da un passaggio al successivo. Sono particolarmente utili per analizzare i tassi di conversione, identificare colli di bottiglia e monitorare l’efficienza dei processi di vendita o di marketing.
L’esempio utilizza AddChart per aggiungere un grafico a imbuto alla prima diapositiva di una presentazione esistente. Sostituisce i dati predefiniti con sei categorie e valori compresi tra 50 e 500, quindi salva la presentazione aggiornata. La presentazione di input deve contenere almeno una diapositiva.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation("test.pptx");
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.Funnel, 50, 50, 500, 400);
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
var categoryCell = workbook.GetCell(0, "A1", "Category 1");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "A2", "Category 2");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "A3", "Category 3");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "A4", "Category 4");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "A5", "Category 5");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "A6", "Category 6");
chart.ChartData.Categories.Add(categoryCell);
var series = chart.ChartData.Series.Add(ChartType.Funnel);
var valueCell = workbook.GetCell(0, "B1", 50);
series.DataPoints.AddDataPointForFunnelSeries(valueCell);
valueCell = workbook.GetCell(0, "B2", 100);
series.DataPoints.AddDataPointForFunnelSeries(valueCell);
valueCell = workbook.GetCell(0, "B3", 200);
series.DataPoints.AddDataPointForFunnelSeries(valueCell);
valueCell = workbook.GetCell(0, "B4", 300);
series.DataPoints.AddDataPointForFunnelSeries(valueCell);
valueCell = workbook.GetCell(0, "B5", 400);
series.DataPoints.AddDataPointForFunnelSeries(valueCell);
valueCell = workbook.GetCell(0, "B6", 500);
series.DataPoints.AddDataPointForFunnelSeries(valueCell);
presentation.Save("Funnel.pptx", SaveFormat.Pptx);
Il risultato:

Creare grafici Sunburst
I grafici Sunburst sono usati per visualizzare dati gerarchici, mostrando i livelli come anelli concentrici. Aiutano a illustrare le relazioni parte-tutto e sono ideali per rappresentare categorie e sotto-categorie nidificate in un formato chiaro e compatto.
L’esempio crea un grafico Sunburst con otto foglie raggruppate in quattro tronchi e due rami. Il metodo SetGroupingItem assegna i tronchi al livello di raggruppamento 1 e i rami al livello 2. Le etichette di categoria identificano i segmenti, e i valori determinano le loro dimensioni relative.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.Sunburst, 20, 20, 500, 300);
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
// Ramo 1
var categoryCell = workbook.GetCell(0, "C1", "Leaf1");
var leaf = chart.ChartData.Categories.Add(categoryCell);
leaf.GroupingLevels.SetGroupingItem(1, "Stem1");
leaf.GroupingLevels.SetGroupingItem(2, "Branch1");
categoryCell = workbook.GetCell(0, "C2", "Leaf2");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "C3", "Leaf3");
leaf = chart.ChartData.Categories.Add(categoryCell);
leaf.GroupingLevels.SetGroupingItem(1, "Stem2");
categoryCell = workbook.GetCell(0, "C4", "Leaf4");
chart.ChartData.Categories.Add(categoryCell);
// Ramo 2
categoryCell = workbook.GetCell(0, "C5", "Leaf5");
leaf = chart.ChartData.Categories.Add(categoryCell);
leaf.GroupingLevels.SetGroupingItem(1, "Stem3");
leaf.GroupingLevels.SetGroupingItem(2, "Branch2");
categoryCell = workbook.GetCell(0, "C6", "Leaf6");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "C7", "Leaf7");
leaf = chart.ChartData.Categories.Add(categoryCell);
leaf.GroupingLevels.SetGroupingItem(1, "Stem4");
categoryCell = workbook.GetCell(0, "C8", "Leaf8");
chart.ChartData.Categories.Add(categoryCell);
var series = chart.ChartData.Series.Add(ChartType.Sunburst);
series.Labels.DefaultDataLabelFormat.ShowCategoryName = true;
var valueCell = workbook.GetCell(0, "D1", 4);
series.DataPoints.AddDataPointForSunburstSeries(valueCell);
valueCell = workbook.GetCell(0, "D2", 5);
series.DataPoints.AddDataPointForSunburstSeries(valueCell);
valueCell = workbook.GetCell(0, "D3", 3);
series.DataPoints.AddDataPointForSunburstSeries(valueCell);
valueCell = workbook.GetCell(0, "D4", 6);
series.DataPoints.AddDataPointForSunburstSeries(valueCell);
valueCell = workbook.GetCell(0, "D5", 9);
series.DataPoints.AddDataPointForSunburstSeries(valueCell);
valueCell = workbook.GetCell(0, "D6", 9);
series.DataPoints.AddDataPointForSunburstSeries(valueCell);
valueCell = workbook.GetCell(0, "D7", 4);
series.DataPoints.AddDataPointForSunburstSeries(valueCell);
valueCell = workbook.GetCell(0, "D8", 3);
series.DataPoints.AddDataPointForSunburstSeries(valueCell);
presentation.Save("Sunburst.pptx", SaveFormat.Pptx);
Il risultato:

Creare grafici istogramma
I grafici istogramma mostrano come i valori numerici siano distribuiti tra intervalli (bin). Per report mensili sui tempi di elaborazione degli ordini, usa la stessa larghezza del bin e le stesse soglie per mantenere gli intervalli coerenti e rendere più semplice il confronto delle distribuzioni.
Imposta l’asse orizzontale AggregationType su una modalità di AxisAggregationType. BinWidth si applica solo a ByBinWidth; NumberOfBins si applica solo a ByNumberOfBins. Usa Automatic per lasciare che il grafico scegli i bin.
Usare una larghezza fissa del bin
L’esempio raggruppa i tempi di elaborazione in intervalli di 10 minuti, con bin separati per i tempi ≤ 10 minuti e > 40 minuti. Abilita questi bin con IsUnderflowBin e IsOverflowBin. Quando si forniscono soglie esplicite per UnderflowBin e OverflowBin, imposta IsAutomaticUnderflowBin e IsAutomaticOverflowBin su false.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.Histogram, 20, 20, 680, 200);
chart.HasLegend = false;
chart.HasTitle = false;
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
var series = chart.ChartData.Series.Add(ChartType.Histogram);
double[] values = { 5, 10, 10, 11, 15, 20, 20, 21, 25, 30, 30, 31, 35, 40, 40, 41, 50 };
for (var i = 0; i < values.Length; i++)
{
var valueCell = workbook.GetCell(0, i, 0, values[i]);
series.DataPoints.AddDataPointForHistogramSeries(valueCell);
}
var axis = chart.Axes.HorizontalAxis;
axis.AggregationType = AxisAggregationType.ByBinWidth;
axis.BinWidth = 10;
axis.IsUnderflowBin = true;
axis.IsAutomaticUnderflowBin = false;
axis.UnderflowBin = 10;
axis.IsOverflowBin = true;
axis.IsAutomaticOverflowBin = false;
axis.OverflowBin = 40;
presentation.Save("Histogram-bin-width.pptx", SaveFormat.Pptx);
La presentazione visualizzata in PowerPoint mostra conteggi dei bin di 3, 4, 4, 4 e 2. Il bin di sottoscorrimento include 5, 10, 10, quindi la sua soglia è inclusiva. Il bin di sovracorrimento include solo 41, 50, quindi la sua soglia è esclusiva; entrambi i valori uguali a 40 rimangono nell’intervallo precedente. I bin interni contengono valori superiori al loro limite inferiore e inferiori o uguali al loro limite superiore.

Specificare il numero di bin
Per un livello di dettaglio fisso anziché intervalli fissi, richiedi cinque bin per gli stessi dati. Questo esempio lascia i bin di sottoscorrimento e sovracorrimento disabilitati. Il grafico determina la larghezza dai dati, quindi i confini possono cambiare tra i report.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.Histogram, 20, 20, 680, 200);
chart.HasLegend = false;
chart.HasTitle = false;
chart.ChartData.Categories.Clear();
chart.ChartData.Series.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
var series = chart.ChartData.Series.Add(ChartType.Histogram);
double[] values = { 5, 10, 10, 11, 15, 20, 20, 21, 25, 30, 30, 31, 35, 40, 40, 41, 50 };
for (var i = 0; i < values.Length; i++)
{
var valueCell = workbook.GetCell(0, i, 0, values[i]);
series.DataPoints.AddDataPointForHistogramSeries(valueCell);
}
var axis = chart.Axes.HorizontalAxis;
axis.AggregationType = AxisAggregationType.ByNumberOfBins;
axis.NumberOfBins = 5;
presentation.Save("Histogram-bin-count.pptx", SaveFormat.Pptx);
I cinque bin hanno conteggi di 4, 4, 4, 4 e 1. Confronta questa distribuzione con il risultato a larghezza fissa sopra: i valori di input sono identici, ma i confini differiscono.

Creare grafici radar
I grafici radar sono usati per visualizzare dati multivariati in un formato bidimensionale, consentendo un facile confronto di diverse variabili simultaneamente. Sono particolarmente utili per identificare pattern, punti di forza e debolezze attraverso più metriche di performance o attributi.
L’esempio utilizza AddChart per creare un grafico radar con le serie e le categorie predefinite, quindi salva la presentazione come PPTX.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
slide.Shapes.AddChart(ChartType.Radar, 20, 20, 500, 300);
presentation.Save("Radar-chart.pptx", SaveFormat.Pptx);
Il risultato:

Creare grafici multi-categoria
I grafici multi-categoria sono usati per visualizzare dati che coinvolgono più di un raggruppamento categorico, consentendo di confrontare valori su più dimensioni simultaneamente. Sono particolarmente utili quando è necessario analizzare tendenze e relazioni all’interno di dataset complessi e a più livelli.
L’esempio crea un grafico a colonne raggruppate con otto categorie disposte in quattro coppie. Il metodo SetGroupingItem assegna a ciascuna coppia un’etichetta condivisa al livello di raggruppamento 1, producendo un asse delle categorie con etichette sia individuali che di gruppo.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.ClusteredColumn, 20, 20, 500, 300);
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
workbook.Clear(0);
var worksheetIndex = 0;
var categoryCell = workbook.GetCell(0, "c2", "A");
var category = chart.ChartData.Categories.Add(categoryCell);
category.GroupingLevels.SetGroupingItem(1, "Group1");
categoryCell = workbook.GetCell(0, "c3", "B");
category = chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "c4", "C");
category = chart.ChartData.Categories.Add(categoryCell);
category.GroupingLevels.SetGroupingItem(1, "Group2");
categoryCell = workbook.GetCell(0, "c5", "D");
category = chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "c6", "E");
category = chart.ChartData.Categories.Add(categoryCell);
category.GroupingLevels.SetGroupingItem(1, "Group3");
categoryCell = workbook.GetCell(0, "c7", "F");
category = chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(0, "c8", "G");
category = chart.ChartData.Categories.Add(categoryCell);
category.GroupingLevels.SetGroupingItem(1, "Group4");
categoryCell = workbook.GetCell(0, "c9", "H");
category = chart.ChartData.Categories.Add(categoryCell);
var seriesNameCell = workbook.GetCell(0, "D1", "Series 1");
var series = chart.ChartData.Series.Add(seriesNameCell, ChartType.ClusteredColumn);
var valueCell = workbook.GetCell(worksheetIndex, "D2", 10);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, "D3", 20);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, "D4", 30);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, "D5", 40);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, "D6", 50);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, "D7", 60);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, "D8", 70);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, "D9", 80);
series.DataPoints.AddDataPointForBarSeries(valueCell);
presentation.Save("AsposeChart.pptx", SaveFormat.Pptx);
Il risultato:

Rimuovere un’etichetta di trimestre
Scarica quarters.pptx e posizionalo nella directory di lavoro. Il suo grafico mostra nove mesi e una singola serie di vendite con valori da 10 a 18. L’asse di categoria ha tre etichette di trimestre: Q1 copre Gen‑Mar, Q2 copre Apr‑Giu, e Q3 copre Lug‑Set.
Aprile è la quarta categoria e quella su cui Q2 è stato impostato esplicitamente. Chiama DeleteGroupingItem sulla collezione GroupingLevels di Aprile. Il livello è relativo ad Aprile: il livello 1 è il suo trimestre, e il livello 0 è il mese stesso. Il metodo non può rimuovere la categoria foglia al livello 0.
using System;
using System.Linq;
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using (var presentation = new Presentation("quarters.pptx"))
{
var chart = (IChart)presentation.Slides[0].Shapes[0];
var april = chart.ChartData.Categories[3];
april.GroupingLevels.DeleteGroupingItem(1);
presentation.Save("quarters-updated.pptx", SaveFormat.Pptx);
}
using var reopened = new Presentation("quarters-updated.pptx");
var updatedChart = (IChart)reopened.Slides[0].Shapes[0];
var data = updatedChart.ChartData;
Console.WriteLine(string.Join(", ", data.Categories.Select(month => month.AsCell.Value)));
Console.WriteLine(string.Join(", ", data.Series[0].DataPoints.Select(point => point.Value.Data)));
Il grafico riaperto stampa i mesi originali (Gen, Feb, Mar, Apr, Mag, Giu, Lug, Ago, Set) e i valori di vendita (10, 11, 12, 13, 14, 15, 16, 17, 18) nello stesso ordine. Apri quarters-updated.pptx in PowerPoint per confrontare le etichette dell’asse. L’etichetta Q2 e il confine di gruppo prima di Aprile scompaiono, quindi Q1 copre da Gennaio a Giugno. Q3 inizia ancora a Luglio.

Creare grafici mappa
I grafici mappa sono usati per visualizzare dati geografici mappando informazioni a posizioni specifiche come paesi, stati o città. Sono particolarmente utili per analizzare tendenze regionali, dati demografici e distribuzioni spaziali in modo chiaro e visivamente coinvolgente.
L’esempio utilizza AddChart per creare un grafico mappa con i dati geografici predefiniti e lo salva per la visualizzazione in PowerPoint.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.Map, 20, 20, 500, 300);
presentation.Save("mapChart.pptx", SaveFormat.Pptx);
Il risultato:

Note
L’immagine sopra mostra la presentazione salvata aperta in PowerPoint. Aspose.Slides scrive correttamente il grafico mappa e i suoi dati, ma non disegna i grafici mappa da solo: quando una diapositiva che ne contiene una viene resa in un’immagine o convertita in PDF o SVG, l’area del grafico risulta vuota. Le altre forme sulla stessa diapositiva non sono interessate.Creare grafici combinati
Un grafico combinato (o combo chart) combina due o più tipi di grafico in un unico diagramma. Questo grafico consente di evidenziare, confrontare o esaminare differenze tra due o più set di dati, aiutando a identificare le relazioni tra di essi.

L’esempio combina due serie a colonne raggruppate con una serie a linee su quattro categorie. Le colonne usano l’asse verticale primario, mentre PlotOnSecondAxis posiziona la linea su un asse verticale secondario a destra. Le funzioni di supporto configurano titolo, legenda, etichette degli assi e linee della griglia.
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
CreateComboChart();
static void CreateComboChart()
{
using var presentation = new Presentation();
var chart = CreateChartWithFirstSeries(presentation.Slides[0]);
AddSecondSeriesToChart(chart);
AddThirdSeriesToChart(chart);
SetPrimaryAxesFormat(chart);
SetSecondaryAxesFormat(chart);
presentation.Save("combo-chart.pptx", SaveFormat.Pptx);
}
static IChart CreateChartWithFirstSeries(ISlide slide)
{
var chart = slide.Shapes.AddChart(ChartType.ClusteredColumn, 50, 50, 600, 400);
chart.HasTitle = true;
chart.ChartTitle.AddTextFrameForOverriding("Chart Title");
chart.ChartTitle.Overlay = false;
var portionFormat = chart.ChartTitle.TextFrameForOverriding.Paragraphs[0].ParagraphFormat.DefaultPortionFormat;
portionFormat.FontBold = NullableBool.False;
portionFormat.FontHeight = 18f;
chart.Legend.Position = LegendPositionType.Bottom;
chart.Legend.TextFormat.PortionFormat.FontHeight = 12f;
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
var worksheetIndex = 0;
var categoryCell = workbook.GetCell(worksheetIndex, 1, 0, "Category 1");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(worksheetIndex, 2, 0, "Category 2");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(worksheetIndex, 3, 0, "Category 3");
chart.ChartData.Categories.Add(categoryCell);
categoryCell = workbook.GetCell(worksheetIndex, 4, 0, "Category 4");
chart.ChartData.Categories.Add(categoryCell);
var seriesNameCell = workbook.GetCell(worksheetIndex, 0, 1, "Series 1");
var series = chart.ChartData.Series.Add(seriesNameCell, chart.Type);
series.ParentSeriesGroup.Overlap = -25;
series.ParentSeriesGroup.GapWidth = 220;
var valueCell = workbook.GetCell(worksheetIndex, 1, 1, 4.3);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 2, 1, 2.5);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 3, 1, 3.5);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 4, 1, 4.5);
series.DataPoints.AddDataPointForBarSeries(valueCell);
return chart;
}
static void AddSecondSeriesToChart(IChart chart)
{
var workbook = chart.ChartData.ChartDataWorkbook;
const int worksheetIndex = 0;
var seriesNameCell = workbook.GetCell(worksheetIndex, 0, 2, "Series 2");
var series = chart.ChartData.Series.Add(seriesNameCell, ChartType.ClusteredColumn);
series.ParentSeriesGroup.Overlap = -25;
series.ParentSeriesGroup.GapWidth = 220;
var valueCell = workbook.GetCell(worksheetIndex, 1, 2, 2.4);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 2, 2, 4.4);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 3, 2, 1.8);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 4, 2, 2.8);
series.DataPoints.AddDataPointForBarSeries(valueCell);
}
static void AddThirdSeriesToChart(IChart chart)
{
var workbook = chart.ChartData.ChartDataWorkbook;
const int worksheetIndex = 0;
var seriesNameCell = workbook.GetCell(worksheetIndex, 0, 3, "Series 3");
var series = chart.ChartData.Series.Add(seriesNameCell, ChartType.Line);
var valueCell = workbook.GetCell(worksheetIndex, 1, 3, 2.0);
series.DataPoints.AddDataPointForLineSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 2, 3, 2.0);
series.DataPoints.AddDataPointForLineSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 3, 3, 3.0);
series.DataPoints.AddDataPointForLineSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 4, 3, 5.0);
series.DataPoints.AddDataPointForLineSeries(valueCell);
series.PlotOnSecondAxis = true;
}
static void SetPrimaryAxesFormat(IChart chart)
{
var horizontalAxis = chart.Axes.HorizontalAxis;
horizontalAxis.TextFormat.PortionFormat.FontHeight = 12f;
horizontalAxis.Format.Line.FillFormat.FillType = FillType.NoFill;
SetAxisTitle(horizontalAxis, "X Axis");
var verticalAxis = chart.Axes.VerticalAxis;
verticalAxis.TextFormat.PortionFormat.FontHeight = 12f;
verticalAxis.Format.Line.FillFormat.FillType = FillType.NoFill;
SetAxisTitle(verticalAxis, "Y Axis 1");
var majorGridLinesFormat = verticalAxis.MajorGridLinesFormat.Line.FillFormat;
majorGridLinesFormat.FillType = FillType.Solid;
majorGridLinesFormat.SolidFillColor.Color = Color.FromArgb(217, 217, 217);
}
static void SetSecondaryAxesFormat(IChart chart)
{
var secondaryHorizontalAxis = chart.Axes.SecondaryHorizontalAxis;
secondaryHorizontalAxis.Position = AxisPositionType.Bottom;
secondaryHorizontalAxis.CrossType = CrossesType.Maximum;
secondaryHorizontalAxis.IsVisible = false;
secondaryHorizontalAxis.MajorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
secondaryHorizontalAxis.MinorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
var secondaryVerticalAxis = chart.Axes.SecondaryVerticalAxis;
secondaryVerticalAxis.Position = AxisPositionType.Right;
secondaryVerticalAxis.TextFormat.PortionFormat.FontHeight = 12f;
secondaryVerticalAxis.Format.Line.FillFormat.FillType = FillType.NoFill;
secondaryVerticalAxis.MajorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
secondaryVerticalAxis.MinorGridLinesFormat.Line.FillFormat.FillType = FillType.NoFill;
SetAxisTitle(secondaryVerticalAxis, "Y Axis 2");
}
static void SetAxisTitle(IAxis axis, string axisTitle)
{
axis.HasTitle = true;
axis.Title.Overlay = false;
var titlePortionFormat = axis.Title.AddTextFrameForOverriding(axisTitle).Paragraphs[0].ParagraphFormat.DefaultPortionFormat;
titlePortionFormat.FontBold = NullableBool.False;
titlePortionFormat.FontHeight = 12f;
}
Aggiornare i grafici
Aspose.Slides per .NET permette di aggiornare i grafici PowerPoint modificando i dati del grafico, la formattazione e lo stile. Questa funzionalità semplifica il processo di mantenere le presentazioni aggiornate con contenuti dinamici e garantisce che i grafici riflettano accuratamente i dati attuali e gli standard visivi.
L’esempio aggiorna il grafico denominato “My chart” sulla prima diapositiva di una presentazione esistente. Richiede due serie di colonna con tre punti dati ciascuna, con etichette di categoria nella colonna A e nomi di serie nella prima riga del primo foglio di lavoro. L’esempio utilizza GetCell per modificare le celle del foglio di lavoro, aggiorna i valori delle serie, aggiunge una terza serie e cambia il grafico in cilindri raggruppati tramite Type.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
const string chartName = "My chart";
using var presentation = new Presentation("ExistingChart.pptx");
var slide = presentation.Slides[0];
foreach (IShape shape in slide.Shapes)
{
if (shape is IChart chart && chart.Name == chartName)
{
var worksheetIndex = 0;
var workbook = chart.ChartData.ChartDataWorkbook;
workbook.GetCell(worksheetIndex, 1, 0, "Modified Category 1");
workbook.GetCell(worksheetIndex, 2, 0, "Modified Category 2");
var series = chart.ChartData.Series[0];
workbook.GetCell(worksheetIndex, 0, 1, "New_Series 1");
series.DataPoints[0].Value.Data = 90;
series.DataPoints[1].Value.Data = 123;
series.DataPoints[2].Value.Data = 44;
series = chart.ChartData.Series[1];
workbook.GetCell(worksheetIndex, 0, 2, "New_Series 2");
series.DataPoints[0].Value.Data = 23;
series.DataPoints[1].Value.Data = 67;
series.DataPoints[2].Value.Data = 99;
var seriesNameCell = workbook.GetCell(worksheetIndex, 0, 3, "Series 3");
series = chart.ChartData.Series.Add(seriesNameCell, chart.Type);
var valueCell = workbook.GetCell(worksheetIndex, 1, 3, 20);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 2, 3, 50);
series.DataPoints.AddDataPointForBarSeries(valueCell);
valueCell = workbook.GetCell(worksheetIndex, 3, 3, 30);
series.DataPoints.AddDataPointForBarSeries(valueCell);
chart.Type = ChartType.ClusteredCylinder;
}
}
presentation.Save("AsposeChartModified.pptx", SaveFormat.Pptx);
Impostare l’intervallo dati per un grafico
Per ispezionare l’intervallo già utilizzato da un grafico esistente, vedi Retrieve a Chart’s Data Range.
Aspose.Slides per .NET offre la flessibilità di definire un intervallo dati specifico da un foglio di lavoro come origine per i dati del tuo grafico. Ciò significa che è possibile mappare direttamente una porzione del foglio di lavoro al grafico, consentendo di controllare quali celle contribuiscono alle serie e alle categorie del grafico. Di conseguenza, è possibile aggiornare e sincronizzare facilmente i grafici con le ultime modifiche dei dati nel foglio di lavoro, garantendo che le presentazioni PowerPoint riflettano informazioni attuali e accurate.
L’esempio utilizza SetRange per impostare l’origine dati del grafico denominato “My chart” sulla prima diapositiva di una presentazione esistente a Sheet1!A1:B4. Il foglio di lavoro del grafico deve contenere quel foglio e un intervallo popolato con un’intestazione di serie nella prima riga, etichette di categoria nella colonna A e tre valori nella colonna B.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
const string chartName = "My chart";
using var presentation = new Presentation("ExistingChart.pptx");
var slide = presentation.Slides[0];
foreach (IShape shape in slide.Shapes)
{
if (shape is IChart chart && chart.Name == chartName)
{
chart.ChartData.SetRange("Sheet1!A1:B4");
}
}
presentation.Save("SetDataRange.pptx", SaveFormat.Pptx);
Usare i marcatori predefiniti nei grafici
Quando si usano i marcatori predefiniti nei grafici, ogni serie del grafico ottiene automaticamente un simbolo di marcatore predefinito diverso.
L’esempio utilizza AddChart per creare un grafico con due serie a linee e una legenda. Le impostazioni Marker mantengono i simboli di marcatore assegnati automaticamente. La prima serie include un valore negativo e un valore mancante, rappresentato da null, mentre la seconda ha un valore per ogni categoria.
using Aspose.Slides;
using Aspose.Slides.Charts;
using Aspose.Slides.Export;
using var presentation = new Presentation();
var slide = presentation.Slides[0];
var chart = slide.Shapes.AddChart(ChartType.LineWithMarkers, 10, 10, 400, 400);
chart.ChartData.Series.Clear();
chart.ChartData.Categories.Clear();
var workbook = chart.ChartData.ChartDataWorkbook;
var seriesNameCell = workbook.GetCell(0, 0, 1, "Series 1");
var series = chart.ChartData.Series.Add(seriesNameCell, chart.Type);
var categoryCell = workbook.GetCell(0, 1, 0, "C1");
chart.ChartData.Categories.Add(categoryCell);
var valueCell = workbook.GetCell(0, 1, 1, 24);
series.DataPoints.AddDataPointForLineSeries(valueCell);
categoryCell = workbook.GetCell(0, 2, 0, "C2");
chart.ChartData.Categories.Add(categoryCell);
valueCell = workbook.GetCell(0, 2, 1, 23);
series.DataPoints.AddDataPointForLineSeries(valueCell);
categoryCell = workbook.GetCell(0, 3, 0, "C3");
chart.ChartData.Categories.Add(categoryCell);
valueCell = workbook.GetCell(0, 3, 1, -10);
series.DataPoints.AddDataPointForLineSeries(valueCell);
categoryCell = workbook.GetCell(0, 4, 0, "C4");
chart.ChartData.Categories.Add(categoryCell);
valueCell = workbook.GetCell(0, 4, 1, null);
series.DataPoints.AddDataPointForLineSeries(valueCell);
seriesNameCell = workbook.GetCell(0, 0, 2, "Series 2");
var secondSeries = chart.ChartData.Series.Add(seriesNameCell, chart.Type);
valueCell = workbook.GetCell(0, 1, 2, 30);
secondSeries.DataPoints.AddDataPointForLineSeries(valueCell);
valueCell = workbook.GetCell(0, 2, 2, 10);
secondSeries.DataPoints.AddDataPointForLineSeries(valueCell);
valueCell = workbook.GetCell(0, 3, 2, 60);
secondSeries.DataPoints.AddDataPointForLineSeries(valueCell);
valueCell = workbook.GetCell(0, 4, 2, 40);
secondSeries.DataPoints.AddDataPointForLineSeries(valueCell);
chart.HasLegend = true;
chart.Legend.Overlay = false;
presentation.Save("DefaultMarkersInChart.pptx", SaveFormat.Pptx);
FAQ
Quali tipi di grafico sono supportati da Aspose.Slides per .NET?
Aspose.Slides per .NET supporta una vasta gamma di tipi di grafico, tra cui barre, linee, torta, area, dispersione, istogramma, radar e molti altri. Questa flessibilità ti permette di scegliere il tipo di grafico più appropriato per le tue esigenze di visualizzazione dei dati.
Come aggiungere un nuovo grafico a una diapositiva?
Per aggiungere un grafico, innanzitutto crei un’istanza della classe Presentation, recuperi la diapositiva desiderata usando il suo indice, e poi chiami il metodo per aggiungere un grafico, specificando il tipo di grafico e i dati iniziali. Questo processo integra il grafico direttamente nella tua presentazione.
Come posso aggiornare i dati visualizzati in un grafico?
Puoi aggiornare i dati di un grafico accedendo alla sua cartella di lavoro dei dati (IChartDataWorkbook), cancellando eventuali serie e categorie predefinite, e poi aggiungendo i tuoi dati personalizzati. Questo ti consente di aggiornare programmaticamente il grafico per riflettere i dati più recenti.
È possibile personalizzare l’aspetto del grafico?
Sì, Aspose.Slides per .NET offre ampie opzioni di personalizzazione. Puoi modificare colori, caratteri, etichette, legende e altri elementi di formattazione per adattare l’aspetto del grafico ai tuoi specifici requisiti di progettazione.
Funzionalità correlata: Creare grafici modificabili.