安装包 npm i echarts
封装组件 EchartComponent
- 新建 src/components/EchartComponent.vue
<template>
<div ref="echartComponentRef" style="height: 100%"></div>
</template>
<script>
import * as echarts from "echarts";
import debounce from "lodash/debounce";
import LineBarEchartModel from "./model/lineBarEchartModel";
export default {
name: "BasicEchart",
components: {},
props: {
options: {
type: LineBarEchartModel,
required: true,
default: () => ({}),
},
},
computed: {},
data() {
return {
echartsInstance: {},
resizeCb: () => {},
};
},
created() {},
beforeDestroy() {
window.removeEventListener("resize", this.resizeCb);
},
destroyed() {
this.echartsInstance.dispose && this.echartsInstance.dispose();
},
watch: {
options: {
handler(options) {
console.log("watch---options---start");
this.echartRender(options);
},
deep: true,
},
},
mounted() {
this.resizeCb = debounce(this.resizeCallBack, 500);
window.addEventListener("resize", this.resizeCb);
},
methods: {
resizeCallBack() {
console.log("EchartComponent---resizeCallBack");
this.echartRender(this.options);
},
echartRender(options = {}) {
this.echartsInstance.dispose && this.echartsInstance.dispose();
const chartEle = this.$refs.echartComponentRef;
this.echartsInstance = echarts.init(chartEle, "", Object.keys(options.containerStyle).length > 0 ? options.containerStyle : null);
LineBarEchartModel.supplyOptions(options);
console.log("echartRender---options", JSON.stringify(options));
options && this.echartsInstance.setOption(options);
},
},
};
</script>
<style lang="scss" scoped></style>
- 新建 src/components/model/lineBarEchartModel.js
export default class LineBarEchartModel {
constructor() {
this.type = "line";
this.title = {};
this.legend = {};
this.series = {};
this.xAxis = {};
this.containerStyle = {};
this.isSeriesStack = false;
this.yAxis = [
{
type: "value",
splitLine: { show: false },
},
];
this.grid = { left: "10px", bottom: "10px", top: "60px", containLabel: true };
this.tooltip = { trigger: "axis", extraCssText: "z-index:999", confine: true };
}
setType(type = "line") {
this.type = type;
return this;
}
setTooltip(tooltip = {}) {
this.tooltip = tooltip;
return this;
}
setColors(colors = []) {
this.color = colors;
return this;
}
setIsSeriesStack(isSeriesStack = false) {
this.isSeriesStack = isSeriesStack;
return this;
}
setTitle(title = {}) {
this.title = title;
return this;
}
setLegend(legend = {}) {
this.legend = legend;
return this;
}
setLegendData(legendData = []) {
this.legend = { data: legendData };
return this;
}
setSeries(series = []) {
this.series = series;
return this;
}
setSeriesData(seriesData = []) {
this.series = seriesData.map((series) => ({ data: series }));
return this;
}
setXAxis(xAxis = {}) {
this.xAxis = { ...xAxis, boundaryGap: false };
return this;
}
setXAxisData(xAxisData = []) {
this.xAxis = { data: xAxisData, boundaryGap: false };
return this;
}
setYAxisMinOrMax(minValue = 0, maxValue) {
this.yAxis.forEach((item) => {
if (minValue || minValue === 0) {
item.min = minValue;
}
if (maxValue || maxValue === 0) {
item.max = maxValue;
}
});
return this;
}
setDoubleYAxisMinOrMax({ y1min = 0, y1max, y2min = 0, y2max }) {
this.yAxis[0].min = y1min;
this.yAxis[0].max = y1max;
this.yAxis[1].min = y2min;
this.yAxis[1].max = y2max;
return this;
}
setYAxis(yAxis = []) {
this.yAxis = yAxis;
return this;
}
setDataZoom() {
this.dataZoom = [
{ type: "inside", start: 0, end: 100 },
{ start: 0, end: 100 },
];
return this;
}
setGrid(grid = {}) {
this.grid = grid;
return this;
}
setContainerStyle(containerStyle = {}) {
this.containerStyle = containerStyle;
return this;
}
static supplyOptions(options = {}) {
const { type } = options;
options?.series?.forEach((item, index) => {
if (!item.type) {
item.type = type;
}
item.smooth = true;
if (options.isSeriesStack) {
item.stack = "barSeriesStack";
}
if (Array.isArray(options.yAxis) && options.yAxis.length > 1) {
item.yAxisIndex = index;
}
});
options?.legend?.data?.forEach((legendName, index) => {
options.series[index].name = legendName;
});
options.xAxis.boundaryGap = type === "bar";
Object.assign(options);
}
}
<template>
<div id="app">
<div class="echart_wrapper" style="height: 200px">
<EchartComponent :options="lineBarEchartOpiton" />
</div>
<div class="echart_wrapper" style="height: 200px">
<EchartComponent :options="lineBarEchartOpiton1" />
</div>
</div>
</template>
<script>
import LineBarEchartModel from "./components/model/lineBarEchartModel";
import EchartComponent from "./components/EchartComponent.vue";
export default {
name: "App",
components: {
EchartComponent,
},
data() {
return {
lineBarEchartOpiton: new LineBarEchartModel(),
lineBarEchartOpiton1: new LineBarEchartModel(),
};
},
mounted() {
this.lineBarEchartOpiton.setSeries([{ data: [100, 300, 200, 600, 50, 30] }]).setXAxis({ data: ["Mon", "Tue", "Wed", "T", "E", "F"] });
this.lineBarEchartOpiton1
.setLegendData(["O/N", "1W", "2W"])
.setSeriesData([[100, 300, 200, 600, 50, 30],[300, 100, 30, 600, 50, 30],[10, 30, 20, 60, 150, 130]])
.setXAxisData(["Mon", "Tue", "Wed", "T", "E", "F"]);
},
};
</script>

