-
Hi, I also read https://github.com/KeRNeLith/QuikGraph/wiki/Creating-Graphs but it seems to address edge "tags" and https://github.com/KeRNeLith/QuikGraph/wiki/Edges that says the vertex can be any type. I expected the resulting dot to use the strings as node labels but they are now automatically assigned numbers. Can you point me to what's wrong in the code below? using System;
using QuikGraph;
using QuikGraph.Graphviz;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var edges = new[] {
new SEquatableEdge<string>("Node 1", "Node 2"),
new SEquatableEdge<string>("Node 0", "Node 1"),
new SEquatableEdge<string>("Node 0", "Node 3"),
new SEquatableEdge<string>("Node 2", "Node 3")
};
var graph = edges.ToAdjacencyGraph<string, SEquatableEdge<string>>();
Console.WriteLine(graph.ToGraphviz());
Console.ReadKey();
}
}
} Result digraph G {
0;
1;
2;
3;
0 -> 1;
1 -> 3;
2 -> 0;
2 -> 3;
}
I also tried to play with HTML labels based on #27 but there is no readily available sample, sadly. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 1 reply
-
So I decided to bite the bullet and dig a bit deeper.
OnFormatVertex Output.Write($"{_verticesIds[vertex]}");
FormatVertexEventHandler<TVertex> formatVertex = FormatVertex;
if (formatVertex != null)
{
var vertexFormat = new GraphvizVertex();
formatVertex(this, new FormatVertexEventArgs<TVertex>(vertex, vertexFormat));
string dot = vertexFormat.InternalToDot(CommonVertexFormat);
// continued where /// <summary>
/// Fired when formatting a vertex.
/// </summary>
public event FormatVertexEventHandler<TVertex> FormatVertex; is a property of GraphvizAlgorithm. It allows different kinds of graphs (EdgeMergeCondensatedGraphRenderer, CondensatedGraphRenderer) to output stuff differently, but is null by default. Notice The label is not set anywhere (EdgeMergeCondensatedGraphRenderer does it). However, I don't see GraphRendererBase being referenced anywhere. It might be some internal stuff and there'd be no way at this point to set the label from a library consumer point of view. InternalToDot() is also interesting because it calls ShapeRelatedPropertiesToDot which calls LabelOrRecordToDot, accessing the Label property of the GraphvizVertex (formatVertex). Since |
Beta Was this translation helpful? Give feedback.
-
CustomGraphRenderer.csClick to expand!using System;
using JetBrains.Annotations;
using QuikGraph.Utils;
namespace QuikGraph.Graphviz
{
/// <summary>
/// Edge merge condensation graph to DOT renderer.
/// </summary>
/// <typeparam name="TVertex">Vertex type.</typeparam>
/// <typeparam name="TEdge">Edge type.</typeparam>
public class CustomGraphRenderer<TVertex, TEdge> : GraphRendererBase<TVertex, TEdge>
where TEdge : IEdge<TVertex>
{
/// <summary>
/// Initializes a new instance of the <see cref="CustomGraphRenderer{TVertex,TEdge}"/> class.
/// </summary>
/// <param name="graph">Graph to convert to DOT.</param>
/// <exception cref="T:System.ArgumentNullException"><paramref name="graph"/> is <see langword="null"/>.</exception>
public CustomGraphRenderer(
[NotNull] IEdgeListGraph<TVertex, TEdge> graph)
: base(graph)
{
}
/// <inheritdoc />
protected override void Initialize()
{
base.Initialize();
Graphviz.FormatVertex += OnFormatVertex;
}
/// <inheritdoc />
protected override void Clean()
{
Graphviz.FormatVertex -= OnFormatVertex;
base.Clean();
}
private static void OnFormatVertex([NotNull] object sender, [NotNull] FormatVertexEventArgs<TVertex> args)
{
args.VertexFormat.Label = args.Vertex.ToString();
}
public string Generate()
{
using (GenerationScope())
{
return Graphviz.Generate();
}
#region Local function
IDisposable GenerationScope()
{
Initialize();
return DisposableHelpers.Finally(Clean);
}
#endregion
}
}
} Program.csusing System;
using QuikGraph;
using QuikGraph.Graphviz;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
var edges = new[] {
new SEquatableEdge<string>("Node 1", "Node 2"),
new SEquatableEdge<string>("Node 0", "Node 1"),
new SEquatableEdge<string>("Node 0", "Node 3"),
new SEquatableEdge<string>("Node 2", "Node 3")
};
var graph = edges.ToAdjacencyGraph<string, SEquatableEdge<string>>();
var renderer = new CustomGraphRenderer<string, SEquatableEdge<string>>(graph);
Console.WriteLine(renderer.Generate());
Console.ReadKey();
}
}
} yields this: digraph G {
node [fontname="Tahoma", fontsize=8.25, shape=box, style=filled, fillcolor="#FFFFE0FF"];
edge [fontname="Tahoma", fontsize=8.25];
0 [label="Node 1"];
1 [label="Node 2"];
2 [label="Node 0"];
3 [label="Node 3"];
0 -> 1;
1 -> 3;
2 -> 0;
2 -> 3;
} |
Beta Was this translation helpful? Give feedback.
-
Option 2 (much simpler)Described here: https://github.com/KeRNeLith/QuikGraph/wiki/Visualization-Using-Graphviz#graphviz-algorithm-by-extension var option2 = graph.ToGraphviz(algorithm =>
{
algorithm.FormatVertex +=
(_, eventArgs) => eventArgs.VertexFormat.Label = eventArgs.Vertex.ToString();
});
Console.WriteLine(option2); HTML: var htmldot = graph.ToGraphviz(algorithm =>
{
algorithm.FormatVertex += (_, eventArgs) =>
{
eventArgs.VertexFormat.IsHtmlLabel = true;
eventArgs.VertexFormat.Label = $"<U>{eventArgs.Vertex}</U>";
};
}); |
Beta Was this translation helpful? Give feedback.
-
Hello, I'm so sorry for the delay to answer your post. I noticed that you found out the wiki page on the topic which indeed ends in your second solution being the most efficient/simpler to do! I had in mind adding like a sample project that would take the form of unit tests where each unit test will be a sample that will be more easily runnable and a more concrete example. I have a branch for that on the project but it quite hard to find out a "good" organization and the right amount of granularity. So did you manage to achieve all you were expecting to do? |
Beta Was this translation helpful? Give feedback.
-
Thanks. Only after diving into the source code (using intellisearch and search in files to find references) did I figure out what was going on, and eventually found the correct link in the wiki sidebar. |
Beta Was this translation helpful? Give feedback.
Option 2 (much simpler)
Described here: https://github.com/KeRNeLith/QuikGraph/wiki/Visualization-Using-Graphviz#graphviz-algorithm-by-extension
HTML: