private static void CalculateInitialX(TreeNodeModel <T> node) { foreach (var child in node.Children) { CalculateInitialX(child); } // if no children if (node.IsLeaf()) { // if there is a previous sibling in this set, set X to prevous sibling + designated distance if (!node.IsLeftMost()) { node.X = node.GetPreviousSibling().X + nodeSize + siblingDistance; } else { // if this is the first node in a set, set X to 0 node.X = 0; } } // if there is only one child else if (node.Children.Count == 1) { // if this is the first node in a set, set it's X value equal to it's child's X value if (node.IsLeftMost()) { node.X = node.Children[0].X; } else { node.X = node.GetPreviousSibling().X + nodeSize + siblingDistance; node.Mod = node.X - node.Children[0].X; } } else { var leftChild = node.GetLeftMostChild(); var rightChild = node.GetRightMostChild(); var mid = (leftChild.X + rightChild.X) / 2; if (node.IsLeftMost()) { node.X = mid; } else { node.X = node.GetPreviousSibling().X + nodeSize + siblingDistance; node.Mod = node.X - mid; } } if (node.Children.Count > 0 && !node.IsLeftMost()) { // Since subtrees can overlap, check for conflicts and shift tree right if needed CheckForConflicts(node); } }
private void DrawNode(TreeNodeModel <SampleDataModel> node, Graphics g) { // rectangle where node will be positioned var nodeRect = new Rectangle( Convert.ToInt32(NODE_MARGIN_X + (node.X * (NODE_WIDTH + NODE_MARGIN_X))), NODE_MARGIN_Y + (node.Y * (NODE_HEIGHT + NODE_MARGIN_Y)) , NODE_WIDTH, NODE_HEIGHT); // draw box g.DrawRectangle(NODE_PEN, nodeRect); // draw content g.DrawString(node.ToString(), this.Font, Brushes.Black, nodeRect.X + 10, nodeRect.Y + 10); // draw line to parent if (node.Parent != null) { var nodeTopMiddle = new Point(nodeRect.X + (nodeRect.Width / 2), nodeRect.Y); g.DrawLine(NODE_PEN, nodeTopMiddle, new Point(nodeTopMiddle.X, nodeTopMiddle.Y - (NODE_MARGIN_Y / 2))); } // draw line to children if (node.Children.Count > 0) { var nodeBottomMiddle = new Point(nodeRect.X + (nodeRect.Width / 2), nodeRect.Y + nodeRect.Height); g.DrawLine(NODE_PEN, nodeBottomMiddle, new Point(nodeBottomMiddle.X, nodeBottomMiddle.Y + (NODE_MARGIN_Y / 2))); // draw line over children if (node.Children.Count > 1) { var childrenLineStart = new Point( Convert.ToInt32(NODE_MARGIN_X + (node.GetRightMostChild().X *(NODE_WIDTH + NODE_MARGIN_X)) + (NODE_WIDTH / 2)), nodeBottomMiddle.Y + (NODE_MARGIN_Y / 2)); var childrenLineEnd = new Point( Convert.ToInt32(NODE_MARGIN_X + (node.GetLeftMostChild().X *(NODE_WIDTH + NODE_MARGIN_X)) + (NODE_WIDTH / 2)), nodeBottomMiddle.Y + (NODE_MARGIN_Y / 2)); g.DrawLine(NODE_PEN, childrenLineStart, childrenLineEnd); } } foreach (var item in node.Children) { DrawNode(item, g); } }