1、配置文件
百度下載“echarts.js”,將其複製到web工程對應的js文件夾下;
2、將echarts文件變成jsp文件(代碼分段解析)
1)加入jsp文件的開頭內容(格式、路徑等)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
2)在網頁的 <head>中引入echarts.js
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ECharts</title>
<!-- 引入 echarts.js -->
方法1:(將js作爲靜態資源引入本地全路徑,路徑必須準確)
舉例: <script src="${pageContext.request.contextPath }/assets/js/echart/echarts.js"></script>
方法2:(引入百度官方路徑)
代碼: <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/echarts-all-3.js"></script>
</head>
3)在網頁的 <body>中爲echarts準備一個Dom
<body>
<!-- 爲ECharts準備一個具備大小(寬高)的Dom -->
<div id="main" style="width: 600px;height:400px;"></div>
4) 基於準備好的dom,初始化echarts實例
<script type="text/javascript">
var myChart = echarts.init(document.getElementById('main'));
// 指定圖表的配置項和數據
var option = {
title: {
text: 'ECharts 入門示例'
},
tooltip: {},
legend: {
data:['銷量']
},
xAxis: {
data: ["襯衫","羊毛衫","雪紡衫","褲子","高跟鞋","襪子"]
},
yAxis: {},
series: [{
name: '銷量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
};
5) 使用剛指定的配置項和數據顯示圖表
myChart.setOption(option);
</script>
</body>
</html>
3、ssm框架下對應jsp文件編寫controller文件
具體詳見本人上一篇博客。
4、結果展示
新增頁面最好關閉eclipse,重新啓動Tomcat,否則有可能一直報404錯誤,顯示找不到頁面。