1

二次开发 封装 百度echarts 图表 百分比图 公司需求 样式如下图:

image.png

话不多说上代码:

<template>
  <div ref="ringChart" class="ringChart"></div>
</template>

<script>
  import echarts from 'echarts'
  export default {
    name: "Ring",
    props: {
      //颜色
      color: {
        type: String,
        default () {
          return 'red'
        }
      },
      //数据
      data: {
        type: Object,
        default () {
          return {}
        }
      }
    },
    data () {
      return {
      }
    },
    mounted(){
      
      const option = {
        legend: {
            orient: 'vertical',
            left: 'center',
            top: '10%',
            selectedMode:false,
            icon:'none',
            formatter: [
                `{a|${this.data.text}}{b|/${this.data.subtext}}`,
            ].join('\n'),
            textStyle: {
                rich: {
                    a: {
                        color: this.color,
                        textShadowColor:this.color,
                        textShadowBlur:2,
                        fontSize: 14
                    },
                    b: {
                        color: '#DCDCDC',
                        fontSize: 12,
                    },
                }
            }
        },
        title: {
            text: `${parseFloat(this.data.value)}%`,
            textStyle: {
                color: this.color,
                textShadowColor:this.color,
                textShadowBlur:2,
                fontSize: 22
            },
            left: 'center',
            top: '80%'
          },
        angleAxis: {
            max: 100,
            show: false,
        },
        radiusAxis: {
            type: 'category',
            show: true,
            axisLabel: {
                show: false,
            },
            axisLine: {
                show: false,

            },
            axisTick: {
                show: false
            },
        },
        polar: {
            radius: '75%',
            center: ['50%', '50%'],
        },
        series: [{
            type: 'bar',
            // 圆角
            // roundCap: true,
            barWidth: 40,
            showBackground: true,
            backgroundStyle: {
                color: "rgba(219,219,219,0.3)"
            },
            data: [parseFloat(this.data.value)],
            coordinateSystem: 'polar',
            name: `${parseFloat(this.data.value)}`,
            label: {
                show: true,
            },
            itemStyle: {
                normal: {
                    opacity: 1,
                    color: this.color,
                }
            },
        }],
      }
      const chartObj = echarts.init(this.$refs.ringChart);
      chartObj.setOption(option)
    }
  }
</script>
<style lang="scss" scoped>
  .ringChart{
    width: 100%;
    height: 100%;
  }
</style>

调用方法(注册组件我就不上代码了)

<ring color="#289DE7" :data="{value: 83.33, text:'疑似资金', subtext:'总资金'}"/>

传递参数总共是两个:

  • color 颜色
  • data 值

    • value 百分比数值 number类型
    • text 主标题 string类型
    • subtext 副标题 string类型

杭州蘇小小
178 声望12 粉丝

我是一名 WEB前端 开发者不断在前端这条路上努力拼搏着。我喜欢代码的逻辑有序性,每一行代码都有其不同的作用,所有的代码加起来就可以让用户能愉快的体验我的产品。我喜欢研究,遇到难题我不退缩,我相信我可以...