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);
}

效果展示:

相关推荐
iDao技术魔方7 小时前
深入Vue 3响应式系统:为什么嵌套对象修改后界面不更新?
javascript·vue.js·ecmascript
念念不忘 必有回响7 小时前
viepress:vue组件展示和源码功能
前端·javascript·vue.js
吹牛不交税17 小时前
admin.net-v2 框架使用笔记-netcore8.0/10.0版
vue.js·.netcore
MZ_ZXD00118 小时前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php
_codemonster20 小时前
Vue的三种使用方式对比
前端·javascript·vue.js
wqq63108551 天前
Python基于Vue的实验室管理系统 django flask pycharm
vue.js·python·django
Deng9452013141 天前
Vue + Flask 前后端分离项目实战:从零搭建一个完整博客系统
前端·vue.js·flask
Hello.Reader1 天前
Flink 文件系统通用配置默认文件系统与连接数限制实战
vue.js·flink·npm
EchoEcho1 天前
深入理解 Vue.js 渲染机制:从声明式到虚拟 DOM 的完整实现
vue.js
C澒1 天前
Vue 项目渐进式迁移 React:组件库接入与跨框架协同技术方案
前端·vue.js·react.js·架构·系统架构