首页 > 其他分享 >vue3 - 封装图表组件

vue3 - 封装图表组件

时间:2022-08-30 10:58:42浏览次数:42  
标签:const color true value vue3 组件 封装 type

 

 

把相同或者类似的图表进行封装

父组件使用:

<Report
        :info="main4"
        :xdata="RXData4"
        :sdata="RSData4"
        :title="title4"
        :color="color4"
        :barBorderRadius="barBorderRadius4"
        class="div"
      ></Report>

  引入组件

import Report from "@/components/Report/index";

  定义变量

const data = reactive({
  RXData4: [],
  RSData4: [],
});
const title4 = ref("重点应用单位");
const color4 = ref(["#9FF6F6", "#00E3E6"]);
const barBorderRadius4 = ref([5, 5, 0, 0]);
const main4 = ref("main4");
const {
  RXData4,
  RSData4,
} = toRefs(data);

  然后自己从接口里面取数据给RXData4和RSData4填充数据

 

子组件:

<template>
  <div :id="info"></div>
</template>

<script setup>
import * as echarts from "echarts";
const props = defineProps({
  info: String, //dom的id
  title: String,
  xdata: {
    type: Array,
    default: [],
  },
  sdata: {
    type: Array,
    default: [],
  },
  barBorderRadius: {
    type: Array,
    default: [],
  },
  color: {
    type: Array,
    default: [],
  },
});

const { info, title, xdata, sdata, barBorderRadius, color } = toRefs(props);

watch(
  sdata,
  (newData, oldData) => {
    nextTick(() => {
      initReport();
    });
  },
  { immediate: true, deep: true }
);
function initReport() {
  const myChart = echarts.init(document.getElementById(info.value));
  const option = {
    title: {
      text: title.value,
      left: "center",
      padding: [40, 10],
      textStyle: {
        color: "#fff",
        fontWeight: "lighter",
        fontSize: 16,
      },
    },
    tooltip: {
      trigger: "axis", //控制鼠标移入是否有提示信息
      axisPointer: {
        type: "line",
      },
    },
    grid: {
      top: "30%",
      left: "5%",
      right: "5%",
      bottom: "6%",
      containLabel: true,
    },
    xAxis: {
      type: "category",
      axisTick: {
        show: true,
      },
      splitLine: {
        lineStyle: {
          color: "#666",
        },
      },
      axisLabel: {
        interval: 0,
        rotate: 25,
      },
      axisLine: {
        show: true,
        lineStyle: {
          color: "#fff",
        },
      },
      data: xdata.value,
    },
    yAxis: {
      type: "value",
      axisTick: {
        show: true,
      },
      splitLine: {
        lineStyle: {
          color: "#666",
        },
      },
      axisLine: {
        show: true,
        lineStyle: {
          color: "#fff",
        },
      },
    },
    series: [
      {
        data: sdata.value,
        type: "bar",
        barWidth: 15,
        itemStyle: {
          normal: {
            // barBorderRadius: [5, 5, 0, 0],
            barBorderRadius: barBorderRadius.value,
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: color.value[0], // 0% 处的颜色
              },
              {
                offset: 1,
                color: color.value[1], // 100% 处的颜色
              },
            ]),
          },
        },
      },
    ],
  };
  myChart.setOption(option);
  window.addEventListener("resize", () => {
    myChart.resize();
  });
}
</script>
<style>
</style>

  

 

ps:需要注意同步异步取数据的问题,我这边用的是在子组件用watch监听数组的方法,也可以在父组件中用v-if来实现,考虑到实现效果的美观度建议用watch和watcheffect

 

标签:const,color,true,value,vue3,组件,封装,type
From: https://www.cnblogs.com/huchong-bk/p/16638527.html

相关文章

  • vue3项目-小兔鲜儿笔记-首页03
    1.面板封装提取首页的公用面板进行复用头部标题和副标题由props传入右侧内容由具名插槽right传入查看更多封装成全局组件主体由默认插槽传入......
  • Spring MVC组件之HandlerAdapter
    SpringMVC组件之HandlerAdapterHandlerAdapter概述HandlerAdapter组件是一个处理器Handler的适配器。HandlerAdapter组件的主要作用是适配特定的Handler来处理相应的请......
  • vue3 基础-事件绑定 & 修饰符
    无非就是js的一些事件,按键,鼠标等的一些绑定在vue的实现而已,很好理解.先来看一个基础例子.事件初体验<!DOCTYPEhtml><htmllang="en"><head><title>事......
  • 类与封装
    packagemainimport"fmt"//如果类名首字母大写,表示其他包也能够访问typeHerostruct{//如果说类的属性首字母大写,表示该属性是对外能够访问的,否则的话只能......
  • APICloud AVM框架 封装车牌号输入键盘组件
    AVM(Application-View-Model)前端组件化开发模式基于标准WebComponents组件化思想,提供包含虚拟DOM和Runtime的编程框架avm.js以及多端统一编译工具,完全兼容WebComponents标......
  • vue3基础入门
    vue3基础入门官方网站:https://v3.vuejs.org/中文文档:https://staging-cn.vuejs.org/guide/introduction.html1、简介1.1、vue是什么?Vue.js(读音/vjuː/,类似于vi......
  • 大家都能看得懂的源码 - 那些关于DOM的常见Hook封装(一)
    本文是深入浅出ahooks源码系列文章的第十四篇,该系列已整理成文档-地址。觉得还不错,给个 star 支持一下哈,Thanks。上一篇我们探讨了ahooks对DOM类Hooks使用规范,......
  • TDesign组件库 小程序组件怎么自定义CSS样式
    不知道有没有人跟我有同样的疑问,看了几遍文档后我懂了Html代码<t-searchclass="searchBox"model:value="{{searchVal}}"placeholder="请输入要查询的商品名称"/......
  • Vue封装的过渡与动画
    一.作用:在插入、更新或移除DOM元素时,在合适的时候给元素添加样式类名。二.图示: 三.写法:1.准备好样式元素进入的样式:v-enter:进入的起点v-enter......
  • vue3+vuex 的 mutations 的 使用
    <template><divclass="app">姓名:{{$store.state.nameVuex}}<button@click="btn">基本方法:修改名字</button><br/><button@click="btn1">传递值......