Home > Web Front-end > Vue.js > How Vue encapsulates Echarts charts

How Vue encapsulates Echarts charts

醉折花枝作酒筹
Release: 2021-08-12 18:02:30
forward
1606 people have browsed it

Before we start, we first follow the normal component registration process, create a new component named radar-chart in the project components directory, and then introduce the component to a Demo page for use.

New radar-chart component content:

// radar-chart.vue (子组件)
<template>
    <p style="width: 100%; height: 100%;"></p>
</template>

<script>
export default {
    name: &#39;radar-chart&#39;
};
</script>

<style scoped>

</style>
Copy after login

Demo page code:

// demo.vue (父组件)
<template>
    <p style="border: 1px solid black; width: 400px; height: 300px; margin: 5px;">
        <radar-chart></radar-chart>
    </p>
</template>

<script>
import radarChart from &#39;@/components/proj-components/echarts/radar-chart&#39;;
export default {
    name: &#39;radar-chart-demo&#39;,
    components: {
        radarChart,
    },
};
</script>

<style scoped>

</style>
Copy after login

Demo page rendering 1:

How Vue encapsulates Echarts charts

Initialize the chart

After the preparation work is completed, what we have to do is to introduce ECharts and initialize an ECharts instance in the component. Here you can first copy the instance from the official website and data.
(1) Introduce ECharts in radar-chart.vue:

// radar-chart.vue (子组件)
import echarts from &#39;echarts&#39;;
Copy after login

(2) Create chart configuration data in methods. For data format, please refer to Echarts official website:

// radar-chart.vue (子组件)

    methods: {
        // 初始化图表配置
        initOption() {
            let vm = this;
            vm.option = {
                title: {
                    text: &#39;基础雷达图&#39;
                },
                tooltip: {},
                legend: {
                    data: [&#39;预算分配(Allocated Budget)&#39;, &#39;实际开销(Actual Spending)&#39;]
                },
                radar: {
                    // shape: &#39;circle&#39;,
                    name: {
                        textStyle: {
                            color: &#39;#fff&#39;,
                            backgroundColor: &#39;#999&#39;,
                            borderRadius: 3,
                            padding: [3, 5]
                        }
                    },
                    indicator: [{ name: &#39;销售(sales)&#39;, max: 6500}, { name: &#39;管理(Administration)&#39;, max: 16000}, { name: &#39;信息技术(Information Techology)&#39;, max: 30000}, { name: &#39;客服(Customer Support)&#39;, max: 38000}, { name: &#39;研发(Development)&#39;, max: 52000}, { name: &#39;市场(Marketing)&#39;, max: 25000}]
                },
                series: [{
                    name: &#39;预算 vs 开销(Budget vs spending)&#39;,
                    type: &#39;radar&#39;,
                    // areaStyle: {normal: {}},
                    data: [{value: [4300, 10000, 28000, 35000, 50000, 19000], name: &#39;预算分配(Allocated Budget)&#39;}, {value: [5000, 14000, 28000, 31000, 42000, 21000], name: &#39;实际开销(Actual Spending)&#39;}]
                }]
            };
        },
    },
Copy after login

(3 ) Initialize the chart: In the component hook mounted method:

// radar-chart.vue (子组件)
    mounted() {
        this.initOption();
        this.$nextTick(() => { // 这里的 $nextTick() 方法是为了在下次 DOM 更新循环结束之后执行延迟回调。也就是延迟渲染图表避免一些渲染问题
            this.ready();
        });
    },
Copy after login

In methods:

// radar-chart.vue (子组件)
   ready() {
      let vm = this;
      let dom = document.getElementById(&#39;radar-chart&#39;);

      vm.myChart = echarts.init(dom);
      vm.myChart && vm.myChart.setOption(vm.option);
   },
Copy after login

Demo page rendering 2:

How Vue encapsulates Echarts charts

There are three steps here, including introducing ECharts, initializing the chart configuration, and initializing the chart. Finally, you can see on the Demo page that the radar chart of ECharts has been initially displayed in the project.

Extract chart configuration properties (emphasis)

We have successfully created a radar chart above, but it is obvious that the data in radar-chart.vue is written Dead and cannot be called repeatedly. Next, let’s start with packaging.

The idea of ​​encapsulation is as follows:

1. demo.vue passes a set of personalized data to radar-chart.vue

2. radar-chart.vue passes props option receives data

3. Refine the received data and overwrite the configuration data option

4. Initialize the chart

Specific implementation: Pass data to the sub-component in data Define variables and assign values ​​in mounted

// demo.vue (父组件)
<template>
    <p style="border: 1px solid black; width: 900px; height: 600px; margin: 5px;">
        <radar-chart :indicator="indicator" :legendData="radarData"></radar-chart>
    </p>
</template>

<script>
import radarChart from &#39;@/components/proj-components/echarts/radar-chart&#39;;
export default {
    name: &#39;radar-chart-demo&#39;,
    components: {
        radarChart,
    },
    mounted() {
        this.indicator = [
            { name: &#39;销售&#39;, max: 6500 },
            { name: &#39;管理&#39;, max: 16000 },
            { name: &#39;信息技术&#39;, max: 30000 },
            { name: &#39;客服&#39;, max: 38000 },
        ];
        this.radarData = [
            {
                value: [4000, 15000, 26000, 21000],
                name: &#39;实际开销(Actual Spending)&#39;,
            }
        ];
    },
    data() {
        return {
            indicator: [], // 雷达指示器数据
            legendData: [], // 雷达图例数据
        };
    },
};
</script>

<style scoped>

</style>
Copy after login

Receive data from the parent component in props

// radar-chart.vue (子组件)

    props: {
        // 指示器数据,必传项
        // 格式举例 [{ name: &#39;a&#39;, max: 1},{ name: &#39;a&#39;, max: 1},{ name: &#39;a&#39;, max: 1}]
        indicator: {
            type: Array,
            default: () => []
        },
        // 图例数据,必填项。
        // 格式举例 [{ value: [5000, 14000, 28000], name: &#39;name&#39; },{ value: [5000, 14000, 28000], name: &#39;name&#39; }]
        legendData: {
            type: Array,
            default: () => []
        },
    },
Copy after login

Update chart data option in ready() If you update the indicator and data attributes here value, there is no need to initialize these two values ​​​​in initOption()

// radar-chart.vue (子组件)

    ready() {
       let vm = this;
       let dom = document.getElementById(&#39;radar-chart&#39;);

       vm.myChart = echarts.init(dom);

       // 得到指示器数据
       vm.option.radar.indicator = vm.indicator;
       // 得到图例数据
       vm.option.series[0].data = vm.legendData;

       vm.myChart && vm.myChart.setOption(vm.option);
    },
Copy after login

Demo page rendering picture 3:

How Vue encapsulates Echarts charts

##Detail optimization and other considerations:

1. When a page has multiple charts, chart IDs are automatically generated.

// radar-chart.vue (子组件)
<template>
    <p :id="chartId" style="height: 100%; width: 100%;"></p>
</template>

<script>
let chartIdSeed = 1;

export default {
    data() {
        return {
            chartId: 1,
        };
    },
    mounted() {
        let vm = this;
        vm.chartId = &#39;radar-chart_&#39; + chartIdSeed++;
    },
    methods: {
        let vm = this;
        let dom = document.getElementById(vm.chartId);
    }
};
</script>
Copy after login

2. Chart data attributes are received with props, and the default configuration attributes of the chart are saved with defaultConfig. The configuration attribute chartConfig passed in by the parent component is directly obtained through $attrs, and finally merged into finallyConfig for use, which is conducive to expansion and maintenance.

// radar-chart.vue (子组件)

<script>
export default {
    data() {
        return {
            // 默认配置项。以下配置项可以在父组件 :chartConfig 进行配置,会覆盖这里的默认配置
            defaultConfig: {
                tooltipShow: true
            },
            finallyConfig: {}, // 最后配置项
        };
    },
    mounted() {
        // 在这里合并默认配置与父组件传进来的配置
        vm.finallyConfig = Object.assign({}, vm.defaultConfig, vm.$attrs.chartConfig);
    },
    methods: {
        initOption() {
            vm.option = {
                tooltip: {
                    show: vm.finallyConfig.tooltipShow, // 在这里使用最终配置
                },
            }
        },
    }
};
</script>
Copy after login

3. Use watch to monitor chart data updates

// radar-chart.vue (子组件)
    watch: {
        legendData() {
            this.$nextTick(() => {
                this.ready();
            });
        }
    },
Copy after login

4. Add window resize event and chart click event

// radar-chart.vue (子组件)

export default {
    data() {
        return {
            chartResizeTimer: null, // 定时器,用于resize事件函数节流
        };
    },
    methods: {
        ready() {
            // 添加窗口resize事件
            window.addEventListener(&#39;resize&#39;, vm.handleChartResize);
            
            // 触发父组件的 @chartClick 事件
            vm.myChart.on(&#39;click&#39;, function(param) {
                vm.$emit(&#39;chartClick&#39;, param);
            });
        },
        
        // 处理窗口resize事件
        handleChartResize() {
            let vm = this;
            clearTimeout(vm.chartResizeTimer);
            vm.chartResizeTimer = setTimeout(function() {
                vm.myChart && vm.myChart.resize();
            }, 200);
        },
    },
    beforeDestroy() {
        // 释放该图例资源,较少页面卡顿情况
        if (this.myChart) this.myChart.clear();
        // 移除窗口resize事件
        window.removeEventListener(&#39;resize&#39;, this.handleChartResize);
    }
};
Copy after login
[Related recommendations:《

vue.js Tutorial》】

The above is the detailed content of How Vue encapsulates Echarts charts. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template