vue echart3个饼图

概览:根据UI设计需要做3个饼图且之间有关联,并且处理后端返回的数据。

参考链接:

echart 官网的一个案例,3个饼图

实现思路:

根据案例,把数据处理成对应的。

参考代码:

1.处理后端数据:

javascript 复制代码
/**
 * 处理接口数据
 * 注意:echart是在渲染的时候就传递数据
 */
const getMetarialCondition = () => {
  api.getMetarialList.post({ shipGuid: data.shipGuid }).then((res) => {
    if (res.data.code == 200) {
      // 返回data是否为空
      if (res.data.data.length !== 0) {
        // //库存总数
        let totalHouseNum = res.data.data?.map((item, index) => {
          return item.num
        }).reduce((preValue, curValue) => {
          return preValue += curValue
        })
        //库存入库
        let putHouseNum = res.data.data?.map((item, index) => {
          return item.putNum
        }).reduce((preValue, curValue) => {
          return preValue += curValue
        })
        //库存出库
        let outHouseNum = res.data.data?.map((item, index) => {
          return item.outNum
        }).reduce((preValue, curValue) => {
          return preValue += curValue
        })
        /**
         * 优化数据
         */
        //库存余量
        let myModifyTotalNum = res.data.data?.map((item, index) => {
          return [item.name, item.num, '库存余量']
        })
        //库存入库
        let myModifyPutNum = res.data.data?.map((item, index) => {
          return [item.name, item.putNum, '库存入库']
        })
        //库存出库
        let myModifyOutNum = res.data.data?.map((item, index) => {
          return [item.name, item.outNum, '库存出库']
        })
        let myModifyData = [...myModifyTotalNum, ...myModifyPutNum, ...myModifyOutNum]
        /**
         * 更新状态
         */
        metarialData.totalHouseNum = totalHouseNum
        metarialData.putHouseNum = putHouseNum
        metarialData.outHouseNum = outHouseNum
        reDrawChartMetarial(myModifyData)
      } else {
        let nullData = []
        reDrawChartMetarial(nullData)
      }

    }
  })
};

2.ecahrt渲染的参数:

javascript 复制代码
/**
 * 渲染echart的方法
 * @param {[]} value 传参
 */
const reDrawChartMetarial = (value) => {
  const mySource = [
    ['name', 'value', 'myTag'],
    ...value
  ]
  let metarialOption = {
    tooltip: {
      trigger: 'item',
      position: 'right',  //提示框浮层的位置
    },
    grid: {
      left: "3%",
      right: "4%",
      bottom: "3%",
      width: "820px",
      height: "320px",
      containLabel: true
    },
    legend: {
      orient: 'horizontal',
      left: 'center',
      textStyle: {
        color: '#000',
        fontSize: fontSize(14),
      },
      formatter: function (a) {
        if (a.length > 5) {
          a = a.slice(0, 5) + "...";   //截断拼接省略号    
        }
        return a;
      }
    },
    dataset: [
      {
        source: mySource,
      },
      {
        transform: {
          type: 'filter',
          config: { dimension: 'myTag', value: '库存余量' }
        }
      },
      {
        transform: {
          type: 'filter',
          config: { dimension: 'myTag', value: '库存入库' }
        }
      },
      {
        transform: {
          type: 'filter',
          config: { dimension: 'myTag', value: '库存出库' }
        }
      }
    ],
    series: [
      {
        type: 'pie',
        radius: 50,
        top: 100,
        left: -100,
        center: ['10%', '50%'],
        datasetIndex: 1,
        textStyle: {
          color: "#000",
          align: "right",
          fontSize: fontSize(16),
        },
        label: {
          formatter: function (a) {
            if (a.name) {
              a = a.name.slice(0, 12) + "...";   //截断拼接省略号    
            }
            return a;
          },
        }
      },
      {
        type: 'pie',
        radius: 50,
        top: 100,
        center: ['50%', '50%'],
        datasetIndex: 2,
        label: {
          normal: {
            formatter: function (a) {
              if (a.name) {
                a = a.name.slice(0, 12) + "...";   //截断拼接省略号    
              }
              return a;
            },
          }
        }
      },
      {
        type: 'pie',
        radius: 50,
        top: 100,
        left: 300,
        center: ['90%', '50%'],
        datasetIndex: 3,
        label: {
          normal: {
            formatter: function (a) {
              if (a.name) {
                a = a.name.slice(0, 5) + "...";   //截断拼接省略号    
              }
              return a;
            },
          }
        }
      }
    ],
    media: [
      {
        query: { minAspectRatio: 1 },
        option: {
          series: [
            { center: ['25%', '50%'] },
            { center: ['50%', '50%'] },
            { center: ['75%', '50%'] }
          ]
        }
      },
      {
        option: {
          series: [
            { center: ['50%', '25%'] },
            { center: ['50%', '50%'] },
            { center: ['50%', '75%'] }
          ]
        }
      }
    ]
  };
  metarialOption && dataEcharts.metaChart.setOption(metarialOption, true);
}

效果展示:

相关推荐
花菜会噎住7 小时前
Vue3核心语法进阶(computed与监听)
前端·javascript·vue.js
I'mxx7 小时前
【vue(2)插槽】
javascript·vue.js
花菜会噎住8 小时前
Vue3核心语法基础
前端·javascript·vue.js·前端框架
练习前端两年半9 小时前
Vue3 源码深度剖析:有状态组件的渲染机制与生命周期实现
前端·vue.js
533_12 小时前
[vue3 echarts] echarts 动态数据更新 setInterval
vue.js·echarts
jstart千语13 小时前
【vue】创建响应式数据ref和reactive的区别
前端·javascript·vue.js
盗德13 小时前
Vue渲染引擎的范式革命:从虚拟DOM到Vapor模式
前端·vue.js
Dolphin_海豚14 小时前
vapor 的 IR 是如何被 generate 到 render 函数的
前端·vue.js·vapor
初遇你时动了情15 小时前
JS中defineProperty/Proxy 数据劫持 vue3/vue2双向绑定实现原理,react 实现原理
javascript·vue.js·react.js