在vue組件化開發項目中,Echarts的使用是很頻繁的,但是具體是怎麼使用呢?下面做一下簡單的介紹:
第1步:安裝Echarts
npm install echarts --save
第2步:設置指定的容器,用於顯示圖標信息
<!-- 統計圖容器 -->
<div ref="myChart" style="width: 100%;height: 270px;"></div>
備註:這個容器必須是指定的寬度與高度的容器。
第3步:畫圖
drawLine() {
// 初始化echarts實例
let myChart = echarts.init(this.$refs.myChart);
// 配置參數
myChart.setOption({
title: {},
tooltip: {
trigger: "axis",
axisPointer: {
type: "cross",
label: {
backgroundColor: "#6a7985"
}
}
},
legend: {
data: ["郵件營銷", "聯盟廣告", "視頻廣告", "直接訪問", "搜索引擎"]
},
toolbox: {
feature: {
saveAsImage: {}
}
},
grid: {
left: "3%",
right: "4%",
bottom: "3%",
containLabel: true
},
xAxis: [
{
type: "category",
boundaryGap: false,
data: ["週一", "週二", "週三", "週四", "週五", "週六", "週日"]
}
],
yAxis: [
{
type: "value"
}
],
series: [
{
name: "郵件營銷",
type: "line",
stack: "總量",
areaStyle: {},
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: "聯盟廣告",
type: "line",
stack: "總量",
areaStyle: {},
data: [220, 182, 191, 234, 290, 330, 310]
},
{
name: "視頻廣告",
type: "line",
stack: "總量",
areaStyle: {},
data: [150, 232, 201, 154, 190, 330, 410]
},
{
name: "直接訪問",
type: "line",
stack: "總量",
areaStyle: { normal: {} },
data: [320, 332, 301, 334, 390, 330, 320]
},
{
name: "搜索引擎",
type: "line",
stack: "總量",
label: {
normal: {
show: true,
position: "top"
}
},
areaStyle: { normal: {} },
data: [820, 932, 901, 934, 1290, 1330, 1320]
}
]
});
}
備註:畫圖包含2個步驟:(1)初始化echarts實例;(2)配置參數;
效果圖: