<template>
  <div>
    <div class="pie-chart" :id="chart1Id" />
  </div>
</template>

<script>
import { onMounted, toRefs } from 'vue'
import * as Echarts from 'echarts'
//扇形颜色分布
const color = ['#9ec8f2', '#e8ab6d', '#75c4b8', '#a7a8ac']

export default {
  props: ['genderData'],

  setup(props, context) {
    const { genderData } = toRefs(props)
    const chart1Id = 'ranking-chart1'
    let chart

    const updateChart = () => {
      function createOption() {
        return {
          tooltip: {
            trigger: 'item',
            formatter: '{b}：{c}人',
            textStyle: {
              fontSize: 28
            }
          },
          legend: {
            y: 'bottom',
            x: 'center',
            itemWidth: 10,
            itemHeight: 10,
            formatter: function (name) {
              return name
            },
            textStyle: {
              fontSize: 20,
              color: '#333'
            }
          },
          series: [
            {
              top: 0,
              name: '男女比例',
              type: 'pie',
              //饼图的半径。可以为如下类型
              radius: '60%',
              //饼图的扇区是否是顺时针排布。
              selectedMode: 'multiple',
              selectedOffset: 10,
              clockwise: true,
              center: ['50%', '50%'],
              color,
              data: genderData.value,
              // 是否为不规则饼状图
              // roseType: 'radius',
              label: {
                show: true,
                fontSize: 20,
                color: 'rgba(255, 255, 255, 0.3)',
                borderWidth: 0,
                formatter: function (data) {
                  const nameStyle = `color${data.dataIndex}`
                  console.log('nameStyle', nameStyle)
                  //   return `{${nameStyle}|${data.name}}{valueStyle|${data.value}}`
                  return `{${nameStyle}|${data.name}}`
                },
                rich: {
                  //定义不同地方的文字的字体大小和颜色
                  color0: {
                    fontSize: 20,
                    color: '#9ec8f2'
                  },
                  color1: {
                    fontSize: 20,
                    color: '#e8ab6d'
                  },
                  color2: {
                    fontSize: 20,
                    color: '#75c4b8'
                  },
                  color3: {
                    fontSize: 20,
                    color: '#a7a8ac'
                  },
                  valueStyle: {
                    color: '#fff',
                    fontSize: 20,
                    padding: [0, 0, 0, 20]
                  }
                }
              }
            }
          ]
        }
      }
      if (!chart) {
        chart = Echarts.init(document.getElementById(chart1Id))
      }
      chart.setOption(createOption())
    }

    onMounted(() => {
      updateChart()
    })

    return {
      chart1Id
    }
  }
}
</script>

<style lang="scss" scoped>
.pie-chart {
  width: 100%;
  height: 450px;
}

.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

.panel-group {
  margin-top: 18px;

  .card-panel-col {
    margin-bottom: 32px;
  }
  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, 0.05);
    border-color: rgba(0, 0, 0, 0.05);
    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }
    .card-panel-icon {
      float: left;
      font-size: 48px;
    }
    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;
      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }
      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}
</style>
