treeChart导图组件

3 篇文章 0 订阅

就是这样的,横线就是名字将就看吧

 

首先vue创建组件:

<template>
    <table v-if="treeData.data">
        <tr>
            <td :colspan="Array.isArray(treeData.children) ? treeData.children.length * 2 : 1" :class="{
                parentLevel: Array.isArray(treeData.children) && treeData.children.length,
                extend: Array.isArray(treeData.children) && treeData.children.length && treeData.extend
            }" >
                <div :class="{ node: true, hasMate: treeData.mate }">
                    <div class="person"  :class="treeData.rankNum==4 ? 'rootNode' :''">
                        <div class="right" @contextmenu="$emit('click-node', treeData)">
                            <div class="name"><span>{{ treeData.data }}</span></div>
                        </div>
                    </div>
                </div>
                <div class="extend_handle" v-if="Array.isArray(treeData.children) && treeData.children.length"
                    @click="toggleExtend(treeData)">
                </div>
            </td>
        </tr>
        <!-- 这是一个递归组件,注意,这里还要调用,需要传递的数据这里也要传递,否则操作时拿不到子级的数据 -->
        <tr v-if="Array.isArray(treeData.children) && treeData.children.length && treeData.extend">
            <td v-for="(children, index) in treeData.children" :key="index" colspan="2" class="childLevel">
                <treeChart :json="children" @click-node="$emit('click-node', $event)" />
            </td>
        </tr>
    </table>
</template>

<script>
export default {
    name: "treeChart",
    props: ["json"],
    data() {
        return {
            treeData: {}
        }
    },
    created(){
    },
    watch: {
        // 遍历当前的数据
        json: {
            handler: function (Props) {
                let extendKey = function (jsonData) {
                    jsonData.extend = (jsonData.extend === void 0 ? true : !!jsonData.extend);
                    if (Array.isArray(jsonData.children)) {
                        jsonData.children.forEach(c => {
                            extendKey(c)
                        })
                    }
                    return jsonData;
                }
                if (Props) {
                    this.treeData = extendKey(Props);
                }
            },
            immediate: true
        }
    },
    methods: {
        toggleExtend: function (treeData) {
            treeData.extend = !treeData.extend;
            this.$forceUpdate();
        },
    }
}
</script>

<style scoped lang="scss">
table {
    margin: 0 auto;
    border-collapse: separate !important;
    border-spacing: 0 !important;
}

td {
    position: relative;
    vertical-align: top;
    padding: 0 0 40px 0;
    text-align: center;
}

.extend_handle:hover:before {
    border-color: #333 #333 transparent transparent;
}

.extend .extend_handle:before {
    transform: rotateZ(-45deg);
}

.extend::after {
    content: "";
    position: absolute;
    left: 50%;
    bottom: 15px;
    height: 20px;
    border-left: 2px solid #ccc;
    transform: translate3d(-1px, 0, 0)
}

.childLevel {
    padding: 0px;
}

.childLevel::before {
    content: "";
    position: absolute;
    left: 50%;
    bottom: 100%;
    height: 15px;
    border-left: 2px solid #ccc;
    transform: translate3d(-1px, 0, 0)
}

.childLevel::after {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    top: -15px;
    border-top: 2px solid #ccc;
}

.childLevel:first-child:before,
.childLevel:last-child:before {
    display: none;
}

.childLevel:first-child:after {
    left: 50%;
    height: 15px;
    border: 2px solid;
    border-color: #ccc transparent transparent #ccc;
    border-radius: 6px 0 0 0;
    transform: translate3d(1px, 0, 0);
}

.childLevel:last-child:after {
    right: 50%;
    height: 15px;
    border: 2px solid;
    border-color: #ccc #ccc transparent transparent;
    border-radius: 0 6px 0 0;
    transform: translate3d(-1px, 0, 0);
}

.childLevel:first-child.childLevel:last-child::after {
    left: auto;
    border-radius: 0;
    border-color: transparent #ccc transparent transparent;
    transform: translate3d(1px, 0, 0)
}

.node {
    position: relative;
    display: inline-block;
    margin: 0 0.5em;
    box-sizing: border-box;
    text-align: center;
}

.node .person {
    position: relative;
    display: inline-block;
    z-index: 2;
    overflow: hidden;
    border-radius: 4px;
    display: flex;
    /* padding: 17px 0px 20px 10px; */
}

.node .person:nth-child(1) {
    border: 1px solid #0084FF;
    background: #0084FF;
}

.node .person .avat {
    display: block;
    width: 4em;
    height: 60px;
    overflow: hidden;
    background: #fff;
    border-radius: 8px;
    border: 1px solid #ccc;
    box-sizing: border-box;
}

.right {
    text-align: center;
    color: #fff;
    width: 100%;
    margin: 0 10px;
}

.node .person .avat img {
    width: 100%;
    height: 60px;
}

.node .person .name {
    height: 2em;
    line-height: 2em;
    overflow: hidden;
    color: #fff;
    letter-spacing: 1px;
    width: 140px;
}

