ECharts使用心得
前言
上週項目組要臨時給客戶做一個演示的原型,首頁設計的是一箇中國地圖,本來打算用谷歌的地圖,但是,做出來之後,整體的效果看起來太差了,最後就在網上搜相關的地圖呈現的控件,然後就找到了這個ECharts報表呈現組件,挺不錯的一個組件,而且地圖數據都是離線的,真心很贊。但是,使用起來卻頗費了一番工夫。所以就把使用中的一些心得體會跟大家分享一下。
1. 插件的下載
以下是ECharts的下載鏈接,需要注意的是ECharts內部也是依賴於另一個插件的叫ZRender,當然對於大部分圖表而言不需要ZRender的,但是對於地圖控件及其他複雜的呈現控件而已都是需要ZRender的。爲了避免不要的問題出現,建議大家在下載ECharts時同時也要下載ZRender。
ECharts下載地址: http://echarts.baidu.com/
ZRender下載地址:http://ecomfe.github.io/zrender/index.html
下載之後解壓各自的文件目錄結構如下:
ECharts:
ZRender:
2. 插件的引用
首先,新建一個Web應用程序,然後添加剛剛下載的文件,具體的目錄結構如下:
這裏有以下幾點需要說明:
l 所有的跟ECharts有關的文件全部都在echarts文件夾下
l echarts文件夾的內容分爲兩部分
1) 一部分是以echarts開頭的js文件,這些文件全部來自於1.中的ECharts文件目錄中的js文件夾下的文件,也就是1.中的圖中紅框標註的js下的文件。如下:
2) 另一部分是一個名爲zrender的文件夾,這裏需要特別注意的是該文件夾的命名必須爲zrender,因爲在echarts的js文件中對zrender的引用都是以zrender爲根目錄的,zrender文件夾的內容即爲1.中zrender文件目錄中的src文件夾下的內容,如下:
3. 在頁面中的具體使用
按照上邊的步驟配置過之後,我們就可以在頁面中引用了,這裏我主要是演示地圖控件的使用方式,因爲地圖的引用跟其他的基本圖形的引用不太一樣。其他的圖形的呈現也會做一個簡要的演示。
3.1 MapChart
首先在跟2中的echarts文件夾同一個目錄(也就是Modules文件夾)下添加一個aspx頁或html頁,需要注意的是,如果是在aspx頁中使用echarts時,需要把要渲染的div放在form標籤之外,否則圖形是顯示不出來的。
3.1.1 在head標籤中添加對echarts的引用如下:
- <head runat="server">
- <title></title>
- <script src="echarts/esl.js" type="text/javascript"></script>
- </head>
3.1.2 在body標記中,form標記之外,添加一個div,用來做圖表渲染的容器。如下:
- <body>
- <div id="main"style="height:500px;border:1px solid #ccc;padding:10px;"></div>
- ……………
- ……………
- </body>
3.1.3 在3.1.3中添加的div標記下,添加如下的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'
- }
- });
- //動態加載echarts,在回掉函數中使用,要注意保持按需加載結構定義圖表路徑
- require(
- [
- 'echarts',
- 'echarts/chart/map'
- ],
- function (ec) {
- varmyChart=ec.init(document.getElementById('main'));
- //option主要是圖標的一些設置,這不是這篇文章的重點,關於具體的設置可以參考官方的文檔說明文檔
- option= {
- title: {
- text:'iphone銷¨²量¢?',
- subtext:'純ä?屬º?虛¨¦構1',
- x:'center'
- },
- tooltip: {
- trigger:'item'
- },
- legend: {
- orient:'vertical',
- x:'left',
- data: ['iphone3','iphone4','iphone5']
- },
- dataRange: {
- min:0,
- max:2500,
- text: ['高?','低̨ª'], // 文?本À?,ê?默?認¨?爲a數ºy值¦Ì文?本À?
- 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:'遼¢¨¦寧t',value:Math.round(Math.random() *1000) },
- { name:'黑¨²龍¢¨²江-',value:Math.round(Math.random() *1000) },
- { name:'湖t南?',value:Math.round(Math.random() *1000) },
- { name:'安ã2徽?',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:'湖t北À¡À',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:'吉a林¢?',value:Math.round(Math.random() *1000) },
- { name:'福¡ê建¡§',value:Math.round(Math.random() *1000) },
- { name:'貴¨®州Y',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:'寧t夏?',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:'安ã2徽?',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:'吉a林¢?',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:'寧t夏?',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) }
- ]
- }
- ]
- };
- //以下的這段代碼主要是用來處理用戶的選擇,應用場景是可以做地圖的交互,比如點擊地圖上的某一個省,獲取相應的省的指標變化等。
- //需要特別注意的是,如果需要點擊某一省作地圖的操作交互的話,還需要爲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); //這裏只是簡單的做一個事例說明,彈出用戶所選的省,如需做其他的擴展,可以在這裏邊添加相應的操作
- }
- }
- });
- myChart.setOption(option);
- }
- );
- </script>
3.1.4 完成以上操作之後,效果如下圖所示:
3.2 LineChart
除了地圖圖表之外的其他的圖標的使用方式都差不多。事實上其他的圖表跟地圖圖表的區別在於對配置文件的引用。這裏只做一個折線圖的示例,其它的示例都是一樣的。
3.2.1 跟3.1.1一樣,引入echarts腳本
3.2.2 跟3.2.2一樣,添加渲染圖表的div容器
3.2.3 在3.2.2添加的div下添加如下js代碼塊
- <scripttype="text/javascript">
- require.config({
- paths: {
- echarts:'./echarts/echarts',
- 'echarts/chart/bar':'./echarts/echarts',//這裏需要注意的是除了mapchart使用的配置文件爲echarts-map之外,
- 其他的圖形引用的配置文件都爲echarts,這也是一般的圖形跟地圖的區別
- 'echarts/chart/line':'./echarts/echarts'
- }
- });
- require(
- [
- 'echarts',
- 'echarts/chart/bar',
- 'echarts/chart/line'
- ],
- function (ec) {
- varmyChart=ec.init(document.getElementById('main'));
- option= {
- tooltip: {
- trigger:'axis'
- },
- legend: {
- data: ['郵®¨º件t營®a銷¨²','聯¢a盟?廣?告?','視º¨®頻¦Ì廣?告?','直¡À接¨®訪¤?問¨º','搜?索¡Â引°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: ['周¨¹一°?','周¨¹二t','周¨¹三¨y','周¨¹四?','周¨¹五?','周¨¹六¢¨´','周¨¹日¨?']
- }
- ],
- yAxis: [
- {
- type:'value',
- splitArea: { show:true }
- }
- ],
- series: [
- {
- name:'郵®¨º件t營®a銷¨²',
- type:'line',
- stack:'總Á¨¹量¢?',
- data: [120,132,101,134,90,230,210]
- },
- {
- name:'聯¢a盟?廣?告?',
- 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:'搜?索¡Â引°y擎?',
- 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=ec.init(document.getElementById('main1'));
- option= {
- title: {
- text:'未¡ä來¤¡ä一°?周¨¹氣?溫?變À?化¡¥',
- subtext:'純ä?屬º?虛¨¦構1'
- },
- 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: ['周¨¹一°?','周¨¹二t','周¨¹三¨y','周¨¹四?','周¨¹五?','周¨¹六¢¨´','周¨¹日¨?']
- }
- ],
- yAxis: [
- {
- type:'value',
- axisLabel: {
- formatter:'{value} °?C'
- },
- splitArea: { show:true }
- }
- ],
- series: [
- {
- name:'最Á?高?氣?溫?',
- type:'line',
- itemStyle: {
- normal: {
- lineStyle: {
- shadowColor:'rgba(0,0,0,0.4)'
- }
- }
- },
- data: [11,11,15,13,12,13,10]
- },
- {
- name:'最Á?低̨ª氣?溫?',
- type:'line',
- itemStyle: {
- normal: {
- lineStyle: {
- shadowColor:'rgba(0,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:'某3樓£¤盤¨¬銷¨²售º?情¨¦況?',
- subtext:'純ä?屬º?虛¨¦構1'
- },
- tooltip: {
- trigger:'axis'
- },
- legend: {
- data: ['意°a向¨°','預¡è購o','成¨¦交?']
- },
- toolbox: {
- show:true,
- feature: {
- mark:true,
- dataView: { readOnly:false },
- magicType: ['line','bar'],
- restore:true,
- saveAsImage:true
- }
- },
- calculable:true,
- xAxis: [
- {
- type:'category',
- boundaryGap:false,
- data: ['周¨¹一°?','周¨¹二t','周¨¹三¨y','周¨¹四?','周¨¹五?','周¨¹六¢¨´','周¨¹日¨?']
- }
- ],
- yAxis: [
- {
- type:'value'
- }
- ],
- series: [
- {
- name:'成¨¦交?',
- type:'line',
- smooth:true,
- itemStyle: { normal: { areaStyle: { type:'default'}} },
- data: [10,12,21,54,260,830,710]
- },
- {
- name:'預¡è購o',
- type:'line',
- smooth:true,
- itemStyle: { normal: { areaStyle: { type:'default'}} },
- data: [30,182,434,791,390,30,10]
- },
- {
- name:'意°a向¨°',
- type:'line',
- smooth:true,
- itemStyle: { normal: { areaStyle: { type:'default'}} },
- data: [1320,1132,601,234,120,90,20]
- }
- ]
- };
- myChart.setOption(option);
- }
- );
- </script>
3.2.4 完成以上操作之後效果圖如下:
好了,今天就總結到這裏了,希望能給大家帶來一些幫助。這裏主要是介紹地圖控件的使用,因爲其官方文檔上關於路徑的配置寫的很不清楚,所以纔會想把這些走的彎路給總結一下。對於其他的圖表控件的使用都很簡單,大家可以參考官方的文檔。http://echarts.baidu.com/doc/doc.html