Crea o Aggiorna Grafici per Presentazioni PowerPoint in PHP
Panoramica
Questo articolo fornisce una guida completa su come creare e personalizzare i grafici utilizzando Aspose.Slides per PHP via Java. Imparerai come aggiungere programmaticamente un grafico a una diapositiva, popolarlo con dati e applicare varie opzioni di formattazione per soddisfare i tuoi requisiti di progettazione specifici. Nel corso dell’articolo, esempi di codice dettagliati illustrano ogni passaggio, dall’inizializzazione della presentazione e dell’oggetto grafico alla configurazione di serie, assi e legende. Seguendo questa guida, otterrai una solida comprensione di come integrare la generazione dinamica di grafici nelle tue applicazioni PHP, semplificando il processo di creazione di presentazioni basate sui dati.
Crea un grafico
I grafici aiutano le persone a visualizzare rapidamente i dati e a ottenere intuizioni che potrebbero non essere immediatamente evidenti da una tabella o un foglio di calcolo.
Perché creare grafici?
- aggregare, condensare o sintetizzare grandi quantità di dati su una singola diapositiva in una presentazione;
- evidenziare modelli e tendenze nei dati;
- dedurre la direzione e la spinta dei dati nel tempo o rispetto a un’unità di misura specifica;
- individuare valori anomali, aberrazioni, deviazioni, errori e dati non sensati;
- comunicare o presentare dati complessi.
In PowerPoint, puoi creare grafici tramite la funzione Insert, che fornisce modelli per progettare molti tipi di grafici. Utilizzando Aspose.Slides, puoi creare sia grafici regolari (basati su tipi di grafico popolari) sia grafici personalizzati.
Note
Utilizza l’enumerazione ChartType nello spazio dei nomi aspose\slides. I valori di questa enumerazione corrispondono a diversi tipi di grafico.Crea grafici a colonne raggruppate
Questo 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 incorporata.
use aspose\slides\ChartType;
use aspose\slides\FillType;
use aspose\slides\NullableBool;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try
{
$slide = $presentation->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::ClusteredColumn, 20, 20, 500, 300);
$chart->getChartTitle()->addTextFrameForOverriding("Sample Title");
$chart->getChartTitle()->getTextFrameForOverriding()->getTextFrameFormat()->setCenterText(NullableBool::True);
$chart->getChartTitle()->setHeight(20);
$chart->setTitle(true);
$workbook = $chart->getChartData()->getChartDataWorkbook();
$worksheetIndex = 0;
$chart->getChartData()->getSeries()->clear();
$chart->getChartData()->getCategories()->clear();
$seriesNameCell = $workbook->getCell($worksheetIndex, 0, 1, "Series 1");
$chart->getChartData()->getSeries()->add($seriesNameCell, $chart->getType());
$seriesNameCell = $workbook->getCell($worksheetIndex, 0, 2, "Series 2");
$chart->getChartData()->getSeries()->add($seriesNameCell, $chart->getType());
$categoryCell = $workbook->getCell($worksheetIndex, 1, 0, "Category 1");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell($worksheetIndex, 2, 0, "Category 2");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell($worksheetIndex, 3, 0, "Category 3");
$chart->getChartData()->getCategories()->add($categoryCell);
$series = $chart->getChartData()->getSeries()->get_Item(0);
$valueCell = $workbook->getCell($worksheetIndex, 1, 1, 20);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, 2, 1, 50);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, 3, 1, 30);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$series->getFormat()->getFill()->setFillType(FillType::Solid);
$color = new Java("java.awt.Color", 255, 0, 0);
$series->getFormat()->getFill()->getSolidFillColor()->setColor($color);
$series = $chart->getChartData()->getSeries()->get_Item(1);
$valueCell = $workbook->getCell($worksheetIndex, 1, 2, 30);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, 2, 2, 10);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, 3, 2, 60);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$series->getFormat()->getFill()->setFillType(FillType::Solid);
$color = new Java("java.awt.Color", 0, 128, 0);
$series->getFormat()->getFill()->getSolidFillColor()->setColor($color);
$label = $series->getDataPoints()->get_Item(0)->getLabel();
$label->getDataLabelFormat()->setShowCategoryName(true);
$label = $series->getDataPoints()->get_Item(1)->getLabel();
$label->getDataLabelFormat()->setShowSeriesName(true);
$label = $series->getDataPoints()->get_Item(2)->getLabel();
$label->getDataLabelFormat()->setShowValue(true);
$label->getDataLabelFormat()->setShowSeriesName(true);
$label->getDataLabelFormat()->setSeparator("/");
$presentation->save("AsposeChart.pptx", SaveFormat::Pptx);
}
finally
{
$presentation->dispose();
}
Il risultato:

Crea grafici a dispersione
I grafici a dispersione (noti anche come scatter plot o grafici x-y) vengono spesso utilizzati per verificare modelli o dimostrare correlazioni tra due variabili.
Usa un grafico a dispersione quando:
- Hai dati numerici accoppiati.
- Hai due variabili che si associano bene.
- Vuoi determinare se le due variabili sono correlate.
- Hai una variabile indipendente che ha più valori per una variabile dipendente.
Questo esempio traccia due insiemi di valori x‑y. La prima serie utilizza linee rette con marcatori a stella, mentre la seconda utilizza linee morbide con marcatori a cerchio; entrambe usano una dimensione del marcatore di 10, configurata con setSize. Il metodo setSymbol controlla la forma del marcatore.
use aspose\slides\ChartType;
use aspose\slides\MarkerStyleType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try
{
$slide = $presentation->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::ScatterWithSmoothLines, 20, 20, 500, 300);
$workbook = $chart->getChartData()->getChartDataWorkbook();
$worksheetIndex = 0;
$chart->getChartData()->getSeries()->clear();
$seriesNameCell = $workbook->getCell($worksheetIndex, 1, 1, "Series 1");
$chart->getChartData()->getSeries()->add($seriesNameCell, $chart->getType());
$seriesNameCell = $workbook->getCell($worksheetIndex, 1, 3, "Series 2");
$chart->getChartData()->getSeries()->add($seriesNameCell, $chart->getType());
$series = $chart->getChartData()->getSeries()->get_Item(0);
$xValueCell = $workbook->getCell($worksheetIndex, 2, 1, 1);
$yValueCell = $workbook->getCell($worksheetIndex, 2, 2, 3);
$series->getDataPoints()->addDataPointForScatterSeries($xValueCell, $yValueCell);
$xValueCell = $workbook->getCell($worksheetIndex, 3, 1, 2);
$yValueCell = $workbook->getCell($worksheetIndex, 3, 2, 10);
$series->getDataPoints()->addDataPointForScatterSeries($xValueCell, $yValueCell);
$series->setType(ChartType::ScatterWithStraightLinesAndMarkers);
$series->getMarker()->setSize(10);
$series->getMarker()->setSymbol(MarkerStyleType::Star);
$series = $chart->getChartData()->getSeries()->get_Item(1);
$xValueCell = $workbook->getCell($worksheetIndex, 2, 3, 5);
$yValueCell = $workbook->getCell($worksheetIndex, 2, 4, 2);
$series->getDataPoints()->addDataPointForScatterSeries($xValueCell, $yValueCell);
$xValueCell = $workbook->getCell($worksheetIndex, 3, 3, 3);
$yValueCell = $workbook->getCell($worksheetIndex, 3, 4, 1);
$series->getDataPoints()->addDataPointForScatterSeries($xValueCell, $yValueCell);
$xValueCell = $workbook->getCell($worksheetIndex, 4, 3, 2);
$yValueCell = $workbook->getCell($worksheetIndex, 4, 4, 2);
$series->getDataPoints()->addDataPointForScatterSeries($xValueCell, $yValueCell);
$xValueCell = $workbook->getCell($worksheetIndex, 5, 3, 5);
$yValueCell = $workbook->getCell($worksheetIndex, 5, 4, 1);
$series->getDataPoints()->addDataPointForScatterSeries($xValueCell, $yValueCell);
$series->getMarker()->setSize(10);
$series->getMarker()->setSymbol(MarkerStyleType::Circle);
$presentation->save("AsposeChart.pptx", SaveFormat::Pptx);
}
finally
{
$presentation->dispose();
}
Il risultato:

Crea grafici a torta
I grafici a torta sono ideali per mostrare la relazione parte‑tutto nei dati, soprattutto quando i dati contengono etichette categoriche con valori numerici. Tuttavia, se i tuoi dati contengono molte parti o etichette, potresti considerare l’uso di un grafico a barre.
Questo 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 valore, percentuale e informazioni sulla serie. Il metodo setFirstSliceAngle imposta la prima fetta a partire da 180 gradi.
use aspose\slides\ChartType;
use aspose\slides\FillType;
use aspose\slides\LineDashStyle;
use aspose\slides\LineStyle;
use aspose\slides\NullableBool;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try
{
$slide = $presentation->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::Pie, 20, 20, 500, 300);
$chart->getChartTitle()->addTextFrameForOverriding("Sample Title");
$chart->getChartTitle()->getTextFrameForOverriding()->getTextFrameFormat()->setCenterText(NullableBool::True);
$chart->getChartTitle()->setHeight(20);
$chart->setTitle(true);
$chart->getChartData()->getSeries()->get_Item(0)->getLabels()->getDefaultDataLabelFormat()->setShowValue(true);
$workbook = $chart->getChartData()->getChartDataWorkbook();
$worksheetIndex = 0;
$chart->getChartData()->getSeries()->clear();
$chart->getChartData()->getCategories()->clear();
$categoryCell = $workbook->getCell(0, 1, 0, "1st Qtr");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, 2, 0, "2nd Qtr");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, 3, 0, "3rd Qtr");
$chart->getChartData()->getCategories()->add($categoryCell);
$seriesNameCell = $workbook->getCell(0, 0, 1, "Series 1");
$series = $chart->getChartData()->getSeries()->add($seriesNameCell, $chart->getType());
$valueCell = $workbook->getCell($worksheetIndex, 1, 1, 20);
$series->getDataPoints()->addDataPointForPieSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, 2, 1, 50);
$series->getDataPoints()->addDataPointForPieSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, 3, 1, 30);
$series->getDataPoints()->addDataPointForPieSeries($valueCell);
$chart->getChartData()->getSeriesGroups()->get_Item(0)->setColorVaried(true);
$firstSlice = $series->getDataPoints()->get_Item(0);
$firstSlice->getFormat()->getFill()->setFillType(FillType::Solid);
$color = new Java("java.awt.Color", 0, 255, 255);
$firstSlice->getFormat()->getFill()->getSolidFillColor()->setColor($color);
$firstSlice->getFormat()->getLine()->getFillFormat()->setFillType(FillType::Solid);
$color = new Java("java.awt.Color", 128, 128, 128);
$firstSlice->getFormat()->getLine()->getFillFormat()->getSolidFillColor()->setColor($color);
$firstSlice->getFormat()->getLine()->setWidth(3.0);
$firstSlice->getFormat()->getLine()->setStyle(LineStyle::ThinThick);
$firstSlice->getFormat()->getLine()->setDashStyle(LineDashStyle::LargeDash);
$secondSlice = $series->getDataPoints()->get_Item(1);
$secondSlice->getFormat()->getFill()->setFillType(FillType::Solid);
$color = new Java("java.awt.Color", 165, 42, 42);
$secondSlice->getFormat()->getFill()->getSolidFillColor()->setColor($color);
$secondSlice->getFormat()->getLine()->getFillFormat()->setFillType(FillType::Solid);
$color = new Java("java.awt.Color", 0, 0, 255);
$secondSlice->getFormat()->getLine()->getFillFormat()->getSolidFillColor()->setColor($color);
$secondSlice->getFormat()->getLine()->setWidth(3.0);
$secondSlice->getFormat()->getLine()->setStyle(LineStyle::Single);
$secondSlice->getFormat()->getLine()->setDashStyle(LineDashStyle::LargeDashDot);
$thirdSlice = $series->getDataPoints()->get_Item(2);
$thirdSlice->getFormat()->getFill()->setFillType(FillType::Solid);
$color = new Java("java.awt.Color", 255, 127, 80);
$thirdSlice->getFormat()->getFill()->getSolidFillColor()->setColor($color);
$thirdSlice->getFormat()->getLine()->getFillFormat()->setFillType(FillType::Solid);
$color = new Java("java.awt.Color", 255, 0, 0);
$thirdSlice->getFormat()->getLine()->getFillFormat()->getSolidFillColor()->setColor($color);
$thirdSlice->getFormat()->getLine()->setWidth(2.0);
$thirdSlice->getFormat()->getLine()->setStyle(LineStyle::ThinThin);
$thirdSlice->getFormat()->getLine()->setDashStyle(LineDashStyle::LargeDashDotDot);
$firstLabel = $series->getDataPoints()->get_Item(0)->getLabel();
$firstLabel->getDataLabelFormat()->setShowValue(true);
$secondLabel = $series->getDataPoints()->get_Item(1)->getLabel();
$secondLabel->getDataLabelFormat()->setShowValue(true);
$secondLabel->getDataLabelFormat()->setShowLegendKey(true);
$secondLabel->getDataLabelFormat()->setShowPercentage(true);
$thirdLabel = $series->getDataPoints()->get_Item(2)->getLabel();
$thirdLabel->getDataLabelFormat()->setShowSeriesName(true);
$thirdLabel->getDataLabelFormat()->setShowPercentage(true);
$series->getLabels()->getDefaultDataLabelFormat()->setShowLeaderLines(true);
$chart->getChartData()->getSeriesGroups()->get_Item(0)->setFirstSliceAngle(180);
$presentation->save("PieChart.pptx", SaveFormat::Pptx);
}
finally
{
$presentation->dispose();
}
Il risultato:

Per migliorare la leggibilità delle etichette all’esterno della torta, consulta Format Leader Lines per un esempio che posiziona le etichette e personalizza colore, larghezza, trasparenza e stile delle linee.
Crea grafici a linee
I grafici a linee (noti anche come line graph) sono ideali in situazioni in cui si desidera mostrare cambiamenti di valore nel tempo. Utilizzando un grafico a linee, è possibile confrontare una grande quantità di dati contemporaneamente, tracciare cambiamenti e tendenze nel tempo, evidenziare anomalie nelle serie di dati e altro.
Questo esempio utilizza addChart per creare un grafico a linee con le serie e le categorie predefinite, quindi salva la presentazione in formato PPTX.
use aspose\slides\ChartType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try
{
$slide = $presentation->getSlides()->get_Item(0);
$lineChart = $slide->getShapes()->addChart(ChartType::Line, 20, 20, 500, 300);
$presentation->save("lineChart.pptx", SaveFormat::Pptx);
}
finally
{
$presentation->dispose();
}
Per impostazione predefinita, i punti di un grafico a linee sono collegati da linee continue rette. Se desideri che i punti siano collegati da linee tratteggiate, chiama setDashStyle con un valore da LineDashStyle:
use aspose\slides\ChartType;
use aspose\slides\LineDashStyle;
use aspose\slides\Presentation;
$presentation = new Presentation();
try
{
$slide = $presentation->getSlides()->get_Item(0);
$lineChart = $slide->getShapes()->addChart(ChartType::Line, 20, 20, 500, 300);
for ($i = 0; $i < java_values($lineChart->getChartData()->getSeries()->size()); $i++)
{
$series = $lineChart->getChartData()->getSeries()->get_Item($i);
$series->getFormat()->getLine()->setDashStyle(LineDashStyle::Dash);
}
}
finally
{
$presentation->dispose();
}
Il risultato:

