echarts制作图表同时有3d柱状图与折线图

echarts制作图表同时有3d柱状图与折线图

工作遇到,小可爱们可直接抄作业,使用的pictorialBar

  • echarts封装,使用了mixin(这个方式向大佬取经获得),下面是目录结构
    目录结构

  • 在main.js引入

引入

  • index.js代码
import * as echarts from "echarts"; // 引入echarts
import Vue from "vue"
Vue.prototype.$echarts = echarts
// 引入封装好的图表组件
import VillageWaterAccumulation from "@/utils/s-charts/VillageWaterAccumulation";
// 导出
export default {
  install(Vue) {
    Vue.mixin({
      methods: {
        initChart(container, option) {
          let myChart = echarts.getInstanceByDom(container)
          if (myChart == null) { // 如果不存在,就进行初始化
            myChart = echarts.init(container);
          }
          myChart.setOption(option);
          window.addEventListener('resize', function () {
            myChart.resize();
          });
        },
      }
    })
    // 注册为全局组件
    Vue.component('s-VillageWaterAccumulation', VillageWaterAccumulation)
  }
}
  • 图表组件代码
<template>
  <div ref="VillageWaterAccumulation" :style="{ width, height }"></div>
</template>
<script>
export default {
  props: {
    width: {
      type: String,
      default: "500px",
    },
    height: {
      type: String,
      default: "300px",
    },
    data: {
      type: Object,
      required: true,
    },
  },
  watch: {
    //监听data值的变化,避免接口过慢页面不发生更新
    data: function (newVal, oldVal) {
      this.data = newVal; //发生变化时,重新赋值
      newVal && this.draw(); //newVal存在的话执行draw函数,重新绘图
    },
  },
  data() {
    return {
      blueBar: require("../../assets/villagebigscreen/blue_bar.png"),
    };
  },
  mounted() {
    this.draw();
  },
  methods: {
    draw() {
      let barList = this.data.water.map((v, k) => {
        return {
          value: v,
          symbol: "image://" + this.blueBar,
          symbolSize: [10, "100%"],
        };
      });
      let option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            crossStyle: {
              color: "#999",
            },
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          top: "25%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: this.data.create_time,
            axisPointer: {
              type: "shadow",
            },
            axisLine: {
              lineStyle: {
                color: "#00ddff",
              },
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            name: "吨",
            axisLabel: {
              formatter: "{value}",
            },
            axisLine: {
              lineStyle: {
                color: "#00ddff",
              },
            },
          },
        ],
        series: [
          {
            name: "水量",
            type: "pictorialBar",
            data: barList
          },
          {
            name: "水量累计",
            type: "line",
            data: this.data.r2094,
            lineStyle: {
              normal: {
                color: "#5afe8c",
                width: 2,
              },
            },
            itemStyle: {
              normal: {
                color: "#5afe8c",
              },
            },
          },
        ],
      };
      this.initChart(this.$refs.VillageWaterAccumulation, option);
    },
  },
};
</script>
<style lang="less" scoped>
</style>
  • 最终效果
    在这里插入图片描述
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值