Example #1
0
        /// <summary>
        ///     Saves the given <see cref="ITreeNode" /> to an image file.
        /// </summary>
        /// <param name="rootNode">The root node of the tree to be saved.</param>
        /// <param name="basePath">The path in which to save the given tree</param>
        /// <param name="fileName">The name of the image file to be saved (without extension)</param>
        /// <param name="formatVertex">The delegate used to format the vertexes.</param>
        /// <param name="formatEdge">The delegate used to format the edges.</param>
        /// <param name="imageType">The type of image file in which to save the tree.</param>
        /// <param name="timeout">The maximum time to wait for Graphviz to create the image file.</param>
        /// <returns>The path to file where the tree image file was saved.</returns>
        public static string ToGraphvizFile(
            this ITreeNode rootNode, string basePath, string fileName,
            FormatVertexEventHandler <Vertex> formatVertex,
            FormatEdgeAction <Vertex, Edge> formatEdge,
            GraphvizImageType imageType = GRAPHVIZ_IMAGE_TYPE, int timeout = GRAPHVIZ_TIMEOUT_MS)
        {
            var graph = new AdjacencyGraph <Vertex, Edge>();

            GraphAdd(rootNode, graph, new Dictionary <ITreeNode, Vertex>());

            var filePath = Path.Combine(basePath, $"{fileName}.dot");

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            var viz = new GraphvizAlgorithm <Vertex, Edge>(graph)
            {
                ImageType = imageType
            };

            if (formatVertex != null)
            {
                viz.FormatVertex += formatVertex;
            }
            if (formatEdge != null)
            {
                viz.FormatEdge += formatEdge;
            }
            return(viz.Generate(new FileDotEngine(timeout), filePath));
        }
Example #2
0
        private string RenderAnalysisGraph(TypeDependencyGraph tdg, GraphvizImageType imageType, string fileName)
        {
            string            output;
            GraphvizAlgorithm renderer;

            renderer           = new GraphvizAlgorithm(tdg);
            renderer.ImageType = imageType;
            renderer.GraphFormat.RankDirection = GraphvizRankDirection.LR;
            Color[] colors =
            {
                Color.Beige,
                Color.Cornsilk,
                Color.DimGray,
                Color.Khaki,
                Color.PeachPuff,
                Color.Wheat,
                Color.Olive,
                Color.Moccasin,
                Color.LightCoral,
                Color.LightGoldenrodYellow,
                Color.LightGray,
                Color.LightGreen,
                Color.LightPink,
                Color.LightSalmon,
                Color.LightSeaGreen,
                Color.LightSkyBlue,
                Color.LightSlateGray,
                Color.LightSteelBlue,
                Color.LightYellow,
                Color.Lime,
                Color.MediumAquamarine,
                Color.MediumBlue,
                Color.MediumOrchid,
                Color.MediumPurple,
                Color.MediumSeaGreen,
                Color.MediumSlateBlue,
                Color.MediumSpringGreen,
                Color.MediumTurquoise,
                Color.MediumVioletRed,
                Color.MintCream,
            };
            int nextColorInd = 0;
            Dictionary <int, Color>  colormap = new Dictionary <int, Color>();
            FormatVertexEventHandler fvertex  = delegate(Object s, FormatVertexEventArgs args)
            {
                TypeVertex v = (TypeVertex)args.Vertex;
                args.VertexFormatter.Label = v.Name;
                args.VertexFormatter.Font  = new Font(FontFamily.GenericSerif, 8);
                if (v.SCCNum >= 0)
                {
                    Color c;
                    if (!colormap.TryGetValue(v.SCCNum, out c))
                    {
                        if (nextColorInd > colors.GetUpperBound(0))
                        {
                            nextColorInd = 0;
                        }
                        c = colors[nextColorInd++];
                        colormap[v.SCCNum] = c;
                    }
                    args.VertexFormatter.FillColor = c;
                    args.VertexFormatter.Style     = GraphvizVertexStyle.Filled;
                }
            };

            FormatEdgeEventHandler Fedge = delegate(Object s, FormatEdgeEventArgs args)
            {
                args.EdgeFormatter.Head      = new GraphvizEdgeExtremity(true);
                args.EdgeFormatter.HeadArrow = new GraphvizArrow(GraphvizArrowShape.Dot);
            };

            renderer.FormatVertex += fvertex;

            renderer.FormatEdge += Fedge;
            output = renderer.Write(fileName);
            return(output);
        }