基于vue的echarts 线形图

<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>

直接把数据直接替换即可

效果图

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值