一、效果
二、代碼
2.1、maven依賴
<!-- lombok插件 start--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.jetbrains</groupId> <artifactId>annotations</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> <!-- lombok插件 end--> <!-- poi 操作excel start--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <!-- poi 操作excel end--> <!-- 繪制圖表 star--> <!-- https://mvnrepository.com/artifact/org.jfree/jfreechart --> <dependency> <groupId>org.jfree</groupId> <artifactId>jfreechart</artifactId> <version>1.0.19</version> </dependency> <!-- 繪制圖表 end-->
2.2、代碼實現
原理:把獲取到的服務器性能數據放到Excel表格中(獲取方法),然后把表格中的數據通過JFree繪制成折線圖
折線圖繪制活動概要圖
excel文件數據
ChartDataVo(圖表數據)

package report; import lombok.Data; import org.testng.internal.collections.Pair; import java.awt.*; import java.util.List; /** * @Author: Jarvis * @Date: 2020/6/17 12:24 * @Version: v1.0.0 * @Description: 圖表數據 */ @Data public class ChartDataVo { String title; // 標題 List<Pair<String, List<Pair<String, String>>>> dataList; // 數據 String typeName; // 分類名稱 String units; // y軸單位 Font font; // 字體 }
ServerDataVo(服務器數據)

package report; import lombok.Data; /** * @Author: Jarvis * @Date: 2020/6/16 20:52 * @Version: v1.0.0 * @Description: 服務器數據 */ @Data public class ServerDataVo { private String cpu; private String memory; private String date; }
ChartReport
package report; import java.awt.*; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.*; import java.util.List; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.CategoryItemRenderer; import org.jfree.chart.title.LegendTitle; import org.jfree.data.category.DefaultCategoryDataset; import org.testng.internal.collections.Pair; /** * @Author: Jarvis * @Date: 2020/6/16 18:02 * @Version: v1.0.0 * @Description: 輸出各種圖形圖表(如折線圖、柱狀圖等) */ public class ChartReport { private static final int EXCEL_DATA_DATE = 0; private static final int EXCEL_DATA_CPU = 1; private static final int EXCEL_DATA_MEMORY = 2; /** * 功能:獲取Excel中服務器的性能數據 * 備注:默認表格中的數據是按時間升序排列的 * * @param excelPath 文件路徑 * @return */ public static List<ServerDataVo> getServerDataByExcel(String excelPath) { ServerDataVo data; List<ServerDataVo> dataVoList = new ArrayList<>(); try { FileInputStream fileInputStream = new FileInputStream(excelPath); XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream); XSSFSheet sheet = workbook.getSheet("ServerData"); int lastRowNum = sheet.getLastRowNum(); // 遍歷所有行 for (int i = 1; i <= lastRowNum; i++) { data = new ServerDataVo(); // 獲取時間 data.setDate(sheet.getRow(i).getCell(EXCEL_DATA_DATE).getStringCellValue()); // 獲取CPU data.setCpu(String.valueOf(sheet.getRow(i).getCell(EXCEL_DATA_CPU).getNumericCellValue())); // 獲取內存 data.setMemory(String.valueOf(sheet.getRow(i).getCell(EXCEL_DATA_MEMORY).getNumericCellValue())); dataVoList.add(data); } return dataVoList; } catch (IOException e) { e.printStackTrace(); } return null; } /** * 功能:折線圖 * * @param outputReportPath 圖表輸出路徑 * @param chartDataVo 折線圖基本樣式及數據設置 */ public static void lineChart(String outputReportPath, ChartDataVo chartDataVo) { int lineTypeCount = 0; // 折線圖的種類數量 try { // 種類數據集 DefaultCategoryDataset dcd = new DefaultCategoryDataset(); // 折線圖數據寫入DefaultCategoryDataset for (Pair<String, List<Pair<String, String>>> da1 : chartDataVo.getDataList()) { lineTypeCount = da1.second().size(); for (Pair<String, String> da2 : da1.second()) { dcd.setValue( Double.valueOf(da2.second()), // 當前種類的折線圖值 da2.first(), // rowKey 當前折線圖種類名稱 da1.first() // columnKey 橫坐標軸數據 ); System.out.println(String.format("%s-%s-%s", Double.valueOf(da2.second()), da2.first(), da1.first())); } } //創建折線圖,折線圖分水平顯示和垂直顯示兩種 JFreeChart chart = ChartFactory.createLineChart(chartDataVo.getTitle(), chartDataVo.typeName, chartDataVo.getUnits(), dcd,//2D折線圖 PlotOrientation.VERTICAL, true, true, true); // JFreeChart chart = ChartFactory.createLineChart3D(title, typeName, units, dcd,//3D折線圖 // PlotOrientation.VERTICAL, // true, true, false); //設置整個圖片的標題字體 chart.getTitle().setFont(chartDataVo.getFont()); // 得到折線示例標識 chartDataVo.setFont(new Font("宋體", Font.BOLD, 15)); Font font = chartDataVo.getFont(); font = new Font("宋體", Font.BOLD, 15); LegendTitle legend = chart.getLegend(); legend.setItemFont(font); // 設置折線示例標識字體 // legend.setItemPaint(Color.WHITE); // 折線示例標識字體顏色 // legend.setBackgroundPaint(null); // 折線示例標識背景 // 設置圖表外層背景色 // chart.setBackgroundPaint(new Color(56, 56, 56)); //得到繪圖區 CategoryPlot plot = (CategoryPlot) chart.getPlot(); //得到繪圖區的域軸(橫軸),設置標簽的字體 plot.getDomainAxis().setLabelFont(font); //設置橫軸標簽項字體 plot.getDomainAxis().setTickLabelFont(font); // plot.setBackgroundPaint(null); // 設置繪圖區的背景色 plot.setOutlineVisible(false); // 設置繪圖區的邊框 plot.setBackgroundAlpha(0.0f); // 設置繪圖區背景透明度 plot.setForegroundAlpha(1.0f); // 設置圖形中折線的透明度(0.0f - 1.0f) //設置范圍軸(縱軸)字體 font = new Font("宋體", Font.BOLD, 18); plot.getRangeAxis().setLabelFont(font); plot.setDomainGridlinesVisible(false); // 是否顯示繪圖區縱軸網格線 plot.setRangeGridlinesVisible(true); // 是否顯示繪圖區橫軸網格線 plot.setRangeGridlinePaint(Color.BLACK); // 設置繪圖區橫軸網格線顏色 // 得到線條 CategoryItemRenderer renderer = plot.getRenderer(); // 設置折線圖中所有線條的寬度 for (int i = 0; i < lineTypeCount; i++) { renderer.setSeriesStroke(i, new BasicStroke(2F)); // 設置第i個折線的寬度 } //存儲成圖片 ChartUtilities.saveChartAsJPEG(new File(outputReportPath), chart, 1500, 800); } catch (Exception e) { e.printStackTrace(); } } /** * 功能:讀取表格中的服務器數據並生成圖表 * * @param excelPath 服務器數據 * @param outputReportPath 圖表輸出路徑 */ public static void outputChartReport(String excelPath, String outputReportPath) { List<ServerDataVo> serverDataList = getServerDataByExcel(excelPath); List<Pair<String, List<Pair<String, String>>>> data = new ArrayList<>(); ChartDataVo chartDataVo = new ChartDataVo(); List<Pair<String, String>> da; // 折線圖每列數據 for (int i = 0; i < serverDataList.size(); i++) { da = new ArrayList<>(); da.add(new Pair<>("CPU", serverDataList.get(i).getCpu())); // CPU折線圖縱軸數據 da.add(new Pair<>("內存", serverDataList.get(i).getMemory())); // 內存折線圖縱軸數據 data.add(new Pair<>(serverDataList.get(i).getDate(), da)); // 橫軸數據 } Font font = new Font("宋體", Font.BOLD, 20); chartDataVo.setTitle("XX服務器性能數據圖"); chartDataVo.setDataList(data); chartDataVo.setTypeName(""); chartDataVo.setUnits("使用率(%)"); chartDataVo.setFont(font); ChartReport.lineChart(outputReportPath, chartDataVo); } public static void main(String[] args) { String excelPath = "src/main/java/report/ServerDatas.xlsx"; String outputReportPath = "lineReport.png"; outputChartReport(excelPath, outputReportPath); } }