User.vue 7 KB
<template>
  <div class='user-box'>
    <div class="user-title">
      <div>
        <img src="~@a/man.png">
        男
        <span>{{manPercent}}</span>
      </div>
      <div style="padding-left:130px;">
        <img src="~@a/women.png">
        女
        <span>{{womenPercent}}</span>
      </div>
    </div>
    <div
      class='user-data'
      ref='data'
    >
    </div>
  </div>
</template>

<script type='text/ecmascript-6'>
export default {
  name: 'User',
  data () {
    return {
      labelData: ['00后', '90后', '80后', '70后', '60后'],
      manPercent: '0%',
      womenPercent: '0%'
    }
  },
  props: {
    list: {
      type: Object,
      default: {}
    }
  },
  mounted () {

  },
  watch: {
    'list' (n, o) {
      if (n) {
        let data = n
        let female = n.women.total
        let male = n.man.total
        let womenPercent = (female / (male + female) * 100).toFixed(1)
        let manPercent = 100 - womenPercent
        this.manPercent = `${manPercent}%`
        this.womenPercent = `${womenPercent}%`
        this.createdCanvas(male, female)
      }
    }
  },
  methods: {
    createdCanvas (male, female) {
      let obj = this.list
      let manData = []
      let womenData = []
      obj.man.data.forEach(e => {
        e = (e / male * 100).toFixed(1)
        manData.push(e * -1)
      })
      obj.women.data.forEach(e => {
        e = (e / female * 100).toFixed(1)
        womenData.push(e)
      })
      let canvas = echarts.init(this.$refs.data)
      let options = {
        xAxis: [{
          type: 'value',
          min: -100,
          max: 0,
          gridIndex: 0,
          axisTick: { show: false, inside: false },
          axisLabel: { show: false },
          axisLine: {// Y轴轴线样式
            show: false,
          },
          splitLine: {
            show: false
          }
        },
        {
          gridIndex: 1,
          show: false,
        }, {
          type: 'value',
          gridIndex: 2,
          min: 0,
          max: 100,
          axisTick: { show: false }, //是否显示刻度
          axisLine: { show: false, },
          axisLabel: { show: false },
          splitLine: { show: false }
        }],
        yAxis: [{
          type: 'category',
          gridIndex: 0,
          inverse: true,
          data: this.labelData,
          axisTick: { show: false },
          axisLabel: { show: false },
          axisLine: { show: false }
        },
        {
          gridIndex: 1,
          type: 'category',
          inverse: true,
          position: 'left',
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: '#ffffff',
              fontSize: 26,
            },

          },
          data: this.labelData
        }, {
          type: 'category',
          gridIndex: 2,
          inverse: true,
          data: this.labelData,
          axisTick: { show: false },

          axisLabel: {
            show: false,
            textStyle: {
              fontSize: 20,
              color: '#ffffff',
            }
          },
          axisLine: {
            show: false //是否显示轴线
          }
        }
        ],
        grid: [{
          top: 20,
          bottom: 0,
          width: '180px',
          left: '204px',
          gridIndex: 0,
          containLabel: true,
        },
        {
          show: false,
          left: '54.5%',
          top: 20,
          bottom: 0,
          gridIndex: 1,
          width: '0%',
        },
        {
          width: '180px',
          top: 20,
          bottom: 0,
          right: '204px',
          gridIndex: 2,
          containLabel: true,
        }],
        color: ['#2C91FF', '#FF5526'],
        series: [
          {
            name: 'man',
            type: 'bar',
            barWidth: '10',
            gridIndex: 0,
            itemStyle: {
              normal: {
                show: true,
                color: '#2C91FF',
                barBorderRadius: 4,
              }
            },
            data: manData
          },
          {
            name: 'manBorder',
            type: 'bar',
            barWidth: '10',
            gridIndex: 0,
            label: {
              color: '#ffffff',
              fontSize: 24,
              position: 'left',
              offset: [-36, 0],
              show: true,
              formatter: val => {
                let value = 0
                manData.forEach((e, index) => {
                  if (index == val.dataIndex) value = e
                })
                return `${value * -1}%`
              }
            },
            itemStyle: {
              normal: {
                barBorderRadius: 4,
                color: 'rgba(44, 145, 255, 0.1)' //rgba设置透明度0.14
              }
            },
            barGap: '-100%',
            z: 0,
            data: [-100, -100, -100, -100, -100]
          },
          {
            name: 'women',
            type: 'bar',
            barWidth: '10',
            xAxisIndex: 2,
            yAxisIndex: 2,
            itemStyle: {
              normal: {
                show: true,
                color: '#FF5526',
                barBorderRadius: 4,
                borderWidth: 0,
                borderColor: '#333',
              }
            },
            data: womenData
          },
          {
            name: 'womenBorder',
            type: 'bar',
            barWidth: '10',
            xAxisIndex: 2,
            yAxisIndex: 2,
            label: {
              color: '#ffffff',
              fontSize: 24,
              show: true,
              position: 'right',
              offset: [36, 0],
              formatter: (val, index) => {
                let value = ''
                womenData.forEach((e, index) => {
                  if (index == val.dataIndex) value = e
                })
                return `${value}%`
              }
            },
            itemStyle: {
              normal: {
                barBorderRadius: 4,
                color: 'rgba(23, 105, 152, 0.2)',
              }
            },
            barGap: '-100%',
            z: 0,
            data: [100, 100, 100, 100, 100]
          },
        ]
      }
      canvas.setOption(options)
    }
  }
}
</script>

<style scoped lang='scss'>
.user {
  &-box {
    height: 310px;
    box-sizing: border-box;
    padding-top: 10px;
  }
  &-title {
    font-size: 26px;
    color: #ffffff;
    letter-spacing: 1px;
    display: flex;
    height: 53px;
    line-height: 53px;
    position: relative;
    i {
      position: absolute;
      top: 50%;
      left: 50%;
      width: 2px;
      height: 44px;
      transform: translate(-50%, -50%);
      background: rgba($color: #2d99ba, $alpha: 0.8);
    }
    div {
      flex: 1;
      text-align: left;
      padding-left: 80px;
    }
    img {
      width: 24px;
      height: 50px;
      vertical-align: top;
      margin-right: 15px;
    }
  }
  &-data {
    width: 100%;
    height: calc(100% - 53px);
  }
}
</style>