一、介紹
關於echarts的介紹及測試環境用法請看:echarts圖表的用法。下面介紹的是echarts正式環境的用法,在正式環境中需要從後臺數據庫讀取數據並轉化爲echarts各種圖表的option中需要的數據格式,把轉化後的數據放到option中相應的屬性上才能實現真實的效果。由於根據echarts官網解釋echarts圖表的option是配置數據的萬能接口。所以在使用的時候注意到圖表變化是通過更新option中的數據並重新設置option來實現的,所以把option和數據格式化抽取到一個文件中,把後臺讀取的數據格式化並放到option中相應的位置上即可。
二、使用
下面將個人在開發項目實現各個echarts圖表的一般流程記錄下來,以備以後可以使用。
該項目使用的是spring、springmvc、mybatis框架,前端使用jquery,echarts圖表接入真實數據一般需要根據url調用後臺查詢數據庫中的數據。
新建一個web工程ssmec
準備:配置ssm框架,配置echarts相關文件及jquery文件請看:echarts圖表的使用,由於在《echarts圖表的使用》中描述了基本的用法,所以這裏我將《echarts圖表的使用》創建的testec中的ui複製過來並將不需要的刪除,把testec中的ecjsp中的文件複製到ssmec中的WEB-INF/jsp下並把不需要的文件刪掉。
0、控制層文件名:TestEc,文件頭註解@RequestMapping("tec"),構造數據在這個類EcDtUtils中實現;圖表option和數據格式化方法放到cfgopts.js中。
1、實現柱狀圖Bar
(1)、寫後臺action方法及返回json數據的方法
action方法:
- @RequestMapping("/toBar")
- public ModelAndView toBar(HttpServletRequest request) {
- return new ModelAndView("ecjsp/testBar");
- }
返回json數據方法:
- @RequestMapping("/barData")
- @ResponseBody
- public List<HashMap<String, Object>> barData(HttpServletRequest request) {
- /*
- 測試數據,正式環境從數據庫讀取
- */
- List<HashMap<String, Object>> bList = EcDtUtils.getBarRndVal();
- return bList;
- }
說明:這裏只是模擬從後臺讀取數據,在正式使用的時候是從數據庫讀取的,我這裏在一個類EcDtUtils中寫一個方法構造數據,具體實現看源代碼。
(2)、在cfgopts.js中寫數據格式化方法:
- /**
- * 格式化data,data格式如:[{name:nameValue,value:valueVal},...]
- *
- * @param data
- * @returns {object}
- */
- formtBarData: function (data) {
- var xAxData = [];
- var serData = [];
-
- for (var i = 0; i < data.length; i++) {
- xAxData.push(data[i].name || "");
- serData.push({
- name: data[i].name,
- value: data[i].value || 0
- });
- }
-
- return {
- xAxData: xAxData,
- serData: serData
- };
- }
(3)、在testBar文件中調用後臺,格式化數據,設置option:
- function showBar() {
- $.ajax({
- type: "GET",
- url: "${ctx}/tec/barData.html",
- dataType: 'json',
- success: function (data) {
- var barOpt = getBarOpt();
- barOpt.title.text = '柱形圖';
- barOpt.xAxis[0].name = '日期';
- barOpt.yAxis[0].name = '數量';
- barOpt.series[0].name = '柱形圖';
- var fData = fmt.formtBarData(data);
- barOpt.xAxis[0].data = fData.xAxData;
- barOpt.series[0].data = fData.serData;
- barChart.setOption(barOpt);
- }
- });
- }
說明:getBarOpt()方法用於獲取bar的option,具體請在cfgopts.js中查看
運行ssmec,在瀏覽器地址輸入:http://localhost:8080/ssmec/tec/toBar.html
運行效果:
testBar.jsp文件代碼:
- <%@ page language="java" contentType="text/html; charset=utf-8" %>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
- %>
- <c:set var="ctx" value="${pageContext.request.contextPath}"></c:set>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
-
- <title>My JSP 'map.jsp' starting page</title>
-
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- <script type="text/javascript" src="${ctx }/ui/jquery-1.7.2.min.js"></script>
- <script type="text/javascript" src="${ctx }/ui/echarts/echarts-3.2.3/dist/echarts.js"></script>
- <script type="text/javascript" src="${ctx }/ui/echarts/echarts-3.2.3/options/cfgopts.js"></script>
- </head>
-
- <body>
- <style>
- html, body, #main {
- width: 100%;
- height: 100%;
- margin: 0;
- background-color: #00CC33;
- }
- </style>
- <div id="bar" style="width: 600px;height: 400px;"></div>
- <script>
- var barChart;
- $(function () {
- barChart = echarts.init(document.getElementById("bar"));
-
- showBar();
- });
-
- function showBar() {
- $.ajax({
- type: "GET",
- url: "${ctx}/tec/barData.html",
- dataType: 'json',
- success: function (data) {
- var barOpt = getBarOpt();
- barOpt.title.text = '柱形圖';
- barOpt.xAxis[0].name = '日期';
- barOpt.yAxis[0].name = '數量';
- barOpt.series[0].name = '柱形圖';
- var fData = fmt.formtBarData(data);
- barOpt.xAxis[0].data = fData.xAxData;
- barOpt.series[0].data = fData.serData;
- barChart.setOption(barOpt);
- }
- });
- }
- </script>
- </body>
- </html>
2、實現折線圖Line
(1)、寫後臺action方法及返回json數據的方法
action方法:
- @RequestMapping("/toLine")
- public ModelAndView toLine(HttpServletRequest request) {
- return new ModelAndView("ecjsp/testLine");
- }
返回json數據方法:
- @RequestMapping("/lineData")
- @ResponseBody
- public List<HashMap<String, Object>> lineData(HttpServletRequest request) {
- /*
- 測試數據,正式環境從數據庫讀取
- */
- List<HashMap<String, Object>> lList = EcDtUtils.getLineRndVal();
- return lList;
- }
說明:這裏只是模擬從後臺讀取數據,在正式使用的時候是從數據庫讀取的,我這裏在一個類EcDtUtils中寫一個方法構造數據,具體實現看源代碼。
(2)、在cfgopts.js中寫數據格式化方法:
- /**
- * 格式化data,data格式如:[{group:groupVal,name:nameValue,value:valueVal,...]
- *
- * @param data
- * @returns {object}
- */
- formtLineData: function (data) {
- var xAxis = [];
- var group = [];
- var series = [];
- var type = 'line';
-
- for (var i = 0; i < data.length; i++) {
- for (var j = 0; j < xAxis.length && xAxis[j] != data[i].name; j++);
- if (j == xAxis.length) {
- xAxis.push(data[i].name);
- }
- for (var k = 0; k < group.length && group[k] != data[i].group; k++);
- if (k == group.length) {
- group.push(data[i].group);
- }
- }
-
- for (var i = 0; i < group.length; i++) {
- var tData = [];
- var valMap = {};
-
- for (var j = 0; j < data.length; j++) {
- if (group[i] == data[j].group) {
- valMap[data[j].name] = data[j].value;
- }
- }
-
- for (var n = 0; n < xAxis.length; n++) {
- tData.push(valMap[xAxis[n]]);
- }
-
- var tSeries = {
- name: group[i],
- data: tData,
- type: type
- };
-
- series.push(tSeries);
- }
-
- return {
- category: group,
- xAxis: xAxis,
- series: series
- };
- }
(3)、在testLine文件中調用後臺,格式化數據,設置option:
- function showLine() {
- $.ajax({
- type: "GET",
- url: "${ctx}/tec/lineData.html",
- dataType: 'json',
- success: function (data) {
- var lineOpt = getLineOpt();
- lineOpt.title.text = '折線圖';
- var fData = fmt.formtLineData(data);
- lineOpt.xAxis[0].data = fData.xAxis;
- lineOpt.series = fData.series;
- lineChart.setOption(lineOpt);
- }
- });
- }
說明:getLineOpt()方法用於獲取line的option,具體請在cfgopts.js中查看
運行ssmec,在瀏覽器地址輸入:http://localhost:8080/ssmec/tec/toLine.html
運行效果:
testLine.jsp文件代碼:
- <%@ page language="java" contentType="text/html; charset=utf-8" %>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
- %>
- <c:set var="ctx" value="${pageContext.request.contextPath}"></c:set>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
-
- <title>My JSP 'map.jsp' starting page</title>
-
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- <script src="${ctx }/ui/jquery-1.7.2.min.js"></script>
- <script src="${ctx }/ui/echarts/echarts-3.2.3/dist/echarts.js"></script>
- <script src="${ctx }/ui/echarts/echarts-3.2.3/options/cfgopts.js"></script>
- </head>
-
- <body>
- <style>
- html, body, #main {
- width: 100%;
- height: 100%;
- margin: 0;
- background-color: #00CC33;
- }
- </style>
- <div id="line" style="width: 600px;height: 400px;"></div>
- <script>
- var lineChart;
- $(function () {
- lineChart = echarts.init(document.getElementById("line"));
-
- showLine();
- });
-
- function showLine() {
- $.ajax({
- type: "GET",
- url: "${ctx}/tec/lineData.html",
- dataType: 'json',
- success: function (data) {
- var lineOpt = getLineOpt();
- lineOpt.title.text = '折線圖';
- var fData = fmt.formtLineData(data);
- lineOpt.xAxis[0].data = fData.xAxis;
- lineOpt.series = fData.series;
- lineChart.setOption(lineOpt);
- }
- });
- }
- </script>
- </body>
- </html>
3、實現雷達圖Radar
(1)、寫後臺action方法及返回json數據的方法
action方法:
- @RequestMapping("/toRadar")
- public ModelAndView toRadar(HttpServletRequest request) {
- return new ModelAndView("ecjsp/testRadar");
- }
返回json數據方法:
- @RequestMapping("/radarData")
- @ResponseBody
- public List<HashMap<String, Object>> radarData(HttpServletRequest request) {
- /*
- 測試數據,正式環境從數據庫讀取
- */
- List<HashMap<String, Object>> rList = EcDtUtils.getRadarRndVal();
- return rList;
- }
說明:這裏只是模擬從後臺讀取數據,在正式使用的時候是從數據庫讀取的,我這裏在一個類EcDtUtils中寫一個方法構造數據,具體實現看源代碼。
(2)、在cfgopts.js中寫數據格式化方法:
- /**
- * 格式化雷達data,data格式如:[{group:groupVal,name:nameValue,value:valueVal},...]
- * @param data
- * @param type
- * @returns {Object}
- */
- formtRadarData: function (data) {
- var indicator = [];
- var group = [];
- var serDats = [];
- var mVal = 100;
- for (var i = 0, len = data.length; i < len; i++) {
- if (data[i].value > mVal) {
- mVal = data[i].value;
- }
- }
- for (var i = 0; i < data.length; i++) {
- for (var j = 0; j < indicator.length && indicator[j].text != data[i].name; j++);
- if (j == indicator.length) {
- indicator.push({
- max: mVal + 10,
- text: data[i].name
- });
- }
- for (var k = 0; k < group.length && group[k] != data[i].group; k++);
- if (k == group.length) {
- group.push(data[i].group);
- }
- }
-
- for (var i = 0; i < group.length; i++) {
- var tData = [];
- var valMap = {};
-
- for (var j = 0; j < data.length; j++) {
- if (group[i] == data[j].group) {
- valMap[data[j].name] = data[j].value;
- }
- }
-
- for (var n = 0; n < indicator.length; n++) {
- tData.push(valMap[indicator[n].text]);
- }
-
- var tSeries = {
- name: group[i],
- value: tData
- };
- serDats.push(tSeries);
- }
- return {
- category: group,
- indicator: indicator,
- serData: serDats
- };
- }
(3)、在testRadar文件中調用後臺,格式化數據,設置option:
- function showRadar() {
- $.ajax({
- type: "POST",
- url: "${ctx}/tec/radarData.html",
- dataType: 'json',
- success: function (data) {
- var fData = fmt.formtRadarData(data);
- var radarOpt = getRadarOpt(fData);
- radarOpt.title.text = '雷達圖';
- radarOpt.series[0].name = '雷達圖';
- radarChart.setOption(radarOpt);
- }
- });
- }
說明:getRadarOpt(data)方法用於獲取radar的option,具體請在cfgopts.js中查看
運行ssmec,在瀏覽器地址輸入:http://localhost:8080/ssmec/tec/toRadar.html
運行效果:
testRadar.jsp文件代碼:
- <%@ page language="java" contentType="text/html; charset=utf-8" %>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
- %>
- <c:set var="ctx" value="${pageContext.request.contextPath}"></c:set>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
-
- <title>My JSP 'map.jsp' starting page</title>
-
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
-
- <title>My JSP 'map.jsp' starting page</title>
-
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content