Echarts是百度的頁面圖表框架,使用其稍作配置就能在web上整出漂亮的圖表。
本篇文章簡單的介紹一下“JSP使用Echarts的最簡單的例子“,例子圖如下
關於echarts詳細資料可以看這里:http://echarts.baidu.com/index.html
圖表顯示是需要數據的,但是其官網教程中為了演示方便直接在頁面js中填入數據,如下面鏈接所示,周一、周二等數據都是在頁面直接寫好:
http://echarts.baidu.com/doc/example/line1.html
xAxis : [
{
type : 'category',
boundaryGap : false,
data : ['周一','周二','周三','周四','周五','周六','周日']
}
]
而我們更經常的要做的操作是從遠程服務器將數據取出,放入圖表。熟悉ajax的人,自然可以將上面的代碼少做修改,但網上的例子太少,我這邊簡單貼一下
頁面端參考代碼:
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <!-- 引入 ECharts 文件 -->
- <script src="js/echarts3/echarts.common.min.js"></script>
- <script type="text/javascript"
- src="${pageContext.request.contextPath}/js/jquery-3.2.1.js"></script>
- </head>
- <body>
- <!-- 為 ECharts 准備一個具備大小(寬高)的 DOM -->
- <div id="main" style="width: 600px; height: 400px;"></div>
- <script type="text/javascript">
- // 基於准備好的dom,初始化echarts實例
- var myChart = echarts.init(document.getElementById('main'));
- var url = '${pageContext.request.contextPath}/GetAllDataServlet';
- $.getJSON(url).done(function(json) {
- // 2.獲取數據
- salesVolume = json.salesVolume;//銷量
- bussinessVolume = json.bussinessVolume;//營業額
- months = json.months;//月份
- // 3.更新圖表myChart的數據
- var option = {
- title : {
- text : 'ECharts 入門示例'
- },
- tooltip : {},
- legend : {
- data : [ '銷量' ],
- data : [ '營業額' ]
- },
- xAxis : {
- data : months
- },
- yAxis : {},
- series : [ {
- name : '銷量',
- type : 'bar',
- data : salesVolume
- }, {
- name : '營業額',
- type : 'line',
- data : bussinessVolume
- } ],
- toolbox : {
- show : true,
- feature : {
- mark : {
- show : true
- },
- dataView : {
- show : true,
- readOnly : false
- },
- magicType : {
- show : true,
- type : [ 'line', 'bar' ]
- },
- restore : {
- show : true
- },
- saveAsImage : {
- show : true
- }
- }
- },
- }
- myChart.setOption(option);
- })
- </script>
- </body>
- </html>
服務器端參考代碼:
- @WebServlet("/GetAllDataServlet")
- public class GetAllDataServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- public GetAllDataServlet() {
- super();
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- /*銷量*/
- Integer[] salesVolume = {10,100,20,56,35,80};
- /*營業額*/
- double[] bussinessVolume = {10*10,100*8.5,20*9.5,56*9,35*9.5,80*9};
- /*橫軸, 月份數據*/
- String[] months = {"1","2","3","4","5","6"};
- Map<String, Object> map = new HashMap<>();
- map.put("salesVolume", salesVolume);
- map.put("bussinessVolume",bussinessVolume);
- map.put("months", months);
- response.getWriter().println(JSON.toJSONString(map));
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doGet(request, response);
- }
- }
使用了FastJson生成json字符串,格式形如:
- {"bussinessVolume":[100.0,850.0,190.0,504.0,332.5,720.0],"months":["1","2","3","4","5","6"],"salesVolume":[10,100,20,56,35,80]}
其他:
實際上你也可以不用json工具,完全自己手寫得到上述格式化字符串。
項目參考代碼:
有一個EclipseJEE的參考代碼似乎傳不上來,只好傳到資源哪里了:
http://download.csdn.net/detail/zhrubin/9837529
項目相關Git地址:
https://git.coding.net/zhrb/echartsdemo.git
轉載自https://blog.csdn.net/zhrubin/article/details/46123771