A
scatter chart, also known as an XY scatter chart, refers to a chart consisting
of some scattered points, where each point has its own X and Y values. We can
choose whether to connect these points or not as needed. This article will
introduce how to create a scatter chart
in a PowerPoint slide using Java codes with Free Spire.Presentation for Java.
ABOUT
FREE SPIRE.PRESENTATION FOR JAVA
Free
Spire.Presentation for Java is a free professional PowerPoint API that enables
developers to create, read, write, convert and save PowerPoint documents in
Java Applications. As an independent Java library, it doesn't need Microsoft
PowerPoint to be installed on system.
DEPENDENCY
First
of all, we need to download the Spire.Presentation.jar file from the link, and
add it to the Java program as a dependency. Or we can create a Maven project
and add the following code to the pom.xml file to import the JAR file in the
application.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://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>
USING THE
CODE
Free Spire.Presentation for
Java offers
ShapeCollection.appendChart(ChartType
type, Rectangle2D rectangle, boolean init) method to insert a specific type
of charts into a presentation slide. The
ChartType enumeration pre-defines 73 chart types, including scatter chart,
column chart, pie chart, etc. Here are detailed steps to follow.
l Create a Presentation object.
l Add a scatter chart to the
specific slide using ShapeCollection.appendChart()
method.
l Set the chart data through ChartData.get().setValue() method.
l Set the chart title, axes
titles, series labels, etc. using the methods under IChart interface.
l Set the grid line style and
data point line style.
l Save the document to another file
using Presentation.saveToFile() method.
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideSizeType;
import com.spire.presentation.TextLineStyle;
import com.spire.presentation.charts.ChartType;
import com.spire.presentation.charts.IChart;
import com.spire.presentation.charts.entity.ChartDataLabel;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class CreateScatterChart {
public static void main(String[] args) throws Exception {
//Create a Presentation object
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//Add a scatter chart to the first slide
IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.SCATTER_SMOOTH_LINES_AND_MARKERS,new Rectangle2D.Float(40, 80, 550, 320),false);
//Set the chart title
chart.getChartTitle().getTextProperties().setText("Scatter Chart");
chart.getChartTitle().getTextProperties().isCentered(true);
chart.getChartTitle().setHeight(20f);
chart.hasTitle(true);
//Set the chart data
Double[] xData = new Double[] { 1.0, 2.4, 5.0, 8.9 };
Double[] yData = new Double[] { 5.3, 15.2, 6.7, 8.0 };
chart.getChartData().get(0,0).setText("X-Values");
chart.getChartData().get(0,1).setText("Y-Values");
for (int i = 0; i < xData.length; i++) {
chart.getChartData().get(i+1,0).setValue(xData[i]);
chart.getChartData().get(i+1,1).setValue(yData[i]);
}
//Set the series label
chart.getSeries().setSeriesLabel(chart.getChartData().get("B1","B1"));
//Set the X and Y values
chart.getSeries().get(0).setXValues(chart.getChartData().get("A2","A5"));
chart.getSeries().get(0).setYValues(chart.getChartData().get("B2","B5"));
//Add data labels
for (int i = 0; i < 4; i++)
{
ChartDataLabel dataLabel = chart.getSeries().get(0).getDataLabels().add();
dataLabel.setLabelValueVisible(true);
}
//Set the primary axis title and the secondary axis title
chart.getPrimaryValueAxis().hasTitle(true);
chart.getPrimaryValueAxis().getTitle().getTextProperties().setText("X-Axis Title");
chart.getSecondaryValueAxis().hasTitle(true);
chart.getSecondaryValueAxis().getTitle().getTextProperties().setText("Y-Axis Title");
//Set the grid line
chart.getSecondaryValueAxis().getMajorGridTextLines().setFillType(FillFormatType.SOLID);
chart.getSecondaryValueAxis().getMajorGridTextLines().setStyle(TextLineStyle.THIN_THIN);
chart.getSecondaryValueAxis().getMajorGridTextLines().getSolidFillColor().setColor(Color.GRAY);
chart.getPrimaryValueAxis().getMajorGridTextLines().setFillType(FillFormatType.NONE);
//Set the data point line
chart.getSeries().get(0).getLine().setFillType(FillFormatType.SOLID);
chart.getSeries().get(0).getLine().setWidth(0.1f);
chart.getSeries().get(0).getLine().getSolidFillColor().setColor(Color.BLUE);
//Save the document to file
presentation.saveToFile("output/ScatterChart.pptx", FileFormat.PPTX_2013);
}
}