Crea grafici a mappa ad albero
I grafici a mappa ad albero sono ideali per i dati di vendita quando si desidera mostrare la dimensione relativa delle categorie di dati e richiamare rapidamente l’attenzione sugli elementi che contribuiscono maggiormente all’interno di ciascuna categoria.
Questo 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 delle categorie e le etichette genitore sovrapposte identificano la gerarchia.
use aspose\slides\ChartType;
use aspose\slides\ParentLabelLayoutType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try
{
$slide = $presentation->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::Treemap, 20, 20, 500, 300);
$chart->getChartData()->getCategories()->clear();
$chart->getChartData()->getSeries()->clear();
$workbook = $chart->getChartData()->getChartDataWorkbook();
$workbook->clear(0);
// Ramo 1
$categoryCell = $workbook->getCell(0, "C1", "Leaf1");
$leaf = $chart->getChartData()->getCategories()->add($categoryCell);
$leaf->getGroupingLevels()->setGroupingItem(1, "Stem1");
$leaf->getGroupingLevels()->setGroupingItem(2, "Branch1");
$categoryCell = $workbook->getCell(0, "C2", "Leaf2");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, "C3", "Leaf3");
$leaf = $chart->getChartData()->getCategories()->add($categoryCell);
$leaf->getGroupingLevels()->setGroupingItem(1, "Stem2");
$categoryCell = $workbook->getCell(0, "C4", "Leaf4");
$chart->getChartData()->getCategories()->add($categoryCell);
// Ramo 2
$categoryCell = $workbook->getCell(0, "C5", "Leaf5");
$leaf = $chart->getChartData()->getCategories()->add($categoryCell);
$leaf->getGroupingLevels()->setGroupingItem(1, "Stem3");
$leaf->getGroupingLevels()->setGroupingItem(2, "Branch2");
$categoryCell = $workbook->getCell(0, "C6", "Leaf6");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, "C7", "Leaf7");
$leaf = $chart->getChartData()->getCategories()->add($categoryCell);
$leaf->getGroupingLevels()->setGroupingItem(1, "Stem4");
$categoryCell = $workbook->getCell(0, "C8", "Leaf8");
$chart->getChartData()->getCategories()->add($categoryCell);
$series = $chart->getChartData()->getSeries()->add(ChartType::Treemap);
$series->getLabels()->getDefaultDataLabelFormat()->setShowCategoryName(true);
$valueCell = $workbook->getCell(0, "D1", 4);
$series->getDataPoints()->addDataPointForTreemapSeries($valueCell);
$valueCell = $workbook->getCell(0, "D2", 5);
$series->getDataPoints()->addDataPointForTreemapSeries($valueCell);
$valueCell = $workbook->getCell(0, "D3", 3);
$series->getDataPoints()->addDataPointForTreemapSeries($valueCell);
$valueCell = $workbook->getCell(0, "D4", 6);
$series->getDataPoints()->addDataPointForTreemapSeries($valueCell);
$valueCell = $workbook->getCell(0, "D5", 9);
$series->getDataPoints()->addDataPointForTreemapSeries($valueCell);
$valueCell = $workbook->getCell(0, "D6", 9);
$series->getDataPoints()->addDataPointForTreemapSeries($valueCell);
$valueCell = $workbook->getCell(0, "D7", 4);
$series->getDataPoints()->addDataPointForTreemapSeries($valueCell);
$valueCell = $workbook->getCell(0, "D8", 3);
$series->getDataPoints()->addDataPointForTreemapSeries($valueCell);
$series->setParentLabelLayout(ParentLabelLayoutType::Overlapping);
$presentation->save("Treemap.pptx", SaveFormat::Pptx);
}
finally
{
$presentation->dispose();
}
Il risultato:

Crea grafici a cascata
Un grafico a cascata mostra come le variazioni positive e negative contribuiscono a un valore finale. Ad esempio, un dettaglio di profitto può iniziare dal fatturato, sottrarre i costi e aggiungere altri introiti. Le colonne di variazione fluttuano tra il valore corrente precedente e quello nuovo, mentre le colonne totali si estendono dallo zero al valore accumulato.
La tabella 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 proventi | 50 | Aumento |
| Utile netto | 450 | Totale finale |
Per contrassegnare un punto come totale intermedio o finale, chiama il suo setSetAsTotal con 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). Il metodo contrassegna la colonna come totale; non calcola né popola il valore della cella. Non usare zero come segnaposto né 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 PHP crea un grafico usando 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 di valore viene aggiunta con addDataPointForWaterfallSeries. Gli indici dei punti basati su zero 2 e 5 identificano i totali intermedi e finali. Chiamando setShowValue con true si visualizzano gli importi sulle colonne. Non è necessaria alcuna presentazione o cartella di lavoro di ingresso.
use aspose\slides\ChartType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try
{
$slide = $presentation->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::Waterfall, 20, 20, 680, 200);
$chart->setTitle(true);
$chart->getChartTitle()->addTextFrameForOverriding("Profit breakdown (USD thousands)");
$chart->getChartTitle()->setOverlay(false);
$chart->setLegend(false);
$chart->getChartData()->getSeries()->clear();
$chart->getChartData()->getCategories()->clear();
$workbook = $chart->getChartData()->getChartDataWorkbook();
$worksheetIndex = 0;
$workbook->clear($worksheetIndex);
$seriesNameCell = $workbook->getCell($worksheetIndex, 0, 1, "Profit");
$series = $chart->getChartData()->getSeries()->add($seriesNameCell, ChartType::Waterfall);
$categories = [ "Revenue", "Cost of sales", "Gross profit", "Operating expenses", "Other income", "Net profit" ];
$values = [ 1000, -400, 600, -200, 50, 450 ];
for ($i = 0; $i < count($categories); $i++)
{
$categoryCell = $workbook->getCell($worksheetIndex, $i + 1, 0, $categories[$i]);
$chart->getChartData()->getCategories()->add($categoryCell);
$valueCell = $workbook->getCell($worksheetIndex, $i + 1, 1, $values[$i]);
$series->getDataPoints()->addDataPointForWaterfallSeries($valueCell);
}
// Il terzo e il sesto punto rappresentano totali, non variazioni aggiuntive.
$series->getDataPoints()->get_Item(2)->setSetAsTotal(true);
$series->getDataPoints()->get_Item(5)->setSetAsTotal(true);
$series->setShowConnectorLines(true);
$series->getLabels()->getDefaultDataLabelFormat()->setShowValue(true);
$presentation->save("WaterfallChart.pptx", SaveFormat::Pptx);
}
finally
{
$presentation->dispose();
}
Chiama il metodo a livello di serie setShowConnectorLines con true per mostrare le linee che collegano le colonne consecutive e rendere più facile seguire il valore corrente. Per nascondere queste linee, sostituisci la chiamata al metodo nell’esempio con $series->setShowConnectorLines(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, entrambi i totali disegnati dallo zero:

Crea grafici azionari
I grafici azionari vengono utilizzati per visualizzare dati finanziari come prezzi di apertura, massimi, minimi e chiusura, contribuendo ad analizzare le tendenze di mercato e la volatilità. Offrono informazioni essenziali sulle performance di un titolo, aiutando investitori e analisti a prendere decisioni informate.
Questo esempio crea un grafico open‑high‑low‑close con quattro serie in quest’ordine e tre categorie. Il metodo setUpDownBars abilita le barre su/giù, e getHiLowLinesFormat controlla le linee alto‑basso che mostrano gli intervalli di prezzo, mentre le linee delle singole serie e le linee della griglia orizzontale minore sono nascoste.
use aspose\slides\ChartType;
use aspose\slides\FillType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try
{
$slide = $presentation->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::OpenHighLowClose, 20, 20, 500, 300, false);
$workbook = $chart->getChartData()->getChartDataWorkbook();
$categoryCell = $workbook->getCell(0, 1, 0, "A");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, 2, 0, "B");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, 3, 0, "C");
$chart->getChartData()->getCategories()->add($categoryCell);
$seriesNameCell = $workbook->getCell(0, 0, 1, "Open");
$chart->getChartData()->getSeries()->add($seriesNameCell, ChartType::OpenHighLowClose);
$seriesNameCell = $workbook->getCell(0, 0, 2, "High");
$chart->getChartData()->getSeries()->add($seriesNameCell, ChartType::OpenHighLowClose);
$seriesNameCell = $workbook->getCell(0, 0, 3, "Low");
$chart->getChartData()->getSeries()->add($seriesNameCell, ChartType::OpenHighLowClose);
$seriesNameCell = $workbook->getCell(0, 0, 4, "Close");
$chart->getChartData()->getSeries()->add($seriesNameCell, ChartType::OpenHighLowClose);
$series = $chart->getChartData()->getSeries()->get_Item(0);
$valueCell = $workbook->getCell(0, 1, 1, 72);
$series->getDataPoints()->addDataPointForStockSeries($valueCell);
$valueCell = $workbook->getCell(0, 2, 1, 25);
$series->getDataPoints()->addDataPointForStockSeries($valueCell);
$valueCell = $workbook->getCell(0, 3, 1, 38);
$series->getDataPoints()->addDataPointForStockSeries($valueCell);
$series = $chart->getChartData()->getSeries()->get_Item(1);
$valueCell = $workbook->getCell(0, 1, 2, 172);
$series->getDataPoints()->addDataPointForStockSeries($valueCell);
$valueCell = $workbook->getCell(0, 2, 2, 57);
$series->getDataPoints()->addDataPointForStockSeries($valueCell);
$valueCell = $workbook->getCell(0, 3, 2, 57);
$series->getDataPoints()->addDataPointForStockSeries($valueCell);
$series = $chart->getChartData()->getSeries()->get_Item(2);
$valueCell = $workbook->getCell(0, 1, 3, 12);
$series->getDataPoints()->addDataPointForStockSeries($valueCell);
$valueCell = $workbook->getCell(0, 2, 3, 12);
$series->getDataPoints()->addDataPointForStockSeries($valueCell);
$valueCell = $workbook->getCell(0, 3, 3, 13);
$series->getDataPoints()->addDataPointForStockSeries($valueCell);
$series = $chart->getChartData()->getSeries()->get_Item(3);
$valueCell = $workbook->getCell(0, 1, 4, 25);
$series->getDataPoints()->addDataPointForStockSeries($valueCell);
$valueCell = $workbook->getCell(0, 2, 4, 38);
$series->getDataPoints()->addDataPointForStockSeries($valueCell);
$valueCell = $workbook->getCell(0, 3, 4, 50);
$series->getDataPoints()->addDataPointForStockSeries($valueCell);
$chart->getChartData()->getSeriesGroups()->get_Item(0)->getUpDownBars()->setUpDownBars(true);
$chart->getChartData()->getSeriesGroups()->get_Item(0)->getHiLowLinesFormat()->getLine()->getFillFormat()->setFillType(FillType::Solid);
for ($i = 0; $i < java_values($chart->getChartData()->getSeries()->size()); $i++)
{
$stockSeries = $chart->getChartData()->getSeries()->get_Item($i);
$stockSeries->getFormat()->getLine()->getFillFormat()->setFillType(FillType::NoFill);
}
$chart->getAxes()->getVerticalAxis()->getMinorGridLinesFormat()->getLine()->getFillFormat()->setFillType(FillType::NoFill);
$presentation->save("Stock-chart.pptx", SaveFormat::Pptx);
}
finally
{
$presentation->dispose();
}
Il risultato:

Crea grafici a scatola e baffi
I grafici a scatola e baffi vengono utilizzati per mostrare la distribuzione dei dati riassumendo misure statistiche chiave, come la mediana, i quartili e i potenziali 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.
Questo esempio crea un grafico a scatola e baffi da sei valori. Il metodo setQuartileMethod seleziona il calcolo dei quartili esclusivi. L’esempio abilita anche le linee della media, i marcatori della media, i punti interni e i punti anomali.
use aspose\slides\ChartType;
use aspose\slides\Presentation;
use aspose\slides\QuartileMethodType;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try
{
$slide = $presentation->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::BoxAndWhisker, 20, 20, 500, 300);
$chart->getChartData()->getCategories()->clear();
$chart->getChartData()->getSeries()->clear();
$workbook = $chart->getChartData()->getChartDataWorkbook();
$workbook->clear(0);
$categoryCell = $workbook->getCell(0, "A1", "Category 1");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, "A2", "Category 2");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, "A3", "Category 3");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, "A4", "Category 4");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, "A5", "Category 5");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, "A6", "Category 6");
$chart->getChartData()->getCategories()->add($categoryCell);
$series = $chart->getChartData()->getSeries()->add(ChartType::BoxAndWhisker);
$series->setQuartileMethod(QuartileMethodType::Exclusive);
$series->setShowMeanLine(true);
$series->setShowMeanMarkers(true);
$series->setShowInnerPoints(true);
$series->setShowOutlierPoints(true);
$valueCell = $workbook->getCell(0, "B1", 15);
$series->getDataPoints()->addDataPointForBoxAndWhiskerSeries($valueCell);
$valueCell = $workbook->getCell(0, "B2", 41);
$series->getDataPoints()->addDataPointForBoxAndWhiskerSeries($valueCell);
$valueCell = $workbook->getCell(0, "B3", 16);
$series->getDataPoints()->addDataPointForBoxAndWhiskerSeries($valueCell);
$valueCell = $workbook->getCell(0, "B4", 10);
$series->getDataPoints()->addDataPointForBoxAndWhiskerSeries($valueCell);
$valueCell = $workbook->getCell(0, "B5", 23);
$series->getDataPoints()->addDataPointForBoxAndWhiskerSeries($valueCell);
$valueCell = $workbook->getCell(0, "B6", 16);
$series->getDataPoints()->addDataPointForBoxAndWhiskerSeries($valueCell);
$presentation->save("BoxAndWhisker.pptx", SaveFormat::Pptx);
}
finally
{
$presentation->dispose();
}
Crea grafici a imbuto
I grafici a imbuto vengono utilizzati per visualizzare processi che coinvolgono fasi sequenziali, dove il volume dei dati diminuisce man mano che avanza da una fase all’altra. Sono particolarmente utili per analizzare i tassi di conversione, identificare colli di bottiglia e monitorare l’efficienza dei processi di vendita o marketing.
Questo 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 ingresso deve contenere almeno una diapositiva.
use aspose\slides\ChartType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation("test.pptx");
try
{
$slide = $presentation->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::Funnel, 50, 50, 500, 400);
$chart->getChartData()->getCategories()->clear();
$chart->getChartData()->getSeries()->clear();
$workbook = $chart->getChartData()->getChartDataWorkbook();
$workbook->clear(0);
$categoryCell = $workbook->getCell(0, "A1", "Category 1");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, "A2", "Category 2");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, "A3", "Category 3");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, "A4", "Category 4");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, "A5", "Category 5");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, "A6", "Category 6");
$chart->getChartData()->getCategories()->add($categoryCell);
$series = $chart->getChartData()->getSeries()->add(ChartType::Funnel);
$valueCell = $workbook->getCell(0, "B1", 50);
$series->getDataPoints()->addDataPointForFunnelSeries($valueCell);
$valueCell = $workbook->getCell(0, "B2", 100);
$series->getDataPoints()->addDataPointForFunnelSeries($valueCell);
$valueCell = $workbook->getCell(0, "B3", 200);
$series->getDataPoints()->addDataPointForFunnelSeries($valueCell);
$valueCell = $workbook->getCell(0, "B4", 300);
$series->getDataPoints()->addDataPointForFunnelSeries($valueCell);
$valueCell = $workbook->getCell(0, "B5", 400);
$series->getDataPoints()->addDataPointForFunnelSeries($valueCell);
$valueCell = $workbook->getCell(0, "B6", 500);
$series->getDataPoints()->addDataPointForFunnelSeries($valueCell);
$presentation->save("Funnel.pptx", SaveFormat::Pptx);
}
finally
{
$presentation->dispose();
}
Il risultato:

Crea grafici a raggiera
I grafici a raggiera vengono utilizzati per visualizzare dati gerarchici, mostrando i livelli come anelli concentrici. Aiutano a illustrare le relazioni parte‑tutto e sono ideali per rappresentare categorie e sottocategorie nidificate in un formato chiaro e compatto.
Questo esempio crea un grafico a raggiera 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 delle categorie identificano i segmenti e i valori determinano le loro dimensioni relative.
use aspose\slides\ChartType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try
{
$slide = $presentation->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::Sunburst, 20, 20, 500, 300);
$chart->getChartData()->getCategories()->clear();
$chart->getChartData()->getSeries()->clear();
$workbook = $chart->getChartData()->getChartDataWorkbook();
$workbook->clear(0);
// Ramo 1
$categoryCell = $workbook->getCell(0, "C1", "Leaf1");
$leaf = $chart->getChartData()->getCategories()->add($categoryCell);
$leaf->getGroupingLevels()->setGroupingItem(1, "Stem1");
$leaf->getGroupingLevels()->setGroupingItem(2, "Branch1");
$categoryCell = $workbook->getCell(0, "C2", "Leaf2");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, "C3", "Leaf3");
$leaf = $chart->getChartData()->getCategories()->add($categoryCell);
$leaf->getGroupingLevels()->setGroupingItem(1, "Stem2");
$categoryCell = $workbook->getCell(0, "C4", "Leaf4");
$chart->getChartData()->getCategories()->add($categoryCell);
// Ramo 2
$categoryCell = $workbook->getCell(0, "C5", "Leaf5");
$leaf = $chart->getChartData()->getCategories()->add($categoryCell);
$leaf->getGroupingLevels()->setGroupingItem(1, "Stem3");
$leaf->getGroupingLevels()->setGroupingItem(2, "Branch2");
$categoryCell = $workbook->getCell(0, "C6", "Leaf6");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, "C7", "Leaf7");
$leaf = $chart->getChartData()->getCategories()->add($categoryCell);
$leaf->getGroupingLevels()->setGroupingItem(1, "Stem4");
$categoryCell = $workbook->getCell(0, "C8", "Leaf8");
$chart->getChartData()->getCategories()->add($categoryCell);
$series = $chart->getChartData()->getSeries()->add(ChartType::Sunburst);
$series->getLabels()->getDefaultDataLabelFormat()->setShowCategoryName(true);
$valueCell = $workbook->getCell(0, "D1", 4);
$series->getDataPoints()->addDataPointForSunburstSeries($valueCell);
$valueCell = $workbook->getCell(0, "D2", 5);
$series->getDataPoints()->addDataPointForSunburstSeries($valueCell);
$valueCell = $workbook->getCell(0, "D3", 3);
$series->getDataPoints()->addDataPointForSunburstSeries($valueCell);
$valueCell = $workbook->getCell(0, "D4", 6);
$series->getDataPoints()->addDataPointForSunburstSeries($valueCell);
$valueCell = $workbook->getCell(0, "D5", 9);
$series->getDataPoints()->addDataPointForSunburstSeries($valueCell);
$valueCell = $workbook->getCell(0, "D6", 9);
$series->getDataPoints()->addDataPointForSunburstSeries($valueCell);
$valueCell = $workbook->getCell(0, "D7", 4);
$series->getDataPoints()->addDataPointForSunburstSeries($valueCell);
$valueCell = $workbook->getCell(0, "D8", 3);
$series->getDataPoints()->addDataPointForSunburstSeries($valueCell);
$presentation->save("Sunburst.pptx", SaveFormat::Pptx);
}
finally
{
$presentation->dispose();
}
Il risultato:

Crea istogrammi
Gli istogrammi mostrano come i valori numerici sono distribuiti in intervalli (bin). Per i report mensili sui tempi di elaborazione degli ordini, utilizza la stessa larghezza di bin e le stesse soglie per mantenere gli intervalli coerenti e rendere più facile confrontare le distribuzioni.
Chiama setAggregationType sull’asse orizzontale con una modalità da AxisAggregationType. setBinWidth si applica solo a ByBinWidth; setNumberOfBins si applica solo a ByNumberOfBins. Usa Automatic per consentire al grafico di scegliere i bin.
Usa una larghezza di bin fissa
Questo esempio raggruppa i tempi di elaborazione in intervalli di 10 minuti, con bin separati per tempi pari o inferiori a 10 minuti e superiori a 40 minuti. Abilita questi bin con setUnderflowBin e setOverflowBin. Quando fornisci soglie esplicite tramite setUnderflowBin e setOverflowBin, chiama setAutomaticUnderflowBin e setAutomaticOverflowBin con false.
use aspose\slides\AxisAggregationType;
use aspose\slides\ChartType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try
{
$slide = $presentation->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::Histogram, 20, 20, 680, 200);
$chart->setLegend(false);
$chart->setTitle(false);
$chart->getChartData()->getCategories()->clear();
$chart->getChartData()->getSeries()->clear();
$workbook = $chart->getChartData()->getChartDataWorkbook();
$workbook->clear(0);
$series = $chart->getChartData()->getSeries()->add(ChartType::Histogram);
$values = [ 5, 10, 10, 11, 15, 20, 20, 21, 25, 30, 30, 31, 35, 40, 40, 41, 50 ];
for ($i = 0; $i < count($values); $i++)
{
$valueCell = $workbook->getCell(0, $i, 0, $values[$i]);
$series->getDataPoints()->addDataPointForHistogramSeries($valueCell);
}
$axis = $chart->getAxes()->getHorizontalAxis();
$axis->setAggregationType(AxisAggregationType::ByBinWidth);
$axis->setBinWidth(10);
$axis->setUnderflowBin(true);
$axis->setAutomaticUnderflowBin(false);
$axis->setUnderflowBin(10);
$axis->setOverflowBin(true);
$axis->setAutomaticOverflowBin(false);
$axis->setOverflowBin(40);
$presentation->save("Histogram-bin-width.pptx", SaveFormat::Pptx);
}
finally
{
$presentation->dispose();
}
La presentazione visualizzata in PowerPoint mostra conteggi dei bin di 3, 4, 4, 4 e 2. Il bin di underflow include 5, 10, 10, quindi la sua soglia è inclusiva. Il bin di overflow include solo 41, 50, quindi la sua soglia è esclusiva; entrambi i valori uguali a 40 rimangono nell’intervallo precedente. I bin interni contengono valori maggiori del loro limite inferiore e minori o uguali al loro limite superiore.

