Vue + Echart封装函数实现轮播功能

1、创建LoopShowTooltip.js

import Vue from 'vue'

Vue.mixin({
  methods: {
    // 错误
    $LoopShowTooltip (chart, chartOption, options) {
      let defaultOptions = {
        interval: 2000,
        loopSeries: false,
        seriesIndex: 0,
        updateData: null
      };
  
      if (!chart || !chartOption) {
        return {};
      }
  
      let dataIndex = 0; // 数据索引,初始化为-1,是为了判断是否是第一次执行
      let seriesIndex = 0; // 系列索引
      let timeTicket = 0;
      let seriesLen = chartOption.series.length; // 系列个数
      let dataLen = 0; // 某个系列数据个数
      let chartType; // 系列类型
      let first = true;
  
      // 不循环series时seriesIndex指定显示tooltip的系列,不指定默认为0,指定多个则默认为第一个
      // 循环series时seriesIndex指定循环的series,不指定则从0开始循环所有series,指定单个则相当于不循环,指定多个
      // 要不要添加开始series索引和开始的data索引?
  
      if (options) {
        options.interval = options.interval || defaultOptions.interval;
        options.loopSeries = options.loopSeries || defaultOptions.loopSeries;
        options.seriesIndex = options.seriesIndex || defaultOptions.seriesIndex;
        options.updateData = options.updateData || defaultOptions.updateData;
      } else {
        options = defaultOptions;
      }
  
      // 如果设置的seriesIndex无效,则默认为0
      if (options.seriesIndex < 0 || options.seriesIndex >= seriesLen) {
        seriesIndex = 0;
      } else {
        seriesIndex = options.seriesIndex;
      }
  
      function autoShowTip() {
        function showTip() {
          // 判断是否更新数据
          if (dataIndex === 0 && !first && typeof options.updateData === "function") {
            options.updateData();
            chart.setOption(chartOption);
          }
  
          let series = chartOption.series;
          chartType = series[seriesIndex].type; // 系列类型
          dataLen = series[seriesIndex].data.length; // 某个系列的数据个数
  
          let tipParams = {seriesIndex: seriesIndex};
          switch (chartType) {
            case 'map':
            case 'pie':
            case 'chord':
              tipParams.name = series[seriesIndex].data[dataIndex].name;
              break;
            case 'radar': // 雷达图
              tipParams.seriesIndex = seriesIndex;
              tipParams.dataIndex = dataIndex;
              break;
            default:
              tipParams.dataIndex = dataIndex;
              break;
          }
  
          if (chartType === 'pie' || chartType === 'radar') {
            // 取消之前高亮的图形
            chart.dispatchAction({
              type: 'downplay',
              seriesIndex: options.loopSeries ? (seriesIndex === 0 ? seriesLen - 1 : seriesIndex - 1) : seriesIndex,
              dataIndex: dataIndex === 0 ? dataLen - 1 : dataIndex - 1
            });
  
            // 高亮当前图形
            chart.dispatchAction({
              type: 'highlight',
              seriesIndex: seriesIndex,
              dataIndex: dataIndex
            });
          }
  
          // 显示 tooltip
          tipParams.type = 'showTip';
          chart.dispatchAction(tipParams);
  
          dataIndex = (dataIndex + 1) % dataLen;
          if (options.loopSeries && dataIndex === 0 && !first) { // 数据索引归0表示当前系列数据已经循环完
            seriesIndex = (seriesIndex + 1) % seriesLen;
          }
  
          first = false;
        }
  
        showTip();
        timeTicket = setInterval(showTip, options.interval);
      }
  
      // 关闭轮播
      function stopAutoShow() {
        if (timeTicket) {
          clearInterval(timeTicket);
          timeTicket = 0;
  
          if (chartType === 'pie' || chartType === 'radar') {
            // 取消高亮的图形
            chart.dispatchAction({
              type: 'downplay',
              seriesIndex: options.loopSeries ? (seriesIndex === 0 ? seriesLen - 1 : seriesIndex - 1) : seriesIndex,
              dataIndex: dataIndex === 0 ? dataLen - 1 : dataIndex - 1
            });
          }
        }
      }
  
      let zRender = chart.getZr();
  
      function zRenderMouseMove(param) {
        if (param.event) {
          // 阻止canvas上的鼠标移动事件冒泡
          param.event.cancelBubble = true;
        }
  
        stopAutoShow();
      }
  
      // 离开echarts图时恢复自动轮播
      function zRenderGlobalOut() {
        if (!timeTicket) {
          autoShowTip();
        }
      }
  
      // 鼠标在echarts图上时停止轮播
      chart.on('mousemove', stopAutoShow);
      zRender.on('mousemove', zRenderMouseMove);
      zRender.on('globalout', zRenderGlobalOut);
  
      autoShowTip();
  
      return {
        clearLoop: function () {
          if (timeTicket) {
            clearInterval(timeTicket);
            timeTicket = 0;
          }
  
          chart.off('mousemove', stopAutoShow);
          zRender.off('mousemove', zRenderMouseMove);
          zRender.off('globalout', zRenderGlobalOut);
        }
      };
    }
  }
})

2、在main.js引入函数文件

import './utils/LoopShowTooltip'

3、使用

let myChart = this.$echarts.init(document.getElementById('myChart'));
let option = {
        title: {
                    show: true,
                    text: '总数',
                    itemGap: 6,
                    x: "49%",
                    y: '20%',
                    subtext: pieValue,
                    textAlign: 'center',
                    textStyle: {
                        fontSize: 14,
                        color: '#333333'
                    },
                    subtextStyle: {
                        fontSize: 18,
                        fontWeight: "bold",
                        color: '#333333'
                    },
                },
           ...
    }

option && myChart.setOption(option)
this.$LoopShowTooltip(myChart, option, { loopSeries: true, interval: 4000 })

<script lang="ts" setup> import type { EChartsOption } from 'echarts' import echarts from '@/plugins/echarts' import { debounce } from 'lodash-es' import 'echarts-wordcloud' import { propTypes } from '@/utils/propTypes' import { PropType } from 'vue' import { useAppStore } from '@/store/modules/app' import { isString } from '@/utils/is' import { useDesign } from '@/hooks/web/useDesign' import { cloneDeep } from 'lodash-es' import 'echarts/lib/component/markPoint' import 'echarts/lib/component/markLine' import 'echarts/lib/component/markArea' defineOptions({ name: 'EChart' }) const { getPrefixCls, variables } = useDesign() const prefixCls = getPrefixCls('echart') const appStore = useAppStore() const props = defineProps({ options: { type: Object as PropType<EChartsOption>, required: true }, width: propTypes.oneOfType([Number, String]).def(''), height: propTypes.oneOfType([Number, String]).def('100%'), // 新增:指定图表类型(用于区分配置) chartType: { type: String as PropType<'line' | 'pie' | 'bar' | 'bars'>, required: true }, carousel: { type: Object as PropType<{ enabled: boolean interval?: number seriesIndex?: number }>, default: () => ({ enabled: false }) } }) const isDark = computed(() => appStore.getIsDark) const theme = computed(() => { const echartTheme: boolean | string = unref(isDark) ? true : 'auto' return echartTheme }) const options = computed(() => { const baseOptions = cloneDeep(props.options) return Object.assign(baseOptions, { darkMode: unref(theme) }) }) const elRef = ref<ElRef>() let echartRef: Nullable<echarts.ECharts> = null const contentEl = ref<Element>() // 轮播相关 let carouselTimer: Nullable<NodeJS.Timeout> = null const currentHighlightIndex = ref(0) const styles = computed(() => { const width = isString(props.width) ? props.width : `${props.width}px` const height = isString(props.height) ? props.height : `${props.height}px` return { width, height } }) // 初始化轮播 const initCarousel = () => { if (!props.carousel?.enabled || !echartRef) return clearCarousel() const seriesIndex = props.carousel.seriesIndex ?? 0 const seriesData = (props.options.series?.[seriesIndex]?.data as any[]) || [] if (seriesData.length === 0) return const interval = props.carousel.interval || 3000 carouselTimer = setInterval(() => { currentHighlightIndex.value = (currentHighlightIndex.value + 1) % seriesData.length highlightDataPoint(seriesIndex, currentHighlightIndex.value) }, interval) } // 高亮数据点 const highlightDataPoint = (seriesIndex: number, dataIndex: number) => { if (!echartRef) return echartRef.dispatchAction({ type: 'downplay', seriesIndex: seriesIndex }) echartRef.dispatchAction({ type: 'highlight', seriesIndex: seriesIndex, dataIndex: dataIndex }) echartRef.dispatchAction({ type: 'showTip', seriesIndex: seriesIndex, dataIndex: dataIndex }) } // 清除轮播 const clearCarousel = () => { if (carouselTimer) { clearInterval(carouselTimer) carouselTimer = null } } const initChart = () => { if (unref(elRef) && props.options) { echartRef = echarts.init(unref(elRef) as HTMLElement) echartRef?.setOption(unref(options)) if (props.carousel?.enabled) { initCarousel() } } } // 监听鼠标事件,暂停轮播 const handleMouseEnter = () => { if (props.carousel?.enabled) { clearCarousel() } } const handleMouseLeave = () => { if (props.carousel?.enabled) { initCarousel() } } watch( () => options.value, (options) => { if (echartRef) { // echartRef?.setOption(options) // 图表大小自适应时,option重新赋值 initOption() echartRef?.resize() if (props.carousel?.enabled) { initCarousel() } } }, { deep: true } ) // 监听轮播配置变化 watch( () => props.carousel, (newCarousel) => { clearCarousel() if (newCarousel?.enabled && echartRef) { initCarousel() } }, { deep: true } ) const resizeHandler = debounce(() => { if (echartRef) { // 图表大小自适应时,option重新赋值 initOption() echartRef.resize() } }, 100) // 字体大小计算函数 function fontSize(size: number) { const clientWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth if (!clientWidth) return size const fontSize = clientWidth / 1920 return size * fontSize } const initOption = () => { if (!props.options || !echartRef) return // 深拷贝原始配置,避免修改原对象 let option = cloneDeep(props.options) as EChartsOption // ========== 通用 fontSize 重置逻辑 ========== // 1. Legend 图例 if (option.legend) { const handleLegend = (legend: any) => { if (!legend.textStyle) legend.textStyle = {} legend.textStyle.fontSize = fontSize(16) legend.itemWidth = fontSize(14) legend.itemHeight = fontSize(14) legend.itemGap = fontSize(20) } if (Array.isArray(option.legend)) { option.legend.forEach(handleLegend) } else { handleLegend(option.legend) } } // 2. Tooltip 提示框 if (option.tooltip) { const tooltip = option.tooltip as any if (!tooltip.textStyle) tooltip.textStyle = {} tooltip.textStyle.fontSize = fontSize(16) if (typeof tooltip.padding === 'number') { tooltip.padding = fontSize(12) } tooltip.borderWidth = fontSize(1) } // 3. X轴配置 if (option.xAxis) { const handleXAxis = (axis: any) => { if (!axis.axisLabel) axis.axisLabel = {} axis.axisLabel.fontSize = fontSize(16) if (axis.splitLine?.lineStyle) { axis.splitLine.lineStyle.width = fontSize(1) } } if (Array.isArray(option.xAxis)) { option.xAxis.forEach(handleXAxis) } else { handleXAxis(option.xAxis) } } // 4. Y轴配置 if (option.yAxis) { const handleYAxis = (axis: any) => { if (!axis.axisLabel) axis.axisLabel = {} axis.axisLabel.fontSize = fontSize(16) if (!axis.nameTextStyle) axis.nameTextStyle = {} axis.nameTextStyle.fontSize = fontSize(16) if (axis.splitLine?.lineStyle) { axis.splitLine.lineStyle.width = fontSize(1) } // 横柱状图Y轴特殊配置 if (axis.axisLabel && props.chartType === 'bars') { axis.axisLabel.width = fontSize(80) axis.axisLabel.lineHeight = fontSize(20) } } if (Array.isArray(option.yAxis)) { option.yAxis.forEach(handleYAxis) } else { handleYAxis(option.yAxis) } } // ========== 处理 Series 先统一转为数组再遍历 ========== // 解决 series 可能是单个对象/数组的类型问题 const seriesList = Array.isArray(option.series) ? option.series : option.series ? [option.series] : [] // ========== 根据图表类型针对性重置 ========== switch (props.chartType) { case 'line': // 折线图系列配置 seriesList.forEach((series: any) => { if (series.lineStyle) { series.lineStyle.width = fontSize(4) } if (series.emphasis?.itemStyle) { series.emphasis.itemStyle.borderWidth = fontSize(4) } }) break case 'pie': // 饼图系列配置 seriesList.forEach((series: any) => { if (series.type === 'pie' && series.itemStyle) { series.itemStyle.borderWidth = fontSize(2) } }) break case 'bar': // 竖柱状图自定义渲染配置 seriesList.forEach((series: any) => { if (series.type === 'custom' && series.renderItem) { const originalRenderItem = series.renderItem // 明确类型 renderItem 类型报错 series.renderItem = ((params: any, api: any) => { const renderResult = originalRenderItem(params, api) // 严格判断类型 + 类型断言 if (renderResult && renderResult.type === 'rect') { const rectResult = renderResult as any rectResult.shape = { x: api.coord([api.value(0), 0])[0] - fontSize(20), y: api.coord([api.value(0), api.value(1)])[1], width: fontSize(40), height: api.coord([0, 0])[1] - api.coord([0, api.value(1)])[1] } if (rectResult.style) { rectResult.style.lineWidth = fontSize(1) } } return renderResult }) as typeof originalRenderItem } }) break case 'bars': // 横柱状图系列配置 seriesList.forEach((series: any) => { if (series.type === 'bar') { series.barWidth = fontSize(10) if (series.itemStyle) { series.itemStyle.borderWidth = fontSize(1) } } }) break } // 将处理后的 series 放回 option option.series = seriesList // 应用更新后的配置 echartRef.setOption(option, true) } const contentResizeHandler = async (e: TransitionEvent) => { if (e.propertyName === 'width') { resizeHandler() } } onMounted(() => { initChart() // 添加鼠标事件监听 if (elRef.value) { elRef.value.addEventListener('mouseenter', handleMouseEnter) elRef.value.addEventListener('mouseleave', handleMouseLeave) } window.addEventListener('resize', resizeHandler) contentEl.value = document.getElementsByClassName(`${variables.namespace}-layout-content`)[0] unref(contentEl) && (unref(contentEl) as Element).addEventListener('transitionend', contentResizeHandler) }) onBeforeUnmount(() => { clearCarousel() // 移除鼠标事件监听 if (elRef.value) { elRef.value.removeEventListener('mouseenter', handleMouseEnter) elRef.value.removeEventListener('mouseleave', handleMouseLeave) } window.removeEventListener('resize', resizeHandler) unref(contentEl) && (unref(contentEl) as Element).removeEventListener('transitionend', contentResizeHandler) }) onActivated(() => { if (echartRef) { echartRef.resize() if (props.carousel?.enabled) { initCarousel() } } }) onDeactivated(() => { clearCarousel() }) </script> <template> <div ref="elRef" :class="[$attrs.class, prefixCls]" :style="styles"></div> </template> 现在是这种方法 解决的 但是 缩放后 汇编模糊 字体啥的
最新发布
12-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QQ1447419295

谢谢老弟

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值