动手分装一个饼状图

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

 

文章目录

 


前言

提示:动手分装一个饼状图

 

一、使用步骤

1.引入库

安装echarts依赖

npm install echarts -S
或者使用淘宝的镜像
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install echarts -S



作者:sjj
链接:https://www.jianshu.com/p/cf0a54374419
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

2.新建vue文件

pieChat.vue

<template>

  <div :class="className" :style="{height:height,width:width}" />

</template>



<script>

import 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: '300px'

    },

    data: {

      type: Array,

      default: function() {}

    },

    xdata: {

      type: Array,

      default: function() {}

    },

    legend: {

      type: Array,

      default: function() {}

    },

    percent: {

      type: Array,

      default: function() {}

    }

  },

  data() {

    return {

      chart: null

    }

  },

  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.chart.setOption({

        backgroundColor: '#fff',

        tooltip: {

          trigger: 'item',

          formatter: '{a} <br/>{b} : {c} ({d}%)'

        },

        color: ['#3ba0ff', '#36cbcb', '#4dcb73', '#fad337', '#f2637b'],

        legend: {

          orient: 'vertical',

          right: 'right',

          left: '60%',

          top: '20%',

          icon: 'circle',

          width: '10%',

          data: this.xdata,

          itemGap: 16, // 设置legend的间距

          formatter: (name) => {

            let index = 0

            this.xdata.forEach((value, i) => {

              if (value === name) {

                index = i

              }

            })

            return '{a|' + name + '}' + '|    ' + '{a|' + this.percent[index] + '}' + this.legend[index]

          }

        },

        textStyle: {

          rich: {

            a: {

              width: 70

            }

          }

        },

        series: [

          {

            name: '',

            type: 'pie',

            radius: ['50%', '70%'],

            center: ['30%', '50%'],

            label: {

              formatter: '{per|{d}%}  ',

              rich: {

                a: {

                  color: '#999',

                  lineHeight: 22,

                  align: 'center'

                },

                b: {

                  fontSize: 16,

                  lineHeight: 33

                }

              }

            },

            data: this.data,

            itemStyle: {

              borderWidth: 5, // 设置border的宽度有多大

              borderColor: '#fff'

            },

            emphasis: {

              itemStyle: {

                shadowBlur: 10,

                shadowOffsetX: 0,

                shadowColor: '#fff',

                borderWidth: 0, // 设置border的宽度有多大

                borderColor: '#fff'

              }

            }

          }

        ]

      })

    }

  }

}

</script>

<style scoped>

.line-chart-box{

  /* width: 100%; */

  height: 350px;

}

.chart{

  margin-left: 2.5%;

  margin-bottom: 30px;

}

</style>

3.使用

import PieChart from 'PieChart'

<template>

<div>

<pie-chart :data="dataChart" :xdata="xdata" :legend="legend" :percent="percent"/>

</div>

</template>



data数据

data() {

return {

dataChart: [

        { value: 335, name: '姓名1' },

        { value: 310, name: '姓名2' },

        { value: 234, name: '姓名3'},

        { value: 135, name: '姓名4' },

        { value: 1548, name: '姓名5' }

      ],

xdata: ['姓名1', '姓名2', '姓名3', '姓名4', '姓名5'],

legend: [

        '335', '310', '234', '135', '1548'

      ],

percent: ['13.08%', '12.1%', '9.13%', '5.27%', '60.42%'],

}

}

展示

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shuleijia

您的鼓励是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值