echart簡介_動力節(jié)點(diǎn)Java學(xué)院整理
1. 插件的下載
以下是ECharts的下載鏈接,需要注意的是ECharts內(nèi)部也是依賴于另一個插件的叫ZRender,當(dāng)然對于大部分圖表而言不需要ZRender的,但是對于地圖控件及其他復(fù)雜的呈現(xiàn)控件而已都是需要ZRender的。為了避免不要的問題出現(xiàn),建議大家在下載ECharts時同時也要下載ZRender。
ECharts下載地址: http://echarts.baidu.com/
ZRender下載地址:http://ecomfe.github.io/zrender/index.html
下載之后解壓各自的文件目錄結(jié)構(gòu)如下:
ECharts:
ZRender:

2. 插件的引用
首先,新建一個Web應(yīng)用程序,然后添加剛剛下載的文件,具體的目錄結(jié)構(gòu)如下:

這里有以下幾點(diǎn)需要說明:
- 所有的跟ECharts有關(guān)的文件全部都在echarts文件夾下
- echarts文件夾的內(nèi)容分為兩部分
1) 一部分是以echarts開頭的js文件,這些文件全部來自于1.中的ECharts文件目錄中的js文件夾下的文件,也就是1.中的圖中紅框標(biāo)注的js下的文件。如下:

2)另一部分是一個名為zrender的文件夾,這里需要特別注意的是該文件夾的命名必須為zrender,因為在echarts的js文件中對zrender的引用都是以zrender為根目錄的,zrender文件夾的內(nèi)容即為1.中zrender文件目錄中的src文件夾下的內(nèi)容,如下:

