echarts半饼状图实现动态数据

1、实现效果

表格数据都是发请求获取到的动态的
在这里插入图片描述

2、官网示例

在这里插入图片描述

代码

在这里插入图片描述

3、动态实现

我的后台数据格式是这样的

 [
      {
        "value": 88,
        "name": "wahoo"
      },
      {
        "value": 118,
        "name": "CYCPLUS"
      },
      {
        "value": 121,
        "name": "ANT+"
      },
      {
        "value": 12,
        "name": "ThinkRide"
      },
      {
        "value": 15,
        "name": "T600"
      }
    ]

用的是ts,对数据格式已经限制了,用computed 添加空白项并且规定它的样式是实现不了的,,会报错

const brandData = () => {
  let count = 0
  let data = store.cycleBrandData
  data.forEach((item) => {
    count += item.value
  })
  data.push({
    value: count,
    itemStyle: {
      color: 'none',
      decal: {
        symbol: 'none'
      }
    },
    label: {
      show: false
    }
  })
  return data
}

在这里插入图片描述
表格也显示不出来
在这里插入图片描述

尝试

data:
       [
        store.cycleBrandData[0],
        store.cycleBrandData[1],
        store.cycleBrandData[2],
        store.cycleBrandData[3],
        store.cycleBrandData[4],
        {
          value:
            store.cycleBrandData[0].value +
            store.cycleBrandData[1].value +
            store.cycleBrandData[2].value +
            store.cycleBrandData[3].value +
            store.cycleBrandData[4].value,
          itemStyle: {
            color: 'none',
            decal: {
              symbol: 'none'
            }
          },
          label: {
            show: false
          }
        }
      ]

把动态数据都拆解出来然后就可以了

完整代码

onMounted(() => {
  store.getBrand()
  setTimeout(() => {
    drawChart()
  }, 500)
})
const drawChart = () => {
  var myChart = echarts.init(document.getElementById('main'))
  let options = {
    color: ['#3366CC', '#6699FF', '#0033CC', '#3366FF', '#0033FF', '000033'],
    grid: {
      left: '3%', //默认10%
      right: '0', //默认10%
      bottom: '5%', //默认60
      top: '7%',
      containLabel: true
      //grid区域是否包含坐标轴的刻度标签
    },
    series: {
      type: 'pie',
      name: 'brand',
      radius: ['50%', '60%'],
      center: ['90%', '56%'],
      // cener 控制图表的中心位置,第一个值是x方向上,越大越往右,
      // 第二个值控制y方向,越大越往下
      startAngle: -90,
      colorBy: 'data',
      avoidLabelOverlap: true,
      top: 20,
      itemStyle: {
        borderRadius: 10,
        borderWidth: 2
      },
      tooltip: {
        trigger: 'item'
      },
      emphasis: {
        label: {
          show: true,
          fontSize: 23,
          fontWeight: 'bold'
        },
        itemStyle: {
          borderRadius: 10,
          borderWidth: 4
        }
      },
      label: {
        show: true,
        position: 'outside', //默认文本标签的位置,用线引导
        margin: 2,
        formatter: function (data: { name: string; value: string }) {
          return ['{b|' + data.name + '}', '{c|' + data.value + '}' + '{p|人次}'].join('\n\n')
        },
        rich: {
          b: {
            color: '#12d9e2',
            lineHeight: 12,
            fontSize: 16
          },
          c: {
            color: 'green',
            fontSize: 16,
            padding: [2, 5, 2, 5],
            backgroundColor: '#05131d',
            borderColor: '#eee',
            borderWidth: 1,
            fontWeight: 600,
            fontStyle: 'italic'
          },
          p: {
            fontSize: 16,
            color: '#12d9e2'
          }
        }
      },

      labelLine: {
        show: true,
        length: 35,
        length2: 20
      },
      data:
       [
        store.cycleBrandData[0],
        store.cycleBrandData[1],
        store.cycleBrandData[2],
        store.cycleBrandData[3],
        store.cycleBrandData[4],
        {
          value:
            store.cycleBrandData[0].value +
            store.cycleBrandData[1].value +
            store.cycleBrandData[2].value +
            store.cycleBrandData[3].value +
            store.cycleBrandData[4].value,
          itemStyle: {
            color: 'none',
            decal: {
              symbol: 'none'
            }
          },
          label: {
            show: false
          }
        }
      ]
    }
  }
  myChart.setOption(options, { notMerge: true })
}
实现饼状图动态数据的过程分为两步: 1. 初始化echarts实例,并设置饼状图的基本配置。 2. 在Vue组件中通过异步请求数据,将数据传入到echarts实例中,动态更新饼状图的数据。 下面是一个简单的示例代码: ```html <template> <div id="pieChart" style="width: 100%; height: 400px;"></div> </template> <script> import echarts from 'echarts'; export default { data() { return { pieChart: null, pieData: [ { name: 'A', value: 0 }, { name: 'B', value: 0 }, { name: 'C', value: 0 }, ], }; }, mounted() { this.initPieChart(); }, methods: { initPieChart() { // 初始化echarts实例 this.pieChart = echarts.init(document.getElementById('pieChart')); // 设置饼状图基本配置 this.pieChart.setOption({ title: { text: '饼状图示例' }, tooltip: { trigger: 'item' }, legend: { data: ['A', 'B', 'C'] }, series: [ { name: '数据', type: 'pie', radius: '55%', center: ['50%', '60%'], data: this.pieData, itemStyle: { emphasis: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)', }, }, }, ], }); }, async fetchData() { // 异步请求数据 const response = await fetch('http://example.com/data'); const data = await response.json(); // 更新饼状图数据 this.pieData = [ { name: 'A', value: data.a }, { name: 'B', value: data.b }, { name: 'C', value: data.c }, ]; this.pieChart.setOption({ series: [ { data: this.pieData, }, ], }); }, }, }; </script> ``` 在mounted方法中初始化echarts实例,并在initPieChart方法中设置饼状图的基本配置。然后在fetchData方法中异步请求数据,将数据传入到echarts实例中,动态更新饼状图的数据。最后,在模板中渲染一个div元素作为echarts的容器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值