/// <summary>
        /// Converts a <see cref="DirectedGraph"/> graph object into <see cref="mxGraph"/>
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns></returns>
        public static mxGraph ConvertToMXGraph(this DirectedGraph input)
        {
            // Creates graph with model
            mxGraph graph = new mxGraph();

            mxFastOrganicLayout layout = new mxFastOrganicLayout(graph);

            Object parent = graph.GetDefaultParent();

            graph.Model.BeginUpdate();

            Dictionary <String, mxCell> nodeId = new Dictionary <string, mxCell>();

            foreach (DGML.core.Node node in input.Nodes)
            {
                if (!nodeId.ContainsKey(node.Id))
                {
                    nodeId.Add(node.Id, (mxCell)graph.InsertVertex(parent, node.Id, node.Label, 0, 0, 200, 35, node.Category));
                }
            }

            foreach (DGML.core.Link link in input.Links)
            {
                if (!nodeId.ContainsKey(link.Source))
                {
                    continue;
                }
                if (!nodeId.ContainsKey(link.Target))
                {
                    continue;
                }

                var source = nodeId[link.Source];
                var target = nodeId[link.Target];

                if ((source != null) && (target != null))
                {
                    graph.InsertEdge(parent, link.Id, link.Label, source, target);
                }
            }

            layout.execute(parent);

            return(graph);

            //    layout.execute(
        }
Example #2
0
        public LayoutForm()
        {
            mxGraph graph  = new mxGraph();
            Object  parent = graph.GetDefaultParent();

            graph.Model.BeginUpdate();
            try
            {
                int nodeCount = 100;
                int edgeCount = 100;

                Object[] nodes = new Object[nodeCount];

                for (int i = 0; i < nodeCount; i++)
                {
                    nodes[i] = graph.InsertVertex(parent, null, 'N' + i, 0, 0, 30, 30);
                }

                Random r = new Random();

                for (int i = 0; i < edgeCount; i++)
                {
                    int r1 = (int)(r.NextDouble() * nodeCount);
                    int r2 = (int)(r.NextDouble() * nodeCount);
                    graph.InsertEdge(parent, null, r1 + '-' + r2,
                                     nodes[r1], nodes[r2]);
                }

                mxIGraphLayout layout = new mxFastOrganicLayout(graph);
                layout.execute(parent);
            }
            finally
            {
                graph.Model.EndUpdate();
            }

            graph.View.Scale = 0.2;

            graphControl      = new GraphControl(graph);
            graphControl.Dock = DockStyle.Fill;
            Controls.Add(graphControl);
            Size = new Size(320, 200);
        }