以图形方式显示员工层次结构,没有顶级

时间:2017-12-20 15:56:53

标签: c# d3.js

典型的员工层次结构看起来像至少有一个根。

+----------+----------+
| Employee | Manager  |
+----------+----------+
| Person A |          |
| Person B | Person A |
| Person C | Person A |
| Person D | Person A |
| Person E | Person A |
| Person F | Person B |
| Person G | Person B |
| Person H | Person B |
| Person I | Person B |
| Person J | Person B |
| Person K | Person B |
+----------+----------+

在graphviz

中看起来像这样

enter image description here

我的问题是我的数据现在已经改变,并且没有根,因为人员A /人B彼此管理:

+----------+----------+
| Employee | Manager  |
+----------+----------+
| Person A | Person B |
| Person B | Person A |
| Person C | Person A |
| Person D | Person A |
| Person E | Person A |
| Person F | Person B |
| Person G | Person B |
| Person H | Person B |
| Person I | Person B |
| Person J | Person B |
| Person K | Person B |
+----------+----------+

enter image description here

虽然我质疑它的有效性,但我想知道如何处理这些数据以图形方式表示它与使用C#/ d3.js的graphviz相似

我认为问题在于我正在构建的模型,下面的代码期望至少有一个根,所以不返回任何内容。如果没有根,我想我需要计算一个深度或权重来大致计算出层次结构顶部的位置,但我不知道从哪里开始。

public static class AssignmentNodeEnumerable
{
    public static IList<AssignmentNode> BuildTree(this IEnumerable<AssignmentNode> source)
    {
        var assignments = source.GroupBy(i => i.SupervisorPersonId);

        var roots = assignments.FirstOrDefault(g => g.Key.HasValue == false).ToList();

        if (roots.Count > 0)
        {
            var dict = assignments.Where(g => g.Key.HasValue).ToDictionary(g => g.Key.Value, g => g.ToList());
            for (int i = 0; i < roots.Count; i++)
                AddChildren(roots[i], dict);
        }

        return roots;
    }

    private static void AddChildren(AssignmentNode node, IDictionary<int, List<AssignmentNode>> source)
    {
        if (source.ContainsKey(node.PersonId))
        {
            node.Children = source[node.PersonId];
            for (int i = 0; i < node.Children.Count; i++)
                AddChildren(node.Children[i], source);
        }
        else
        {
            node.Children = new List<AssignmentNode>();
        }
    }
}

0 个答案:

没有答案