// 使用刚指定的配置项和数据显示图表。
this.myChart.setOption(option);
let index=0;
let myChart =this.myChart;//解决echarts中this指向问题
myChart.dispatchAction({
type: 'highlight',
seriesIndex: 0,
dataIndex: 0
});
myChart.on("mouseover", function(e) {
if (e.dataIndex != index) {
myChart.dispatchAction({
type: "downplay",
seriesIndex: 0,
dataIndex: index
});
}
});
myChart.on("mouseout", function(e) {
index = e.dataIndex;
myChart.dispatchAction({
type: "highlight",
seriesIndex: 0,
dataIndex: e.dataIndex
});
});
上面的代码写在this.myChart.setOption(option)的后面即可在vue中写用的echarts,不能写成this.myChart.dispatchAction({})因为echart会导致this指向问题,这样饼图就不能默认显示。所以将this.myChart赋给变量myChart就可以解决这个问题。下面是实现的效果图,默认显示也可以动态切换显示
如果想默认显示的是黄色部分的就给dataIndex赋值为0,如果想默认显示红色部分就将dataIndex改为1,如果想默认显示紫色就将dataIndex改为2,以此类推,可以改变默认显示的位置