eachrts 极坐标图计算百分比

文章描述了如何使用Vue.js和ECharts库在网页上创建一个动态的区县项目数量占比饼图,通过监听时间范围变化实时更新数据和图表大小。
摘要由CSDN通过智能技术生成

在这里插入图片描述

<template>
  <!--  各区县项目数量占比-->
  <div class="charts-box">
    <div class="charts-instance" ref="chartRef">

    </div>
    <div class="charts-note">
      <span v-for="(items, index) in data.list" class="charts-legend">
        <span class="legend" :style="{background:colorList[index]}"></span>
        <span>{{items.name}}</span>
        <span style="color: #999999;margin-left: 5px;">{{((items.value / props.total)*100).toFixed(2)}}%</span>
      </span>
    </div>
  </div>
</template>
<script setup>
import {ref, onMounted, onUnmounted, reactive, watch} from 'vue'
import * as echarts from 'echarts'
import useEmitter from "@/hooks/useEmitter";
import {findRightTopPie} from "@/views/BangFuData/service";

let props = defineProps(['timeRange', 'dictItem', 'total'])
const emitter = useEmitter()
const chartRef = ref()
const colorList = ['#F0685E', '#F9A43A', '#7ACDFE', '#5085FF', '#44CEA4', '#A18EEA', '#FCEB5D', '#299A99', '#80C269', '#556FB5']
const data = reactive({
  name: [],
  value: [],
  list:  [
        {
            "name": "**市",
            "value": "30",
            "count": null,
            "type": null
        },
        {
            "name": "***区",
            "value": "58",
            "count": null,
            "type": null
        },
        {
            "name": "**县",
            "value": "80",
            "count": null,
            "type": null
        },
        {
            "name": "**市",
            "value": "27",
            "count": null,
            "type": null
        },
        {
            "name": "**县",
            "value": "52",
            "count": null,
            "type": null
        },
        {
            "name": "**县",
            "value": "17",
            "count": null,
            "type": null
        },
        {
            "name": "**县",
            "value": "17",
            "count": null,
            "type": null
        },
        {
            "name": "***县",
            "value": "22",
            "count": null,
            "type": null
        },
        {
            "name": "**县",
            "value": "56",
            "count": null,
            "type": null
        },
        {
            "name": "百**委",
            "value": "8",
            "count": null,
            "type": null
        }
    ],
})

const findData = (callback) => {
  let rightTopPieRes = findRightTopPie(props.timeRange === 'previous' ? 2 : 1)
  rightTopPieRes.then(res => {
    let name = []
    let value = []
    res.find(item => {
      name.push(item.name)
      // value.push(item.value)
      value.push(((item.value / props.total)*100).toFixed(2))
    })
    data.name = name;
    data.value = value;
    data.list = res;
    callback()
  })
}

let myChart = null
const option = ref(null)
const buildOption = () => {
  option.value = {
    polar: {
      radius: [30, '80%'],
    },
    radiusAxis: {
      max: (parseInt((Math.max(...data.value)+'').slice(0,1))+1)+'0',
      axisLabel: {
        show: true, // 取消刻度
        formatter: function (value) {
          return (value)+'%';
        }
      },
      axisTick: {
        show: false
      },
      axisLine: {
        show: false,
      },
      splitLine: {
        lineStyle: {
          color: '#dddddd',
        },
      },
    },
    angleAxis: {
      type: 'category',
      data: data.name,
      startAngle: 90,
      axisTick: {
        show: false
      },
      axisLabel: {
        textStyle: {
          color: 'transparent'
        }
      },
      axisLine:{
        lineStyle:{
          color:'#dddddd'
        }
      }
    },
    tooltip: {
      show: false
    },
    series: {
      type: 'bar',
      data: data.value,
      coordinateSystem: 'polar',
      itemStyle: {
        normal: {
          // 定制显示(按顺序)
          color: function (params) {
            var colorLists = colorList
            return colorLists[params.dataIndex]
          }
        }
      },
      // label: {
      //   show: true,
      //   position: 'middle',
      //   formatter: '{b}: {c}'
      // }
    },
    animation: false
  }
}

watch(() => props.timeRange, (nv, ov) => {
  findData(()=>buildOption())
})
watch(()=>option.value,(newOption,oldOption)=>{
  if(myChart == null) {
    myChart = echarts.init(chartRef.value);
  }
  option.value && myChart && myChart.setOption(newOption)
})
onMounted(()=>{
  myChart = echarts.init(chartRef.value);
  findData(()=>buildOption());
  emitter.on('echartsTimeRangeChange',()=>setTimeout(() => myChart && myChart.resize({width:chartRef.value.clientWidth,height:chartRef.value.clientHeight}), 50))
  emitter.on('echartsUpdate',()=>myChart && myChart.resize({width:chartRef.value.clientWidth,height:chartRef.value.clientHeight}))
})
onUnmounted(()=>{
  emitter.off('echartsTimeRangeChange')
  emitter.off('echartsUpdate')
  myChart.dispose()
})
</script>
<style scoped>
.charts-box{
  height: calc(100% - 48px);
}
.charts-instance{
  height: 60%;
  width: 100%;
  overflow: hidden;
}
.charts-note{
  margin-left: 15%;
  height: 40%;
  overflow: hidden;
}
.charts-legend {
  display: inline-block;
  vertical-align: middle;
  margin-top: 15px;
  width: 50%;
  font-size: 12px;
}
.legend {
  display: inline-block;
  vertical-align: middle;
  margin-right:5px;
  width: 12px;
  height: 12px;
  border-radius: 2px;
}
</style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值