<div
ref="echarts"
id="echarts"
style="width: 350px; height: 300px; margin: auto"
></div>
import * as echarts from "echarts";
export default {
mounted() {
this.funEchart();
},
methods: {
funEchart() {
let chartDom = this.$refs.echarts;
let myChart = echarts.init(chartDom);
const dataset = {
dimensions: ["name", "score"],
source: [
["一", 314],
["二", 351],
["三", 287],
["四", 219],
["五", 253],
["六", 165],
["七", 318],
["八", 366],
],
};
const pieOption = {
dataset: [dataset],
legend: {
top: "bottom",
show: true,
},
series: [
{
type: "pie",
id: "Score",
radius: [0, "50%"],
universalTransition: true,
animationDurationUpdate: 1000,
label: {
position: "outer",
alignTo: "labelLine",
bleedMargin: 5,
},
},
],
title: [
{
subtext: 'alignTo: "饼图"',
left: "50%",
textAlign: "center",
},
],
xAxis: {
show: false,
},
};
const barOption = {
dataset: [dataset],
legend: {
show: false,
},
xAxis: {
type: "category",
show: true,
},
yAxis: {},
series: [
{
type: "bar",
id: "Score",
colorBy: "data",
encode: { x: "name", y: "score" },
universalTransition: true,
animationDurationUpdate: 1000,
},
],
title: [
{
subtext: 'alignTo: "矩形图"',
left: "50%",
textAlign: "center",
},
],
};
const lineOption = {
dataset: [dataset],
legend: {
show: false,
},
xAxis: {
type: "category",
show: true,
splitLine: {
show: true,
lineStyle: {
width: 1,
type: "solid",
},
},
},
yAxis: {},
series: [
{
type: "line",
id: "Score",
colorBy: "data",
encode: { x: "name", y: "score" },
universalTransition: true,
animationDurationUpdate: 1000,
},
],
title: [
{
subtext: '标题 : "折线图"',
left: "50%",
textAlign: "center",
backgroundColor: "purple",
borderColor: "#ff3040",
textStyle: {
color: "red",
},
subtextStyle: {
color: "#fff",
fontStyle: "bold",
fontSize: 14,
},
},
],
};
let option = lineOption;
let i = 0;
myChart.setOption(option);
var timer = setInterval(() => {
if (i % 3 == 0) {
option = barOption;
} else if (i % 3 == 1) {
option = pieOption;
} else if (i % 3 == 2) {
option = lineOption;
}
myChart.setOption(option);
i++;
myChart.on("click", function (params) {
clearInterval(timer);
console.log(params.name);
console.log("停止定时器");
});
}, 3000);
myChart.getZr().on("click", function (event) {
if (!event.target) {
console.log("点击空白处,打开定时器");
clearInterval(timer);
timer= setInterval(() => {
if (i % 3 == 0) {
option = barOption;
} else if (i % 3 == 1) {
option = pieOption;
} else if (i % 3 == 2) {
option = lineOption;
}
myChart.setOption(option);
i++;
myChart.on("click", function (params) {
clearInterval(timer);
console.log(params.name);
console.log("停止定时器");
});
}, 3000);
}
});
},
},
};
</script>