使用介绍(如果只是简单设置数据,推荐setLegendData(datas), setSeriesData(datas),setXAxisData(datas) )
.setType("line")
.setType("bar")
.setType("bar").setIsSeriesStack(true)
.setSeries([{ data: [100, 300, 200, 600, 50, 30] }])
.setXAxis({ data: ["Mon", "Tue", "Wed", 'T', 'E', 'F'] })
.setLegend({ data: ["O/N", "1W", '2W', '3W','4W'] })
.setSeries([{ data: [100, 300, 200, 600, 50, 30] }, { data: [400, 150, 101, 200, 360, 300] }, { data: [300, 132, 101, 300, 100, 400] }, { data: [200, 132, 101, 150, 100, 400] }, { data: [800, 132, 101, 900, 100, 400] }])
.setXAxis({ data: ["Mon", "Tue", "Wed", 'T', 'E', 'F'], })
.setType("bar")
.setSeries([{ data: [100, 300, 200, 600, 50, 30] }])
.setXAxis({ data: ["Mon", "Tue", "Wed", 'T', 'E', 'F'] })
.setType("bar")
.setLegend({ data: ["O/N", "1W", '2W', '3W','4W'] })
.setSeries([{ data: [100, 300, 200, 600, 50, 30] }, { data: [400, 150, 101, 200, 360, 300] }, { data: [300, 132, 101, 300, 100, 400] }, { data: [200, 132, 101, 150, 100, 400] }, { data: [800, 132, 101, 900, 100, 400] }])
.setXAxis({ data: ["Mon", "Tue", "Wed", 'T', 'E', 'F'], })
.setType("bar")
.setIsSeriesStack(true)
.setLegend({ data: ["O/N", "1W", '2W', '3W','4W'] })
.setSeries([{ data: [100, 300, 200, 600, 50, 30] }, { data: [400, 150, 101, 200, 360, 300] }, { data: [300, 132, 101, 300, 100, 400] }, { data: [200, 132, 101, 150, 100, 400] }, { data: [800, 132, 101, 900, 100, 400] }])
.setXAxis({ data: ["Mon", "Tue", "Wed", 'T', 'E', 'F'], })
.setType("bar")
.setLegend({ data: ["O/N", "1W", '2W'] })
.setSeries([{ data: [100, 300, 200, 600, 50, 30], type: "bar" }, { data: [600, 200, 100, 300, 60, 30], type: "bar" }, { data: [400, 150, 101, 200, 360, 300], type: "line" }])
.setXAxis({ data: ["Mon", "Tue", "Wed", 'T', 'E', 'F'] })
.setLegend({ data: ["O/N", "1W"] })
.setSeries([{ data: [100, 300, 200, 600, 50, 30], type: "line" }, { data: [600, 200, 100, 300, 60, 30], type: "line" }])
.setXAxis({ data: ["Mon", "Tue", "Wed", 'T', 'E', 'F'] })
.setYAxis([{name:'左侧Y轴名称', position: 'left'}, {name:'右侧Y轴名称', position: 'right'}])
.setLegend({ data: ["O/N", "1W"] })
.setSeries([{ data: [100, 300, 200, 600, 50, 30], type: "line", markLine: { data: [{name: 'Y 轴值为 100 的水平线',yAxis: 250}]} }, { data: [600, 200, 100, 300, 60, 30], type: "line" }])
.setXAxis({ data: ["Mon", "Tue", "Wed", 'T', 'E', 'F'] })