慕課網(wǎng) Echarts3.0入門基礎(chǔ)與實(shí)戰(zhàn) 授課:蝸牛老濕
http://www.imooc.com/view/687
一.瀏覽器畫圖原理簡介
1.Canvas
基于像素,單個(gè)html,類似于畫筆在畫布上畫畫窝革,詳情可見html5中的canvas介紹。Echarts基于canvas畫圖。
以下是一個(gè)canvas的簡單例子,html中
<!-- 添加canvas標(biāo)簽涌乳,并加上紅色邊框以便于在頁面上查看 -->
<canvas id="myCanvas" width="400px" height="300px">
您的瀏覽器不支持canvas標(biāo)簽。
</canvas>
js中
//獲取Canvas對(duì)象(畫布)
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
//開始一個(gè)新的繪制路徑
ctx.beginPath();
//設(shè)置弧線的顏色為藍(lán)色
ctx.strokeStyle = "blue";
var circle = {
x : 100, //圓心的x軸坐標(biāo)值
y : 100, //圓心的y軸坐標(biāo)值
r : 50 //圓的半徑
};
//沿著坐標(biāo)點(diǎn)(100,100)為圓心甜癞、半徑為50px的圓的順時(shí)針方向繪制弧線
ctx.arc(circle.x, circle.y, circle.r, 0, Math.PI * 2, false);
//按照指定的路徑繪制弧線
ctx.stroke();
運(yùn)行結(jié)果:
2.Svg
基于對(duì)象模型夕晓,多個(gè)圖形元素,高保真
svg例子
<svg width="100%" height="100%" version="1.1"
xmlns="http://www.w3.org/2000/svg">
<!-- circle是圓形標(biāo)簽 -->
<circle cx="70" cy="50" r="40" stroke="black"
stroke-width="2" fill="red"/>
<!-- rect是長方形標(biāo)簽 -->
<rect x='120' width="100" height="100"
style="fill:rgb(0,0,255);stroke-width:1;
stroke:rgb(0,0,0)"/>
</svg>
運(yùn)行結(jié)果:
常見圖形組件
- 標(biāo)題悠咱、工具欄蒸辆、圖例、提示框
- 坐標(biāo)軸:x軸析既、y軸
echart官網(wǎng)上主要看看它的配置項(xiàng)手冊(cè)躬贡,它對(duì)每一個(gè)參數(shù)都做了詳細(xì)的描述
http://www.echartsjs.com/option.html#title
還有它的官方實(shí)例 http://www.echartsjs.com/examples/
二.Echarts 庫簡介
1.簡單的柱狀圖,折線圖示例
在http://www.echartsjs.com/download.html鏈接里下載完整版眼坏,echats.min.js,然后引入到html文件中就可以使用了拂玻,以下是一個(gè)簡單的直方圖例子。
在繪圖前我們需要為 ECharts 準(zhǔn)備一個(gè)具備高寬的 DOM 容器宰译。然后就可以通過 echarts.init 方法初始化一個(gè) echarts 實(shí)例并通過 setOption 方法生成一個(gè)簡單的柱狀圖
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>bar</title>
<!-- 引入 echarts.js -->
<script src="../echarts.min.js"></script>
</head>
<body>
<!-- 為ECharts準(zhǔn)備一個(gè)具備大虚苎痢(寬高)的Dom -->
<div id="main" style="width: 900px;height:600px;"></div>
<script type="text/javascript">
// 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
var myChart = echarts.init(document.getElementById('main'));
// 指定圖表的配置項(xiàng)和數(shù)據(jù)
var option = {
// 標(biāo)題
title: {
text: 'ECharts 入門示例'
},
// 工具箱
toolbox: {
show: true,
feature: {
saveAsImage: {
show: true
}
}
},
// 圖例
legend: {
data: ['銷量']
},
// x軸
xAxis: {
data: ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
},
yAxis: {},
// 數(shù)據(jù)
series: [{
name: '銷量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
};
// 使用剛指定的配置項(xiàng)和數(shù)據(jù)顯示圖表沿侈。
myChart.setOption(option);
</script>
</body>
</html>
運(yùn)行結(jié)果:
以下是一個(gè)簡單的折線圖例子
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>line</title>
<!-- 引入 echarts.js -->
<script src="../echarts.min.js"></script>
</head>
<body>
<!-- 為ECharts準(zhǔn)備一個(gè)具備大写车凇(寬高)的Dom -->
<div id="main" style="width: 900px;height:600px;"></div>
<script type="text/javascript">
// 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
var myChart = echarts.init(document.getElementById('main'));
// 指定圖表的配置項(xiàng)和數(shù)據(jù)
var option = {
title: {
text: 'ECharts 入門示例'
},
toolbox: {
show: true,
feature: {
saveAsImage: {
show: true
}
}
},
legend: {
data: ['銷量']
},
xAxis: {
data: ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]
},
yAxis: {},
series: [{
name: '銷量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
},{
name: '產(chǎn)量',
type: 'line',
data: [7, 30, 50, 11, 40, 80]
}]
};
// 使用剛指定的配置項(xiàng)和數(shù)據(jù)顯示圖表缀拭。
myChart.setOption(option);
</script>
</body>
</html>
標(biāo)題組件title
http://www.echartsjs.com/option.html#title
標(biāo)題負(fù)責(zé)顯示整個(gè)圖表的標(biāo)題咳短,主要配置option下的title
title: {
show:true,
text: 'ECharts 入門示例', //標(biāo)題文字
subtext:'學(xué)習(xí)ECharts就來慕課網(wǎng)', //子標(biāo)題
left:'right', //標(biāo)題位置( 數(shù)字就是像素值 填帽,也可以是 center left right這種值)
borderColor:'red', //邊框顏色
borderWidth:5, //邊框?qū)挾? textStyle:{
fontSize:40
}
},
工具箱組件
http://www.echartsjs.com/option.html#toolbox
組件的工具欄
toolbox: {
show: true, //是否顯示
feature: {
dataView:{
show:true
}, //數(shù)據(jù)視圖
restore:{
show:true
}, //還原
dataZoom:{
show:true
}, //縮放視圖
saveAsImage: {
show: true
}, //保存圖片
magicType: {
type: ['line', 'bar']
} //動(dòng)態(tài)類型切換
}//具體需要顯示的功能
},
彈窗組件
http://www.echartsjs.com/option.html#tooltip
tooltip: {
show:true, //是否顯示
trigger: 'axis' //觸發(fā)類型為x軸觸發(fā)
},
標(biāo)記線和標(biāo)記點(diǎn)markline markpoint
http://www.echartsjs.com/option.html#series-line.markLine
最大值,最小值 平均值的標(biāo)記線
series: [{
name: '銷量',
type: 'line',
data: [5, 20, 36, 10, 10, 20],
markPoint: {
data: [
{type: 'max', name: '最大值'},
{type: 'min', name: '最小值',symbol:'arrow'}
]
},
markLine: {
data: [
{type: 'average', name: '平均值'}
]
}
}]
三.Echar常用圖
餅圖
餅圖展示數(shù)據(jù)的特點(diǎn)一般表示數(shù)據(jù)占全局的百分比咙好,type為pie篡腌,重要的需要設(shè)置的屬性如下
- Center 圓心坐標(biāo)
- Radius 半徑
- Name 圖例名字
- selectedMode 是否支持多選
html中
<!-- 為ECharts準(zhǔn)備一個(gè)具備大小(寬高)的Dom -->
<div id="main" style="width: 900px;height:600px;"></div>
js中敷扫,與折線圖不同的是哀蘑,series中诚卸,type為pie表示餅圖葵第;radius指的是餅圖的半徑,其數(shù)組的第一個(gè)參數(shù)是內(nèi)圓半徑合溺,第二個(gè)參數(shù)為外圓半徑卒密,50%表示容器高寬中較小一項(xiàng)的 50% 長度;center為餅圖的圓心坐標(biāo)棠赛,第一個(gè)參數(shù)為橫坐標(biāo)哮奇,第二個(gè)參數(shù)為縱坐標(biāo)。
// 基于準(zhǔn)備好的dom睛约,初始化echarts實(shí)例
var myChart = echarts.init(document.getElementById('main'));
option = {
tooltip: {
trigger: 'item',
formatter: "{a} <br/>鼎俘: {c} (siabu57%)"
},
legend: {
orient: 'vertical',
x: 'left',
data:['直接訪問','郵件營銷','聯(lián)盟廣告','視頻廣告','搜索引擎']
},
series: [
{
name:'訪問來源',
type:'pie', //圖表為餅圖
radius: ['50%', '70%'], //餅圖的半徑
center: ['50%', '50%'], //餅圖的圓心位置
avoidLabelOverlap: false,
label: {
normal: {
show: false,
position: 'center'
},
emphasis: {
show: true,
textStyle: {
fontSize: '30',
fontWeight: 'bold'
}
}
},
labelLine: {
normal: {
show: false
}
},
data:[
{value:335, name:'直接訪問'},//這里的name必須和legend(圖例)中的data一一對(duì)應(yīng)
{value:310, name:'郵件營銷'},
{value:234, name:'聯(lián)盟廣告'},
{value:135, name:'視頻廣告'},
{value:1548, name:'搜索引擎'}
]
}
]
};
運(yùn)行結(jié)果如下
儀表盤
省略
地圖
該數(shù)據(jù)可以去官網(wǎng)的實(shí)例拷貝雏节,由于代碼太長省略部分?jǐn)?shù)據(jù)止状。geoCoordMap表示各個(gè)城市的經(jīng)緯度剂府,data是每個(gè)城市說對(duì)應(yīng)的圖表想要表達(dá)的數(shù)據(jù)娇未,
var data = [ //具體數(shù)據(jù)
{name: '海門', value: 9},
{name: '鄂爾多斯', value: 12},
..................//此處數(shù)據(jù)省略
];
var geoCoordMap = { //地理坐標(biāo)圖
'海門':[121.15,31.89],
'鄂爾多斯':[109.781327,39.608266],
..................//此處數(shù)據(jù)省略
};
var convertData = function (data) {
var res = [];
for (var i = 0; i < data.length; i++) {
var geoCoord = geoCoordMap[data[i].name];
if (geoCoord) {
res.push({
name: data[i].name,
value: geoCoord.concat(data[i].value)
});
}
}
return res;
};
//上面的函數(shù)處理過后的數(shù)據(jù)
// convertData = [
// {
// name: '海門',
// value: [121.15, 31.89, 9],
// },
// {
// name: "鄂爾多斯",
// value: [109.781327, 39.608266, 12]
// }
// ..................//此處數(shù)據(jù)省略
// ];
function renderItem(params, api) {
var coords = [
[116.7,39.53],
[103.73,36.03],
[112.91,27.87],
[120.65,28.01],
[119.57,39.95]
];
var points = [];
for (var i = 0; i < coords.length; i++) {
points.push(api.coord(coords[i]));
}
var color = api.visual('color');
return {
type: 'polygon',
shape: {
points: echarts.graphic.clipPointsByRect(points, {
x: params.coordSys.x,
y: params.coordSys.y,
width: params.coordSys.width,
height: params.coordSys.height
})
},
style: api.style({
fill: color,
stroke: echarts.color.lift(color)
})
};
}
option = {
// backgroundColor: '#404a59',
title: {
text: '全國主要城市空氣質(zhì)量',
subtext: 'data from PM25.in',
sublink: 'http://www.pm25.in',
left: 'center',
textStyle: {
color: '#fff'
}
},
tooltip : {
trigger: 'item'
},
bmap: {
center: [104.114129, 37.550339],
zoom: 5,
roam: true,
mapStyle: {
styleJson: [
{
"featureType": "water",
"elementType": "all",
"stylers": {
"color": "#044161"
}
},
{
"featureType": "land",
"elementType": "all",
"stylers": {
"color": "#004981"
}
},
{
"featureType": "boundary",
"elementType": "geometry",
"stylers": {
"color": "#064f85"
}
},
{
"featureType": "railway",
"elementType": "all",
"stylers": {
"visibility": "off"
}
},
{
"featureType": "highway",
"elementType": "geometry",
"stylers": {
"color": "#004981"
}
},
{
"featureType": "highway",
"elementType": "geometry.fill",
"stylers": {
"color": "#005b96",
"lightness": 1
}
},
{
"featureType": "highway",
"elementType": "labels",
"stylers": {
"visibility": "off"
}
},
{
"featureType": "arterial",
"elementType": "geometry",
"stylers": {
"color": "#004981"
}
},
{
"featureType": "arterial",
"elementType": "geometry.fill",
"stylers": {
"color": "#00508b"
}
},
{
"featureType": "poi",
"elementType": "all",
"stylers": {
"visibility": "off"
}
},
{
"featureType": "green",
"elementType": "all",
"stylers": {
"color": "#056197",
"visibility": "off"
}
},
{
"featureType": "subway",
"elementType": "all",
"stylers": {
"visibility": "off"
}
},
{
"featureType": "manmade",
"elementType": "all",
"stylers": {
"visibility": "off"
}
},
{
"featureType": "local",
"elementType": "all",
"stylers": {
"visibility": "off"
}
},
{
"featureType": "arterial",
"elementType": "labels",
"stylers": {
"visibility": "off"
}
},
{
"featureType": "boundary",
"elementType": "geometry.fill",
"stylers": {
"color": "#029fd4"
}
},
{
"featureType": "building",
"elementType": "all",
"stylers": {
"color": "#1a5787"
}
},
{
"featureType": "label",
"elementType": "all",
"stylers": {
"visibility": "off"
}
}
]
}
},
series : [ //系列列表
{
name: 'pm2.5', //系列名稱媚狰,用于圖例篩選锅知,tooltip顯示
type: 'scatter', //散點(diǎn)(氣泡)圖
coordinateSystem: 'bmap',//該系列使用的坐標(biāo)系
data: convertData(data),
symbolSize: function (val) { //標(biāo)記的大小
return val[2] / 10;
},
label: { //圖形上的文本標(biāo)簽
normal: {
formatter: '康震',
position: 'right',
show: false
},
emphasis: { //高亮的圖形和標(biāo)簽樣式
show: true
}
},
itemStyle: {
normal: {
color: '#ddb926'
}
}
},
{
name: 'Top 5',
type: 'effectScatter',//帶有漣漪特效動(dòng)畫的散點(diǎn)(氣泡)圖
coordinateSystem: 'bmap',
data: convertData(data.sort(function (a, b) {
return b.value - a.value;
}).slice(0, 6)),
symbolSize: function (val) {
return val[2] / 10;
},
showEffectOn: 'emphasis',
rippleEffect: {
brushType: 'stroke'
},
hoverAnimation: true,
label: {
normal: {
formatter: '吮螺',
position: 'right',
show: true
}
},
itemStyle: {
normal: {
color: '#f4e925',
shadowBlur: 10,
shadowColor: '#333'
}
},
zlevel: 1
},
{
type: 'custom',//自定義系列
coordinateSystem: 'bmap',
renderItem: renderItem,
itemStyle: {
normal: {
opacity: 0.5
}
},
animation: false,
silent: true,
data: [0],
z: -10
}
]
};
四.Echart中級(jí)使用
多個(gè)坐標(biāo)軸
// 基于準(zhǔn)備好的dom商膊,初始化echarts實(shí)例
var myChart = echarts.init(document.getElementById('main'));
// 指定圖表的配置項(xiàng)和數(shù)據(jù)
option = {
tooltip: {
trigger: 'axis'
},
toolbox: {
feature: {
dataView: {show: true, readOnly: false},
magicType: {show: true, type: ['line', 'bar']},
restore: {show: true},
saveAsImage: {show: true}
}
},
legend: {
data:['蒸發(fā)量','降水量','平均溫度']
},
xAxis: [
{
type: 'category',
data: ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
}
],
yAxis: [
{
type: 'value',
name: '水量',
min: 0,
max: 250,
interval: 50,
axisLabel: {
formatter: '{value} ml'
}
},
{
type: 'value',
name: '溫度',
min: 0,
max: 25,
interval: 5,
axisLabel: {
formatter: '{value} °C'
}
}
],
series: [
{
name:'蒸發(fā)量',
type:'bar',
data:[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
},
{
name:'降水量',
type:'bar',
data:[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
},
{
name:'平均溫度',
type:'line',
yAxisIndex: 1,
data:[2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]
}
]
};
// 使用剛指定的配置項(xiàng)和數(shù)據(jù)顯示圖表伏伐。
myChart.setOption(option);