一、前言
最近做的一个图片信息提取,
用户上传的图片的信息丰富,使用的过程产生大量的数据,为了可视化数据库中的数据,生成数据报表,获取有用的信息、结论。
试着用 Java 的去实现
二、效果与代码
点击生成报表按钮,过了一会儿刷新网页,就生成右边的图片
本来图片里边的数据应由数据库中提取,这里就简单地写死在代码里边了。
1、Java 后台生成图片
package com.cun.controller;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.jfree.chart.ChartUtilities;
@RestController
@RequestMapping("/student")
public class StudentController {
@RequestMapping("/chart")
public void chartFileName(HttpSession session) throws IOException {
DefaultCategoryDataset dcd = new DefaultCategoryDataset();
dcd.addValue(2, "04008", "Centos");
dcd.addValue(3, "04008", "web");
dcd.addValue(1, "04008", "Android");
JFreeChart chart = ChartFactory.createBarChart("ITAEM member", "ITAEM", "number", dcd, PlotOrientation.VERTICAL,
true, true, true);
FileOutputStream out =new FileOutputStream(new File("C:\\LLLLLLLLLLLLLLLLLLL\\itaem.png"));
ChartUtilities.writeChartAsPNG(out,chart,700,500);
}
}
2、index.html 显示图片
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Excel表格数据导入导出</title>
</head>
<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
<script type="text/javascript">
function upd() {
alert('66')
$.ajax({
type : "POST",
data : "",
url : "http://localhost/student/chart"
});
}
</script>
<body>
<button id="btn" onclick="upd()">生成报表</button>
<!-- 注意这里不要使用绝对路径,只是在eclipse里边浏览器行得通,在 firefox、chrome都显示不出来 -->
<img src="C:/LLLLLLLLLLLLLLLLLLL/itaem.png" width="700" height="500" border="0" />
</body>
</html>
三、小结
看了Java 的 JFreeChart 框架 API文档,用了一下,感觉不咋的。
深切地感受到视觉层的东西应交给前端,Java后台还是注重自己的业务逻辑就好了
因为前端有很多 UI 框架等
如百度的 ECharts
还有国外的 Highcharts、Highstock、Highmaps等
几行 JavaScript 代码,就可以显示的富有动感的数据