示例#1
0
 private void PaintNodeAndChildren(Graphics g, LayoutGraph graph, Node node)
 {
     PaintNode(g, graph, node);
     INodeLabelLayout[] labels = layoutGraph.GetLabelLayout(node);
     if (labels != null && labels.Length > 0)
     {
         foreach (INodeLabelLayout label in labels)
         {
             PaintNodeLabel(g, layoutGraph, node, label);
         }
     }
     if (grouping.IsGroupNode(node))
     {
         SolidBrush oldBrush = this.nodeFillBrush;
         Color      color    = Color.FromArgb(Math.Min(255, (int)(oldBrush.Color.R * 0.9f)),
                                              Math.Min(255, (int)(oldBrush.Color.G * 0.9f)),
                                              Math.Min(255, (int)(oldBrush.Color.B * 0.9f)));
         this.nodeFillBrush = new SolidBrush(color);
         for (ListCell cell = this.grouping.GetChildren(node).FirstCell; cell != null; cell = cell.Succ())
         {
             Node child = (Node)cell.Info;
             PaintNodeAndChildren(g, layoutGraph, child);
         }
         this.nodeFillBrush.Dispose();
         this.nodeFillBrush = oldBrush;
     }
 }
            public GraphCanvas(LayoutGraph graph)
            {
                this.RenderTransform = new TranslateTransform(_padding, _padding);
                var grouping = new GroupingSupport(graph);

                // Add all edges
                foreach (var edge in graph.Edges)
                {
                    IEdgeLayout el = graph.GetLayout(edge);
                    var         l  = new Polyline();
                    l.Stroke = Brushes.Black;
                    l.Points.Add(new Point(graph.GetSourcePointAbs(edge).X, graph.GetSourcePointAbs(edge).Y));
                    for (int i = 0; i < el.PointCount(); i++)
                    {
                        Point p = new Point(el.GetPoint(i).X, el.GetPoint(i).Y);
                        l.Points.Add(p);
                    }
                    l.Points.Add(new Point(graph.GetTargetPointAbs(edge).X, graph.GetTargetPointAbs(edge).Y));
                    this.Children.Add(l);

                    // edge labels
                    var edgeLabelLayout = graph.GetLabelLayout(edge);
                    foreach (var labelLayout in edgeLabelLayout)
                    {
                        var orientedRectangle = labelLayout.LabelModel.GetLabelPlacement(
                            labelLayout.BoundingBox,
                            graph.GetLayout(edge),
                            graph.GetLayout(edge.Source),
                            graph.GetLayout(edge.Target),
                            labelLayout.ModelParameter);
                        this.Children.Add(GetPolygon(orientedRectangle));
                    }
                }

                // add all nodes
                foreach (var node in graph.Nodes)
                {
                    INodeLayout nl    = graph.GetLayout(node);
                    Color       color = grouping.IsGroupNode(node) ? Color.FromArgb(60, 255, 60, 0) : Color.FromArgb(255, 255, 255, 0);


                    var rect = new Rectangle();
                    this.Children.Add(rect);
                    rect.Stroke = new SolidColorBrush()
                    {
                        Color = Colors.Black
                    };
                    rect.Fill = new SolidColorBrush()
                    {
                        Color = color
                    };
                    rect.Width  = nl.Width;
                    rect.Height = nl.Height;
                    Canvas.SetTop(rect, nl.Y);
                    Canvas.SetLeft(rect, nl.X);

                    // display the node index
                    var text = new TextBlock()
                    {
                        Text = String.Empty + node.Index,
                        HorizontalAlignment = HorizontalAlignment.Center,
                        VerticalAlignment   = VerticalAlignment.Center
                    };

                    this.Children.Add(text);
                    text.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
                    Canvas.SetTop(text, nl.Y + nl.Height / 2 - text.DesiredSize.Height / 2);
                    Canvas.SetLeft(text, nl.X + nl.Width / 2 - text.DesiredSize.Width / 2);
                }
            }