数据请求以后实现前端分页,实现图表间隔2秒更新一次,实现组件切换时,清除定时器,当鼠标移入到图表中的时候图表不会产生间隔性刷新数据,移开后继续实现间隔性更新图表:
一.柱状图功能实现的 完整版-具体代码如下:
<template>
<div class="com-container">
<div class="com-chart" ref="rank_ref"></div>
</div>
</template>
<script>
export default {
data() {
return {
chartInstance: null,
allData: null,
startValue: 0,
endValue: 9,
timerId: null,
};
},
mounted() {
this.initChart();
this.getDate();
window.addEventListener("resize", this.screenAdapter);
this.screenAdapter();
},
destroyed() {
window.removeEventListener("resize", this.screenAdapter);
clearInterval(this.timerId);
},
methods: {
initChart() {
this.chartInstance = this.$echarts.init(this.$refs.rank_ref, "chalk");
const initOption = {
title: {
text: "▎地区销售排行",
left: 20,
top: 20,
},
grid: {
top: "40%",
left: "5%",
right: "5%",
bottom: "5%",
containLabel: true,
},
tooltip: {
show: true,
},
xAxis: {
type: "category",
},
yAxis: {
type: "value",
},
series: [
{
type: "bar",
},
],
};
this.chartInstance.setOption(initOption);
this.chartInstance.on("mouseover", () => {
clearInterval(this.timerId);
});
this.chartInstance.on("mouseout", () => {
this.startInterval();
});
},
async getDate() {
const res = await this.$http.get("接口地址");
this.allData = res.data;
this.allData.sort((a, b) => {
return b.value - a.value;
});
this.updateChart();
this.startInterval();
},
updateChart() {
const colorArr = [
["#0BA82C", "#4FF778"],
["#2E72BF", "#23E5E5"],
["#5052EE", "#AB6EE5"],
];
const provinceArr = this.allData.map((item) => {
return item.name;
});
const valueArr = this.allData.map((item) => {
return item.value;
});
const dataOption = {
xAxis: {
data: provinceArr,
},
dataZoom: {
show: true,
startValue: this.startValue,
endValue: this.endValue,
},
series: [
{
data: valueArr,
itemStyle: {
color: (arg) => {
let targetColorArr = null;
if (arg.value > 300) {
targetColorArr = colorArr[0];
} else if (arg.value > 200) {
targetColorArr = colorArr[1];
} else {
targetColorArr = colorArr[2];
}
return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: targetColorArr[0],
},
{
offset: 1,
color: targetColorArr[1],
},
]);
},
},
},
],
};
this.chartInstance.setOption(dataOption);
},
screenAdapter() {
const titleFontSize = (this.$refs.rank_ref.offsetWidth / 100) * 3.6;
const adapterOption = {
title: {
textStyle: {
fontSize: titleFontSize,
},
},
series: [
{
barWidth: titleFontSize,
itemStyle: {
barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0],
},
},
],
};
this.chartInstance.setOption(adapterOption);
this.chartInstance.resize();
},
startInterval() {
if (this.timerId) {
clearInterval(this.timerId);
}
this.timerId = setInterval(() => {
this.startValue++;
this.endValue++;
if (this.endValue > this.allData.length - 1) {
this.startValue = 0;
this.endValue = 9;
}
this.updateChart();
}, 2000);
},
},
};
</script>
<style lang="less" scoped>
</style>
二.折线图实现的功能完整版代码--具体如下:
<template>
<div class="com-container">
<!-- 下拉切换的主题 -->
<div class="title" :style="comStyle">
<span>{{ '▎ ' + showTitle }}</span>
<span class="iconfont title-icon" @click="showChice = !showChice" :style="comStyle">

</span>
<div class="select-con" v-show="showChice" :style="marginStyle">
<div
class="select-item"
style="cursor: pointer"
v-for="item in selectTypes"
:key="item.key"
@click="handleSelect(item)"
>
{{ item.text }}
</div>
</div>
</div>
<div class="com-chart" ref="trend_ref"></div>
</div>
</template>
<script>
export default {
data() {
return {
chartInstane: null,
allData: null,
showChice: false,
choiceType: "map",
titleFontSize: 0
};
},
computed: {
selectTypes() {
return this.allData
? this.allData.type.filter((item) => item.key !== this.choiceType)
: [];
},
showTitle() {
return this.allData ? this.allData[this.choiceType].title : [];
},
comStyle() {
return {
fontSize: this.titleFontSize + "px",
};
},
marginStyle() {
return {
marginLeft: this.titleFontSize + "px",
};
},
},
mounted() {
this.initChart();
this.getData();
window.addEventListener("resize", this.screenAdapter);
this.screenAdapter();
},
destroyed() {
window.removeEventListener("resize", this.screenAdapter);
},
methods: {
handleSelect(currentValue) {
this.choiceType = currentValue.key;
this.showChice = false;
this.updateChart();
},
screenAdapter() {
this.titleFontSize = (this.$refs.trend_ref.offsetWidth / 100) * 3.6;
const adapterOption = {
legend: {
itemWidth: this.titleFontSize,
itemHeight: this.titleFontSize,
itemGap: this.titleFontSize,
textStyle: {
fontSize: this.titleFontSize / 2,
},
},
};
this.chartInstane.setOption(adapterOption);
this.chartInstane.resize();
},
initChart() {
this.chartInstane = this.$echarts.init(this.$refs.trend_ref, "chalk");
const initOption = {
grid: {
left: "3%",
top: "35%",
right: "4%",
bottom: "1%",
containLabel: true,
},
tooltip: {
trigger: "axis",
},
legend: {
left: 20,
top: "15%",
icon: "circle",
},
xAxis: {
type: "category",
boundaryGap: false,
},
yAxis: {
type: "value",
},
};
this.chartInstane.setOption(initOption);
},
async getData() {
const { data: res } = await this.$http.get("接口地址");
this.allData = res;
this.updateChart();
},
updateChart() {
const colorArr1 = [
"rgba(11, 168, 44, 0.5)",
"rgba(44, 110, 255, 0.5)",
"rgba(22, 242, 217, 0.5)",
"rgba(254, 33, 30, 0.5)",
"rgba(250, 105, 0, 0.5)",
];
const colorArr2 = [
"rgba(11, 168, 44, 0)",
"rgba(44, 110, 255, 0)",
"rgba(22, 242, 217, 0)",
"rgba(254, 33, 30, 0)",
"rgba(250, 105, 0, 0)",
];
const timeArr = this.allData.common.month;
const valueArr = this.allData[this.choiceType].data;
const seriesArr = valueArr.map((item, index) => {
return {
name: item.name,
type: "line",
data: item.data,
stack: this.choiceType,
areaStyle: {
color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
{
offset: 0,
color: colorArr1[index],
},
{
offset: 1,
color: colorArr2[index],
},
]),
},
};
});
const legendArr = valueArr.map((item) => item.name);
const dataOption = {
xAxis: {
data: timeArr,
},
legend: {
data: legendArr,
},
series: seriesArr,
};
this.chartInstane.setOption(dataOption);
},
},
};
</script>
<style lang="less" scoped>
.title {
position: absolute;
left: 20px;
top: 20px;
z-index: 10;
color: white;
.title-icon {
margin-left: 10px;
cursor: pointer;
}
.select-con {
background-color: #222733;
}
}
</style>