Crea o aggiorna grafici per presentazioni PowerPoint in PHP
Panoramica
Questo articolo fornisce una guida completa su come creare e personalizzare i grafici usando Aspose.Slides. Imparerai a aggiungere programmaticamente un grafico a una diapositiva, a popolarlo con i dati e ad 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 di serie, assi e legende. Seguendo questa guida, otterrai una solida comprensione di come integrare la generazione dinamica di grafici nelle tue applicazioni, 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 insight che potrebbero non essere immediatamente evidenti da una tabella o un foglio di calcolo.
Perché creare grafici?
Usando i grafici, è possibile:
- aggregare, condensare o riassumere grandi quantità di dati in una singola diapositiva di una presentazione
- evidenziare pattern e tendenze nei dati
- dedurre la direzione e il ritmo dei dati nel tempo o rispetto a una specifica unità di misura
- individuare outlier, aberrazioni, deviazioni, errori, dati privi di senso, ecc.
- comunicare o presentare dati complessi
In PowerPoint è possibile creare grafici tramite la funzione Inserisci, che fornisce modelli per la progettazione di molti tipi di grafici. Usando Aspose.Slides, è possibile creare sia grafici standard (basati su tipi di grafico popolari) sia grafici personalizzati.
Nota
Per creare grafici, utilizzare la classe ChartType. I campi di questa classe corrispondono a diversi tipi di grafico.Creare grafici a colonne raggruppate
Questa sezione spiega come creare grafici a colonne raggruppate usando Aspose.Slides. Imparerai a inizializzare una presentazione, aggiungere un grafico e personalizzare i suoi elementi come titolo, dati, serie, categorie e stile. Segui i passaggi seguenti per vedere come viene generato un grafico a colonne raggruppate standard:
- Creare un’istanza della classe Presentation.
- Ottenere un riferimento a una diapositiva usando il suo indice.
- Aggiungere un grafico con alcuni dati e specificare il tipo
ChartType::ClusteredColumn. - Aggiungere un titolo al grafico.
- Accedere al foglio di dati del grafico.
- Cancella tutte le serie e categorie predefinite.
- Aggiungere nuove serie e categorie.
- Aggiungere nuovi dati al grafico per le serie.
- Applicare un colore di riempimento alle serie del grafico.
- Aggiungere etichette alle serie del grafico.
- Salvare la presentazione modificata come file PPTX.
Questo codice C# dimostra come creare un grafico a colonne raggruppate:
# Istanzia una classe di presentazione che rappresenta un file PPTX
$pres = new Presentation();
try {
# Accede alla prima diapositiva
$sld = $pres->getSlides()->get_Item(0);
# Aggiunge un grafico con i suoi dati predefiniti
$chart = $sld->getShapes()->addChart(ChartType::ClusteredColumn, 0, 0, 500, 500);
# Imposta il titolo del grafico
$chart->getChartTitle()->addTextFrameForOverriding("Sample Title");
$chart->getChartTitle()->getTextFrameForOverriding()->getTextFrameFormat()->setCenterText(NullableBool::True);
$chart->getChartTitle()->setHeight(20);
$chart->hasTitle();
# Imposta la prima serie per mostrare i valori
$chart->getChartData()->getSeries()->get_Item(0)->getLabels()->getDefaultDataLabelFormat()->setShowValue(true);
# Imposta l'indice per il foglio dati del grafico
$defaultWorksheetIndex = 0;
# Ottiene il foglio di lavoro dei dati del grafico
$fact = $chart->getChartData()->getChartDataWorkbook();
# Elimina le serie e le categorie generate di default
$chart->getChartData()->getSeries()->clear();
$chart->getChartData()->getCategories()->clear();
$s = $chart->getChartData()->getSeries()->size();
$s = $chart->getChartData()->getCategories()->size();
# Aggiunge nuove serie
$chart->getChartData()->getSeries()->add($fact->getCell($defaultWorksheetIndex, 0, 1, "Series 1"), $chart->getType());
$chart->getChartData()->getSeries()->add($fact->getCell($defaultWorksheetIndex, 0, 2, "Series 2"), $chart->getType());
# Aggiunge nuove categorie
$chart->getChartData()->getCategories()->add($fact->getCell($defaultWorksheetIndex, 1, 0, "Caetegoty 1"));
$chart->getChartData()->getCategories()->add($fact->getCell($defaultWorksheetIndex, 2, 0, "Caetegoty 2"));
$chart->getChartData()->getCategories()->add($fact->getCell($defaultWorksheetIndex, 3, 0, "Caetegoty 3"));
# Prende la prima serie del grafico
$series = $chart->getChartData()->getSeries()->get_Item(0);
# Ora popola i dati della serie
$series->getDataPoints()->addDataPointForBarSeries($fact->getCell($defaultWorksheetIndex, 1, 1, 20));
$series->getDataPoints()->addDataPointForBarSeries($fact->getCell($defaultWorksheetIndex, 2, 1, 50));
$series->getDataPoints()->addDataPointForBarSeries($fact->getCell($defaultWorksheetIndex, 3, 1, 30));
# Imposta il colore di riempimento per la serie
$series->getFormat()->getFill()->setFillType(FillType::Solid);
$series->getFormat()->getFill()->getSolidFillColor()->setColor(java("java.awt.Color")->RED);
# Prende la seconda serie del grafico
$series = $chart->getChartData()->getSeries()->get_Item(1);
# Popola i dati della serie
$series->getDataPoints()->addDataPointForBarSeries($fact->getCell($defaultWorksheetIndex, 1, 2, 30));
$series->getDataPoints()->addDataPointForBarSeries($fact->getCell($defaultWorksheetIndex, 2, 2, 10));
$series->getDataPoints()->addDataPointForBarSeries($fact->getCell($defaultWorksheetIndex, 3, 2, 60));
# Imposta il colore di riempimento per la serie
$series->getFormat()->getFill()->setFillType(FillType::Solid);
$series->getFormat()->getFill()->getSolidFillColor()->setColor(java("java.awt.Color")->GREEN);
# Crea etichette personalizzate per ogni categoria della nuova serie
# Imposta la prima etichetta per mostrare il nome della categoria
$lbl = $series->getDataPoints()->get_Item(0)->getLabel();
$lbl->getDataLabelFormat()->setShowCategoryName(true);
$lbl = $series->getDataPoints()->get_Item(1)->getLabel();
$lbl->getDataLabelFormat()->setShowSeriesName(true);
# Mostra il valore per la terza etichetta
$lbl = $series->getDataPoints()->get_Item(2)->getLabel();
$lbl->getDataLabelFormat()->setShowValue(true);
$lbl->getDataLabelFormat()->setShowSeriesName(true);
$lbl->getDataLabelFormat()->setSeparator("/");
# Salva la presentazione con il grafico
$pres->save("output.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Creare grafici a dispersione
I grafici a dispersione (noti anche come scatter plot o grafici x‑y) sono spesso usati per verificare pattern o dimostrare correlazioni tra due variabili.
Usa un grafico a dispersione quando:
- hai dati numerici accoppiati
- hai due variabili che si abbinano bene
- vuoi determinare se due variabili sono correlate
- hai una variabile indipendente che ha più valori per una variabile dipendente
- Segui i passaggi in Creare grafici a colonne raggruppate.
- Per il terzo passaggio, aggiungi un grafico con alcuni dati e specifica il tipo di grafico come uno dei seguenti:
- ChartType::ScatterWithMarkers - Rappresenta un grafico a dispersione.
- ChartType::ScatterWithSmoothLinesAndMarkers - Rappresenta un grafico a dispersione collegato da curve, con marcatori dati.
- ChartType::ScatterWithSmoothLines - Rappresenta un grafico a dispersione collegato da curve, senza marcatori dati.
- ChartType::ScatterWithStraightLinesAndMarkers - Rappresenta un grafico a dispersione collegato da linee, con marcatori dati.
- ChartType::ScatterWithStraightLines - Rappresenta un grafico a dispersione collegato da linee, senza marcatori dati.
Questo codice PHP mostra come creare un grafico a dispersione con marcatori diversi per ciascuna serie:
# Istanzia una classe di presentazione che rappresenta un file PPTX
$pres = new Presentation();
try {
# Accede alla prima diapositiva
$slide = $pres->getSlides()->get_Item(0);
# Crea il grafico predefinito
$chart = $slide->getShapes()->addChart(ChartType::ScatterWithSmoothLines, 0, 0, 400, 400);
# Ottiene l'indice del foglio dati del grafico predefinito
$defaultWorksheetIndex = 0;
# Ottiene il foglio dati del grafico
$fact = $chart->getChartData()->getChartDataWorkbook();
# Elimina le serie demo
$chart->getChartData()->getSeries()->clear();
# Aggiunge nuove serie
$chart->getChartData()->getSeries()->add($fact->getCell($defaultWorksheetIndex, 1, 1, "Series 1"), $chart->getType());
$chart->getChartData()->getSeries()->add($fact->getCell($defaultWorksheetIndex, 1, 3, "Series 2"), $chart->getType());
# Prende la prima serie del grafico
$series = $chart->getChartData()->getSeries()->get_Item(0);
# Aggiunge un nuovo punto (1:3) alla serie
$series->getDataPoints()->addDataPointForScatterSeries($fact->getCell($defaultWorksheetIndex, 2, 1, 1), $fact->getCell($defaultWorksheetIndex, 2, 2, 3));
# Aggiunge un nuovo punto (2:10)
$series->getDataPoints()->addDataPointForScatterSeries($fact->getCell($defaultWorksheetIndex, 3, 1, 2), $fact->getCell($defaultWorksheetIndex, 3, 2, 10));
# Cambia il tipo della serie
$series->setType(ChartType::ScatterWithStraightLinesAndMarkers);
# Cambia il marcatore della serie del grafico
$series->getMarker()->setSize(10);
$series->getMarker()->setSymbol(MarkerStyleType::Star);
# Prende la seconda serie del grafico
$series = $chart->getChartData()->getSeries()->get_Item(1);
# Aggiunge un nuovo punto (5:2) lì
$series->getDataPoints()->addDataPointForScatterSeries($fact->getCell($defaultWorksheetIndex, 2, 3, 5), $fact->getCell($defaultWorksheetIndex, 2, 4, 2));
# Aggiunge un nuovo punto (3:1)
$series->getDataPoints()->addDataPointForScatterSeries($fact->getCell($defaultWorksheetIndex, 3, 3, 3), $fact->getCell($defaultWorksheetIndex, 3, 4, 1));
# Aggiunge un nuovo punto (2:2)
$series->getDataPoints()->addDataPointForScatterSeries($fact->getCell($defaultWorksheetIndex, 4, 3, 2), $fact->getCell($defaultWorksheetIndex, 4, 4, 2));
# Aggiunge un nuovo punto (5:1)
$series->getDataPoints()->addDataPointForScatterSeries($fact->getCell($defaultWorksheetIndex, 5, 3, 5), $fact->getCell($defaultWorksheetIndex, 5, 4, 1));
# Cambia il marcatore della serie del grafico
$series->getMarker()->setSize(10);
$series->getMarker()->setSymbol(MarkerStyleType::Circle);
$pres->save("AsposeChart_out.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Creare grafici a torta
I grafici a torta sono ideali per mostrare la relazione parte‑intero nei dati, specialmente quando i dati contengono etichette categoriche con valori numerici. Tuttavia, se i dati contengono molte parti o etichette, potresti considerare l’uso di un grafico a barre.
- Creare un’istanza della classe Presentation.
- Ottenere un riferimento a una diapositiva usando il suo indice.
- Aggiungere un grafico con dati predefiniti e specificare il tipo ChartType::Pie.
- Accedere al foglio di lavoro dei dati del grafico ChartDataWorkbook.
- Cancellare le serie e le categorie predefinite.
- Aggiungere nuove serie e categorie.
- Aggiungere nuovi dati al grafico per le serie.
- Aggiungere nuovi punti al grafico e applicare colori personalizzati per i settori del grafico a torta.
- Impostare le etichette per le serie.
- Abilitare le linee guida per le etichette delle serie.
- Impostare l’angolo di rotazione per i settori del grafico a torta.
- Salvare la presentazione modificata come file PPTX.
Questo codice PHP mostra come creare un grafico a torta:
# Istanzia una classe di presentazione che rappresenta un file PPTX
$pres = new Presentation();
try {
# Accede alla prima diapositiva
$slides = $pres->getSlides()->get_Item(0);
# Aggiunge un grafico con dati predefiniti
$chart = $slides->getShapes()->addChart(ChartType::Pie, 100, 100, 400, 400);
# Imposta il titolo del grafico
$chart->getChartTitle()->addTextFrameForOverriding("Sample Title");
$chart->getChartTitle()->getTextFrameForOverriding()->getTextFrameFormat()->setCenterText(NullableBool::True);
$chart->getChartTitle()->setHeight(20);
$chart->setTitle(true);
# Imposta la prima serie per mostrare i valori
$chart->getChartData()->getSeries()->get_Item(0)->getLabels()->getDefaultDataLabelFormat()->setShowValue(true);
# Imposta l'indice per il foglio dati del grafico
$defaultWorksheetIndex = 0;
# Ottiene il foglio di lavoro dei dati del grafico
$fact = $chart->getChartData()->getChartDataWorkbook();
# Elimina le serie e le categorie generate di default
$chart->getChartData()->getSeries()->clear();
$chart->getChartData()->getCategories()->clear();
# Aggiunge nuove categorie
$chart->getChartData()->getCategories()->add($fact->getCell(0, 1, 0, "First Qtr"));
$chart->getChartData()->getCategories()->add($fact->getCell(0, 2, 0, "2nd Qtr"));
$chart->getChartData()->getCategories()->add($fact->getCell(0, 3, 0, "3rd Qtr"));
# Aggiunge nuove serie
$series = $chart->getChartData()->getSeries()->add($fact->getCell(0, 0, 1, "Series 1"), $chart->getType());
# Popola i dati della serie
$series->getDataPoints()->addDataPointForPieSeries($fact->getCell($defaultWorksheetIndex, 1, 1, 20));
$series->getDataPoints()->addDataPointForPieSeries($fact->getCell($defaultWorksheetIndex, 2, 1, 50));
$series->getDataPoints()->addDataPointForPieSeries($fact->getCell($defaultWorksheetIndex, 3, 1, 30));
# Non funziona nella nuova versione
# Aggiunta di nuovi punti e impostazione del colore del settore
# series.IsColorVaried = true;
$chart->getChartData()->getSeriesGroups()->get_Item(0)->setColorVaried(true);
$point = $series->getDataPoints()->get_Item(0);
$point->getFormat()->getFill()->setFillType(FillType::Solid);
$point->getFormat()->getFill()->getSolidFillColor()->setColor(java("java.awt.Color")->CYAN);
# Imposta il bordo del settore
$point->getFormat()->getLine()->getFillFormat()->setFillType(FillType::Solid);
$point->getFormat()->getLine()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->GRAY);
$point->getFormat()->getLine()->setWidth(3.0);
$point->getFormat()->getLine()->setStyle(LineStyle->ThinThick);
$point->getFormat()->getLine()->setDashStyle(LineDashStyle->DashDot);
$point1 = $series->getDataPoints()->get_Item(1);
$point1->getFormat()->getFill()->setFillType(FillType::Solid);
$point1->getFormat()->getFill()->getSolidFillColor()->setColor(java("java.awt.Color")->ORANGE);
# Imposta il bordo del settore
$point1->getFormat()->getLine()->getFillFormat()->setFillType(FillType::Solid);
$point1->getFormat()->getLine()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->BLUE);
$point1->getFormat()->getLine()->setWidth(3.0);
$point1->getFormat()->getLine()->setStyle(LineStyle->Single);
$point1->getFormat()->getLine()->setDashStyle(LineDashStyle->LargeDashDot);
$point2 = $series->getDataPoints()->get_Item(2);
$point2->getFormat()->getFill()->setFillType(FillType::Solid);
$point2->getFormat()->getFill()->getSolidFillColor()->setColor(java("java.awt.Color")->YELLOW);
# Imposta il bordo del settore
$point2->getFormat()->getLine()->getFillFormat()->setFillType(FillType::Solid);
$point2->getFormat()->getLine()->getFillFormat()->getSolidFillColor()->setColor(java("java.awt.Color")->RED);
$point2->getFormat()->getLine()->setWidth(2.0);
$point2->getFormat()->getLine()->setStyle(LineStyle->ThinThin);
$point2->getFormat()->getLine()->setDashStyle(LineDashStyle->LargeDashDotDot);
# Crea etichette personalizzate per ciascuna categoria della nuova serie
$lbl1 = $series->getDataPoints()->get_Item(0)->getLabel();
# lbl.ShowCategoryName = true;
$lbl1->getDataLabelFormat()->setShowValue(true);
$lbl2 = $series->getDataPoints()->get_Item(1)->getLabel();
$lbl2->getDataLabelFormat()->setShowValue(true);
$lbl2->getDataLabelFormat()->setShowLegendKey(true);
$lbl2->getDataLabelFormat()->setShowPercentage(true);
$lbl3 = $series->getDataPoints()->get_Item(2)->getLabel();
$lbl3->getDataLabelFormat()->setShowSeriesName(true);
$lbl3->getDataLabelFormat()->setShowPercentage(true);
# Mostra le linee di collegamento per il grafico
$series->getLabels()->getDefaultDataLabelFormat()->setShowLeaderLines(true);
# Imposta l'angolo di rotazione per i settori del grafico a torta
$chart->getChartData()->getSeriesGroups()->get_Item(0)->setFirstSliceAngle(180);
# Salva la presentazione con il grafico
$pres->save("PieChart_out.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Creare grafici a linee
I grafici a linee (noti anche come grafici a linee) sono ideali quando si desidera dimostrare variazioni di valore nel tempo. Usando 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 ancora.
- Creare un’istanza della classe Presentation.
- Ottenere un riferimento a una diapositiva usando il suo indice.
- Aggiungere un grafico con dati predefiniti e specificare il tipo ChartType::Line.
- Accedere al foglio di lavoro dei dati del grafico (ChartDataWorkbook).
- Cancellare le serie e le categorie predefinite.
- Aggiungere nuove serie e categorie.
- Aggiungere nuovi dati al grafico per le serie.
- Salvare la presentazione modificata come file PPTX.
Questo codice PHP mostra come creare un grafico a linee:
$pres = new Presentation();
try {
$lineChart = $pres->getSlides()->get_Item(0)->getShapes()->addChart(ChartType::Line, 10, 50, 600, 350);
$pres->save("lineChart.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Per impostazione predefinita, i punti di un grafico a linee sono collegati da linee continue dritte. Se desideri che i punti siano collegati da linee tratteggiate, puoi specificare il tipo di tratto desiderato come segue:
$pres = new Presentation();
try {
$lineChart = $pres->getSlides()->get_Item(0)->getShapes()->addChart(ChartType::Line, 10, 50, 600, 350);
$seriesCollection = $lineChart->getChartData()->getSeries();
foreach ($seriesCollection as $series) {
$series->getFormat()->getLine()->setDashStyle(LineDashStyle::Dash);
}
$pres->save("lineChart.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Creare grafici ad albero (Tree Map)
I grafici ad albero sono ideali per dati di vendita quando vuoi mostrare la dimensione relativa delle categorie di dati e attirare rapidamente l’attenzione sugli elementi che contribuiscono maggiormente all’interno di ciascuna categoria.
- Creare un’istanza della classe Presentation.
- Ottenere un riferimento a una diapositiva usando il suo indice.
- Aggiungere un grafico con dati predefiniti e specificare il tipo ChartType::Treemap.
- Accedere al foglio di lavoro dei dati del grafico ChartDataWorkbook.
- Cancellare le serie e le categorie predefinite.
- Aggiungere nuove serie e categorie.
- Aggiungere nuovi dati al grafico per le serie.
- Salvare la presentazione modificata come file PPTX.
Questo codice PHP mostra come creare un grafico ad albero:
$pres = new Presentation();
try {
$chart = $pres->getSlides()->get_Item(0)->getShapes()->addChart(ChartType::Treemap, 50, 50, 500, 400);
$chart->getChartData()->getCategories()->clear();
$chart->getChartData()->getSeries()->clear();
$wb = $chart->getChartData()->getChartDataWorkbook();
$wb->clear(0);
# ramo 1
$leaf = $chart->getChartData()->getCategories()->add($wb->getCell(0, "C1", "Leaf1"));
$leaf->getGroupingLevels()->setGroupingItem(1, "Stem1");
$leaf->getGroupingLevels()->setGroupingItem(2, "Branch1");
$chart->getChartData()->getCategories()->add($wb->getCell(0, "C2", "Leaf2"));
$leaf = $chart->getChartData()->getCategories()->add($wb->getCell(0, "C3", "Leaf3"));
$leaf->getGroupingLevels()->setGroupingItem(1, "Stem2");
$chart->getChartData()->getCategories()->add($wb->getCell(0, "C4", "Leaf4"));
# ramo 2
$leaf = $chart->getChartData()->getCategories()->add($wb->getCell(0, "C5", "Leaf5"));
$leaf->getGroupingLevels()->setGroupingItem(1, "Stem3");
$leaf->getGroupingLevels()->setGroupingItem(2, "Branch2");
$chart->getChartData()->getCategories()->add($wb->getCell(0, "C6", "Leaf6"));
$leaf = $chart->getChartData()->getCategories()->add($wb->getCell(0, "C7", "Leaf7"));
$leaf->getGroupingLevels()->setGroupingItem(1, "Stem4");
$chart->getChartData()->getCategories()->add($wb->getCell(0, "C8", "Leaf8"));
$series = $chart->getChartData()->getSeries()->add(ChartType::Treemap);
$series->getLabels()->getDefaultDataLabelFormat()->setShowCategoryName(true);
$series->getDataPoints()->addDataPointForTreemapSeries($wb->getCell(0, "D1", 4));
$series->getDataPoints()->addDataPointForTreemapSeries($wb->getCell(0, "D2", 5));
$series->getDataPoints()->addDataPointForTreemapSeries($wb->getCell(0, "D3", 3));
$series->getDataPoints()->addDataPointForTreemapSeries($wb->getCell(0, "D4", 6));
$series->getDataPoints()->addDataPointForTreemapSeries($wb->getCell(0, "D5", 9));
$series->getDataPoints()->addDataPointForTreemapSeries($wb->getCell(0, "D6", 9));
$series->getDataPoints()->addDataPointForTreemapSeries($wb->getCell(0, "D7", 4));
$series->getDataPoints()->addDataPointForTreemapSeries($wb->getCell(0, "D8", 3));
$series->setParentLabelLayout(ParentLabelLayoutType::Overlapping);
$pres->save("Treemap.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Creare grafici di tipo Stock
- Creare un’istanza della classe Presentation.
- Ottenere un riferimento a una diapositiva usando il suo indice.
- Aggiungere un grafico con dati predefiniti e specificare il tipo ChartType::OpenHighLowClose.
- Accedere al foglio di lavoro dei dati del grafico ChartDataWorkbook.
- Cancellare le serie e le categorie predefinite.
- Aggiungere nuove serie e categorie.
- Aggiungere nuovi dati al grafico per le serie.
- Specificare il formato delle linee high‑low.
- Salvare la presentazione modificata come file PPTX.
Questo codice PHP mostra come creare un grafico Stock:
$pres = new Presentation();
try {
$chart = $pres->getSlides()->get_Item(0)->getShapes()->addChart(ChartType::OpenHighLowClose, 50, 50, 600, 400, false);
$chart->getChartData()->getSeries()->clear();
$chart->getChartData()->getCategories()->clear();
$wb = $chart->getChartData()->getChartDataWorkbook();
$chart->getChartData()->getCategories()->add($wb->getCell(0, 1, 0, "A"));
$chart->getChartData()->getCategories()->add($wb->getCell(0, 2, 0, "B"));
$chart->getChartData()->getCategories()->add($wb->getCell(0, 3, 0, "C"));
$chart->getChartData()->getSeries()->add($wb->getCell(0, 0, 1, "Open"), $chart->getType());
$chart->getChartData()->getSeries()->add($wb->getCell(0, 0, 2, "High"), $chart->getType());
$chart->getChartData()->getSeries()->add($wb->getCell(0, 0, 3, "Low"), $chart->getType());
$chart->getChartData()->getSeries()->add($wb->getCell(0, 0, 4, "Close"), $chart->getType());
$series = $chart->getChartData()->getSeries()->get_Item(0);
$series->getDataPoints()->addDataPointForStockSeries($wb->getCell(0, 1, 1, 72));
$series->getDataPoints()->addDataPointForStockSeries($wb->getCell(0, 2, 1, 25));
$series->getDataPoints()->addDataPointForStockSeries($wb->getCell(0, 3, 1, 38));
$series = $chart->getChartData()->getSeries()->get_Item(1);
$series->getDataPoints()->addDataPointForStockSeries($wb->getCell(0, 1, 2, 172));
$series->getDataPoints()->addDataPointForStockSeries($wb->getCell(0, 2, 2, 57));
$series->getDataPoints()->addDataPointForStockSeries($wb->getCell(0, 3, 2, 57));
$series = $chart->getChartData()->getSeries()->get_Item(2);
$series->getDataPoints()->addDataPointForStockSeries($wb->getCell(0, 1, 3, 12));
$series->getDataPoints()->addDataPointForStockSeries($wb->getCell(0, 2, 3, 12));
$series->getDataPoints()->addDataPointForStockSeries($wb->getCell(0, 3, 3, 13));
$series = $chart->getChartData()->getSeries()->get_Item(3);
$series->getDataPoints()->addDataPointForStockSeries($wb->getCell(0, 1, 4, 25));
$series->getDataPoints()->addDataPointForStockSeries($wb->getCell(0, 2, 4, 38));
$series->getDataPoints()->addDataPointForStockSeries($wb->getCell(0, 3, 4, 50));
$chart->getChartData()->getSeriesGroups()->get_Item(0)->getUpDownBars()->setUpDownBars(true);
$chart->getChartData()->getSeriesGroups()->get_Item(0)->getHiLowLinesFormat()->getLine()->getFillFormat()->setFillType(FillType::Solid);
$seriesCollection = $chart->getChartData()->getSeries();
foreach ($seriesCollection as $ser) {
$ser->getFormat()->getLine()->getFillFormat()->setFillType(FillType::NoFill);
}
$pres->save("output.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Creare grafici a scatola e baffi (Box and Whisker)
- Creare un’istanza della classe Presentation.
- Ottenere un riferimento a una diapositiva usando il suo indice.
- Aggiungere un grafico con dati predefiniti e specificare il tipo ChartType::BoxAndWhisker.
- Accedere al foglio di lavoro dei dati del grafico ChartDataWorkbook.
- Cancellare le serie e le categorie predefinite.
- Aggiungere nuove serie e categorie.
- Aggiungere nuovi dati al grafico per le serie.
- Salvare la presentazione modificata come file PPTX.
Questo codice PHP mostra come creare un grafico a scatola e baffi:
$pres = new Presentation();
try {
$chart = $pres->getSlides()->get_Item(0)->getShapes()->addChart(ChartType::BoxAndWhisker, 50, 50, 500, 400);
$chart->getChartData()->getCategories()->clear();
$chart->getChartData()->getSeries()->clear();
$wb = $chart->getChartData()->getChartDataWorkbook();
$wb->clear(0);
$chart->getChartData()->getCategories()->add($wb->getCell(0, "A1", "Category 1"));
$chart->getChartData()->getCategories()->add($wb->getCell(0, "A2", "Category 1"));
$chart->getChartData()->getCategories()->add($wb->getCell(0, "A3", "Category 1"));
$chart->getChartData()->getCategories()->add($wb->getCell(0, "A4", "Category 1"));
$chart->getChartData()->getCategories()->add($wb->getCell(0, "A5", "Category 1"));
$chart->getChartData()->getCategories()->add($wb->getCell(0, "A6", "Category 1"));
$series = $chart->getChartData()->getSeries()->add(ChartType::BoxAndWhisker);
$series->setQuartileMethod(QuartileMethodType::Exclusive);
$series->setShowMeanLine(true);
$series->setShowMeanMarkers(true);
$series->setShowInnerPoints(true);
$series->setShowOutlierPoints(true);
$series->getDataPoints()->addDataPointForBoxAndWhiskerSeries($wb->getCell(0, "B1", 15));
$series->getDataPoints()->addDataPointForBoxAndWhiskerSeries($wb->getCell(0, "B2", 41));
$series->getDataPoints()->addDataPointForBoxAndWhiskerSeries($wb->getCell(0, "B3", 16));
$series->getDataPoints()->addDataPointForBoxAndWhiskerSeries($wb->getCell(0, "B4", 10));
$series->getDataPoints()->addDataPointForBoxAndWhiskerSeries($wb->getCell(0, "B5", 23));
$series->getDataPoints()->addDataPointForBoxAndWhiskerSeries($wb->getCell(0, "B6", 16));
$pres->save("BoxAndWhisker.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Creare grafici a imbuto (Funnel)
- Creare un’istanza della classe Presentation.
- Ottenere un riferimento a una diapositiva usando il suo indice.
- Aggiungere un grafico con dati predefiniti e specificare il tipo ChartType::Funnel.
- Salvare la presentazione modificata come file PPTX.
Questo codice PHP mostra come creare un grafico a imbuto:
$pres = new Presentation();
try {
$chart = $pres->getSlides()->get_Item(0)->getShapes()->addChart(ChartType::Funnel, 50, 50, 500, 400);
$chart->getChartData()->getCategories()->clear();
$chart->getChartData()->getSeries()->clear();
$wb = $chart->getChartData()->getChartDataWorkbook();
$wb->clear(0);
$chart->getChartData()->getCategories()->add($wb->getCell(0, "A1", "Category 1"));
$chart->getChartData()->getCategories()->add($wb->getCell(0, "A2", "Category 2"));
$chart->getChartData()->getCategories()->add($wb->getCell(0, "A3", "Category 3"));
$chart->getChartData()->getCategories()->add($wb->getCell(0, "A4", "Category 4"));
$chart->getChartData()->getCategories()->add($wb->getCell(0, "A5", "Category 5"));
$chart->getChartData()->getCategories()->add($wb->getCell(0, "A6", "Category 6"));
$series = $chart->getChartData()->getSeries()->add(ChartType::Funnel);
$series->getDataPoints()->addDataPointForFunnelSeries($wb->getCell(0, "B1", 50));
$series->getDataPoints()->addDataPointForFunnelSeries($wb->getCell(0, "B2", 100));
$series->getDataPoints()->addDataPointForFunnelSeries($wb->getCell(0, "B3", 200));
$series->getDataPoints()->addDataPointForFunnelSeries($wb->getCell(0, "B4", 300));
$series->getDataPoints()->addDataPointForFunnelSeries($wb->getCell(0, "B5", 400));
$series->getDataPoints()->addDataPointForFunnelSeries($wb->getCell(0, "B6", 500));
$pres->save("Funnel.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Creare grafici Sunburst
- Creare un’istanza della classe Presentation.
- Ottenere un riferimento a una diapositiva usando il suo indice.
- Aggiungere un grafico con dati predefiniti e specificare il tipo ChartType::Sunburst.
- Salvare la presentazione modificata come file PPTX.
Questo codice PHP mostra come creare un grafico Sunburst:
$pres = new Presentation();
try {
$chart = $pres->getSlides()->get_Item(0)->getShapes()->addChart(ChartType::Sunburst, 50, 50, 500, 400);
$chart->getChartData()->getCategories()->clear();
$chart->getChartData()->getSeries()->clear();
$wb = $chart->getChartData()->getChartDataWorkbook();
$wb->clear(0);
# ramo 1
$leaf = $chart->getChartData()->getCategories()->add($wb->getCell(0, "C1", "Leaf1"));
$leaf->getGroupingLevels()->setGroupingItem(1, "Stem1");
$leaf->getGroupingLevels()->setGroupingItem(2, "Branch1");
$chart->getChartData()->getCategories()->add($wb->getCell(0, "C2", "Leaf2"));
$leaf = $chart->getChartData()->getCategories()->add($wb->getCell(0, "C3", "Leaf3"));
$leaf->getGroupingLevels()->setGroupingItem(1, "Stem2");
$chart->getChartData()->getCategories()->add($wb->getCell(0, "C4", "Leaf4"));
# ramo 2
$leaf = $chart->getChartData()->getCategories()->add($wb->getCell(0, "C5", "Leaf5"));
$leaf->getGroupingLevels()->setGroupingItem(1, "Stem3");
$leaf->getGroupingLevels()->setGroupingItem(2, "Branch2");
$chart->getChartData()->getCategories()->add($wb->getCell(0, "C6", "Leaf6"));
$leaf = $chart->getChartData()->getCategories()->add($wb->getCell(0, "C7", "Leaf7"));
$leaf->getGroupingLevels()->setGroupingItem(1, "Stem4");
$chart->getChartData()->getCategories()->add($wb->getCell(0, "C8", "Leaf8"));
$series = $chart->getChartData()->getSeries()->add(ChartType::Sunburst);
$series->getLabels()->getDefaultDataLabelFormat()->setShowCategoryName(true);
$series->getDataPoints()->addDataPointForSunburstSeries($wb->getCell(0, "D1", 4));
$series->getDataPoints()->addDataPointForSunburstSeries($wb->getCell(0, "D2", 5));
$series->getDataPoints()->addDataPointForSunburstSeries($wb->getCell(0, "D3", 3));
$series->getDataPoints()->addDataPointForSunburstSeries($wb->getCell(0, "D4", 6));
$series->getDataPoints()->addDataPointForSunburstSeries($wb->getCell(0, "D5", 9));
$series->getDataPoints()->addDataPointForSunburstSeries($wb->getCell(0, "D6", 9));
$series->getDataPoints()->addDataPointForSunburstSeries($wb->getCell(0, "D7", 4));
$series->getDataPoints()->addDataPointForSunburstSeries($wb->getCell(0, "D8", 3));
$pres->save("Sunburst.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Creare grafici istogramma (Histogram)
- Creare un’istanza della classe Presentation.
- Ottenere un riferimento a una diapositiva usando il suo indice.
- Aggiungere un grafico con dati predefiniti e specificare il tipo ChartType::Histogram.
- Accedere al foglio di lavoro dei dati del grafico ChartDataWorkbook.
- Cancellare le serie e le categorie predefinite.
- Aggiungere nuove serie e categorie.
- Salvare la presentazione modificata come file PPTX.
Questo codice PHP mostra come creare un grafico istogramma:
$pres = new Presentation();
$chart = $pres->getSlides()->get_Item(0)->getShapes()->addChart(ChartType::Histogram, 50, 50, 500, 400);
$chart->getChartData()->getCategories()->clear();
$chart->getChartData()->getSeries()->clear();
$wb = $chart->getChartData()->getChartDataWorkbook();
$wb->clear(0);
$series = $chart->getChartData()->getSeries()->add(ChartType::Histogram);
$series->getDataPoints()->addDataPointForHistogramSeries($wb->getCell(0, "A1", 15));
$series->getDataPoints()->addDataPointForHistogramSeries($wb->getCell(0, "A2", -41));
$series->getDataPoints()->addDataPointForHistogramSeries($wb->getCell(0, "A3", 16));
$series->getDataPoints()->addDataPointForHistogramSeries($wb->getCell(0, "A4", 10));
$series->getDataPoints()->addDataPointForHistogramSeries($wb->getCell(0, "A5", -23));
$series->getDataPoints()->addDataPointForHistogramSeries($wb->getCell(0, "A6", 16));
$chart->getAxes()->getHorizontalAxis()->setAggregationType(AxisAggregationType::Automatic);
Creare grafici radar
- Creare un’istanza della classe Presentation.
- Ottenere un riferimento a una diapositiva usando il suo indice.
- Aggiungere un grafico con alcuni dati e specificare il tipo di grafico preferito (ChartType::Radar in questo caso).
- Salvare la presentazione modificata come file PPTX.
Questo codice PHP mostra come creare un grafico radar:
$pres = new Presentation();
try {
$pres->getSlides()->get_Item(0)->getShapes()->addChart(ChartType::Radar, 20, 20, 400, 300);
$pres->save("Radar-chart.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Creare grafici multicategoria
- Creare un’istanza della classe Presentation.
- Ottenere un riferimento a una diapositiva usando il suo indice.
- Aggiungere un grafico con dati predefiniti e specificare il tipo ChartType::ClusteredColumn.
- Accedere al foglio di lavoro dei dati del grafico ChartDataWorkbook.
- Cancellare le serie e le categorie predefinite.
- Aggiungere nuove serie e categorie.
- Aggiungere nuovi dati al grafico per le serie.
- Salvare la presentazione modificata come file PPTX.
Questo codice PHP mostra come creare un grafico multicategoria:
$pres = new Presentation();
try {
$ch = $pres->getSlides()->get_Item(0)->getShapes()->addChart(ChartType::ClusteredColumn, 100, 100, 600, 450);
$ch->getChartData()->getSeries()->clear();
$ch->getChartData()->getCategories()->clear();
$fact = $ch->getChartData()->getChartDataWorkbook();
$fact->clear(0);
$defaultWorksheetIndex = 0;
$category = $ch->getChartData()->getCategories()->add($fact->getCell(0, "c2", "A"));
$category->getGroupingLevels()->setGroupingItem(1, "Group1");
$category = $ch->getChartData()->getCategories()->add($fact->getCell(0, "c3", "B"));
$category = $ch->getChartData()->getCategories()->add($fact->getCell(0, "c4", "C"));
$category->getGroupingLevels()->setGroupingItem(1, "Group2");
$category = $ch->getChartData()->getCategories()->add($fact->getCell(0, "c5", "D"));
$category = $ch->getChartData()->getCategories()->add($fact->getCell(0, "c6", "E"));
$category->getGroupingLevels()->setGroupingItem(1, "Group3");
$category = $ch->getChartData()->getCategories()->add($fact->getCell(0, "c7", "F"));
$category = $ch->getChartData()->getCategories()->add($fact->getCell(0, "c8", "G"));
$category->getGroupingLevels()->setGroupingItem(1, "Group4");
$category = $ch->getChartData()->getCategories()->add($fact->getCell(0, "c9", "H"));
# Aggiunta di serie
$series = $ch->getChartData()->getSeries()->add($fact->getCell(0, "D1", "Series 1"), ChartType::ClusteredColumn);
$series->getDataPoints()->addDataPointForBarSeries($fact->getCell($defaultWorksheetIndex, "D2", 10));
$series->getDataPoints()->addDataPointForBarSeries($fact->getCell($defaultWorksheetIndex, "D3", 20));
$series->getDataPoints()->addDataPointForBarSeries($fact->getCell($defaultWorksheetIndex, "D4", 30));
$series->getDataPoints()->addDataPointForBarSeries($fact->getCell($defaultWorksheetIndex, "D5", 40));
$series->getDataPoints()->addDataPointForBarSeries($fact->getCell($defaultWorksheetIndex, "D6", 50));
$series->getDataPoints()->addDataPointForBarSeries($fact->getCell($defaultWorksheetIndex, "D7", 60));
$series->getDataPoints()->addDataPointForBarSeries($fact->getCell($defaultWorksheetIndex, "D8", 70));
$series->getDataPoints()->addDataPointForBarSeries($fact->getCell($defaultWorksheetIndex, "D9", 80));
# Salva presentazione con grafico
$pres->save("AsposeChart_out.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Creare grafici geografici (Map)
I grafici geografici visualizzano dati geografici e aiutano a confrontare i valori tra regioni.
Questo codice PHP mostra come creare un grafico geografico:
$pres = new Presentation();
try {
$chart = $pres->getSlides()->get_Item(0)->getShapes()->addChart(ChartType::Map, 50, 50, 500, 400);
$pres->save("mapChart.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Creare grafici combinati
Un grafico combinato (o combo) 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, aiutandoti a identificare le relazioni tra di essi.

Il seguente codice PHP mostra come creare il grafico combinato mostrato sopra in una presentazione PowerPoint:
function createComboChart() {
$presentation = new Presentation();
$slide = $presentation->getSlides()->get_Item(0);
try {
$chart = createChartWithFirstSeries($slide);
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);
// Imposta il titolo del grafico.
$chart->setTitle(true);
$chart->getChartTitle()->addTextFrameForOverriding("Chart Title");
$chart->getChartTitle()->setOverlay(false);
$titleParagraph = $chart->getChartTitle()->getTextFrameForOverriding()->getParagraphs()->get_Item(0);
$titleFormat = $titleParagraph->getParagraphFormat()->getDefaultPortionFormat();
$titleFormat->setFontBold(NullableBool::False);
$titleFormat->setFontHeight(18);
// Imposta la legenda del grafico.
$chart->getLegend()->setPosition(LegendPositionType::Bottom);
$chart->getLegend()->getTextFormat()->getPortionFormat()->setFontHeight(12);
// Elimina le serie e le categorie generate di default.
$chart->getChartData()->getSeries()->clear();
$chart->getChartData()->getCategories()->clear();
$worksheetIndex = 0;
$workbook = $chart->getChartData()->getChartDataWorkbook();
// Aggiungi nuove categorie.
$chart->getChartData()->getCategories()->add($workbook->getCell($worksheetIndex, 1, 0, "Category 1"));
$chart->getChartData()->getCategories()->add($workbook->getCell($worksheetIndex, 2, 0, "Category 2"));
$chart->getChartData()->getCategories()->add($workbook->getCell($worksheetIndex, 3, 0, "Category 3"));
$chart->getChartData()->getCategories()->add($workbook->getCell($worksheetIndex, 4, 0, "Category 4"));
// Aggiungi la prima serie.
$seriesNameCell = $workbook->getCell($worksheetIndex, 0, 1, "Series 1");
$series = $chart->getChartData()->getSeries()->add($seriesNameCell, $chart->getType());
$series->getParentSeriesGroup()->setOverlap(-25);
$series->getParentSeriesGroup()->setGapWidth(220);
$series->getDataPoints()->addDataPointForBarSeries($workbook->getCell($worksheetIndex, 1, 1, 4.3));
$series->getDataPoints()->addDataPointForBarSeries($workbook->getCell($worksheetIndex, 2, 1, 2.5));
$series->getDataPoints()->addDataPointForBarSeries($workbook->getCell($worksheetIndex, 3, 1, 3.5));
$series->getDataPoints()->addDataPointForBarSeries($workbook->getCell($worksheetIndex, 4, 1, 4.5));
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);
$series->getDataPoints()->addDataPointForBarSeries($workbook->getCell($worksheetIndex, 1, 2, 2.4));
$series->getDataPoints()->addDataPointForBarSeries($workbook->getCell($worksheetIndex, 2, 2, 4.4));
$series->getDataPoints()->addDataPointForBarSeries($workbook->getCell($worksheetIndex, 3, 2, 1.8));
$series->getDataPoints()->addDataPointForBarSeries($workbook->getCell($worksheetIndex, 4, 2, 2.8));
}
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);
$series->getDataPoints()->addDataPointForLineSeries($workbook->getCell($worksheetIndex, 1, 3, 2.0));
$series->getDataPoints()->addDataPointForLineSeries($workbook->getCell($worksheetIndex, 2, 3, 2.0));
$series->getDataPoints()->addDataPointForLineSeries($workbook->getCell($worksheetIndex, 3, 3, 3.0));
$series->getDataPoints()->addDataPointForLineSeries($workbook->getCell($worksheetIndex, 4, 3, 5.0));
$series->setPlotOnSecondAxis(true);
}
function setPrimaryAxesFormat($chart) {
// Imposta l'asse orizzontale.
$horizontalAxis = $chart->getAxes()->getHorizontalAxis();
$horizontalAxis->getTextFormat()->getPortionFormat()->setFontHeight(12);
$horizontalAxis->getFormat()->getLine()->getFillFormat()->setFillType(FillType::NoFill);
setAxisTitle($horizontalAxis, "X Axis");
// Imposta l'asse verticale.
$verticalAxis = $chart->getAxes()->getVerticalAxis();
$verticalAxis->getTextFormat()->getPortionFormat()->setFontHeight(12);
$verticalAxis->getFormat()->getLine()->getFillFormat()->setFillType(FillType::NoFill);
setAxisTitle($verticalAxis, "Y Axis 1");
// Imposta il colore delle linee della griglia principale verticale.
$majorGridLinesFormat = $verticalAxis->getMajorGridLinesFormat()->getLine()->getFillFormat();
$majorGridLinesFormat->setFillType(FillType::Solid);
$majorGridLinesFormat->getSolidFillColor()->setColor(new java("java.awt.Color", 217, 217, 217));
}
function setSecondaryAxesFormat($chart) {
// Imposta l'asse orizzontale secondario.
$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);
// Imposta l'asse verticale secondario.
$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);
$titleParagraph = $axis->getTitle()->addTextFrameForOverriding($axisTitle)->getParagraphs()->get_Item(0);
$titleFormat = $titleParagraph->getParagraphFormat()->getDefaultPortionFormat();
$titleFormat->setFontBold(NullableBool::False);
$titleFormat->setFontHeight(12);
}
Aggiornare i grafici
- Creare un’istanza della classe Presentation che rappresenta la presentazione contenente il grafico da aggiornare.
- Ottenere un riferimento a una diapositiva usando il suo indice.
- Attraversare tutte le forme per trovare il grafico desiderato.
- Accedere al foglio di dati del grafico.
- Modificare le serie di dati del grafico cambiando i valori delle serie.
- Aggiungere una nuova serie e popolarne i dati.
- Salvare la presentazione modificata come file PPTX.
Questo codice PHP mostra come aggiornare un grafico:
$pres = new Presentation();
try {
# Accedi al primo slideMarker
$sld = $pres->getSlides()->get_Item(0);
# Ottieni il grafico con dati predefiniti
$chart = $sld->getShapes()->get_Item(0);
# Imposta l'indice del foglio dati del grafico
$defaultWorksheetIndex = 0;
# Recupera il foglio di lavoro dei dati del grafico
$fact = $chart->getChartData()->getChartDataWorkbook();
# Modifica il nome della categoria del grafico
$fact->getCell($defaultWorksheetIndex, 1, 0, "Modified Category 1");
$fact->getCell($defaultWorksheetIndex, 2, 0, "Modified Category 2");
# Prendi la prima serie del grafico
$series = $chart->getChartData()->getSeries()->get_Item(0);
# Aggiorna ora i dati della serie
$fact->getCell($defaultWorksheetIndex, 0, 1, "New_Series1"); // Modifica nome della serie
$series->getDataPoints()->get_Item(0)->getValue()->setData(90);
$series->getDataPoints()->get_Item(1)->getValue()->setData(123);
$series->getDataPoints()->get_Item(2)->getValue()->setData(44);
# Prendi la seconda serie del grafico
$series = $chart->getChartData()->getSeries()->get_Item(1);
# Aggiorna ora i dati della serie
$fact->getCell($defaultWorksheetIndex, 0, 2, "New_Series2"); // Modifica nome della serie
$series->getDataPoints()->get_Item(0)->getValue()->setData(23);
$series->getDataPoints()->get_Item(1)->getValue()->setData(67);
$series->getDataPoints()->get_Item(2)->getValue()->setData(99);
# Aggiungi ora una nuova serie
$chart->getChartData()->getSeries()->add($fact->getCell($defaultWorksheetIndex, 0, 3, "Series 3"), $chart->getType());
# Prendi la terza serie del grafico
$series = $chart->getChartData()->getSeries()->get_Item(2);
# Popola ora i dati della serie
$series->getDataPoints()->addDataPointForBarSeries($fact->getCell($defaultWorksheetIndex, 1, 3, 20));
$series->getDataPoints()->addDataPointForBarSeries($fact->getCell($defaultWorksheetIndex, 2, 3, 50));
$series->getDataPoints()->addDataPointForBarSeries($fact->getCell($defaultWorksheetIndex, 3, 3, 30));
$chart->setType(ChartType::ClusteredCylinder);
# Salva la presentazione con il grafico
$pres->save("AsposeChartModified_out.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Impostare l’intervallo dati per un grafico
Per impostare l’intervallo dati per un grafico, esegui quanto segue:
- Creare un’istanza della classe Presentation che rappresenta la presentazione contenente il grafico.
- Ottenere un riferimento a una diapositiva usando il suo indice.
- Attraversare tutte le forme per trovare il grafico desiderato.
- Accedere ai dati del grafico e impostare l’intervallo.
- Salvare la presentazione modificata come file PPTX.
Questo codice PHP mostra come impostare l’intervallo dati per un grafico:
$pres = new Presentation();
try {
$slide = $pres->getSlides()->get_Item(0);
$chart = $slide->getShapes()->get_Item(0);
$chart->getChartData()->setRange("Sheet1!A1:B4");
$pres->save("SetDataRange_out.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
Usare i marcatori predefiniti nei grafici
Quando usi i marcatori predefiniti nei grafici, ogni serie del grafico ottiene automaticamente un simbolo di marcatore diverso.
Questo codice PHP mostra come impostare automaticamente un marcatore per una serie di grafico:
$pres = new Presentation();
try {
$slide = $pres->getSlides()->get_Item(0);
$chart = $slide->getShapes()->addChart(ChartType::LineWithMarkers, 10, 10, 400, 400);
$chart->getChartData()->getSeries()->clear();
$chart->getChartData()->getCategories()->clear();
$fact = $chart->getChartData()->getChartDataWorkbook();
$chart->getChartData()->getSeries()->add($fact->getCell(0, 0, 1, "Series 1"), $chart->getType());
$series = $chart->getChartData()->getSeries()->get_Item(0);
$chart->getChartData()->getCategories()->add($fact->getCell(0, 1, 0, "C1"));
$series->getDataPoints()->addDataPointForLineSeries($fact->getCell(0, 1, 1, 24));
$chart->getChartData()->getCategories()->add($fact->getCell(0, 2, 0, "C2"));
$series->getDataPoints()->addDataPointForLineSeries($fact->getCell(0, 2, 1, 23));
$chart->getChartData()->getCategories()->add($fact->getCell(0, 3, 0, "C3"));
$series->getDataPoints()->addDataPointForLineSeries($fact->getCell(0, 3, 1, -10));
$chart->getChartData()->getCategories()->add($fact->getCell(0, 4, 0, "C4"));
$series->getDataPoints()->addDataPointForLineSeries($fact->getCell(0, 4, 1, null));
$chart->getChartData()->getSeries()->add($fact->getCell(0, 0, 2, "Series 2"), $chart->getType());
# Prendi la seconda serie del grafico
$series2 = $chart->getChartData()->getSeries()->get_Item(1);
# Ora popolando i dati della serie
$series2->getDataPoints()->addDataPointForLineSeries($fact->getCell(0, 1, 2, 30));
$series2->getDataPoints()->addDataPointForLineSeries($fact->getCell(0, 2, 2, 10));
$series2->getDataPoints()->addDataPointForLineSeries($fact->getCell(0, 3, 2, 60));
$series2->getDataPoints()->addDataPointForLineSeries($fact->getCell(0, 4, 2, 40));
$chart->setLegend(true);
$chart->getLegend()->setOverlay(false);
$pres->save("DefaultMarkersInChart.pptx", SaveFormat::Pptx);
} finally {
if (!java_is_null($pres)) {
$pres->dispose();
}
}
FAQ
Quali tipi di grafico sono supportati da Aspose.Slides?
Aspose.Slides supporta una vasta gamma di tipi di grafico, tra cui barre, linee, torta, area, dispersione, istogramma, radar e molti altri. Questa flessibilità ti consente di scegliere il tipo di grafico più adatto alle esigenze di visualizzazione dei dati.
Come aggiungere 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, quindi chiami il metodo per aggiungere un grafico, specificando il tipo di grafico e i dati iniziali. Questo processo integra il grafico direttamente nella presentazione.
Come posso aggiornare i dati visualizzati in un grafico?
Puoi aggiornare i dati di un grafico accedendo al suo foglio di lavoro dei dati (ChartDataWorkbook), cancellando le serie e le categorie predefinite e aggiungendo i tuoi dati personalizzati. Questo ti permette di aggiornare il grafico per riflettere i dati più recenti.
È possibile personalizzare l’aspetto del grafico?
Sì, Aspose.Slides fornisce ampie opzioni di personalizzazione. Puoi modificare colori, caratteri, etichette, legende e altri elementi di formattazione per adattare l’aspetto del grafico ai requisiti di progettazione specifici.