<!-- 為 ECharts 准備一個具備大小(寬高)的 DOM -->
<div id="box" style="width: 600px;height:400px;"></div> //如果需要改背景色直接在這里修改便可以
export default {
data() {
return {
//調用了eacharts官方實例的 ,這里寫option的屬性
// 指定圖表的配置項和數據
option: {
backgroundColor: "#2c343c",
title: {
text: "Customized Pie",
left: "center",
top: 20,
textStyle: {
color: "#ccc"
}
},
tooltip: {
trigger: "item",
formatter: "{a} <br/>{b} : {c} ({d}%)"
},
visualMap: {
show: false,
min: 80,
max: 600,
inRange: {
colorLightness: [0, 1]
}
},
series: [
{
name: "訪問來源",
type: "pie",
radius: "55%",
center: ["50%", "50%"],
data: [
{ value: 335, name: "直接訪問" },
{ value: 310, name: "郵件營銷" },
{ value: 274, name: "聯盟廣告" },
{ value: 235, name: "視頻廣告" },
{ value: 400, name: "搜索引擎" }
].sort(function(a, b) {
return a.value - b.value;
}),
roseType: "radius",
label: {
normal: {
textStyle: {
color: "rgba(255, 255, 255, 0.3)"
}
}
},
labelLine: {
normal: {
lineStyle: {
color: "rgba(255, 255, 255, 0.3)"
},
smooth: 0.2,
length: 10,
length2: 20
}
},
itemStyle: {
normal: {
color: "#c23531",
shadowBlur: 200,
shadowColor: "rgba(0, 0, 0, 0.5)"
}
},
animationType: "scale",
animationEasing: "elasticOut",
animationDelay: function(idx) {
return Math.random() * 200;
}
}
]
},
};
},
methods: {
customized() {
//方法里面第一步// 基於准備好的dom,初始化echarts實例
var myChart = this.$echarts.init(document.getElementById('box'));
// 使用剛指定的配置項和數據顯示圖表。
myChart.setOption(this.option);
}
},
mounted() {
// 注意:一定需要在mounted中調用才行
this.customized();
}
};
最后在main.js入口文件中 全局引入掛載
// 引入echarts
// import echarts from 'echarts'
//掛載到實例對象上
// Vue.prototype.$echarts =echarts
==================================
需要加下面兩句 這個是main全局引入的
import echarts from 'echarts'
Vue.use(echarts)//全局使用echarts
<div class="echartr">
<!-- //右邊環形圖 -->
<div class="bargraph" id="barg2"></div>
</div>
let bargraph = echarts.init(document.getElementById('barg2'));
bargraph.setOption(this.option3);
=======================================
有時候需要加上這些代碼 在頁面尺寸發生改變時 隨着頁面改變而變大或小
mounted() {
let _this = this;
_this.$nextTick(() => {
let bargraph0 = echarts.init(_this.$refs.barg0);
bargraph0.setOption(_this.option0);
window.addEventListener("resize", bargraph0.resize);
let bargraph = echarts.init(_this.$refs.barg);
bargraph.setOption(_this.option2);
window.addEventListener("resize", bargraph.resize);
let bargraph2 = echarts.init(_this.$refs.barg2);
bargraph2.setOption(_this.option3);
window.addEventListener("resize", bargraph2.resize);
});
}