Specifica il numero di bin
Per un livello di dettaglio fisso invece di intervalli fissi, richiedi cinque bin per gli stessi dati. Questo esempio lascia i bin di underflow e overflow disabilitati. Il grafico determina la larghezza dai dati, quindi i limiti possono cambiare tra i report.
use aspose\slides\AxisAggregationType;
use aspose\slides\ChartType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try
{
$slide = $presentation->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::Histogram, 20, 20, 680, 200);
$chart->setLegend(false);
$chart->setTitle(false);
$chart->getChartData()->getCategories()->clear();
$chart->getChartData()->getSeries()->clear();
$workbook = $chart->getChartData()->getChartDataWorkbook();
$workbook->clear(0);
$series = $chart->getChartData()->getSeries()->add(ChartType::Histogram);
$values = [ 5, 10, 10, 11, 15, 20, 20, 21, 25, 30, 30, 31, 35, 40, 40, 41, 50 ];
for ($i = 0; $i < count($values); $i++)
{
$valueCell = $workbook->getCell(0, $i, 0, $values[$i]);
$series->getDataPoints()->addDataPointForHistogramSeries($valueCell);
}
$axis = $chart->getAxes()->getHorizontalAxis();
$axis->setAggregationType(AxisAggregationType::ByNumberOfBins);
$axis->setNumberOfBins(5);
$presentation->save("Histogram-bin-count.pptx", SaveFormat::Pptx);
}
finally
{
$presentation->dispose();
}
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 limiti differiscono.

Crea grafici radar
I grafici radar sono utilizzati per visualizzare dati multivariati in un formato bidimensionale, consentendo un facile confronto di più variabili contemporaneamente. Sono particolarmente utili per identificare pattern, punti di forza e debolezze su più metriche di prestazione o attributi.
Questo esempio utilizza addChart per creare un grafico radar con le serie e le categorie predefinite, quindi salva la presentazione in formato PPTX.
use aspose\slides\ChartType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try
{
$slide = $presentation->getSlides()->get_Item(0);
$slide->getShapes()->addChart(ChartType::Radar, 20, 20, 500, 300);
$presentation->save("Radar-chart.pptx", SaveFormat::Pptx);
}
finally
{
$presentation->dispose();
}
Il risultato:

Crea grafici multi categoria
I grafici multi‑categoria sono utilizzati per visualizzare dati che coinvolgono più raggruppamenti categorici, consentendo di confrontare i valori attraverso più dimensioni simultaneamente. Sono particolarmente utili quando è necessario analizzare tendenze e relazioni all’interno di set di dati complessi e multilivello.
Questo esempio crea un grafico a colonne raggruppate con otto categorie disposte in quattro coppie. Il metodo setGroupingItem assegna a ogni coppia un’etichetta condivisa al livello di raggruppamento 1, producendo un asse delle categorie con etichette sia individuali sia di gruppo.
use aspose\slides\ChartType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try
{
$slide = $presentation->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::ClusteredColumn, 20, 20, 500, 300);
$chart->getChartData()->getSeries()->clear();
$chart->getChartData()->getCategories()->clear();
$workbook = $chart->getChartData()->getChartDataWorkbook();
$workbook->clear(0);
$worksheetIndex = 0;
$categoryCell = $workbook->getCell(0, "c2", "A");
$category = $chart->getChartData()->getCategories()->add($categoryCell);
$category->getGroupingLevels()->setGroupingItem(1, "Group1");
$categoryCell = $workbook->getCell(0, "c3", "B");
$category = $chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, "c4", "C");
$category = $chart->getChartData()->getCategories()->add($categoryCell);
$category->getGroupingLevels()->setGroupingItem(1, "Group2");
$categoryCell = $workbook->getCell(0, "c5", "D");
$category = $chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, "c6", "E");
$category = $chart->getChartData()->getCategories()->add($categoryCell);
$category->getGroupingLevels()->setGroupingItem(1, "Group3");
$categoryCell = $workbook->getCell(0, "c7", "F");
$category = $chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell(0, "c8", "G");
$category = $chart->getChartData()->getCategories()->add($categoryCell);
$category->getGroupingLevels()->setGroupingItem(1, "Group4");
$categoryCell = $workbook->getCell(0, "c9", "H");
$category = $chart->getChartData()->getCategories()->add($categoryCell);
$seriesNameCell = $workbook->getCell(0, "D1", "Series 1");
$series = $chart->getChartData()->getSeries()->add($seriesNameCell, ChartType::ClusteredColumn);
$valueCell = $workbook->getCell($worksheetIndex, "D2", 10);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, "D3", 20);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, "D4", 30);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, "D5", 40);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, "D6", 50);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, "D7", 60);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, "D8", 70);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, "D9", 80);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$presentation->save("AsposeChart.pptx", SaveFormat::Pptx);
}
finally
{
$presentation->dispose();
}
Il risultato:

Crea grafici di mappa
I grafici di mappa sono utilizzati per visualizzare dati geografici mappando le informazioni su posizioni specifiche come paesi, stati o città. Sono particolarmente utili per analizzare tendenze regionali, dati demografici e distribuzioni spaziali in modo chiaro e coinvolgente.
Questo esempio utilizza addChart per creare un grafico di mappa con i dati geografici predefiniti e lo salva per la visualizzazione in PowerPoint.
use aspose\slides\ChartType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try
{
$slide = $presentation->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::Map, 20, 20, 500, 300);
$presentation->save("mapChart.pptx", SaveFormat::Pptx);
}
finally
{
$presentation->dispose();
}
Il risultato:

Note
L’immagine sopra mostra la presentazione salvata aperta in PowerPoint. Aspose.Slides scrive correttamente il grafico di mappa e i suoi dati, ma non disegna i grafici di mappa da solo: quando una diapositiva che ne contiene viene renderizzata in un’immagine o convertita in PDF o SVG, l’area del grafico risulta vuota. Le altre forme sulla stessa diapositiva non sono influenzate.Crea 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 le differenze tra due o più set di dati, aiutando a identificare le relazioni tra essi.

Questo esempio combina due serie a colonne raggruppate con una serie a linee su quattro categorie. Le colonne usano l’asse verticale primario, mentre setPlotOnSecondAxis posiziona la linea su un asse verticale secondario a destra. Le funzioni di supporto configurano titolo, legenda, etichette degli assi e linee della griglia.
use aspose\slides\AxisPositionType;
use aspose\slides\ChartType;
use aspose\slides\CrossesType;
use aspose\slides\FillType;
use aspose\slides\LegendPositionType;
use aspose\slides\NullableBool;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
function createComboChart()
{
$presentation = new Presentation();
try
{
$chart = createChartWithFirstSeries($presentation->getSlides()->get_Item(0));
addSecondSeriesToChart($chart);
addThirdSeriesToChart($chart);
setPrimaryAxesFormat($chart);
setSecondaryAxesFormat($chart);
$presentation->save("combo-chart.pptx", SaveFormat::Pptx);
}
finally
{
$presentation->dispose();
}
}
function createChartWithFirstSeries($slide)
{
$chart = $slide->getShapes()->addChart(ChartType::ClusteredColumn, 50, 50, 600, 400);
$chart->setTitle(true);
$chart->getChartTitle()->addTextFrameForOverriding("Chart Title");
$chart->getChartTitle()->setOverlay(false);
$portionFormat = $chart->getChartTitle()->getTextFrameForOverriding()->getParagraphs()->get_Item(0)->getParagraphFormat()->getDefaultPortionFormat();
$portionFormat->setFontBold(NullableBool::False);
$portionFormat->setFontHeight(18);
$chart->getLegend()->setPosition(LegendPositionType::Bottom);
$chart->getLegend()->getTextFormat()->getPortionFormat()->setFontHeight(12);
$chart->getChartData()->getSeries()->clear();
$chart->getChartData()->getCategories()->clear();
$workbook = $chart->getChartData()->getChartDataWorkbook();
$worksheetIndex = 0;
$categoryCell = $workbook->getCell($worksheetIndex, 1, 0, "Category 1");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell($worksheetIndex, 2, 0, "Category 2");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell($worksheetIndex, 3, 0, "Category 3");
$chart->getChartData()->getCategories()->add($categoryCell);
$categoryCell = $workbook->getCell($worksheetIndex, 4, 0, "Category 4");
$chart->getChartData()->getCategories()->add($categoryCell);
$seriesNameCell = $workbook->getCell($worksheetIndex, 0, 1, "Series 1");
$series = $chart->getChartData()->getSeries()->add($seriesNameCell, $chart->getType());
$series->getParentSeriesGroup()->setOverlap(-25);
$series->getParentSeriesGroup()->setGapWidth(220);
$valueCell = $workbook->getCell($worksheetIndex, 1, 1, 4.3);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, 2, 1, 2.5);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, 3, 1, 3.5);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, 4, 1, 4.5);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
return $chart;
}
function addSecondSeriesToChart($chart)
{
$workbook = $chart->getChartData()->getChartDataWorkbook();
$worksheetIndex = 0;
$seriesNameCell = $workbook->getCell($worksheetIndex, 0, 2, "Series 2");
$series = $chart->getChartData()->getSeries()->add($seriesNameCell, ChartType::ClusteredColumn);
$series->getParentSeriesGroup()->setOverlap(-25);
$series->getParentSeriesGroup()->setGapWidth(220);
$valueCell = $workbook->getCell($worksheetIndex, 1, 2, 2.4);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, 2, 2, 4.4);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, 3, 2, 1.8);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, 4, 2, 2.8);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
}
function addThirdSeriesToChart($chart)
{
$workbook = $chart->getChartData()->getChartDataWorkbook();
$worksheetIndex = 0;
$seriesNameCell = $workbook->getCell($worksheetIndex, 0, 3, "Series 3");
$series = $chart->getChartData()->getSeries()->add($seriesNameCell, ChartType::Line);
$valueCell = $workbook->getCell($worksheetIndex, 1, 3, 2.0);
$series->getDataPoints()->addDataPointForLineSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, 2, 3, 2.0);
$series->getDataPoints()->addDataPointForLineSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, 3, 3, 3.0);
$series->getDataPoints()->addDataPointForLineSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, 4, 3, 5.0);
$series->getDataPoints()->addDataPointForLineSeries($valueCell);
$series->setPlotOnSecondAxis(true);
}
function setPrimaryAxesFormat($chart)
{
$horizontalAxis = $chart->getAxes()->getHorizontalAxis();
$horizontalAxis->getTextFormat()->getPortionFormat()->setFontHeight(12);
$horizontalAxis->getFormat()->getLine()->getFillFormat()->setFillType(FillType::NoFill);
setAxisTitle($horizontalAxis, "X Axis");
$verticalAxis = $chart->getAxes()->getVerticalAxis();
$verticalAxis->getTextFormat()->getPortionFormat()->setFontHeight(12);
$verticalAxis->getFormat()->getLine()->getFillFormat()->setFillType(FillType::NoFill);
setAxisTitle($verticalAxis, "Y Axis 1");
$majorGridLinesFormat = $verticalAxis->getMajorGridLinesFormat()->getLine()->getFillFormat();
$majorGridLinesFormat->setFillType(FillType::Solid);
$color = new Java("java.awt.Color", 217, 217, 217);
$majorGridLinesFormat->getSolidFillColor()->setColor($color);
}
function setSecondaryAxesFormat($chart)
{
$secondaryHorizontalAxis = $chart->getAxes()->getSecondaryHorizontalAxis();
$secondaryHorizontalAxis->setPosition(AxisPositionType::Bottom);
$secondaryHorizontalAxis->setCrossType(CrossesType::Maximum);
$secondaryHorizontalAxis->setVisible(false);
$secondaryHorizontalAxis->getMajorGridLinesFormat()->getLine()->getFillFormat()->setFillType(FillType::NoFill);
$secondaryHorizontalAxis->getMinorGridLinesFormat()->getLine()->getFillFormat()->setFillType(FillType::NoFill);
$secondaryVerticalAxis = $chart->getAxes()->getSecondaryVerticalAxis();
$secondaryVerticalAxis->setPosition(AxisPositionType::Right);
$secondaryVerticalAxis->getTextFormat()->getPortionFormat()->setFontHeight(12);
$secondaryVerticalAxis->getFormat()->getLine()->getFillFormat()->setFillType(FillType::NoFill);
$secondaryVerticalAxis->getMajorGridLinesFormat()->getLine()->getFillFormat()->setFillType(FillType::NoFill);
$secondaryVerticalAxis->getMinorGridLinesFormat()->getLine()->getFillFormat()->setFillType(FillType::NoFill);
setAxisTitle($secondaryVerticalAxis, "Y Axis 2");
}
function setAxisTitle($axis, $axisTitle)
{
$axis->setTitle(true);
$axis->getTitle()->setOverlay(false);
$titlePortionFormat = $axis->getTitle()->addTextFrameForOverriding($axisTitle)->getParagraphs()->get_Item(0)->getParagraphFormat()->getDefaultPortionFormat();
$titlePortionFormat->setFontBold(NullableBool::False);
$titlePortionFormat->setFontHeight(12);
}
createComboChart();
Aggiorna grafici
Aspose.Slides per PHP via Java consente di aggiornare i grafici PowerPoint modificando i dati, la formattazione e lo stile. Questa funzionalità semplifica il mantenimento delle presentazioni aggiornate con contenuti dinamici e garantisce che i grafici riflettano accuratamente i dati correnti e gli standard visivi.
Questo esempio aggiorna il grafico denominato “My chart” nella prima diapositiva di una presentazione esistente. Richiede due serie di colonne con tre punti dati ciascuna, con etichette di categoria nella colonna A e nomi delle serie nella prima riga del primo foglio di lavoro. L’esempio utilizza getCell per modificare le celle del workbook, aggiorna i valori delle serie, aggiunge una terza serie e cambia il tipo del grafico in cilindri raggruppati tramite setType.
use aspose\slides\ChartType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$chartName = "My chart";
$chartClass = new JavaClass("com.aspose.slides.IChart");
$presentation = new Presentation("ExistingChart.pptx");
try
{
$slide = $presentation->getSlides()->get_Item(0);
for ($i = 0; $i < java_values($slide->getShapes()->size()); $i++)
{
$shape = $slide->getShapes()->get_Item($i);
if (java_instanceof($shape, $chartClass) && $chartName === java_values($shape->getName()))
{
$chart = $shape;
$worksheetIndex = 0;
$workbook = $chart->getChartData()->getChartDataWorkbook();
$workbook->getCell($worksheetIndex, 1, 0, "Modified Category 1");
$workbook->getCell($worksheetIndex, 2, 0, "Modified Category 2");
$series = $chart->getChartData()->getSeries()->get_Item(0);
$workbook->getCell($worksheetIndex, 0, 1, "New_Series 1");
$series->getDataPoints()->get_Item(0)->getValue()->setData(90);
$series->getDataPoints()->get_Item(1)->getValue()->setData(123);
$series->getDataPoints()->get_Item(2)->getValue()->setData(44);
$series = $chart->getChartData()->getSeries()->get_Item(1);
$workbook->getCell($worksheetIndex, 0, 2, "New_Series 2");
$series->getDataPoints()->get_Item(0)->getValue()->setData(23);
$series->getDataPoints()->get_Item(1)->getValue()->setData(67);
$series->getDataPoints()->get_Item(2)->getValue()->setData(99);
$seriesNameCell = $workbook->getCell($worksheetIndex, 0, 3, "Series 3");
$series = $chart->getChartData()->getSeries()->add($seriesNameCell, $chart->getType());
$valueCell = $workbook->getCell($worksheetIndex, 1, 3, 20);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, 2, 3, 50);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$valueCell = $workbook->getCell($worksheetIndex, 3, 3, 30);
$series->getDataPoints()->addDataPointForBarSeries($valueCell);
$chart->setType(ChartType::ClusteredCylinder);
}
}
$presentation->save("AsposeChartModified.pptx", SaveFormat::Pptx);
}
finally
{
$presentation->dispose();
}
Imposta l’intervallo dati per un grafico
Per ispezionare l’intervallo già usato da un grafico esistente, consulta Recupera l’intervallo dati di un grafico.
Aspose.Slides per PHP via Java 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 puoi mappare direttamente una parte del foglio di lavoro al grafico, controllando quali celle contribuiscono alle serie e alle categorie del grafico. Di conseguenza, puoi aggiornare e sincronizzare facilmente i tuoi grafici con le ultime modifiche dei dati nel foglio di lavoro, assicurando che le presentazioni PowerPoint riflettano informazioni attuali e accurate.
Questo esempio utilizza setRange per impostare la fonte dati del grafico denominato “My chart” nella prima diapositiva di una presentazione esistente su Sheet1!A1:B4. Il workbook del grafico deve contenere quel foglio di lavoro e un intervallo popolato con un’intestazione di serie nella prima riga, etichette di categoria nella colonna A e tre valori nella colonna B.
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$chartName = "My chart";
$chartClass = new JavaClass("com.aspose.slides.IChart");
$presentation = new Presentation("ExistingChart.pptx");
try
{
$slide = $presentation->getSlides()->get_Item(0);
for ($i = 0; $i < java_values($slide->getShapes()->size()); $i++)
{
$shape = $slide->getShapes()->get_Item($i);
if (java_instanceof($shape, $chartClass) && $chartName === java_values($shape->getName()))
{
$chart = $shape;
$chart->getChartData()->setRange("Sheet1!A1:B4");
}
}
$presentation->save("SetDataRange.pptx", SaveFormat::Pptx);
}
finally
{
$presentation->dispose();
}
Usa i marcatori predefiniti nei grafici
Quando utilizzi i marcatori predefiniti nei grafici, ogni serie del grafico ottiene automaticamente un simbolo di marcatore predefinito diverso.
Questo esempio utilizza addChart per creare un grafico con due serie a linee e una legenda. Le impostazioni del marcatore accessibili tramite getMarker 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.
use aspose\slides\ChartType;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
$presentation = new Presentation();
try
{
$slide = $presentation->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::LineWithMarkers, 10, 10, 400, 400);
$chart->getChartData()->getSeries()->clear();
$chart->getChartData()->getCategories()->clear();
$workbook = $chart->getChartData()->getChartDataWorkbook();
$seriesNameCell = $workbook->getCell(0, 0, 1, "Series 1");
$series = $chart->getChartData()->getSeries()->add($seriesNameCell, $chart->getType());
$categoryCell = $workbook->getCell(0, 1, 0, "C1");
$chart->getChartData()->getCategories()->add($categoryCell);
$valueCell = $workbook->getCell(0, 1, 1, 24);
$series->getDataPoints()->addDataPointForLineSeries($valueCell);
$categoryCell = $workbook->getCell(0, 2, 0, "C2");
$chart->getChartData()->getCategories()->add($categoryCell);
$valueCell = $workbook->getCell(0, 2, 1, 23);
$series->getDataPoints()->addDataPointForLineSeries($valueCell);
$categoryCell = $workbook->getCell(0, 3, 0, "C3");
$chart->getChartData()->getCategories()->add($categoryCell);
$valueCell = $workbook->getCell(0, 3, 1, -10);
$series->getDataPoints()->addDataPointForLineSeries($valueCell);
$categoryCell = $workbook->getCell(0, 4, 0, "C4");
$chart->getChartData()->getCategories()->add($categoryCell);
$valueCell = $workbook->getCell(0, 4, 1, null);
$series->getDataPoints()->addDataPointForLineSeries($valueCell);
$seriesNameCell = $workbook->getCell(0, 0, 2, "Series 2");
$secondSeries = $chart->getChartData()->getSeries()->add($seriesNameCell, $chart->getType());
$valueCell = $workbook->getCell(0, 1, 2, 30);
$secondSeries->getDataPoints()->addDataPointForLineSeries($valueCell);
$valueCell = $workbook->getCell(0, 2, 2, 10);
$secondSeries->getDataPoints()->addDataPointForLineSeries($valueCell);
$valueCell = $workbook->getCell(0, 3, 2, 60);
$secondSeries->getDataPoints()->addDataPointForLineSeries($valueCell);
$valueCell = $workbook->getCell(0, 4, 2, 40);
$secondSeries->getDataPoints()->addDataPointForLineSeries($valueCell);
$chart->setLegend(true);
$chart->getLegend()->setOverlay(false);
$presentation->save("DefaultMarkersInChart.pptx", SaveFormat::Pptx);
}
finally
{
$presentation->dispose();
}
FAQ
Quali tipi di grafico sono supportati da Aspose.Slides per PHP via Java?
Aspose.Slides per PHP via Java supporta un’ampia gamma di tipi di grafico, tra cui barra, linea, torta, area, dispersione, istogramma, radar e molti altri. Questa flessibilità consente di scegliere il tipo di grafico più appropriato per le proprie esigenze di visualizzazione dei dati.
Come aggiungo un nuovo grafico a una diapositiva?
Per aggiungere un grafico, prima 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 al suo workbook dei dati (ChartDataWorkbook), cancellando eventuali serie e categorie predefinite, e quindi aggiungendo i tuoi dati personalizzati. Questo consente di aggiornare programmaticamente il grafico per riflettere gli ultimi dati.
È possibile personalizzare l’aspetto del grafico?
Sì, Aspose.Slides per PHP via Java offre ampie opzioni di personalizzazione. È possibile modificare colori, caratteri, etichette, legende e altri elementi di formattazione per adattare l’aspetto del grafico ai requisiti di progettazione specifici.