符合Echarts数据结构的数组对象转换第二版

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>test</title>
</head>
<body>
	<div id="chart" style="width:600px;height:400px;"></div>
	<script src="https://cdn.jsdelivr.net/npm/echarts@5.4.0/dist/echarts.js"></script>
	<script type="text/javascript">

	let data = [
    {
      "classNum": "夜班",
      "userCountList": [
        {
          "userName": "hulubing",
          "sum": 11
        },
        {
          "userName": "admin123",
          "sum": 4
        },
        {
          "userName": "linn_test",
          "sum": 1
        },
        {
          "userName": "测试",
          "sum": 1
        }
      ]
    },
    {
      "classNum": "晚班",
      "userCountList": [
        {
          "userName": "hulubing",
          "sum": 2
        },
        {
          "userName": "admin123",
          "sum": 2
        }
      ]
    },
    {
      "classNum": "白班",
      "userCountList": [
        {
          "userName": "hulubing",
          "sum": 1
        },
        {
          "userName": "admin123",
          "sum": 1
        }
      ]
    }
  ]

  console.log(data)
	

	for(let item of data){
		let tmp = {}
		// console.log(item)
		console.log(item.userCountList)
		for(let k of item.userCountList){
			// console.log(k)
			tmp[k.userName] = k.sum // 数组转换对象
			item.list = tmp   // 创建新节点存储对象
      // console.log(tmp)
		}
    console.log(tmp)
	}


	let classNums = []
	// 创建一个新数组存储新的数据结构
	let newData = data.map(v => {
    console.log(v)
		
		classNums.push(v.classNum)
		return {
			name: v.classNum,
			type: 'bar',
      barWidth: "20%", // 柱状宽度
			data: Object.values(v.list),
			values: v.userCountList.map(v=>v.userName),
			barCategoryGap:'120px',
          label: {
              show: true,
              position: 'top'
          },
		}
	})


	// console.log(JSON.stringify(newData))

  // 班次
	// console.log(classNums)

  // 姓名
	let classValUsers = newData.map(v=>v.values)
	// console.log(classValUsers)
		


	function initCharts () {
      let myChart = echarts.init(document.getElementById('chart'),'light');

      // 绘制图表
      myChart.setOption({
        color: ['#00aaff', '#00FFA6', '#F4DF58'],
          tooltip: {
            confine: true,
          },
        legend: {
            right: "25%",
            top:"4%",
            data: classNums,
            itemGap: 20,
            itemWidth: 20,
            itemHeight: 10,
            textStyle: {
              color: "#000",
              fontSize: 14,
            },
          },
        grid: {
                left: '10%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
          //工具
          toolbox: {
            //是否显示
            show: true,
            right: "4%",
            top:"2%",
            feature: {
              //折线图 与 柱状图 切换
              magicType: {
                type: ['line', 'bar']
              },
              //下载
              saveAsImage: {}
            }
          },
          tooltip: {
                trigger: "axis",
                backgroundColor: "rgba(33,56,77,1)",
                borderColor: "rgba(33,56,77,1)",
                textStyle: {
                  color: "#fff",
                  fontSize: 14,
                },
                axisPointer: {
                  lineStyle: {
                    color: "rgba(0,133,255,0)",
                  },
                },
              },
          xAxis: {
            type: 'category',
              data: classValUsers[0],
              axisLabel: {
                //坐标轴刻度标签的相关设置
                margin: 15, //刻度标签与轴线之间的距离
              },
          },
          yAxis: [
            {
              type: "value",
              name:"单位(天)",
              namestyle:{
                color:"#000000",

              },
              boundaryGap: ['0', '20%'],
              axisLabel: {
                textStyle: {
                  color: "#000000",
                  fontSize: 14,
                },
              },
              axisTick: { show: true },
              axisLine: { show: true },
              splitLine: { show: true },
            }
          ],
          series: newData
      })
    }

    initCharts()



	</script>
</body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值