Read the complete article on how to create charts in PowerPoint presentations using Java: https://blog.aspose.com/2021/07/29/create-charts-in-powerpoint-using-java/
Last active
July 30, 2021 00:07
-
-
Save aspose-com-gists/84e6152077c1d97df6d8d624c0299ad2 to your computer and use it in GitHub Desktop.
Create Charts in PowerPoint using Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a new presentation | |
Presentation pres = new Presentation(); | |
try { | |
// Add a new column chart and clear the default categories and series | |
IChart ch = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.ClusteredColumn, 100, 100, 600, 450); | |
ch.getChartData().getSeries().clear(); | |
ch.getChartData().getCategories().clear(); | |
// Access the chart data | |
IChartDataWorkbook fact = ch.getChartData().getChartDataWorkbook(); | |
fact.clear(0); | |
int defaultWorksheetIndex = 0; | |
// Add categories | |
IChartCategory 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")); | |
// Add Series | |
IChartSeries 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)); | |
// Save presentation with chart | |
pres.save("column-chart.pptx", SaveFormat.Pptx); | |
} finally { | |
if (pres != null) pres.dispose(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a new presentation | |
Presentation pres = new Presentation(); | |
try { | |
// Add a new histogram chart and clear its categories and series | |
IChart chart = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.Histogram, 50, 50, 500, 400); | |
chart.getChartData().getCategories().clear(); | |
chart.getChartData().getSeries().clear(); | |
// Access chart data | |
IChartDataWorkbook wb = chart.getChartData().getChartDataWorkbook(); | |
wb.clear(0); | |
// Add series | |
IChartSeries 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); | |
// Save the presentation | |
pres.save("histogram-chart.pptx", SaveFormat.Pptx); | |
} finally { | |
if (pres != null) pres.dispose(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Instantiate Presentation class that represents PPTX file | |
Presentation pres = new Presentation(); | |
try { | |
// Access first slide | |
ISlide slides = pres.getSlides().get_Item(0); | |
// Add chart with default data | |
IChart chart = slides.getShapes().addChart(ChartType.Pie, 100, 100, 400, 400); | |
// Set chart Title | |
chart.getChartTitle().addTextFrameForOverriding("Sample Title"); | |
chart.getChartTitle().getTextFrameForOverriding().getTextFrameFormat().setCenterText(NullableBool.True); | |
chart.getChartTitle().setHeight(20); | |
chart.setTitle(true); | |
// Set first series to Show Values | |
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true); | |
// Set the index of chart data sheet | |
int defaultWorksheetIndex = 0; | |
// Get the chart data worksheet | |
IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook(); | |
// Delete default generated series and categories | |
chart.getChartData().getSeries().clear(); | |
chart.getChartData().getCategories().clear(); | |
// Add new categories | |
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")); | |
// Add new series | |
IChartSeries series = chart.getChartData().getSeries().add(fact.getCell(0, 0, 1, "Series 1"), chart.getType()); | |
// Now populating series data | |
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)); | |
// Not working in new version | |
// Adding new points and setting sector color | |
// series.IsColorVaried = true; | |
chart.getChartData().getSeriesGroups().get_Item(0).setColorVaried(true); | |
IChartDataPoint point = series.getDataPoints().get_Item(0); | |
point.getFormat().getFill().setFillType(FillType.Solid); | |
point.getFormat().getFill().getSolidFillColor().setColor(Color.CYAN); | |
// Set Sector border | |
point.getFormat().getLine().getFillFormat().setFillType(FillType.Solid); | |
point.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.GRAY); | |
point.getFormat().getLine().setWidth(3.0); | |
//point.getFormat().getLine().setStyle(LineStyle.ThinThick); | |
//point.getFormat().getLine().setDashStyle(LineDashStyle.DashDot); | |
IChartDataPoint point1 = series.getDataPoints().get_Item(1); | |
point1.getFormat().getFill().setFillType(FillType.Solid); | |
point1.getFormat().getFill().getSolidFillColor().setColor(Color.ORANGE); | |
// Set Sector border | |
point1.getFormat().getLine().getFillFormat().setFillType(FillType.Solid); | |
point1.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.BLUE); | |
point1.getFormat().getLine().setWidth(3.0); | |
//point1.getFormat().getLine().setStyle(LineStyle.Single); | |
//point1.getFormat().getLine().setDashStyle(LineDashStyle.LargeDashDot); | |
IChartDataPoint point2 = series.getDataPoints().get_Item(2); | |
point2.getFormat().getFill().setFillType(FillType.Solid); | |
point2.getFormat().getFill().getSolidFillColor().setColor(Color.YELLOW); | |
// Set Sector border | |
point2.getFormat().getLine().getFillFormat().setFillType(FillType.Solid); | |
point2.getFormat().getLine().getFillFormat().getSolidFillColor().setColor(Color.RED); | |
point2.getFormat().getLine().setWidth(2.0); | |
//point2.getFormat().getLine().setStyle(LineStyle.ThinThin); | |
//point2.getFormat().getLine().setDashStyle(LineDashStyle.LargeDashDotDot); | |
// Create custom labels for each of categories for new series | |
IDataLabel lbl1 = series.getDataPoints().get_Item(0).getLabel(); | |
// lbl.ShowCategoryName = true; | |
lbl1.getDataLabelFormat().setShowValue(true); | |
IDataLabel lbl2 = series.getDataPoints().get_Item(1).getLabel(); | |
lbl2.getDataLabelFormat().setShowValue(true); | |
lbl2.getDataLabelFormat().setShowLegendKey(true); | |
lbl2.getDataLabelFormat().setShowPercentage(true); | |
IDataLabel lbl3 = series.getDataPoints().get_Item(2).getLabel(); | |
lbl3.getDataLabelFormat().setShowSeriesName(true); | |
lbl3.getDataLabelFormat().setShowPercentage(true); | |
// Show Leader Lines for Chart | |
series.getLabels().getDefaultDataLabelFormat().setShowLeaderLines(true); | |
// Set Rotation Angle for Pie Chart Sectors | |
chart.getChartData().getSeriesGroups().get_Item(0).setFirstSliceAngle(180); | |
// Save presentation with chart | |
pres.save("pie-chart.pptx", SaveFormat.Pptx); | |
} finally { | |
if (pres != null) pres.dispose(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Instantiate Presentation class that represents PPTX file | |
Presentation pres = new Presentation(); | |
try { | |
// Access first slide | |
ISlide slide = pres.getSlides().get_Item(0); | |
// Create the default chart | |
IChart chart = slide.getShapes().addChart(ChartType.ScatterWithSmoothLines, 0, 0, 400, 400); | |
// Get the default chart data worksheet index | |
int defaultWorksheetIndex = 0; | |
// Get 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); | |
// Change 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)); | |
// Change the chart series marker | |
series.getMarker().setSize(10); | |
series.getMarker().setSymbol(MarkerStyleType.Circle); | |
// Save the presentation | |
pres.save("scatter-chart.pptx", SaveFormat.Pptx); | |
} finally { | |
if (pres != null) pres.dispose(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a new presentation | |
Presentation pres = new Presentation(); | |
try { | |
// Add chart | |
IChart chart = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.OpenHighLowClose, 50, 50, 600, 400, false); | |
// Clear categories and series | |
chart.getChartData().getSeries().clear(); | |
chart.getChartData().getCategories().clear(); | |
// Access chart data | |
IChartDataWorkbook wb = chart.getChartData().getChartDataWorkbook(); | |
// Add categories | |
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()); | |
// Add series data | |
IChartSeries 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); | |
for (IChartSeries ser : chart.getChartData().getSeries()) | |
{ | |
ser.getFormat().getLine().getFillFormat().setFillType(FillType.NoFill); | |
} | |
// Save the presentation | |
pres.save("output.pptx", SaveFormat.Pptx); | |
} finally { | |
if (pres != null) pres.dispose(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment