echart树状图图表

根据后端返回的树状结构,渲染echart树状图,并且默认二级节点后是折叠的
在这里插入图片描述

父级引入组件

    .tree_chart {
            width: 100%;
            height: calc(100% - 30px);
        }

   <div class="tree_chart">
      <EachartTree :treeData="allData.treeData" />
    </div>

树状图子组件

<template>
    <div class="echart_box">
        <div id="treeEchart"></div>
    </div>
</template>

<script>
import * as echarts from "echarts"
export default {
    props: {
        treeData: {
            type: Array,
            default: () => []
        },
    },
    data () {
        return {
            treeEchart: null,
            treeDataLsit: []
        }
    },
    watch: {
        treeData (newVal, oldVal) {
            if (newVal) {
                this.treeDataLsit = newVal
                this.treeEchart =null
                this.getBarData()
            }
        },

    },
    mounted () {
        this.$nextTick(() => {
            this.getBarData()
        })

        window.addEventListener('resize', () => {
            this.treeEchart.resize()
        });
    },

    methods: {
        getBarData () {
            let option = {
                tooltip: {
                    trigger: 'item',
                    triggerOn: 'mousemove',
                    color: '#fff',
                    backgroundColor: 'skyblue',
                    borderColor: 'skyblue',
                },
                grid: {
                    top: 0,
                    left: 0,
                    right: 0,
                    bottom: 0,
                    containLabel: true
                },
         
                animationDurationUpdate: 1500,
                animationEasingUpdate: 'quinticInOut',
                series: [
                    {
                        type: 'tree',
                        symbolSize: [140, 50],
                        symbol: 'rect',
                        edgeShape: 'polyline',
                        edgeForkPosition: '50%',
                        edgeSymbol: ['circle', 'arrow'],
                        edgeSymbolSize: [4, 10],
                        edgeLabel: {
                            normal: {
                                textStyle: {
                                    fontSize: 18,
                                },
                            },
                        },
                        initialTreeDepth: 2,
                        orient: 'vertical',

                        itemStyle: {
                            color: '#FFC125',
                            borderColor: '#FFC125',
                        },

                        expandAndCollapse: true,
                        animationDuration: 550,
                        animationDurationUpdate: 750,
                        //线条的颜色
                        lineStyle: {
                            normal: {
                                opacity: 0.9,
                                color: '#53B5EA',
                                type: 'dashed',
                                width: 1,
                            },
                        },

                        label: {
                            position: 'inside',
                            fontSize: 12,
                            color: '#fff',
                            formatter: function (params) {
                                return [
                                    `${params.data.name}`,
                                    `10 人`
                                ].join('\n');
                            },


                        },
                        top: 50, // 设置节点顶部和容器顶部的距离
                        left: 0, // 设置节点左侧和容器左侧的距离
                        bottom: 80, // 设置节点底部和容器底部的距离
                        right: 0, // 设置节点右侧和容器右侧的距离

                        // leaves: {
                        //     label: {
                        //         position: 'inside',
                        //         color: '#fff',
                        //     },
                        //     itemStyle: {
                        //         color: '#dfdfdf',
                        //         borderColor: '#dfdfdf',
                        //     },
                        // },
                        level: [  // 设置不同层级的节点间距
                            { nodeSpacing: 50 },  // 第一层节点间距为50
                            { nodeSpacing: 150 },  // 第二层节点间距为30
                            { nodeSpacing: 150 },  // 第三层节点间距为20
                        ],

                        data: this.treeDataLsit
                    },
                ],
            }

            this.treeEchart = echarts.init(document.getElementById("treeEchart"))
            this.treeEchart.setOption(option)

        },
    },
}
</script>

<style lang="scss" scoped>
.echart_box {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    position: relative
}

#treeEchart {
    width: 100%;
    height: 100%;
}
</style>

数据转换、重构

   /* 转换树形结构 */
        transformData (originalData, isParent = false) {

            return originalData.map((node, index) => {
                let obj = {
                    name: node.deptName,
                    itemStyle: {
                        color: isParent ? '#13bc4c' : '#4095e5',
                        borderColor: isParent ? '#13bc4c' : '#4095e5',
                    },
                }
                if (node.child) {
                    obj.children = this.transformData(node.child)
                }
                return obj;
            });
        },
 //获取到树状结构数据
    getOrganizationData(params).then((res) => {
                let arr = [res.data.data]

                this.allData.treeData = this.transformData(arr, true);
                this.allData.treeData.map((item) => {
                    if (item.children) {
                        item.children.map((item1) => {
                            item1.collapsed = true; //这是设置图表折叠的属性
                        })
                    }
                })


            })
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值