microsoft / automatic-graph-layout

A set of tools for graph layout and viewing
Other
1.36k stars 304 forks source link

[Question] How to render graph as image file #184

Open juavol opened 5 years ago

juavol commented 5 years ago

Is there a simple example how to render the graph as an image file? I know there is SvgGraphWriter, but it throws a null-reference exception, which makes me believe that there is something else that needs to be done.

I guess it must be like: add nodes & edges, layout, write image -- with no viewer involved.

Here is my example: var graph = new Graph("example"); graph.AddEdge("A", "B"); graph.AddEdge("B", "C"); SvgGraphWriter.Write(graph, @"C:\tmp\example.svg");

lafrank commented 5 years ago

I don't think so this functionality exists out of the box, but Lev may correct me if I am wrong.

The code to render and save image is part of the internal SaveViewAsImageForm which is instantiated from GViewer.SaveAsImage(). I have made the form SaveViewAsImageForm public in my fork of MSAG https://github.com/lafrank/automatic-graph-layoutL, but one would still need the reference to GViewer instance with the rendered graph :

[image: image.png]

On Fri, Feb 1, 2019 at 1:18 PM juavol notifications@github.com wrote:

Is there a simple example how to render the graph as an image file? I know there is SvgGraphWriter, but it throws a null-reference exception, which makes me believe that there is something else that needs to be done.

I guess it must be like: add nodes & edges, layout, write image -- with no viewer involved.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Microsoft/automatic-graph-layout/issues/184, or mute the thread https://github.com/notifications/unsubscribe-auth/AO-XZELb3LHYPhI1qekD4hCLy94pqlZDks5vJDCEgaJpZM4aeNlG .

levnach commented 5 years ago

SvgGraphWriter assumes that the layout is calculated. The minimal example probably is var g = new Graph("example"); g.AddEdge("A", "B"); g.AddEdge("B", "C"); GraphRenderer gr = new GraphRenderer(g); gr.CalculateLayout(); SvgGraphWriter.Write(g, @"C:\tmp\example.svg");

Please also have a look at Dot2Svg project in "tools".

levnach commented 5 years ago

May I close the issue?

JDCain commented 5 years ago

I found that you can use the following as well: var gr = new GraphRenderer(graph); gr.CalculateLayout(); var bitmap = new Bitmap((int)graph.Width, (int)graph.Height); gr.Render(bitmap); bitmap.Save(Path.Combine(Directory.GetCurrentDirectory(), @"example.png"), ImageFormat.Png);

Related question: does SvgGraphWriter not support writing the colors from the graph? They always come out black and white even though I set 'BlackAndWhite = false'

Edit: I should have been more specific, I mean the Fill color for subgraphs when using SvgGraphWriter