Пример #1
0
        /// <summary>
        /// Desenha o grafo atual.
        /// </summary>
        private void DrawGraph(EDA.Graph graphToDraw, EDA.Node[] highlightedNodes)
        {
            List <EDA.Edge> edges = new List <EDA.Edge>();

            Glee.Graph drawingGraph = new Glee.Graph("Grafo - EDA2");
            // Adiciona nós ao grafo..
            foreach (EDA.Node node in graphToDraw.Nodes)
            {
                Glee.Node drawingNode = drawingGraph.AddNode(node.Name);
                drawingNode.Attr.Shape = Glee.Shape.Circle;
                if (highlightedNodes != null && Array.IndexOf(highlightedNodes, node) >= 0)
                {
                    drawingNode.Attr.Color = Glee.Color.Red;
                }
                // Consolida os arcos..
                edges.AddRange(node.Edges);
            }
            foreach (EDA.Edge edge in edges)
            {
                Glee.Edge drawingEdge = drawingGraph.AddEdge(edge.From.Name, edge.To.Name);
                drawingEdge.Attr.Label = String.Format("{0:F4}", edge.Cost);
            }
            // Gera controle de desenho..
            GleeUI.GViewer viewer = new GleeUI.GViewer();
            viewer.NavigationVisible = false;
            viewer.OutsideAreaBrush  = Brushes.White;
            viewer.RemoveToolbar();
            viewer.Graph = drawingGraph;
            viewer.Dock  = System.Windows.Forms.DockStyle.Fill;
            pnlGraph.Controls.Clear();
            pnlGraph.Controls.Add(viewer);
        }
Пример #2
0
        private void BuildGraph()
        {
            // The only way I can see to do this is to create another graph, and
            // copy what we want to the new graph, which is slow. Grim, we need
            // to rebuild addedNodes and edges as well.

            // Considering options, it's easier to do this each time anyway,
            // and don't build the initial graph at all. TODO, split this.

            GD.Graph newGraph = CreateGraph();
            Dictionary <string, object>         newAddedNodes = new Dictionary <string, object>();
            Dictionary <string, List <object> > newAddedEdges = new Dictionary <string, List <object> >();

            newGraph.GraphAttr.AspectRatio = _options.AspectRatio;
            newGraph.GraphAttr.NodeSep     = _options.NodeSep;

            foreach (KeyValuePair <string, object> pair in _addedNodes)
            {
                GD.Node node = pair.Value as GD.Node;

                if (!_options.ShowOrphans && !_notOrphans.ContainsKey(pair.Key))
                {
                    continue;
                }

                GD.Node newNode = newGraph.AddNode(node.Id);
                newNode.Attr             = node.Attr.Clone();
                newNode.Attr.LabelMargin = _options.TextSpacing;
                newNode.Attr.Fontsize    = _options.FontSize;

                newNode.UserData = node.UserData;
                newAddedNodes.Add(node.Id, newNode);
            }

            foreach (KeyValuePair <string, List <object> > pair in _addedEdges)
            {
                if (pair.Value.Count == 0)
                {
                    return;
                }

                string idFrom = (pair.Value[0] as GD.Edge).Source;
                string idTo   = (pair.Value[0] as GD.Edge).Target;

                if (_options.MultipleEdges)
                {
                    int count = _addedEdges[pair.Key].Count;

                    // Perhaps thicken edges.
                    if (_options.ThickenEdges)
                    {
                        // Interesting, but any greater than 2 looks bad.
                        int thickness = 1;
                        if (count > 1)
                        {
                            thickness = 2;
                        }

                        // Create a thickened edge. Take the properties of the first one.
                        GD.Edge edge    = pair.Value[0] as GD.Edge;
                        GD.Edge newEdge = newGraph.AddEdge(idFrom, idTo);
                        newEdge.Attr           = edge.Attr.Clone();
                        newEdge.Attr.LineWidth = thickness;
                        newAddedEdges.Add(pair.Key, new List <object>()
                        {
                            newEdge
                        });
                    }
                    else
                    {
                        // Show multiple edges.
                        newAddedEdges.Add(pair.Key, new List <object>());
                        foreach (GD.Edge edge in pair.Value)
                        {
                            GD.Edge newEdge = newGraph.AddEdge(idFrom, idTo);
                            newEdge.Attr = edge.Attr.Clone();
                            newAddedEdges[pair.Key].Add(newEdge);
                        }
                    }
                }
                else
                {
                    // Show single edges. Take the properties of the first one.
                    GD.Edge edge    = pair.Value[0] as GD.Edge;
                    GD.Edge newEdge = newGraph.AddEdge(idFrom, idTo);
                    newEdge.Attr = edge.Attr.Clone();
                    newAddedEdges.Add(pair.Key, new List <object> {
                        newEdge
                    });
                }
            }

            _hasOrphans = _notOrphans.Count < _addedNodes.Count;

            _graph      = newGraph;
            _addedEdges = newAddedEdges;
            _addedNodes = newAddedNodes;
        }