vue自定义组件——饼图例子

文章详细介绍了如何在Vue中创建和使用自定义饼图组件,包括组件结构、数据绑定和注册过程。通过实例展示了如何为不同颜色配置饼图,并在多个位置复用组件。
摘要由CSDN通过智能技术生成

vue自定义组件——饼图例子

问题背景

vue中自定义组件是很正常的场景,有很多场景可能需要用到,本文介绍vue中自定义组件的一般操作,使用一个自定义饼图demo举例。

问题分析

(1)Vue中的自定义组件可以通过创建.vue文件来实现。下面是自定义饼图组件的代码。新增文件,pieChart.vue,代码如下:

<template>
    <div :class="className" :style="{ height: height, width: width }" />
</template>
  
<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme

export default {
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '100px'
        },
        autoResize: {
            type: Boolean,
            default: false
        },
        color: {
            type: String,
            default: '#fff'
        },
    },
    data() {
        return {
            chart: null
        }
    },
    watch: {
        option() {
            if (this.chart) {
                this.chart.setOption(); // 设置对应配置项
            }
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        initChart() {
            this.chart = echarts.init(this.$el, 'macarons')
            this.setOptions()
        },
        setOptions() {
            this.chart.setOption({
                title: {
                    // 设置饼图标题,位置设为顶部居中
                    text: "Ld",
                    top: "-4%",
                    left: "10%",

                },
                series: [
                    {
                        name: 'Ln1',
                        type: 'pie',
                        startAngle: 270,                        // 环图起始位置:正下方
                        center: ['25%', '45%'],                 // 圆环中心相对于容器位置
                        radius: ['40%', '50%'],                 // 圆环内径外径
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: true,
                                position: 'center',
                                formatter: 'Ln'
                            }

                        },

                        data: [{
                            name: 'Ln1占比',
                            value: (90 / 360) * 100,

                            itemStyle: {
                                normal: {
                                    color: this.color
                                }
                            }
                        }, {
                            name: '总数',
                            value: 100 - (90 / 360) * 100,
                            label: {
                                normal: {
                                    show: false,
                                    fontSize: 0
                                }
                            },
                            itemStyle: {
                                normal: {
                                    color: '#02364F'
                                },
                                emphasis: {
                                    color: '#02364F'
                                }
                            },
                            hoverAnimation: false
                        }]
                    },
                    {
                        name: '',
                        type: 'pie',
                        startAngle: 270,                        // 环图起始位置:正下方
                        center: ['25%', '45%'],                 // 圆环中心相对于容器位置
                        radius: ['20%', '30%'],                 // 圆环内径外径
                        avoidLabelOverlap: false,
                        label: {
                            normal: {
                                show: true,
                                position: 'center',
                                formatter: 'Ln'
                            }

                        },

                        data: [{
                            name: 'Ln1占比',
                            value: (40 / 360) * 100,

                            itemStyle: {
                                normal: {
                                    color: '#02364F'
                                },
                                emphasis: {
                                    color: '#02364F'
                                }
                            }
                        }, {
                            name: '总数',
                            value: 100 - (40 / 360) * 100,
                            label: {
                                normal: {
                                    show: false,
                                    fontSize: 0
                                }
                            },
                            itemStyle: {
                                normal: {

                                    color: this.color
                                }
                            },
                            hoverAnimation: false
                        }]
                    },
                ]
            })
        }
    }
}
</script>

(2)然后,我们需要将该自定义组件注册到其他地方使用。比如在App.vue或者其他单文件组件中引入并注册,代码如下:

<template>
  <div class="outer">
    <piecharts width="200px" height='100px' :color="color1" />
    <piecharts width="200px" height='100px' :color="color2" />
    <piecharts width="200px" height='100px' :color="color3" />
    <piecharts width="200px" height='100px' :color="color4" />
    <piecharts width="200px" height='100px' :color="color5" />
    <piecharts width="200px" height='100px' :color="color6" />
  </div>
</template>
 
<script>
import piecharts from './utils/echartsSet/pieEchart.vue';
export default {
  data() {
    return {
      color1: "#8A2BE2",
      color2: "#483D8B",
      color3: "#0000FF",
      color4: "#00FFFF",
      color5: "#7FFF00",
      color6: "#FFD700",
      color7: "#8A2BE2",
    }
  },
  components: {
    piecharts // 局部注册我们引入的自定义组件。
  },
  methods: {
  }
}
</script>

<style>
.outer {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
}
</style>

运行结果如下:
在这里插入图片描述

问题总结

本文介绍了vue中自定义组件的一般操作,使用一个自定义饼图demo举例,有兴趣的同学可以进一步深入学习vue组件相关内容。

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值