当前位置: 首页 > article >正文

Echarts 封装通用组件

目录结构

相关文件可以去我的 gitee 下载:https://gitee.com/hao-xiugong/management-vue-ts

在这里插入图片描述

index.ts

import BaseEcharts from "@/components/page-echarts/src/base-echarts.vue";
import LineEcharts from "@/components/page-echarts/src/line-echarts.vue";
import RoseEcharts from "@/components/page-echarts/src/rose-echarts.vue";
import BarEcharts from "@/components/page-echarts/src/bar-echarts.vue";
import MapEcharts from "@/components/page-echarts/src/map-echarts.vue";
import PieEcharts from "@/components/page-echarts/src/pie-echarts.vue";

export default BaseEcharts;
export { PieEcharts, LineEcharts, RoseEcharts, BarEcharts, MapEcharts };

组件封装

base-echarts.vue

<script setup lang="ts">
import * as echarts from "echarts";
import { onMounted, useTemplateRef, watchEffect } from "vue";
import type { EChartsOption } from "echarts";

interface IProps {
  option: EChartsOption;
}

// 注册map
import chinaJSON from "../data/china.json";

echarts.registerMap("china", chinaJSON as any);

const echartsRef = useTemplateRef<HTMLElement>("echartsRef");

const props = defineProps<IProps>();

onMounted(() => {
  const echartsInstance = echarts.init(echartsRef.value!, "light", {
    renderer: "canvas"
  });
  // 第一次进入setOption 监听option变化
  watchEffect(() => echartsInstance.setOption(props.option));
  // 监听window的缩放
  window.addEventListener("resize", () => {
    echartsInstance.resize();
  });
});
</script>

<template>
  <div class="base-echarts">
    <div class="echarts" ref="echartsRef"></div>
  </div>
</template>

<style scoped>
.base-echarts {
  .echarts {
    height: 300px;
  }
}
</style>

然后二次封装,以 map-echarts.vue 为例:

<script setup lang="ts">
import { computed } from 'vue'
import type { EChartsOption } from 'echarts'
import type { IEchartsValueType } from '../types'
import { convertData } from '../utils/convert-data'

interface IMapProps {
  showCitiesGoodsCount: IEchartsValueType[]
}
const props = defineProps<IMapProps>()
const option = computed<EChartsOption>((): any => {
  return {
    title: {},
    tooltip: {
      trigger: 'item',
      formatter: function (params: any) {
        return params.name + ':' + params.value[2]
      }
    },
    legend: {},
    visualMap: {
      min: 0,
      max: 60000,
      left: 20,
      bottom: 20,
      calculabel: true,
      text: ['高', '低'],
      inRange: {
        color: ['rgb(70,240,252)', 'rgb(255,220,46)', 'rgb(245,38,186)']
      },
      textStyle: {
        color: '#fff'
      }
    },
    geo: {
      map: 'china',
      roam: 'scale',
      emphasis: {
        areaColor: '#f4cccc',
        borderColor: 'rgb(9,54,95)',
        itemStyle: {
          areaColor: 'f4cccc'
        }
      }
    },
    series: [
      {
        name: '',
        type: 'scatter',
        coordinateSystem: 'geo',
        map: 'china',
        data: convertData(props.showCitiesGoodsCount),
        symbolSize: 12,
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }
})
</script>
<template>
  <div class="map-echarts">
    <base-echarts :option="option"></base-echarts>
  </div>
</template>
<style scoped lang="less"></style>

在这里插入图片描述


http://www.kler.cn/a/529634.html

相关文章:

  • 9 点结构模块(point.rs)
  • 小程序项目-购物-首页与准备
  • Van-Nav:新年,将自己学习的项目地址统一整理搭建自己的私人导航站,供自己后续查阅使用,做技术的同学应该都有一个自己网站的梦想
  • ASP.NET Core 启动并提供静态文件
  • JavaScript系列(52)--编译优化技术详解
  • android Camera 的进化
  • mysql大表的解决方案,及Hive分页查询
  • 康德哲学与自组织思想的渊源:从《判断力批判》到系统论的桥梁
  • 注解(Annotation)
  • 【深度学习】softmax回归的简洁实现
  • JPA中基本类型集合的映射与操作实例
  • [SAP ABAP] SE11 / SE16N 修改标准表(慎用)
  • java练习(5)
  • GMSL 明星产品之 MAX96724
  • CNN的各种知识点(四): 非极大值抑制(Non-Maximum Suppression, NMS)
  • DeepSeek为什么超越了OpenAI?从“存在主义之问”看AI的觉醒
  • 【Elasticsearch】_all 查询
  • Arduino大师练成手册 -- 控制 AS608 指纹识别模块
  • 低空经济专业课程详解
  • 用deepseek解决python问题——在cmd终端运行python指令弹出应用商店,检查路径已经加入环境变量
  • spacemacs gnuplot
  • 双向链表的快速排序函数
  • 猴子吃桃问题
  • DeepSeek V3 vs R1:大模型技术路径的“瑞士军刀“与“手术刀“进化
  • BUUCTF_[安洵杯 2019]easy_web(preg_match绕过/MD5强碰撞绕过/代码审计)
  • 一文了解DeepSeek