Highcharts圖形報表工具插件的使用方法

Highcharts簡介:

Highcharts 是一個用純JavaScript編寫的一個圖表庫, 能夠很簡單便捷的在web網站或是web應用程序添加有交互性的圖表,並且免費提供給個人學習、個人網站和非商業用途使

用。HighCharts支持的圖表類型有曲線圖、區域圖、柱狀圖、餅狀圖、散狀點圖和綜合圖表。

基於jQuery開發一個圖形報表工具插件

官網:http://www.hcharts.cn/

入門案例:

第一步:將Highcharts相關資源文件複製到項目中

第二步:在頁面中引入相關js文件

<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
<script src="${pageContext.request.contextPath }/js/highcharts/highcharts.js"></script>
<script src="${pageContext.request.contextPath }/js/highcharts/modules/exporting.js"></script>

第三步:在頁面中提供一個div,並指定id屬性

第四步:調用Highcharts提供的方法,動態創建圖表

餅狀圖:

<script type="text/javascript">
	$(function(){
		//頁面加載完成後,動態創建圖表
		$("#test").highcharts({
			title: {
	            text: '各瀏覽器份額'
	        },
	        series: [{
	            type: 'pie',
	            name: '瀏覽器佔比',
	            data: [
	                ['Firefox',   45.0],
	                ['IE',       26.8],
	                ['Safari',    8.5],
	                ['Opera',     6.2],
	                ['Others',   0.7]
	            ]
	        }]
		});
	});
</script>
柱狀圖:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>highcharts</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
<script src="${pageContext.request.contextPath }/js/highcharts/highcharts.js"></script>
<script src="${pageContext.request.contextPath }/js/highcharts/modules/exporting.js"></script>
<script type="text/javascript">
	$(function(){
		$('#test').highcharts({
	        title: {
	            text: '水果銷量'
	        },
	        subtitle:{text:'官方統計'},
	        xAxis: {
	            categories: ['蘋果', ' 橙', '梨', '香蕉', '李']
	        },
	        series: [{
	            type: 'column',
	            name: '小明',
	            data: [3, 2, 1, 3, 4]
	        }, {
	            type: 'column',
	            name: '小紅',
	            data: [2, 3, 5, 7, 6]
	        }, {
	            type: 'column',
	            name: '小白',
	            data: [4, 3, 3, 9, 0]
	        }]
	    });
	});
</script>
</head>
<body>
	<div id="test"></div>
</body>
</html>

上面介紹的都是最基礎的屬性。

項目中使用:發送ajax請求查詢數據庫,動態展示到頁面:

基於餅狀圖:業務流程,點擊按鈕,顯示隱藏域,發送ajax請求data數據,展示到頁面

我們先看一下需要展示數據的格式

第一步:在subarea.jsp頁面中引入Highcharts資源文件

第二步:在jsp頁面中提供按鈕,並提供div窗口,在這個窗口中展示圖表

	<!-- 用於展示圖表 -->
	<div class="easyui-window" title="區域分區分佈圖" id="showSubareaWindow" 
		collapsible="false" minimizable="false" maximizable="false" style="top:20px;left:200px">
		<div id="test"  split="false" border="false" >
		</div>
	</div>

定義窗口屬性:

		$('#showSubareaWindow').window({
	        width: 800,
	        modal: true,
	        shadow: true,
	        closed: true,
	        height: 700,
	        resizable:false
	    });

第三步:定義function點擊事件

function doShowHighcharts(){
		$("#showSubareaWindow").window("open");
		//頁面加載完成後,動態創建圖表
		$.post("subareaAction_findAllByCount.action",function(data){
			$("#test").highcharts({
				title: {
		            text: '區域分區分佈圖'
		        },
		        series: [{
		            type: 'pie',
		            name: '區域分區分佈圖',
		            data: data
		        }]
			});
		});
	}
下面是後端返查詢數據,返回給前端(地區名稱),(統計數量)

第四步:在服務端Action中提供方法

	/**
	 * 查詢區域分區分佈圖數據
	 */
	public String findAllByCount(){
		List<Object> list = subareaService.findAllByCount();
		this.java2Json(list, new String[]{});
		return NONE;
	}

Dao代碼:

@Repository
public class SubareaDaoImpl extends BaseDaoImpl<Subarea> implements ISubareaDao {
	public List<Object> findSubareasGroupByProvince() {
		String hql = "SELECT r.province ,count(*) FROM Subarea s LEFT OUTER JOIN s.region r Group BY r.province";
		return (List<Object>) this.getHibernateTemplate().find(hql);
	}
}







































發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章