将OrgChart / FamilyTree的第一个和最后一个节点居中

时间:2019-03-04 19:18:00

标签: html css orgchart family-tree

我尝试了很多组织结构图,这是独一无二的,因为它是极少数允许多人父母的图表之一。唯一的问题是,第一个节点与应该位于其下方的节点处于同一级别,并且最后一个节点不在父节点的下方

查看图片

enter image description here

此处的关键是使其自动调整。它是使用HTML-CSS制成的。不是我的。

<html>
<head >
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Test Family Tree</title>
<!-- I found and adapted this css code from: https://stackoverflow.com/questions/38192074/family-tree-css -->
<!-- There is also an example on codepen.io at: http://codepen.io/Pestov/pen/BLpgm -->
<!-- I am not sure who the original creator is -->
 
<style>
* {
margin: 0;
padding: 0;
font-family:sans-serif,Arial;
font-size:10pt;
}
.tree {
white-space: nowrap;
min-width: 800px;
min-height:500px;
}
.tree ul {
padding-top: 20px;
position: relative;
transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
}
.tree li {
float: left;
text-align: center;
list-style-type: none;
position: relative;
padding: 20px 5px 0 5px;
transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
}
/*We will use ::before and ::after to draw the connectors*/
.tree li::before, .tree li::after {
content: '';
position: absolute;
top: 0;
right: 50%;
border-top: 1px solid #ccc;
width: 50%;
height: 20px;
}
.tree li::after {
right: auto;
left: 50%;
border-left: 1px solid #ccc;
}
/*We need to remove left-right connectors from elements without any siblings*/
.tree li:only-child::after, .tree li:only-child::before {
display: none;
}
/*Remove space from the top of single children*/
.tree li:only-child {
content: '';
position: absolute;
top: 0;
right: 50%;
width: 50%;

}
/*Remove left connector from first child and right connector from last child*/
.tree li:first-child::before, .tree li:last-child::after {
border: 0 none;
}
/*Adding back the vertical connector to the last nodes*/
.tree li:last-child::before {
border-right: 1px solid #ccc;
border-radius: 0 5px 0 0;
-webkit-border-radius: 0 5px 0 0;
-moz-border-radius: 0 5px 0 0;
}
.tree li:first-child::after {
border-radius: 5px 0 0 0;
-webkit-border-radius: 5px 0 0 0;
-moz-border-radius: 5px 0 0 0;
}
/*Time to add downward connectors from parents*/
.tree ul ul::before {
content: '';
position: absolute;
top: 0;
left: 50%;
border-left: 1px solid #ccc;
width: 0;
height: 20px;
}
.tree li div {
border: 1px solid #ccc;
padding: 5px 10px;
text-decoration: none;
color: #fff;
font-family: arial, verdana, tahoma;
font-size: 11px;
display: inline-block;
min-width: 80px;
min-height: 30px;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
}
.tree li div .male {
background-color:#94A0B4;
display: inline-block;
width:fit-content;
padding:10px;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
}
.tree li div .spacer {
background-color:lightblue;
display: inline-block;
width:10px;
}
/*Time for some hover effects*/
/*We will apply the hover effect the the lineage of the element also*/
.tree li div:hover, .tree li div:hover + ul li div {
background: #c8e4f8;
color: #fff;
border: 1px solid #94a0b4;
}
/*Connector styles on hover*/
.tree li div:hover + ul li::after,
.tree li div:hover + ul li::before,
.tree li div:hover + ul::before,
.tree li div:hover + ul ul::before {
border-color: #94a0b4;
}
</style>
 
<body>
 
<form id="form1">
 
<div class="tree" id="FamilyTreeDiv">
 
<ul>
 
<li>
<div><span class="male">Alpha
</span></div>
</li>
 
 
<li>
<div><span class="male">Beta
</span><span class="spacer"></span><span class="male">Beta-1
</span></div>
 
 
<ul>
 
<li>
 
<div><span class="male">Gamma
</span><span class="spacer"></span><span class="male">Gamma-1
</span></div>

<li>
<div><span class="male">Delta
</span></div>
</li>

<li>
 
<div><span class="male">Epsilon
</span><span class="spacer"></span><span class="male">Epsilon-1
</span></div>
 
 
<ul>
 
<li>
<div><span class="male">Theta
</span><span class="spacer"></span><span class="male">Theta
</span></div>
 
 
<ul>
 
<li>
<div><span class="male">Kappa
</span></div>
</li>
 
</ul>
</li>
 
 
<li>
<div><span class="male">Iota
</span></div>
</li>
 
</ul>
 
</li>
 
 
<li>
<div><span class="male">Eta
</span></div>
</li>
 
</ul>

感激。

1 个答案:

答案 0 :(得分:0)

BALKANGraph开发人员布道者

运行以下代码,查看如何实现所需的功能

window.onload = function () { 

    var familyGroupTag = {
        group: true,
        template: "group_grey",
        groupState: BALKANGraph.EXPAND
    };


    var chart = new OrgChart(document.getElementById("tree"), {
        scaleInitial: BALKANGraph.match.boundary,
        enableDragDrop: true,
        nodeBinding: {
            field_0: "name",
            field_1: "title"
        },
        tags: {
            f1: familyGroupTag,
            f2: familyGroupTag,
            f3: familyGroupTag,
            f4: familyGroupTag,
            f5: familyGroupTag,
            f6: familyGroupTag,
            f7: familyGroupTag,
            f8: familyGroupTag,
            f9: familyGroupTag
        },
        nodes: [
            { id: 1, tags: ["f1"], name: "Alfa" },
            { id: 2, tags: ["f2"], pid: 1, name: "Beta-1" },
            { id: 3, tags: ["f2"], pid: 1, name: "Beta-1" },
            { id: 4, tags: ["f3"], pid: 2, name: "Gamma" },
            { id: 5, tags: ["f3"], pid: 2, name: "Gamma-1" },
            { id: 6, tags: ["f4"], pid: 2, name: "Delat" },
            { id: 7, tags: ["f5"], pid: 2, name: "Epsilon" },
            { id: 8, tags: ["f5"], pid: 2, name: "Epsilon-1" },
            { id: 9, tags: ["f6"], pid: 2, name: "Eta" },
            { id: 10, tags: ["f7"], pid: 7, name: "Tetha" },
            { id: 11, tags: ["f7"], pid: 7, name: "Tetha-1" },
            { id: 12, tags: ["f8"], pid: 7, name: "lata" },
            { id: 13, tags: ["f9"], pid: 11, name: "Kappa" }
        ]
    });
};
html, body {
    margin: 0px;
    padding: 0px;
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-family: Helvetica;
}

#tree {
    width: 100%;
    height: 100%;
}
<script src="https://balkangraph.com/js/latest/OrgChart.js"></script>

<div id="tree"></div>