1. 程式人生 > >專案中使用ECharts外掛實現統計功能

專案中使用ECharts外掛實現統計功能

一、前端介面

// 介面中定義一個div,放圖表
<div id="box" style="width: 600px;height:400px;padding: 12px;"></div>

// 引入js檔案
<script src="${_b}/themes/${_sysTheme}/echarts/echarts.js"></script>

// 配置ECharts外掛(這個實現的是多條折線圖)
<script language="javascript">
	// 基於準備好的dom,初始化echarts例項
    var echartsWarp= document.getElementById('box');  
    var resizeWorldMapContainer = function () {//用於使chart自適應高度和寬度,通過窗體高寬計算容器高寬  
        echartsWarp.style.width = window.innerWidth-70+'px';  
        echartsWarp.style.height = window.innerHeight-100+'px';  
    };  
    resizeWorldMapContainer ();//設定容器高寬
	var myChart = echarts.init(echartsWarp);
	var option = {
		title: {
        	text:"訪問統計"
        },
        legend: {
            data: ['ip', '訪問次數']
        },
        toolbox: { //視覺化的工具箱
			show: true,
			right: "10%",         
			feature: {
				restore: { //重置
					show: true
				},
				saveAsImage: {//儲存圖片
					show: true
				},
			}
		},
		/* 滑鼠懸浮時顯示資料 */
		 tooltip : {
             trigger: 'axis',
             axisPointer : {            // 座標軸指示器,座標軸觸發有效
                 type : 'shadow'        // 預設為直線,可選為:'line' | 'shadow'
             }
         },
		xAxis: {
            data: [
                <#if spl?exists>
                	<#list spl as count>
                		"${count.date?default('')}",
                	</#list>
                </#if>
            ]
        },
    	yAxis: {},
        series: [
        {
            name: 'ip',
            type: 'line',
            barWidth: '30%',
            data: [
                <#if spl?exists>
                	<#list spl as count>
	                	${count.ip_num?default('')},
                	</#list>
                </#if>
            ]
        },
        {
            name: '訪問次數',
            type: 'line',
            barWidth: '30%',
            data: [
                <#if spl?exists>
                	<#list spl as count>
	                	${count.fw_num?default('')},
                	</#list>
                </#if>
            ]
        }],
	}
	myChart.setOption(option);
</script>  

二、後臺控制器Controller

@SuppressWarnings("null")
@RequestMapping(value="/*/fwtjEcharts",method=RequestMethod.POST)
public String fwtjEcharts(
			@RequestParam(value = "beginDate", required = true) String beginDate,
			@RequestParam(value = "endDate", required = true) String endDate,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) throws SQLException, ParseException {
			List<Map<String, Object>> counts = getCounts(beginDate, endDate,request);
			modelMap.addAttribute("spl", counts);
			modelMap.addAttribute("beginDate", beginDate);
			modelMap.addAttribute("endDate", endDate);
		return "fwtj/fwtjEcharts";
	}