若依框架 --- echarts 封装

简介: 若依框架 --- echarts 封装

页面使用 - 引入注册使用

 <div class="left2-info">
       <BarChart :chartData="BarChartData" height="100%" />
 </div>


柱状图 - BarChart.vue

<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>
<script>
import echarts from "echarts";
// require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";
const animationDuration = 6000;
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "300px",
    },
    chartData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOption(val);
      },
    },
  },
  mounted() {
    console.log(this.chartData);
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOption(this.chartData);
    },
    setOption(data) {
      data.series.forEach((item) => {
        item.type = "bar";
        item.barWidth = 20;
      });
      this.chart.setOption({
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器,坐标轴触发有效
            type: "shadow", // 默认为直线,可选为:'line' | 'shadow'
          },
        },
        color: ["#1adffe", "#fe5656"],
        backgroundColor: "",
        legend: {
          textStyle: {
            color: ["#1adffe", "#fe5656", "#fe5653"], //字体颜色
          },
          data: ["阴性", "阳性"],
        },
        grid: {
          top: "15%",
          left: "1%",
          right: "1%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            // data: ["24小时", "48小时", "72小时", "7日内", "7日以为"],
            data: data.xAxis,
            axisTick: {
              alignWithLabel: false,
              show: false,
            },
            // axisLine: {
            //   show: false,
            // },
            axisLine: {
              lineStyle: {
                color: "#235f74",
                width: 2, //这里是为了突出显示加上的
              },
            },
            splitLine: {
              show: false,
            },
            axisLabel: {
              interval: 0,
              color: "#ffffff",
            },
            splitArea: {
              show: false,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            axisTick: {
              show: false,
            },
            // axisLine: {
            //   show: false,
            // },
            axisLine: {
              lineStyle: {
                color: "#235f74",
                width: 2, //这里是为了突出显示加上的
              },
            },
            splitLine: {
              show: false,
            },
            axisLabel: {
              interval: 0,
              color: "#ffffff",
            },
            splitArea: {
              show: false,
            },
            // boundaryGap: [0, 2],
          },
        ],
        series: data.series,
        animationDuration: 2800,
        // series: [
        //   {
        //     name: "阴性",
        //     type: "bar",
        //     data: [79, 52],
        //   },
        //   {
        //     name: "阳性",
        //     type: "bar",
        //     data: [80, 52],
        //   },
        // ],
      });
    },
  },
};
</script>


用到的相关方法  resize.js

import { debounce } from '@/utils'
export default {
  data() {
    return {
      $_sidebarElm: null,
      $_resizeHandler: null
    }
  },
  mounted() {
    this.initListener()
  },
  activated() {
    if (!this.$_resizeHandler) {
      // avoid duplication init
      this.initListener()
    }
    // when keep-alive chart activated, auto resize
    this.resize()
  },
  beforeDestroy() {
    this.destroyListener()
  },
  deactivated() {
    this.destroyListener()
  },
  methods: {
    // use $_ for mixins properties
    // https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential
    $_sidebarResizeHandler(e) {
      if (e.propertyName === 'width') {
        this.$_resizeHandler()
      }
    },
    initListener() {
      this.$_resizeHandler = debounce(() => {
        this.resize()
      }, 100)
      window.addEventListener('resize', this.$_resizeHandler)
      this.$_sidebarElm = document.getElementsByClassName('sidebar-container')[0]
      this.$_sidebarElm && this.$_sidebarElm.addEventListener('transitionend', this.$_sidebarResizeHandler)
    },
    destroyListener() {
      window.removeEventListener('resize', this.$_resizeHandler)
      this.$_resizeHandler = null
      this.$_sidebarElm && this.$_sidebarElm.removeEventListener('transitionend', this.$_sidebarResizeHandler)
    },
    resize() {
      const { chart } = this
      chart && chart.resize()
    }
  }
}


utils/index.js

/**
 * @param {Function} func
 * @param {number} wait
 * @param {boolean} immediate
 * @return {*}
 */
export function debounce(func, wait, immediate) {
  let timeout, args, context, timestamp, result
  const later = function() {
    // 据上一次触发时间间隔
    const last = +new Date() - timestamp
    // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
    if (last < wait && last > 0) {
      timeout = setTimeout(later, wait - last)
    } else {
      timeout = null
      // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
      if (!immediate) {
        result = func.apply(context, args)
        if (!timeout) context = args = null
      }
    }
  }


目录
相关文章
|
7月前
|
JavaScript
自行封装的tabs组件配合echarts而出现的bug以及解决
自行封装的tabs组件配合echarts而出现的bug以及解决
93 0
|
10月前
|
容器
layui框架实战案例(14):tabs选项卡切换时echarts拉伸变形无法正常显示的解决方案
layui框架实战案例(14):tabs选项卡切换时echarts拉伸变形无法正常显示的解决方案
104 0
这样封装echarts简单好用
这样封装echarts简单好用
|
4月前
|
Java BI 数据库
基于SSM框架实现面向小微企业的简历管理系统企业简历管理系统(分前后台spring+springmvc+mybatis+maven+jsp+css+echarts)
基于SSM框架实现面向小微企业的简历管理系统企业简历管理系统(分前后台spring+springmvc+mybatis+maven+jsp+css+echarts)
|
15天前
|
前端开发 JavaScript 定位技术
Docusaurus框架——react+antd+echarts自定义mdx生成图表代码解释文档
Docusaurus框架——react+antd+echarts自定义mdx生成图表代码解释文档
28 0
|
5月前
|
JavaScript
封装echarts china map geo实现dispatch触发geoSelect事件高亮显示某个省份和城市,并定义复杂样式
封装echarts china map geo实现dispatch触发geoSelect事件高亮显示某个省份和城市,并定义复杂样式
|
5月前
|
前端开发 JavaScript
React中封装echarts图表组件以及自适应窗口变化
React中封装echarts图表组件以及自适应窗口变化
71 1
|
10月前
|
前端开发 数据可视化
漏刻有时数据可视化Echarts组件开发(1):报警状态组件CSS代码及封装函数
漏刻有时数据可视化Echarts组件开发(1):报警状态组件CSS代码及封装函数
40 0
|
10月前
Echarts公用代码的变量统一封装调用
Echarts公用代码的变量统一封装调用
40 0
|
10月前
|
数据管理 PHP 容器
layui框架实战案例(2):LayUI表格与Echarts图表交互展示及PHP后台数据管理(搜索、翻页、动态显示)
layui框架实战案例(2):LayUI表格与Echarts图表交互展示及PHP后台数据管理(搜索、翻页、动态显示)
309 0