您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)Java中怎么創(chuàng)建Excel 散點(diǎn)圖,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
JAR包導(dǎo)入:
方法1:下載 Free Spire.XLS for Java包并解壓縮,然后將lib文件夾下的Spire.Xls.jar包作為依賴項(xiàng)導(dǎo)入到Java應(yīng)用程序中。
方法2: 直接通過(guò)Maven倉(cāng)庫(kù)安裝JAR包,配置pom.xml文件的代碼如下
<repositories> <repository> <id>com.e-iceblue</id> <url>http://repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.xls.free</artifactId> <version>2.2.0</version> </dependency> </dependencies>
創(chuàng)建散點(diǎn)圖:
import com.spire.xls.*; import com.spire.xls.core.IChartTrendLine; public class ScatterChart { public static void main(String[] args) { //實(shí)例化Workbook類(lèi)的對(duì)象,并獲取第一個(gè)工作表 Workbook workbook = new Workbook(); Worksheet sheet = workbook.getWorksheets().get(0); //設(shè)置列寬,工作表名 sheet.getCellRange("A1:B1").setColumnWidth(15f);; sheet.setName("散點(diǎn)圖"); //添加圖表數(shù)據(jù)源 sheet.getCellRange("A1").setValue("城市化水平"); sheet.getCellRange("A2").setValue("10"); sheet.getCellRange("A3").setValue("15"); sheet.getCellRange("A4").setValue("17"); sheet.getCellRange("A5").setValue("25"); sheet.getCellRange("A6").setValue("35"); sheet.getCellRange("A7").setValue("40"); sheet.getCellRange("A8").setValue("38"); sheet.getCellRange("A10").setValue ("17"); sheet.getCellRange("A9").setValue ("24"); sheet.getCellRange("B1").setValue("耕地面積"); sheet.getCellRange("B2").setValue ("26780"); sheet.getCellRange("B3").setValue("24086"); sheet.getCellRange("B4").setValue ("20546"); sheet.getCellRange("B5").setValue ("15057"); sheet.getCellRange("B6").setValue ("11036"); sheet.getCellRange("B7").setValue ("12546"); sheet.getCellRange("B8").setValue("9854"); sheet.getCellRange("B9").setValue ("13506"); sheet.getCellRange("B10").setValue ("18756"); //創(chuàng)建散點(diǎn)圖 Chart chart = sheet.getCharts().add(ExcelChartType.ScatterMarkers); chart.setDataRange(sheet.getCellRange("B2:B10")); chart.setSeriesDataFromRange(false); //指定散點(diǎn)圖在sheet中的位置 chart.setLeftColumn(4); chart.setTopRow(1); chart.setRightColumn(15); chart.setBottomRow(25); //添加圖表標(biāo)題、系列標(biāo)簽 chart.setChartTitle("城市化與耕地面積變化散點(diǎn)圖"); chart.getChartTitleArea().isBold(true); chart.getChartTitleArea().setSize(12); chart.getSeries().get(0).setCategoryLabels(sheet.getCellRange("B2:B10")); chart.getSeries().get(0).setValues(sheet.getCellRange("A2:A10")); sheet.getCellRange("A1:B10").getStyle().setHorizontalAlignment(HorizontalAlignType.Center); sheet.getCellRange("A1:B1").getStyle().getFont().isBold(true); //添加趨勢(shì)線 IChartTrendLine trendLine = chart.getSeries().get(0).getTrendLines().add(TrendLineType.Exponential); trendLine.setName("趨勢(shì)線"); //添加坐標(biāo)軸名稱(chēng) chart.getPrimaryValueAxis().setTitle("城市化水平(%)"); chart.getPrimaryCategoryAxis().setTitle("耕地面積(萬(wàn)hm^2)"); //保存文檔 workbook.saveToFile("ScatterChart.xlsx",ExcelVersion.Version2010); workbook.dispose(); } }
看完上述內(nèi)容,你們對(duì)Java中怎么創(chuàng)建Excel 散點(diǎn)圖有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。