private void ProcessVertex(Dictionary<HierarchicalGraphVertex, Point> positions, double tickWidth, double maxDepth, HierarchicalGraphVertex vertex) { double miny = double.MaxValue; double maxy = 0; double minx = double.MaxValue; double maxx = 0; int count = 0; foreach (HierarchicalGraphEdge edge in GetEdges(vertex)) { Point p; if (!positions.TryGetValue(edge.GetOtherVertex(vertex), out p)) continue; miny = Math.Min(p.Y, miny); maxy = Math.Max(p.Y, maxy); minx = Math.Min(p.X, minx); maxx = Math.Max(p.X, maxx); count++; } if (count >= Graph.Degree(vertex) - 1) { //double x = originx + (tickWidth * (((offset + vertex.Depth) * 100) / maxDepth)); double x = OriginX + (tickWidth * ((vertex.Depth * 100) / maxDepth)); double y = (maxy + miny) / 2; var vertexLine = new Line {X1 = x, Y1 = miny - 1, X2 = x, Y2 = maxy + 1, Stroke = Brushes.Black, StrokeThickness = 2, DataContext = vertex}; vertexLine.MouseEnter += vertexLine_MouseEnter; vertexLine.MouseLeave += vertexLine_MouseLeave; Children.Add(vertexLine); _clusterVertices[vertex] = vertexLine; foreach (HierarchicalGraphEdge edge in GetEdges(vertex)) { Point p; if (!positions.TryGetValue(edge.GetOtherVertex(vertex), out p)) continue; var edgeLine = new Line {X1 = p.X - 1, Y1 = p.Y, X2 = x + 1, Y2 = p.Y, Stroke = Brushes.Black, StrokeThickness = 2, DataContext = edge, ToolTip = string.Format("{0:p}", edge.Length)}; edgeLine.MouseEnter += edgeLine_MouseEnter; edgeLine.MouseLeave += edgeLine_OnMouseLeave; Children.Add(edgeLine); _edges[edge] = edgeLine; } positions[vertex] = new Point(x, y); } }
private void HighlightSubtree(HierarchicalGraphVertex vertex, bool highlight) { if (vertex.IsCluster) { Line line = _clusterVertices[vertex]; line.Stroke = highlight ? Brushes.Blue : Brushes.Black; } else { Border border = _varietyVertices[vertex]; border.BorderBrush = highlight ? Brushes.Blue : Brushes.Transparent; } foreach (HierarchicalGraphEdge edge in Graph.OutEdges(vertex)) { Line line = _edges[edge]; line.Stroke = highlight ? Brushes.Blue : Brushes.Black; HighlightSubtree(edge.Target, highlight); } }
private IEnumerable<HierarchicalGraphEdge> GetEdges(HierarchicalGraphVertex vertex) { IEnumerable<HierarchicalGraphEdge> inEdges; if (Graph.TryGetInEdges(vertex, out inEdges)) { foreach (HierarchicalGraphEdge edge in inEdges) yield return edge; } IEnumerable<HierarchicalGraphEdge> outEdges; if (Graph.TryGetOutEdges(vertex, out outEdges)) { foreach (HierarchicalGraphEdge edge in outEdges) yield return edge; } }
private static void GenerateHierarchicalVertices(BidirectionalGraph<HierarchicalGraphVertex, HierarchicalGraphEdge> graph, HierarchicalGraphVertex vertex, IUndirectedGraph<Cluster<Variety>, ClusterEdge<Variety>> tree, Cluster<Variety> parent, Cluster<Variety> cluster) { foreach (ClusterEdge<Variety> edge in tree.AdjacentEdges(cluster).Where(e => e.GetOtherVertex(cluster) != parent)) { Cluster<Variety> target = edge.GetOtherVertex(cluster); double depth = vertex.Depth + edge.Length; var newVertex = target.DataObjects.Count == 1 ? new HierarchicalGraphVertex(target.DataObjects.First(), depth) : new HierarchicalGraphVertex(depth); graph.AddVertex(newVertex); graph.AddEdge(new HierarchicalGraphEdge(vertex, newVertex, edge.Length)); GenerateHierarchicalVertices(graph, newVertex, tree, cluster, target); } }
private static void GenerateHierarchicalVertices(BidirectionalGraph<HierarchicalGraphVertex, HierarchicalGraphEdge> graph, HierarchicalGraphVertex vertex, IBidirectionalGraph<Cluster<Variety>, ClusterEdge<Variety>> tree, Cluster<Variety> cluster) { foreach (ClusterEdge<Variety> edge in tree.OutEdges(cluster)) { double depth = vertex.Depth + edge.Length; var newVertex = edge.Target.DataObjects.Count == 1 ? new HierarchicalGraphVertex(edge.Target.DataObjects.First(), depth) : new HierarchicalGraphVertex(depth); graph.AddVertex(newVertex); graph.AddEdge(new HierarchicalGraphEdge(vertex, newVertex, edge.Length)); GenerateHierarchicalVertices(graph, newVertex, tree, edge.Target); } }
private static IBidirectionalGraph<HierarchicalGraphVertex, HierarchicalGraphEdge> BuildHierarchicalGraph(IUndirectedGraph<Cluster<Variety>, ClusterEdge<Variety>> tree) { var graph = new BidirectionalGraph<HierarchicalGraphVertex, HierarchicalGraphEdge>(); var root = new HierarchicalGraphVertex(0); graph.AddVertex(root); GenerateHierarchicalVertices(graph, root, tree, null, tree.GetCenter()); return graph; }