JAVA实现在面板中添加图表_Java 创建PowerPoint图表并为其添加趋势线
圖表,是指將既得數(shù)據(jù)用圖形的方式表示出來(lái)。在前文中我們介紹過(guò)如何使用Java程序來(lái)為Excel文檔創(chuàng)建圖表的方法。本文將通過(guò)使用Java程序來(lái)演示如何創(chuàng)建PowerPoint圖表及為圖表添加趨勢(shì)線。趨勢(shì)線的運(yùn)用能夠顯示數(shù)據(jù)的變化趨勢(shì),同時(shí)能夠幫助預(yù)測(cè)數(shù)據(jù)的未來(lái)值。
Jar文件獲取及導(dǎo)入:
方法1:通過(guò)官網(wǎng)下載獲取jar包。解壓后將lib文件夾下的Spire.Presentation.jar文件導(dǎo)入Java程序。(如下圖)
方法2:通過(guò)maven倉(cāng)庫(kù)安裝導(dǎo)入。具體安裝教程參見(jiàn)此網(wǎng)頁(yè)。
【示例1】創(chuàng)建圖表
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 AddChart {
public static void main(String[] args) throws Exception {
//實(shí)例化一個(gè)Presentation對(duì)象
Presentation presentation = new Presentation();
//插入柱形圖
Rectangle2D.Double rect = new Rectangle2D.Double(40, 100, 550, 320);
IChart chart = null;
chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.COLUMN_CLUSTERED, rect);
//添加表名
chart.getChartTitle().getTextProperties().setText("銷售報(bào)表");
chart.getChartTitle().getTextProperties().isCentered(true);
chart.getChartTitle().setHeight(30);
chart.hasTitle(true);
//創(chuàng)建后臺(tái)數(shù)據(jù)表
DataTable dataTable = new DataTable();
dataTable.getColumns().add(new DataColumn("銷售額", DataTypes.DATATABLE_STRING));
dataTable.getColumns().add(new DataColumn("谷物", DataTypes.DATATABLE_INT));
dataTable.getColumns().add(new DataColumn("糧油", DataTypes.DATATABLE_INT));
dataTable.getColumns().add(new DataColumn("百貨", DataTypes.DATATABLE_INT));
DataRow row1 = dataTable.newRow();
row1.setString("銷售額", "門店1");
row1.setInt("谷物", 250);
row1.setInt("糧油", 150);
row1.setInt("百貨", 99);
DataRow row2 = dataTable.newRow();
row2.setString("銷售額", "門店2");
row2.setInt("谷物", 270);
row2.setInt("糧油", 150);
row2.setInt("百貨", 99);
DataRow row3 = dataTable.newRow();
row3.setString("銷售額", "門店3");
row3.setInt("谷物", 310);
row3.setInt("糧油", 120);
row3.setInt("百貨", 49);
DataRow row4 = dataTable.newRow();
row4.setString("銷售額", "門店4");
row4.setInt("谷物", 330);
row4.setInt("糧油", 120);
row4.setInt("百貨", 49);
DataRow row5 = dataTable.newRow();
row5.setString("銷售額", "門店5");
row5.setInt("谷物", 360);
row5.setInt("糧油", 150);
row5.setInt("百貨", 141);
DataRow row6 = dataTable.newRow();
row6.setString("銷售額", "門店6");
row6.setInt("谷物", 380);
row6.setInt("糧油", 150);
row6.setInt("百貨", 135);
dataTable.getRows().add(row1);
dataTable.getRows().add(row2);
dataTable.getRows().add(row3);
dataTable.getRows().add(row4);
dataTable.getRows().add(row5);
dataTable.getRows().add(row6);
//將數(shù)據(jù)寫入圖表
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]);
}
}
//設(shè)置系列標(biāo)簽
chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "D1"));
//設(shè)置類別標(biāo)簽
chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A7"));
//為各個(gè)系列賦值
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(2).getFill().setFillType(FillFormatType.SOLID);
chart.getSeries().get(2).getFill().getSolidColor().setKnownColor(KnownColors.LIGHT_BLUE);
//設(shè)置系列重疊
chart.setOverLap(-50);
//設(shè)置類別間距
chart.setGapDepth(200);
//保存文檔
presentation.saveToFile("output/CreateChart.pptx", FileFormat.PPTX_2010);
}
}
創(chuàng)建效果:
【示例2】為圖表添加趨勢(shì)線
import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.charts.IChart;
import com.spire.presentation.charts.ITrendlines;
import com.spire.presentation.charts.TrendlineSimpleType;
public class AddTrendline {
public static void main(String[] args) throws Exception {
//創(chuàng)建Presentation實(shí)例
Presentation ppt = new Presentation();
//加載PowerPoint文檔
ppt.loadFromFile("C:\\Users\\Test1\\Desktop\\CreateChart.pptx");
//獲取第一張幻燈片
ISlide slide = ppt.getSlides().get(0);
//獲取幻燈片上的圖表
IChart chart = (IChart)slide.getShapes().get(0);
//給圖表的第一個(gè)數(shù)據(jù)系列添加線性趨勢(shì)線
ITrendlines trendLine = chart.getSeries().get(0).addTrendLine(TrendlineSimpleType.LINEAR);
//不顯示公式
//trendLine.setdisplayEquation(false);
//不顯示R平方值
//trendLine.setdisplayRSquaredValue(false);
//保存結(jié)果文檔
ppt.saveToFile("output/AddTrendline.pptx", FileFormat.PPTX_2013);
}
}
添加效果:
(本文完)
總結(jié)
以上是生活随笔為你收集整理的JAVA实现在面板中添加图表_Java 创建PowerPoint图表并为其添加趋势线的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python之爬虫(四)之 Reques
- 下一篇: linux下安装mysql说明