本文介绍如何使用Spire.Presentation for Java在PowerPoint文档中插入折线图。
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideSizeType;
import com.spire.presentation.charts.ChartLegendPositionType;
import com.spire.presentation.charts.ChartType;
import com.spire.presentation.charts.IChart;
import java.awt.geom.Rectangle2D;
public class LineChart {
public static void main(String[] args) throws Exception {
//创建Presentation对象
Presentation presentation = new Presentation();
presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);
//插入折线图
Rectangle2D.Double rect = new Rectangle2D.Double(100, 50, 600, 430);
IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.LINE, rect);
//设置图表标题
chart.getChartTitle().getTextProperties().setText("产品月销量趋势");
chart.getChartTitle().getTextProperties().isCentered(true);
chart.getChartTitle().setHeight(30);
chart.hasTitle(true);
//设置轴标题
chart.getPrimaryCategoryAxis().getTitle().getTextProperties().setText("月份");
chart.getPrimaryCategoryAxis().hasTitle(true);
chart.getPrimaryValueAxis().getTitle().getTextProperties().setText("销量");
chart.getPrimaryValueAxis().hasTitle(true);
//写入图表数据
chart.getChartData().get(0,0).setText("月份");
chart.getChartData().get(1,0).setText("一月");
chart.getChartData().get(2,0).setText("二月");
chart.getChartData().get(3,0).setText("三月");
chart.getChartData().get(4,0).setText("四月");
chart.getChartData().get(5,0).setText("五月");
chart.getChartData().get(6,0).setText("六月");
chart.getChartData().get(0,1).setText("台式机");
chart.getChartData().get(1,1).setNumberValue(80);
chart.getChartData().get(2,1).setNumberValue(45);
chart.getChartData().get(3,1).setNumberValue(25);
chart.getChartData().get(4,1).setNumberValue(20);
chart.getChartData().get(5,1).setNumberValue(10);
chart.getChartData().get(6,1).setNumberValue(5);
chart.getChartData().get(0,2).setText("笔记本");
chart.getChartData().get(1,2).setNumberValue(30);
chart.getChartData().get(2,2).setNumberValue(25);
chart.getChartData().get(3,2).setNumberValue(35);
chart.getChartData().get(4,2).setNumberValue(50);
chart.getChartData().get(5,2).setNumberValue(45);
chart.getChartData().get(6,2).setNumberValue(55);
chart.getChartData().get(0,3).setText("平板");
chart.getChartData().get(1,3).setNumberValue(10);
chart.getChartData().get(2,3).setNumberValue(15);
chart.getChartData().get(3,3).setNumberValue(20);
chart.getChartData().get(4,3).setNumberValue(35);
chart.getChartData().get(5,3).setNumberValue(60);
chart.getChartData().get(6,3).setNumberValue(95);
//设置系列标签
chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "D1"));
//设置分类标签
chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A7"));
//设置系列数据区域
chart.getSeries().get(0).setValues(chart.getChartData().get("B2", "B7"));
chart.getSeries().get(1).setValues(chart.getChartData().get("C2", "C7"));
chart.getSeries().get(2).setValues(chart.getChartData().get("D2", "D7"));
//在数据标签中显示数据
chart.getSeries().get(0).getDataLabels().setLabelValueVisible(true);
chart.getSeries().get(1).getDataLabels().setLabelValueVisible(true);
chart.getSeries().get(2).getDataLabels().setLabelValueVisible(true);
//设置图例位置
chart.getChartLegend().setPosition(ChartLegendPositionType.TOP);
//保存文档
presentation.saveToFile("LineChart.pptx", FileFormat.PPTX_2013);
}
}