In general, a chart is a graphical representation of data, and it allows users to see what the results of data to better understand and predict current and future data. Adding charts to a presentation can help create more impact and make data meaningful to your audience. In this article, I’ll show you how to add a chart to a presentation using Java codes programmatically.
Free Spire.Presentation for Java, a free API I used in this tutorial, supports creating many different types of data charts including column charts, cylinder charts, cone charts, pyramid charts, line charts, pie charts and so on. I’ll take a column chart as an example.
Dependency
Before typing codes, you need to
download the package and add the jar file to your project or directly reference
it from the following Maven configurations to the pom.xml.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.presentation.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
Create a column chart
Here are steps to create a column chart in a Presentation slide using Free Spire.Presentation for Java.
1. Create a PowerPoint document
2. Insert chart and set its title and style.
3. Create a dataTable and insert data into it.
4. Import data from dataTable to the column chart.
5. Set the Series label and Category label.
6. Assign data to each Series.
7. Set overlap and gap width.
8. Save the document.
import com.spire.presentation.*;
import com.spire.pdf.tables.table.*;
import com.spire.presentation.charts.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.geom.Rectangle2D;
import java.lang.Object;
public class CreateChart {
public static void main(String[] args) throws Exception {
//Create a presentation instance
Presentation presentation = new Presentation();
//Add a column clustered chart
Rectangle2D.Double rect = new Rectangle2D.Double(40, 100, 550, 320);
IChart chart = null;
chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.COLUMN_CLUSTERED, rect);
//Set chart title
chart.getChartTitle().getTextProperties().setText("Sales Report");
chart.getChartTitle().getTextProperties().isCentered(true);
chart.getChartTitle().setHeight(30);
chart.hasTitle(true);
//Create a dataTable
DataTable dataTable = new DataTable();
dataTable.getColumns().add(new DataColumn("Category", DataTypes.DATATABLE_STRING));
dataTable.getColumns().add(new DataColumn("the sales in the first season", DataTypes.DATATABLE_INT));
dataTable.getColumns().add(new DataColumn("the sales in the second season", DataTypes.DATATABLE_INT));
dataTable.getColumns().add(new DataColumn("the sales in the third season", DataTypes.DATATABLE_INT));
DataRow row1 = dataTable.newRow();
row1.setString("Category", "refrigerator");
row1.setInt("the sales in the first season",143000 );
row1.setInt("the sales in the second season", 102000);
row1.setInt("the sales in the third season", 124000);
DataRow row2 = dataTable.newRow();
row2.setString("Category", "air conditioning");
row2.setInt("the sales in the first season", 115000);
row2.setInt("the sales in the second season", 75000);
row2.setInt("the sales in the third season", 59000);
DataRow row3 = dataTable.newRow();
row3.setString("Category", "washing machine");
row3.setInt("the sales in the first season", 102000);
row3.setInt("the sales in the second season", 38000);
row3.setInt("the sales in the third season", 86000);
DataRow row4 = dataTable.newRow();
row4.setString("Category", "range hood");
row4.setInt("the sales in the first season", 144000);
row4.setInt("the sales in the second season", 56000);
row4.setInt("the sales in the third season", 97000);
dataTable.getRows().add(row1);
dataTable.getRows().add(row2);
dataTable.getRows().add(row3);
dataTable.getRows().add(row4);
//Import data from dataTable to chart data
for (int c = 0; c < dataTable.getColumns().size(); c++) {
chart.getChartData().get(0, c).setText(dataTable.getColumns().get(c).getColumnName());
}
for (int r = 0; r < dataTable.getRows().size(); r++) {
Object[] datas = dataTable.getRows().get(r).getArrayList();
for (int c = 0; c < datas.length; c++) {
chart.getChartData().get(r + 1, c).setValue(datas[c]);
}
}
//Set series label
chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "D1"));
//Set category labels
chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A6"));
//Assign values to each series
chart.getSeries().get(0).setValues(chart.getChartData().get("B2", "B6"));
chart.getSeries().get(1).setValues(chart.getChartData().get("C2", "C6"));
chart.getSeries().get(2).setValues(chart.getChartData().get("D2", "D6"));
chart.getSeries().get(2).getFill().setFillType(FillFormatType.SOLID);
chart.getSeries().get(2).getFill().getSolidColor().setKnownColor(KnownColors.LIGHT_BLUE);
//Set overlap
chart.setOverLap(-50);
//Set gap width
chart.setGapDepth(200);
//Save the document
presentation.saveToFile("output/CreateChart.pptx", FileFormat.PPTX_2010);
}
}Output
No comments:
Post a Comment