microsoft / automatic-graph-layout

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

NullReferenceException when trying to generate SVG from graph #338

Closed olduh29 closed 1 year ago

olduh29 commented 1 year ago

Hello, I am trying to generate a SVG from a Graph object but it fails with a nullref exception when calling SvgGraphWriter.Write()

Here is my simple test case :

private static void TestSVG()
    {
        Microsoft.Msagl.Drawing.Graph graph = new Microsoft.Msagl.Drawing.Graph("graph");
        //create the graph content 
        graph.AddEdge("A", "B");
        graph.AddEdge("B", "C");
        graph.AddEdge("A", "C").Attr.Color = Microsoft.Msagl.Drawing.Color.Green;
        graph.FindNode("A").Attr.FillColor = Microsoft.Msagl.Drawing.Color.Magenta;
        graph.FindNode("B").Attr.FillColor = Microsoft.Msagl.Drawing.Color.MistyRose;
        Microsoft.Msagl.Drawing.Node c = graph.FindNode("C");
        c.Attr.FillColor = Microsoft.Msagl.Drawing.Color.PaleGreen;
        c.Attr.Shape = Microsoft.Msagl.Drawing.Shape.Diamond;

        Stream stream = null;
        using (stream = File.OpenWrite("c:/tmp/go.svg")) {
            var writer = new SvgGraphWriter(stream, graph) {
                Precision = 4
            };
            writer.Write();
        }        
    }

stacktrace is

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Msagl.Drawing.SvgGraphWriter.TransformGraphByFlippingY()
   at Microsoft.Msagl.Drawing.SvgGraphWriter.Write()
   at Program.TestSVG() in C:\Users\olduh\dev\csly-cli\Tester\Program.cs:line 101
   at Program.Main(String[] args) in C:\Users\olduh\dev\csly-cli\Tester\Program.cs:line 33

Is there something I'm missing when trying to write an SVG file ?

levnach commented 1 year ago

https://github.com/GraphLayout/Samples/SvgLayerSample. Please see that this example works. The svg writer expects the geometry present.

olduh29 commented 1 year ago

thanks @levnach, i 've got to make my code output a valid svg. Now I have to find how to tune the geometry to have the SVG look good.