Skip to content

Commit 72372ea

Browse files
committed
Handle circular graphs better
1 parent 5702c55 commit 72372ea

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

mathicsscript/format.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,24 @@ def clamp(value, min=-math.inf, max=math.inf):
291291

292292

293293
DEFAULT_NODE_SIZE = 300.0
294-
DEFAULT_POINT_SIZE = 12
294+
DEFAULT_POINT_SIZE = 16
295295

296296

297-
def harmonize_parameters(draw_options: dict):
297+
def harmonize_parameters(G, draw_options: dict):
298+
299+
global node_size
300+
graph_layout = G.graph_layout if hasattr(G, "graph_layout") else ""
301+
302+
if graph_layout == "tree":
303+
# Call this to compute node_size. Cache the
304+
# results
305+
tree_layout(G)
306+
draw_options["node_size"] = node_size
307+
elif graph_layout == "circular":
308+
node_size = draw_options["node_size"] = (2 * DEFAULT_NODE_SIZE) / math.sqrt(
309+
len(G) + 1
310+
)
311+
298312
if draw_options.get("with_labels", False):
299313
draw_options["edgecolors"] = draw_options.get("edgecolors", "black")
300314
draw_options["node_color"] = draw_options.get("node_color", "white")
@@ -304,6 +318,7 @@ def harmonize_parameters(draw_options: dict):
304318
draw_options["width"] = width
305319

306320
if "font_size" not in draw_options:
321+
# FIXME: should also take into consideration max width of label.
307322
font_size = clamp(
308323
int((node_size * DEFAULT_POINT_SIZE) / DEFAULT_NODE_SIZE), min=1, max=18
309324
)
@@ -346,15 +361,10 @@ def format_graph(G):
346361
if not isinstance(graph_layout, str):
347362
graph_layout = graph_layout.get_string_value()
348363
layout_fn = NETWORKX_LAYOUTS.get(graph_layout, None)
349-
if layout_fn == tree_layout:
350-
# Call this to compute node_size. Cache the
351-
# results
352-
tree_layout(G)
353-
draw_options["node_size"] = node_size
354364
else:
355365
layout_fn = None
356366

357-
harmonize_parameters(draw_options)
367+
harmonize_parameters(G, draw_options)
358368

359369
if layout_fn:
360370
nx.draw(G, pos=layout_fn(G), **draw_options)

0 commit comments

Comments
 (0)