Comment créer des graphiques dans une présentation

Support pour le code héritage

Pour utiliser le code héritage développé avec les versions d’Aspose.Slides pour Java antérieures à 14.x.x, vous devez apporter quelques modifications mineures à votre code et le code fonctionnera comme auparavant. Toutes les classes qui étaient présentes dans l’ancienne API Aspose.Slides pour Java sous les espaces de noms com.aspose.slides et com.aspose.slides.pptx sont maintenant fusionnées dans un espace de noms unique com.aspose.slides. Veuillez jeter un œil au simple extrait de code suivant pour créer un graphique normal à partir de zéro dans une présentation en utilisant l’API Aspose.Slides héritée et suivre les étapes décrivant comment migrer vers la nouvelle API fusionnée.

Approche héritée d’Aspose.Slides pour Java

//Instantiate PresentationEx class that represents PPTX file
PresentationEx pres = new PresentationEx();
//Access first slide
SlideEx sld = pres.getSlides().get_Item(0);
// Add chart with default data
ChartEx chart = sld.getShapes().addChart(ChartTypeEx.ClusteredColumn, 0, 0, 500, 500);
//Setting chart Title
chart.getChartTitle().getText().setText("Sample Title");
chart.getChartTitle().getText().setCenterText(true);
chart.getChartTitle().setHeight(20f);
chart.hasTitle(true);
//Set first series to Show Values
chart.getChartData().getSeries().get_Item(0).getLabels().setShowValue(true);
//Setting the index of chart data sheet
int defaultWorksheetIndex = 0;
//Getting the chart data worksheet
ChartDataCellFactory fact = chart.getChartData().getChartDataCellFactory();
//Delete default generated series and categories
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
int s = chart.getChartData().getSeries().getCapacity();
//Adding new series
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());
//Adding new categories
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"));
//Take first chart series
ChartSeriesEx series = chart.getChartData().getSeries().get_Item(0);
//Now populating series data
series.getValues().add(fact.getCell(defaultWorksheetIndex, 1, 1, 20));
series.getValues().add(fact.getCell(defaultWorksheetIndex, 2, 1, 50));
series.getValues().add(fact.getCell(defaultWorksheetIndex, 3, 1, 30));
//Setting fill color for series
series.getFormat().getFill().setFillType(FillTypeEx.Solid);
series.getFormat().getFill().getSolidFillColor().setColor(new java.awt.Color(com.aspose.slides.PresetColorEx.Red));
//Take second chart series
series = chart.getChartData().getSeries().get_Item(1);
//Now populating series data
series.getValues().add(fact.getCell(defaultWorksheetIndex, 1, 2, 30));
series.getValues().add(fact.getCell(defaultWorksheetIndex, 2, 2, 10));
series.getValues().add(fact.getCell(defaultWorksheetIndex, 3, 2, 60));
//Setting fill color for series
series.getFormat().getFill().setFillType(FillTypeEx.Solid);
series.getFormat().getFill().getSolidFillColor().setColor(new java.awt.Color(PresetColorEx.Green));
//create custom lables for each of categories for new series
//first label will be show Category name
DataLabelEx lbl = new DataLabelEx(series);
lbl.setShowCategoryName( true);
lbl.setId (0);
series.getLabels().add(lbl);
//Show series name for second label
lbl = new DataLabelEx(series);
lbl.setShowSeriesName(true);
lbl.setId(1);
series.getLabels().add(lbl);
//show value for third label
lbl = new DataLabelEx(series);
lbl.setShowValue(true);
lbl.setShowSeriesName(true);
lbl.setSeparator("/");
lbl.setId( 2);
series.getLabels().add(lbl);
// show value and custom text
lbl = new DataLabelEx(series);
lbl.getTextFrame().setText( "My text");
lbl.setId( 3);
series.getLabels().add(lbl);
// Save presentation with chart
pres.write("D:\\Aspose Data\\AsposeChart.pptx");

Nouvelle approche Aspose.Slides pour Java 14.x.x

//Instantiate Presentation class that represents PPTX file
Presentation pres = new Presentation();
//Access first slide
ISlide sld = pres.getSlides().get_Item(0);
// Add chart with default data
IChart chart = sld.getShapes().addChart(ChartType.ClusteredColumn, 0, 0, 500, 500);
//Setting chart Title
//chart.getChartTitle().TextFrameForOverriding.Text = "Sample Title";
chart.getChartTitle().addTextFrameForOverriding("Sample Title");
chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(NullableBool.True);
chart.getChartTitle().setHeight(20);
chart.hasTitle(true);
//Set first series to Show Values
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);
//Setting the index of chart data sheet
int defaultWorksheetIndex = 0;
//Getting the chart data worksheet
IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook();
//Delete default generated series and categories
chart.getChartData().getSeries().clear();
chart.getChartData().getCategories().clear();
int s = chart.getChartData().getSeries().size();
s = chart.getChartData().getCategories().size();
//Adding new series
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());
//Adding new categories
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"));
//Take first chart series
IChartSeries series = chart.getChartData().getSeries().get_Item(0);
//Now populating series data
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));
//Setting fill color for series
series.getFormat().getFill().setFillType(FillType.Solid);
series.getFormat().getFill().getSolidFillColor().setColor(java.awt.Color.RED);
//Take second chart series
series = chart.getChartData().getSeries().get_Item(1);
//Now populating series data
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));
//Setting fill color for series
series.getFormat().getFill().setFillType(FillType.Solid);
series.getFormat().getFill().getSolidFillColor().setColor(java.awt.Color.GREEN);
//create custom labels for each of categories for new series
//first label will be show Category name
IDataLabel lbl = series.getDataPoints().get_Item(0).getLabel();
lbl.getDataLabelFormat().setShowCategoryName(true);
lbl = series.getDataPoints().get_Item(1).getLabel();
lbl.getDataLabelFormat().setShowSeriesName(true);
//Show value for third label
lbl = series.getDataPoints().get_Item(2).getLabel();
lbl.getDataLabelFormat().setShowValue(true);
lbl.getDataLabelFormat().setShowSeriesName(true);
lbl.getDataLabelFormat().setSeparator("/");
//Save presentation with chart
pres.save("AsposeChart.pptx",SaveFormat.Pptx);

Veuillez jeter un œil au simple extrait de code suivant pour créer un graphique dispersé à partir de zéro dans une présentation en utilisant l’API héritée d’Aspose.Slides et comment y parvenir avec la nouvelle API fusionnée.

Approche héritée d’Aspose.Slides pour Java

//Instantiate PresentationEx class that represents PPTX file
PresentationEx pres = new PresentationEx("D:\\AsposeChart.pptx");
//Access first slide
SlideEx sld = pres.getSlides().get_Item(0);
// Add chart with default data
ChartEx chart = (ChartEx)sld.getShapes().get_Item(0);
//Setting the index of chart data sheet
int defaultWorksheetIndex = 0;
//Getting the chart data worksheet
ChartDataCellFactory fact = chart.getChartData().getChartDataCellFactory();
//Take first chart series
ChartSeriesEx series = chart.getChartData().getSeries().getItem(0);
//Now updating series data
fact.getCell(defaultWorksheetIndex, 0, 1, "New_Series1");//modifying series name
series.getValues().get_Item(0).setValue(90);
series.getValues().get_Item(1).setValue(123);
series.getValues().get_Item(2).setValue(44);
//Take Second chart series
series = chart.getChartData().getSeries().getItem(1);
//Now updating series data
fact.getCell(defaultWorksheetIndex, 0, 2, "New_Series2");//modifying series name
series.getValues().get_Item(0).setValue(23);
series.getValues().get_Item(1).setValue(67);
series.getValues().get_Item(2).setValue(99);
//Now, Adding a new series
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 3, "Series 3"), chart.getType());
//Take 3rd chart series
series = chart.getChartData().getSeries().getItem(2);
//Now populating series data
series.getValues().add(fact.getCell(defaultWorksheetIndex, 1, 3, 20));
series.getValues().add(fact.getCell(defaultWorksheetIndex, 2, 3, 50));
series.getValues().add(fact.getCell(defaultWorksheetIndex, 3, 3, 30));
chart.setType(ChartTypeEx.ClusteredCylinder);
// Save presentation with chart
pres.write("D:\\AsposeChartMoodified.pptx");

Nouvelle approche Aspose.Slides pour Java 14.x.x

Presentation pres = new Presentation();
ISlide slide = pres.getSlides().get_Item(0);
//Creating the default chart
IChart chart = slide.getShapes().addChart(ChartType.ScatterWithSmoothLines, 0, 0, 400, 400);
//Getting the default chart data worksheet index
int defaultWorksheetIndex = 0;
//Accessing the chart data worksheet
IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook();
//Delete demo series
chart.getChartData().getSeries().clear();
//Add new series
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());
//Take first chart series
IChartSeries series = chart.getChartData().getSeries().get_Item(0);
//Add new point (1:3) there.
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 1), fact.getCell(defaultWorksheetIndex, 2, 2, 3));
//Add new point (2:10)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 3, 1, 2), fact.getCell(defaultWorksheetIndex, 3, 2, 10));
//Edit the type of series
series.setType (ChartType.ScatterWithStraightLinesAndMarkers);
//Changing the chart series marker
series.getMarker().setSize(10);
series.getMarker().setSymbol(MarkerStyleType.Star);
//Take second chart series
series = chart.getChartData().getSeries().get_Item(1);
//Add new point (5:2) there.
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 2, 3, 5), fact.getCell(defaultWorksheetIndex, 2, 4, 2));
//Add new point (3:1)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 3, 3, 3), fact.getCell(defaultWorksheetIndex, 3, 4, 1));
//Add new point (2:2)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 4, 3, 2), fact.getCell(defaultWorksheetIndex, 4, 4, 2));
//Add new point (5:1)
series.getDataPoints().addDataPointForScatterSeries(fact.getCell(defaultWorksheetIndex, 5, 3, 5), fact.getCell(defaultWorksheetIndex, 5, 4, 1));
//Changing the chart series marker
series.getMarker().setSize(10);
series.getMarker().setSymbol (MarkerStyleType.Circle);
pres.save("AsposeScatterChart.pptx",SaveFormat.Pptx);