最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue3自定義Echars組件附帶全局配置方式

 更新時(shí)間:2024年03月11日 09:15:58   作者:Circle_Key  
這篇文章主要介紹了Vue3自定義Echars組件附帶全局配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Vue3自定義Echars組件附帶全局配置

<template>
  <div ref="chart"  :id="id"  :class="className" :style="{ height: height, width: width }"></div>
</template>
 
<script setup>
import { ref, reactive, toRefs, onMounted, onBeforeUnmount,watch } from 'vue'
import * as echarts from 'echarts';
 
// 傳遞參數(shù)
const props = defineProps({
  className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '280px'
    },
    options: {
      type: Object,
      default: () => { }
    }
})
const chart = ref(null);
 
 
const createChart = () => {
  const myChart = echarts.init(chart.value);
  
   chart.value = echarts.init(chart.value, 'tdTheme')
  myChart.setOption(props.options,true);
    //  chart.value.setOption(props.options, true)
    window.addEventListener("resize",  () => {
        chart.value.resize()
      })
};
 
watch(() => props.options, (options) => {
  if (chart.value) {
    // 設(shè)置true清空echart緩存
    chart.value.setOption(options, true)
  }
}, { deep: true })
onMounted(() => {
  createChart();
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", () => {
        chart.value.resize()
      })
  chart.value.dispose()
  chart.value = null
})
</script>
import Echart from './components/echart/index.vue'
import echarts from 'echarts'
const app = createApp(App)
 
// 將echarts掛載到全局對(duì)象上
app.config.globalProperties.$echarts = echarts
 
// 注冊(cè)全局組件
app.component('Echart', Echart)

案列

<template>
  <div ref="chart" style="width: 600px; height: 400px;"></div>
</template>
 
<script setup>
import { ref, onMounted } from 'vue';
import * as echarts from 'echarts';
 
const chart = ref(null);
 
const createChart = () => {
  const myChart = echarts.init(chart.value);
  myChart.setOption({
    xAxis: {
      type: 'category',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
      type: 'value'
    },
    series: [{
      data: [120, 200, 150, 80, 70, 110, 130],
      type: 'bar'
    }]
  });
};
 
onMounted(() => {
  createChart();
});
</script>

vue3封裝echarts組件

在開發(fā)項(xiàng)目過程中需要封裝echarts組件,方便多次使用,以下是封裝的詳細(xì)過程。

前置動(dòng)作:安裝echarts以及resize-observer-polyfill插件

  • 新建echarts.ts文件
import * as echarts from "echarts/core";
/**
 * 這里按需引入使用到的圖表類型
 */
import { BarChart, LineChart, type LineSeriesOption, PieChart, type PieSeriesOption } from "echarts/charts";
/**
 * 這里按需引入使用到的配置
 * 特別地 包括MarkPoint markLine等這些也要單獨(dú)使用 不然在圖表中無法顯示
 */
import {
  LegendComponent,
  type LegendComponentOption,
  TitleComponent,
  // 組件類型的定義后綴都為 ComponentOption
  type TitleComponentOption,
  TooltipComponent,
  type TooltipComponentOption,
  GridComponent,
  type GridComponentOption,
  DataZoomComponent,
  type DataZoomComponentOption,
  // 數(shù)據(jù)集組件
  DatasetComponent,
  type DatasetComponentOption,
  // 標(biāo)記組件
  MarkAreaComponent,
  type MarkAreaComponentOption,
  MarkLineComponent,
  type MarkLineComponentOption,
  MarkPointComponent,
  type MarkPointComponentOption,
  // 內(nèi)置數(shù)據(jù)轉(zhuǎn)換器組件 (filter, sort)
  TransformComponent,
} from "echarts/components";

import { LabelLayout, UniversalTransition } from "echarts/features";
import { CanvasRenderer } from "echarts/renderers";

import { ref, shallowRef, onMounted, onBeforeUnmount } from "vue";
import ResizeObserver from "resize-observer-polyfill";
import throttle from "lodash/throttle";

/**
 * 引入的配置通過 ComposeOption 來組合出一個(gè)只有必須組件和圖表的 Option 類型
 */
export type ECOption = echarts.ComposeOption<
  | LineSeriesOption
  | PieSeriesOption
  | LegendComponentOption
  | TitleComponentOption
  | TooltipComponentOption
  | DataZoomComponentOption
  | GridComponentOption
  | DatasetComponentOption
  | MarkAreaComponentOption
  | MarkLineComponentOption
  | MarkPointComponentOption
>;

/**
 * 注冊(cè)按需引入的組件
 */
echarts.use([
  LegendComponent,
  TitleComponent,
  TooltipComponent,
  GridComponent,
  DataZoomComponent,
  DatasetComponent,
  MarkAreaComponent,
  MarkLineComponent,
  MarkPointComponent,
  TransformComponent,
  BarChart,
  LineChart,
  PieChart,
  LabelLayout,
  UniversalTransition,
  CanvasRenderer,
]);

export default function useChart() {
  const canvasEl = shallowRef();
  const myChart = shallowRef();
  /**
   * 監(jiān)聽容器寬度變化或者瀏覽器窗口變化對(duì)echarts圖表進(jìn)行自動(dòng)展示
   * 不用window.addEventListener("resize", resizeFn)的原因是這個(gè)方法只是監(jiān)聽瀏覽器窗口的不變化
   *  -對(duì)所在容器的大小變化不進(jìn)行監(jiān)聽 很難滿足開發(fā)場(chǎng)景的需求
   */
  const resizeObserver = ref<ResizeObserver>();
  const bindResize = () => {
    const deboundResize = throttle(() => {
      myChart.value?.resize();
    }, 500);

    resizeObserver.value = new ResizeObserver(() => {
      deboundResize();
      // myChart.value?.resize();
    });
    resizeObserver.value.observe(canvasEl.value);
  };
  // 解綁 resize
  const unbindResize = () => {
    resizeObserver.value?.unobserve(canvasEl.value);
  };

  onMounted(() => {
    /**
     * 注冊(cè)echarts并開啟監(jiān)聽容器大小變化
     */
    myChart.value = echarts.init(canvasEl.value);
    bindResize();
  });

  onBeforeUnmount(() => {
    /**
     * 銷毀監(jiān)聽和echarts
     */
    unbindResize();
    myChart.value?.dispose();
    myChart.value = null;
  });

  return {
    myChart,
    canvasEl,
  };
}

  • 頁面封裝
<template>
  <div>
    <div class="chart-container" :style="containerStyle">
      <div v-show="dataEmptyFlag" class="chart-empty">
        <span class="empty-title">暫無數(shù)據(jù)</span>
      </div>
      <div ref="canvasEl" :style="containerStyle" />
      <div v-show="loading" class="chart-loading"><Spin /></div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { Spin } from "ant-design-vue";
import { ref, watch } from "vue";
import useChart from "./useECharts";
import type { ECOption } from "./useECharts";

interface ChartProps {
  containerStyle?: any;
  loading?: boolean;
  dataEmptyFlag?: boolean;
  options?: ECOption;
}

const props = withDefaults(defineProps<ChartProps>(), {
  containerStyle: {
    height: "990px",
    width: "100%",
  },
  loading: false,
  dataEmptyFlag: false,
});

const { myChart, canvasEl } = useChart();

watch(
  () => props?.options,
  (cur) => {
    if (myChart) {
      /**
       * 這里繪制圖表前先清空上一次數(shù)據(jù) 否則有時(shí)候會(huì)出現(xiàn)數(shù)據(jù)殘余的情況
       */
      myChart.value.clear();
      myChart.value.setOption(cur);
    }
  },
);
</script>
<style scoped>
.chart-container {
  position: relative;
  width: 100%;
}
.chart-loading {
  align-items: center;
  background-color: #ffffff;
  bottom: 0;
  display: flex;
  justify-content: center;
  left: 0;
  position: absolute;
  right: 0;
  top: 0;
  z-index: 1999;
}
.chart-empty {
  position: absolute;
  z-index: 1;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  color: #888;
  font-size: 14px;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
.empty-title {
  font-size: 16px;
  font-weight: 500;
  color: #000;
}
</style>
  • 組件調(diào)用
/**
 * 參入對(duì)應(yīng)的參數(shù)以及圖表配置項(xiàng)
 */
<anewCharts :loading="loading" :data-empty-flag="dataEmptyFlag" :containerStyle="chartStyle" :options="chartOptions"></anewCharts>

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue3+Uniapp實(shí)現(xiàn)環(huán)境多語言方案完整介紹

    Vue3+Uniapp實(shí)現(xiàn)環(huán)境多語言方案完整介紹

    本文主要介紹了Vue3 Uniapp環(huán)境下的多語言實(shí)現(xiàn)方案,核心思路是通過人為翻譯后將內(nèi)容存儲(chǔ)在 JSON 文件中,當(dāng)用戶選擇語言時(shí)調(diào)用對(duì)應(yīng)文件實(shí)現(xiàn)多語言切換,需要的小伙伴可以了解下
    2025-08-08
  • vite+tsc打包報(bào)TS類型錯(cuò)誤的問題及解決過程

    vite+tsc打包報(bào)TS類型錯(cuò)誤的問題及解決過程

    文章主要介紹了在使用vite+tsc打包Vue3項(xiàng)目時(shí),可能會(huì)遇到TS類型錯(cuò)誤的問題,通過配置tsconfig.json文件,可以解決這個(gè)問題,設(shè)置"checkTypes":?false可以關(guān)閉依賴包的類型檢查,從而避免不必要的錯(cuò)誤提示
    2026-03-03
  • Vue keepAlive頁面強(qiáng)制刷新方式

    Vue keepAlive頁面強(qiáng)制刷新方式

    這篇文章主要介紹了Vue keepAlive頁面強(qiáng)制刷新方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Vue實(shí)現(xiàn)多點(diǎn)涂鴉效果的示例代碼

    Vue實(shí)現(xiàn)多點(diǎn)涂鴉效果的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用Vue實(shí)現(xiàn)多點(diǎn)涂鴉效果,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-03-03
  • Vue實(shí)現(xiàn)DateRange選擇器的禁選功能

    Vue實(shí)現(xiàn)DateRange選擇器的禁選功能

    在基于Vue.js構(gòu)建的應(yīng)用程序中,處理日期選擇器是一個(gè)常見的需求,尤其是在涉及到日期范圍的選擇時(shí),Vue提供了多種方式來實(shí)現(xiàn)日期選擇器的功能,并允許我們對(duì)這些組件進(jìn)行高度定制,本文將深入探討如何在Vue應(yīng)用中實(shí)現(xiàn)DateRange選擇器的禁選功能,需要的朋友可以參考下
    2024-10-10
  • Vue如何引入遠(yuǎn)程JS文件

    Vue如何引入遠(yuǎn)程JS文件

    本篇文章主要介紹了Vue引入遠(yuǎn)程JS文件,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • Vue實(shí)現(xiàn)百度下拉提示搜索功能

    Vue實(shí)現(xiàn)百度下拉提示搜索功能

    這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)百度下拉提示搜索功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Vue.js中使用Vuex實(shí)現(xiàn)組件數(shù)據(jù)共享案例

    Vue.js中使用Vuex實(shí)現(xiàn)組件數(shù)據(jù)共享案例

    這篇文章主要介紹了Vue.js中使用Vuex實(shí)現(xiàn)組件數(shù)據(jù)共享案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Vue3實(shí)現(xiàn)AI流式打字機(jī)的完整解決方案

    Vue3實(shí)現(xiàn)AI流式打字機(jī)的完整解決方案

    本文介紹了基于MessageChannel實(shí)現(xiàn)Vue AI流式對(duì)話的方案,通過SSE流式解析、時(shí)間切片、任務(wù)隊(duì)列等技術(shù),實(shí)現(xiàn)了非阻塞UI渲染和完美處理分包粘包問題,同時(shí)提供了一套可復(fù)用的Hook,并進(jìn)行了詳細(xì)的對(duì)比分析,需要的朋友可以參考下
    2026-04-04
  • 在vue3項(xiàng)目中給頁面添加水印的實(shí)現(xiàn)方法

    在vue3項(xiàng)目中給頁面添加水印的實(shí)現(xiàn)方法

    這篇文章主要給大家介紹一下在vue3項(xiàng)目中添加水印的實(shí)現(xiàn)方法,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,感興趣的小伙伴跟著小編一起來看看吧
    2023-08-08

最新評(píng)論

房山区| 潼南县| 丰城市| 旌德县| 库车县| 从江县| 昔阳县| 古交市| 晴隆县| 靖宇县| 望谟县| 嘉峪关市| 平塘县| 瑞安市| 县级市| 寿光市| 灵丘县| 磐石市| 横峰县| 始兴县| 瓮安县| 长宁县| 丹江口市| 惠东县| 新安县| 枝江市| 水富县| 什邡市| 丰都县| 化州市| 栾川县| 洞头县| 阜新市| 光泽县| 伊金霍洛旗| 镇平县| 宁化县| 哈巴河县| 木里| 岗巴县| 信阳市|