溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Vue3?echarts組件化及使用hook進行resize的方法是什么

發(fā)布時間:2023-04-21 16:12:19 來源:億速云 閱讀:119 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細介紹“Vue3 echarts組件化及使用hook進行resize的方法是什么”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當(dāng),希望這篇“Vue3 echarts組件化及使用hook進行resize的方法是什么”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。

    echarts組件化及使用hook進行resize

    hook 本質(zhì)是一個函數(shù),把setup函數(shù)中使用的 Composition API 進行了封裝

    Vue3?echarts組件化及使用hook進行resize的方法是什么

    組件化echarts實例

    <template>
      <div ref="echart" :></div>
    </template>
    
    <script setup>
    import * as echarts from "echarts";
    import useResize from "@/hooks/useResize"; // hook 代碼見下方
    
    const { proxy } = getCurrentInstance(); // 獲取實例中的proxy
    
    let echart;
    let echartInstance;
    
    const props = defineProps({
      // 數(shù)據(jù)
      data: {
        type: Array,
        default: [
          { value: 40, name: "rose 1" },
          { value: 38, name: "rose 2" },
          { value: 32, name: "rose 3" },
        ],
      },
      // 高度
      height: {
        type: [Number, String],
        default: "300px",
      },
      // 寬度
      width: {
        type: [Number, String],
        default: "100%",
      },
    });
    
    const { data } = toRefs(props);
    
    const data1 = reactive({
      option: {
        legend: {
          top: "bottom",
        },
        toolbox: {
          show: false,
          feature: {
            mark: { show: true },
            dataView: { show: true, readOnly: false },
            restore: { show: true },
            saveAsImage: { show: true },
          },
        },
        tooltip: {
          trigger: "item",
          formatter: " : {c} (arnic1c%)",
        },
        series: [
          {
            name: "Nightingale Chart",
            type: "pie",
            radius: [10, 120],
            center: ["50%", "45%"],
            roseType: "area",
            itemStyle: {
              borderRadius: 8,
            },
            data: data.value,
          },
        ],
      },
    });
    
    const { option } = toRefs(data1);
    
    // 觀察 data ,重新繪制
    watch(
      data,
      (newValue) => {
        option.value.series[0].data = newValue;
      },
      { deep: true }
    );
    watch(
      option,
      (newValue) => {
        echartInstance.setOption(newValue, true);
      },
      { deep: true }
    );
    
    onMounted(() => {
      echart = proxy.$refs.echart; // 獲取的DOM根節(jié)點
      echartInstance = echarts.init(echart, "macarons"); // 初始化 echart
      echartInstance.setOption(option.value, true); // 繪制
      // notMerge 可選。是否不跟之前設(shè)置的 option 進行合并。默認(rèn)為 false。即表示合并。合并的規(guī)則,詳見 組件合并模式。如果為 true,表示所有組件都會被刪除,然后根據(jù)新 option 創(chuàng)建所有新組件。
      // setOption 見 https://echarts.apache.org/zh/api.html#echartsInstance.setOption
    });
    
    function resize() {
      echartInstance.resize();
    }
    
    // 暴露函數(shù) (供hook調(diào)用)
    defineExpose({
      resize,
    });
    
    useResize();
    </script>

    hook (useResize)

    export default function () {
        let proxy
    
        onMounted(() => {
            proxy = getCurrentInstance(); // 獲取實例中的proxy
            window.addEventListener('resize', resize)
        })
    
        onBeforeUnmount(() => {
            window.removeEventListener('resize', resize)
        })
    
        function resize() {
            proxy.exposed.resize()
        }
    }

    使用echarts和echarts自適應(yīng)

    首先安裝echarts,這個就不介紹了,直接說怎么使用.

    <!-- 創(chuàng)建一個div去顯示echarts -->
    <div ref="main" ></div>
    import {ref, provide, inject, onMounted, reactive} from "vue";
    import * as echarts from "echarts";
    const main = ref() // 使用ref創(chuàng)建虛擬DOM引用,使用時用main.value
    onMounted(
        () => {
            init()
        }
    )
    function init() {
        // 基于準(zhǔn)備好的dom,初始化echarts實例
        var myChart = echarts.init(main.value);
        // 指定圖表的配置項和數(shù)據(jù)
        var option = {
            /*title: {
                text: 'ECharts 入門示例'
            },*/
            tooltip: {},
            // color:['#779ffe','#a07dfe','#fc9b2e','#63f949','#fb6464','#fce481'],
            /*grid: {
                left: '30%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },*/
            legend: {
                // data: ['國家類型','非國家類型','個人','法人','可公式','非公式']
            },
            xAxis: {
                type: 'category',
                data: ['國家類型','非國家類型','個人','法人','可公式','非公式']
            },
            yAxis: {
                type: 'value',
                scale: true,
                max: 150,
                min: 0,
                splitNumber: 3,
            },
            series: [
                {
                    data: [
                        {
                            value: 120,
                            itemStyle: {
                                color: '#7fa6fe'
                            }
                        },
                        {
                            value: 90,
                            itemStyle: {
                                color: '#a17fff'
                            }
                        },
                        {
                            value: 40,
                            itemStyle: {
                                color: '#fda630'
                            }
                        },
                        {
                            value: 120,
                            itemStyle: {
                                color: '#93fc73'
                            }
                        },
                        {
                            value: 120,
                            itemStyle: {
                                color: '#fb6666'
                            }
                        },
                        {
                            value: 120,
                            itemStyle: {
                                color: '#fbe068'
                            }
                        }
                    ],
                    type: 'bar'
                }
            ]
        };
        // 使用剛指定的配置項和數(shù)據(jù)顯示圖表。
        myChart.setOption(option);
    }

    讀到這里,這篇“Vue3 echarts組件化及使用hook進行resize的方法是什么”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

    向AI問一下細節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI