<template>
<!-- 线性图和柱状图共用-->
<div :class="className" :style="{ height: height, width: width }" />
</template>
<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "@/views/dashboard/mixins/resize";
export default {
name: "Line",
mixins: [resize],
props: {
className: {
type: String,
default: "chart",
},
width: {
type: String,
default: "100%",
},
height: {
type: String,
default: "300px",
},
isRing: {
type: Boolean,
default: true,
},
showOutLine: {
type: Boolean,
default: true,
},
showGap: {
type: Boolean,
default: false,
},
showLegend: {
type: Boolean,
default: false,
},
colors: {
type: Array,
default: ["#5B8FF9", "#5AD8A6", "#5D7092", "#F6BD16", "#6F5EF9"],
},
datas: {
type: Object,
default: () => {
return {
// 传值数据类型
Xdata: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
colors: ["#5B8FF9", "#5AD8A6", "#5D7092", "#F6BD16", "#6F5EF9"],
seriesData: [
{
name: "标题",
type: "bar",
barWidth: "30",
data: [10, 52, 200, 334, 390, 330, 220],
},
{
name: "Direct",
type: "line",
data: [10, 52, 200, 334, 390, 330, 220],
},
],
};
},
},
radius: {},
},
data() {
return {
chart: null,
// 传值的数据格式
};
},
watch: {
datas: {
handler(val) {
this.initChart();
},
deep: true,
// immediate: true,
},
},
mounted() {
this.$nextTick(() => {
this.initChart();
});
},
beforeDestroy() {
if (!this.chart) {
return;
}
this.chart.dispose();
this.chart = null;
},
methods: {
initChart() {
if (!this.chart) {
this.chart = echarts.init(this.$el, "macarons");
}
this.chart.setOption({
tooltip: {
trigger: "axis",
axisPointer: {
type: "shadow",
},
},
color: this.datas.colors ? this.datas.colors : this.colors,
legend: {
data: ["Email", "Union Ads"],
},
grid: {
left: "3%",
right: "4%",
bottom: "3%",
containLabel: true,
},
xAxis: [
{
type: "category",
data: this.datas.Xdata,
axisTick: {
alignWithLabel: true,
},
},
],
yAxis: [
{
type: "value",
},
],
series: this.datas.seriesData,
});
},
},
};
</script>
直接把数据直接替换即可
效果图


6686