.node.hasMate::after {
    content: "";
    position: absolute;
    left: 2em;
    right: 2em;
    top: 2em;
    border-top: 2px solid #ccc;
    z-index: 1;
}

.rootNode {
    .right {
        margin: 0px;

        .name {
            width: 30px;
            height: 100px;
            line-height: 1.5em;
            display: flex;
            align-items: center;
        }
    }
}
</style>

在需要使用此组件的地方:

<template>
    <div class="flowChart">
        <treeChart :json="data1" :class="{ landscape: '' }" @click-node="clickNode" />
        <div class="gl_prs_ctn" :style='[contextstyle]'>
            <ul class='gl_prs_li'>
                <li>添加</li>
                <li>详情</li>
                <li>编辑</li>
                <li>删除</li>
            </ul>
        </div>
    </div>
</template>

<script>
import treeChart from "@/components/treeChart/treeChart";
export default {
    components: {
        treeChart
    },
    data() {
        return {
            landscape: [],
            data1: {
                data: 'XX:XX',
                children: [
                    {
                        data: 'XXXXX:XX',
                        children: [
                            {
                                data: 'XXXXX',
                                children: [
                                    {
                                        data: 'XXX',
                                        rankNum: ["rootNode"],
                                    },
                                    {
                                        data: 'XXX',
                                        rankNum: ["rootNode"],
                                    },
                                ]
                            },
                            {
                                data: 'XXXXX',
                                children: [
                                    {
                                        data: 'XX',
                                        rankNum: ["rootNode"],
                                    },
                                    {
                                        data: 'XXX',
                                        rankNum: ["rootNode"],
                                    },
                                ]
                            },
                            {
                                data: 'XXXXX',
                                children: [
                                    {
                                        data: 'XXX',
                                        rankNum: ["rootNode"],
                                    },
                                    {
                                        data: 'XX',
                                        rankNum: ["rootNode"],
                                    },
                                ]
                            },
                            {
                                data: 'XXXXX',
                                children: [
                                    {
                                        data: 'XX',
                                        rankNum: ["rootNode"],
                                    },
                                    {
                                        data: 'XX',
                                        rankNum: ["rootNode"],
                                    },
                                    {
                                        data: 'XX',
                                        rankNum: ["rootNode"],
                                    },
                                    {
                                        data: 'XX',
                                        rankNum: ["rootNode"],
                                    },
                                ]
                            },
                        ]
                    },
                ],
            },
            contextstyle: {
                display: 'none',
                right: '0px',
                top: '0px',
                left: '0px',
                bottom: '0px',
            },
        }
    },

    created() {
        document.oncontextmenu = () => { return false }
        document.addEventListener("click", (event) => {
            if (this.contextstyle.display == 'block') {
                this.contextstyle.display = 'none'
            }
        })
        this.getFramework();
    },
    mounted() {

    },
    methods: {
        clickNode(node) {
            if (window.event.x + 188 > document.documentElement.clientWidth) {
                this.contextstyle.left = 'unset';
                this.contextstyle.right = document.documentElement.clientWidth - window.event.x + 'px';
            } else {
                this.contextstyle.left = window.event.x + 'px';
            }
            if (window.event.y + 166 > document.documentElement.clientHeight) {
                this.contextstyle.top = 'unset';
                this.contextstyle.bottom = document.documentElement.clientHeight - window.event.y + 'px';
            } else {
                this.contextstyle.top = window.event.y + 'px';
            }
            this.contextstyle.display = 'block';
        },
    }
}
</script>
<style lang="scss" scoped>
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要实现vue+echarts树状图,可以按照以下步骤操作: 1. 安装echarts:在项目中安装echarts依赖,可以使用npm或yarn命令进行安装。 2. 引入echarts:在组件中引入echarts,并创建一个echarts实例。 3. 获取数据:获取需要展示的树状图数据,可以使用ajax或axios等方式进行获取。 4. 处理数据:将获取到的数据进行递归处理,生成符合echarts树状图要求的数据格式。 5. 配置echarts:设置树状图的布局、样式、节点名称等配置。 6. 渲染树状图:将配置好的echarts实例渲染到DOM中。 以下是一个简单的vue+echarts树状图示例代码: ``` <template> <div ref="treeChart" style="height: 500px;"></div> </template> <script> import * as echarts from 'echarts'; export default { mounted() { this.initTreeChart(); }, methods: { initTreeChart() { const treeChart = echarts.init(this.$refs.treeChart); // 模拟数据 const data = { name: 'root', children: [ { name: 'node1', children: [ { name: 'node1.1' }, { name: 'node1.2' } ] }, { name: 'node2' } ] }; // 处理数据 const option = { series: [ { type: 'tree', data: [data], top: '1%', left: '7%', bottom: '1%', right: '20%', symbolSize: 7, label: { position: 'left', verticalAlign: 'middle', align: 'right' }, leaves: { label: { position: 'right', verticalAlign: 'middle', align: 'left' } }, expandAndCollapse: true, animationDuration: 550, animationDurationUpdate: 750 } ] }; // 渲染树状图 treeChart.setOption(option); } } } </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值