3. 在頁面中的具體使用
按照上邊的步驟配置過之后,我們就可以在頁面中引用了,這里我主要是演示地圖控件的使用方式,因為地圖的引用跟其他的基本圖形的引用不太一樣。其他的圖形的呈現(xiàn)也會做一個簡要的演示。
MapChart
首先在跟2中的echarts文件夾同一個目錄(也就是Modules文件夾)下添加一個aspx頁或html頁,需要注意的是,如果是在aspx頁中使用echarts時,需要把要渲染的div放在form標(biāo)簽之外,否則圖形是顯示不出來的。
在head標(biāo)簽中添加對echarts的引用如下:
<head runat="server"> <title></title> <script src="echarts/esl.js" type="text/javascript"></script> </head>
在body標(biāo)記中,form標(biāo)記之外,添加一個div,用來做圖表渲染的容器。如下:
<body> <div id="main"style="height:500px;border:1px solid #ccc;padding:10px;"></div> …………… …………… </body>
在上面中添加的div標(biāo)記下,添加如下的js代碼段,如下:
<script type="text/javascript">
//為模塊加載器配置echarts的路徑,這里主要是配置map圖表的路徑,其他的圖表跟map的配置還不太一樣,下邊也會做另一種類型的圖表事例。
這里引用的主要是echarts文件夾下的echarts-map文件,而其他類型的圖表引用的都是echarts文件夾下的echarts文件。
require.config({
paths: {
echarts:'./echarts/echarts',
'echarts/chart/map':'./echarts/echarts-map'
}
});
//動態(tài)加載echarts,在回掉函數(shù)中使用,要注意保持按需加載結(jié)構(gòu)定義圖表路徑
require(
[
'echarts',
'echarts/chart/map'
],
function (ec) {
varmyChart=ecinit(documentgetElementById('main'));
//option主要是圖標(biāo)的一些設(shè)置,這不是這篇文章的重點(diǎn),關(guān)于具體的設(shè)置可以參考官方的文檔說明文檔
option= {
title: {
text:'iphone銷量',
subtext:'純屬虛構(gòu)',
x:'center'
},
tooltip: {
trigger:'item'
},
legend: {
orient:'vertical',
x:'left',
data: ['iphone3','iphone4','iphone5']
},
dataRange: {
min:0,
max:2500,
text: ['高','低'],
calculable:true,
textStyle: {
color:'orange'
}
},
toolbox: {
show:true,
orient:'vertical',
x:'right',
y:'center',
feature: {
mark:true,
dataView: { readOnly:false },
restore:true,
saveAsImage:true
}
},
series: [
{
name:'iphone3',
type:'map',
mapType:'china',
selectedMode: 'single',
itemStyle: {
normal: { label: { show:true },color:'#ffd700' },// for legend
emphasis: { label: { show:true} }
},
data: [
{ name:'北京',value:Math.round(Math.random() *1000) },
{ name:'天津',value:Math.round(Math.random() *1000) },
{ name:'上海',value:Math.round(Math.random() *1000) },
{ name:'重慶',value:Math.round(Math.random() *1000) },
{ name:'河北',value:Math.round(Math.random() *1000) },
{ name:'河南',value:Math.round(Math.random() *1000) },
{ name:'云南',value:Math.round(Math.random() *1000) },
{ name:'遼寧',value:Math.round(Math.random() *1000) },
{ name:'黑龍江',value:Math.round(Math.random() *1000) },
{ name:'湖南',value:Math.round(Math.random() *1000) },
{ name:'安徽',value:Math.round(Math.random() *1000) },
{ name:'山東',value:Math.round(Math.random() *1000) },
{ name:'新疆',value:Math.round(Math.random() *1000) },
{ name:'江蘇',value:Math.round(Math.random() *1000) },
{ name:'浙江',value:Math.round(Math.random() *1000) },
{ name:'江西',value:Math.round(Math.random() *1000) },
{ name:'湖北',value:Math.round(Math.random() *1000) },
{ name:'廣西',value:Math.round(Math.random() *1000) },
{ name:'甘肅',value:Math.round(Math.random() *1000) },
{ name:'山西',value:Math.round(Math.random() *1000) },
{ name:'內(nèi)蒙古',value:Math.round(Math.random() *1000) },
{ name:'陜西',value:Math.round(Math.random() *1000) },
{ name:'吉林',value:Math.round(Math.random() *1000) },
{ name:'福建',value:Math.round(Math.random() *1000) },
{ name:'貴州',value:Math.round(Math.random() *1000) },
{ name:'廣東',value:Math.round(Math.random() *1000) },
{ name:'青海',value:Math.round(Math.random() *1000) },
{ name:'西藏',value:Math.round(Math.random() *1000) },
{ name:'四川',value:Math.round(Math.random() *1000) },
{ name:'寧夏',value:Math.round(Math.random() *1000) },
{ name:'海南',value:Math.round(Math.random() *1000) },
{ name:'臺灣',value:Math.round(Math.random() *1000) },
{ name:'香港',value:Math.round(Math.random() *1000) },
{ name:'澳門',value:Math.round(Math.random() *1000) }
]
},
{
name:'iphone4',
type:'map',
mapType:'china',
selectedMode: 'single',
itemStyle: {
normal: { label: { show:true },color:'#ff8c00' },// for legend
emphasis: { label: { show:true} }
},
data: [
{ name:'北京',value:Math.round(Math.random() *1000) },
{ name:'天津',value:Math.round(Math.random() *1000) },
{ name:'上海',value:Math.round(Math.random() *1000) },
{ name:'重慶',value:Math.round(Math.random() *1000) },
{ name:'河北',value:Math.round(Math.random() *1000) },
{ name:'安徽',value:Math.round(Math.random() *1000) },
{ name:'新疆',value:Math.round(Math.random() *1000) },
{ name:'浙江',value:Math.round(Math.random() *1000) },
{ name:'江西',value:Math.round(Math.random() *1000) },
{ name:'山西',value:Math.round(Math.random() *1000) },
{ name:'內(nèi)蒙古',value:Math.round(Math.random() *1000) },
{ name:'吉林',value:Math.round(Math.random() *1000) },
{ name:'福建',value:Math.round(Math.random() *1000) },
{ name:'廣東',value:Math.round(Math.random() *1000) },
{ name:'西藏',value:Math.round(Math.random() *1000) },
{ name:'四川',value:Math.round(Math.random() *1000) },
{ name:'寧夏',value:Math.round(Math.random() *1000) },
{ name:'香港',value:Math.round(Math.random() *1000) },
{ name:'澳門',value:Math.round(Math.random() *1000) }
]
},
{
name:'iphone5',
type:'map',
mapType:'china',
selectedMode: 'single',
itemStyle: {
normal: { label: { show:true },color:'#da70d6' },// for legend
emphasis: { label: { show:true} }
},
data: [
{ name:'北京',value:Math.round(Math.random() *1000) },
{ name:'天津',value:Math.round(Math.random() *1000) },
{ name:'上海',value:Math.round(Math.random() *1000) },
{ name:'廣東',value:Math.round(Math.random() *1000) },
{ name:'臺灣',value:Math.round(Math.random() *1000) },
{ name:'香港',value:Math.round(Math.random() *1000) },
{ name:'澳門',value:Math.round(Math.random() *1000) }
]
}
]
};
//以下的這段代碼主要是用來處理用戶的選擇,應(yīng)用場景是可以做地圖的交互,比如點(diǎn)擊地圖上的某一個省,獲取相應(yīng)的省的指標(biāo)變化等。
//需要特別注意的是,如果需要點(diǎn)擊某一省作地圖的操作交互的話,還需要為series屬性的每一項添加一個selectedMode屬性,這里的屬性值為 'single'即單選,也可多選
varecConfig= require('echarts/config');
myChart.on(ecConfig.EVENT.MAP_SELECTED,function (param) {
varselected=param.selected;
varmapSeries=option.series[0];
vardata= [];
varlegendData= [];
varname;
for (varp=0,len=mapSeries.data.length; p<len; p++) {
name=mapSeries.data[p].name;
mapSeries.data[p].selected=selected[name];
if (selected[name]) {
alert(name); //這里只是簡單的做一個事例說明,彈出用戶所選的省,如需做其他的擴(kuò)展,可以在這里邊添加相應(yīng)的操作
}
}
});
myChart.setOption(option);
}
);
</script>
完成以上操作之后,效果如下圖所示:
LineChart
除了地圖圖表之外的其他的圖標(biāo)的使用方式都差不多。事實(shí)上其他的圖表跟地圖圖表的區(qū)別在于對配置文件的引用。這里只做一個折線圖的示例,其它的示例都是一樣的。
<scripttype="text/javascript">
require.config({
paths: {
echarts:'./echarts/echarts',
'echarts/chart/bar':'./echarts/echarts',//這里需要注意的是除了mapchart使用的配置文件為echarts-map之外,
其他的圖形引用的配置文件都為echarts,這也是一般的圖形跟地圖的區(qū)別
'echarts/chart/line':'./echarts/echarts'
}
});
require(
[
'echarts',
'echarts/chart/bar',
'echarts/chart/line'
],
function (ec) {
varmyChart=ecinit(documentgetElementById('main'));
option= {
tooltip: {
trigger:'axis'
},
legend: {
data: ['郵件營銷','聯(lián)盟廣告','視頻廣告','直接問','搜索引擎']
},
toolbox: {
show:true,
feature: {
mark:true,
dataView: { readOnly:false },
magicType: ['line','bar'],
restore:true,
saveAsImage:true
}
},
calculable:true,
xAxis: [
{
type:'category',
boundaryGap:false,
data: ['周一','周二','周三','周四','周五','周六','周日']
}
],
yAxis: [
{
type:'value',
splitArea: { show:true }
}
],
series: [
{
name:'郵件營銷',
type:'line',
stack:'總量',
data: [120,132,101,134,90,230,210]
},
{
name:'聯(lián)盟廣告',
type:'line',
stack:'總量',
data: [220,182,191,234,290,330,310]
},
{
name:'視頻廣告',
type:'line',
stack:'總量',
data: [150,232,201,154,190,330,410]
},
{
name:'直接訪問',
type:'line',
stack:'總量',
data: [320,332,301,334,390,330,320]
},
{
name:'搜索引擎',
type:'line',
stack:'總量',
data: [820,932,901,934,1290,1330,1320]
}
]
};
myChart.setOption(option);
}
);
</script>
<divid="main1"style="height:500px;border:1px solid #ccc;padding:10px;"></div>
<scripttype="text/javascript">
require.config({
paths: {
echarts:'./echarts/echarts',
'echarts/chart/bar':'./echarts/echarts',
'echarts/chart/line':'./echarts/echarts'
}
});
require(
[
'echarts',
'echarts/chart/bar',
'echarts/chart/line'
],
function (ec) {
varmyChart=ecinit(documentgetElementById('main1'));
option= {
title: {
text:'未來一周氣溫變化',
subtext:'純屬虛構(gòu)'
},
tooltip: {
trigger:'axis'
},
legend: {
data: ['最高氣溫'最低氣溫']
},
toolbox: {
show:true,
feature: {
mark:true,
dataView: { readOnly:false },
magicType: ['line','bar'],
restore:true,
saveAsImage:true
}
},
calculable:true,
xAxis: [
{
type:'category',
boundaryGap:false,
data: ['周一','周二','周三','周四','周五','周六','周日']
}
],
yAxis: [
{
type:'value',
axisLabel: {
formatter:'{value} '
},
splitArea: { show:true }
}
],
series: [
{
name:'最高氣溫',
type:'line',
itemStyle: {
normal: {
lineStyle: {
shadowColor:'rgba(0,0,0,4)'
}
}
},
data: [11,11,15,13,12,13,10]
},
{
name:'最低氣溫',
type:'line',
itemStyle: {
normal: {
lineStyle: {
shadowColor:'rgba(0,0,0,4)'
}
}
},
data: [-2,1,2,5,3,2,0]
}
]
};
myChart.setOption(option);
}
);
</script>
<divid="main2"style="height:500px;border:1px solid #ccc;padding:10px;"></div>
<scripttype="text/javascript">
require.config({
paths: {
echarts:'./echarts/echarts',
'echarts/chart/bar':'./echarts/echarts',
'echarts/chart/line':'./echarts/echarts'
}
});
require(
[
'echarts',
'echarts/chart/bar',
'echarts/chart/line'
],
function (ec) {
varmyChart=ec.init(document.getElementById('main2'));
option= {
title: {
text:'某樓盤銷售情況',
subtext:'純屬虛構(gòu)'
},
tooltip: {
trigger:'axis'
},
legend: {
data: ['意向','預(yù)購','成交']
},
toolbox: {
show:true,
feature: {
mark:true,
dataView: { readOnly:false },
magicType: ['line','bar'],
restore:true,
saveAsImage:true
}
},
calculable:true,
xAxis: [
{
type:'category',
boundaryGap:false,
data: ['周','周二','周三','周四','周五','周六','周日']
}
],
yAxis: [
{
type:'value'
}
],
series: [
{
name:'成交',
type:'line',
smooth:true,
itemStyle: { normal: { areaStyle: { type:'default'}} },
data: [10,12,21,54,260,830,710]
},
{
name:'預(yù)購',
type:'line',
smooth:true,
itemStyle: { normal: { areaStyle: { type:'default'}} },
data: [30,182,434,791,390,30,10]
},
{
name:'意向',
type:'line',
smooth:true,
itemStyle: { normal: { areaStyle: { type:'default'}} },
data: [1320,1132,601,234,120,90,20]
}
]
};
myChart.setOption(option);
}
);
</script>
完成以上操作之后效果圖如下:

相關(guān)文章
setTimeout與setInterval在不同瀏覽器下的差異
setTimeout與setInterval是window對象的兩個非常神奇方法,用于實(shí)現(xiàn)定時或延時調(diào)用一個函數(shù)或一段代碼2010-01-01
fckeditor 獲取文本框值的實(shí)現(xiàn)代碼
獲取文本框值的實(shí)現(xiàn)代碼2009-02-02
JS函數(shù)節(jié)流和防抖之間的區(qū)分和實(shí)現(xiàn)詳解
本文主要介紹的是關(guān)于JS中比較常用的函數(shù):節(jié)流函數(shù)和防抖函數(shù),從概念、使用場景到代碼簡單實(shí)現(xiàn)做了一個詳細(xì)的區(qū)分。感興趣的小伙伴們可以參考一下2019-01-01
BootStrap 圖標(biāo)icon符號圖標(biāo)glyphicons不正常顯示的快速解決辦法
這篇文章主要介紹了BootStrap 圖標(biāo)icon符號圖標(biāo)glyphicons不正常顯示的快速解決辦法,需要的朋友可以參考下2016-12-12
總結(jié)Javascript中的隱式類型轉(zhuǎn)換
這篇文章談?wù)凧avaScript的隱式類型轉(zhuǎn)換,我們知道在JavaScript中聲明變量不需指定類型, 對變量賦值也沒有類型檢查,同時JavaScript允許隱式類型轉(zhuǎn)換。這些特征說明JavaScript屬于弱類型的語言。2016-08-08

