Spaces:
Running
Running
import graphviz | |
def add_nodes_and_edges(dot: graphviz.Digraph, parent_id: str, nodes_list: list, current_depth: int, base_color: str): | |
""" | |
Recursively adds nodes and edges to a Graphviz Digraph object, | |
applying a color gradient and consistent styling. | |
Args: | |
dot (graphviz.Digraph): The Graphviz Digraph object to modify. | |
parent_id (str): The ID of the parent node for the current set of nodes. | |
nodes_list (list): A list of dictionaries, each representing a node | |
with 'id', 'label', 'relationship', and optional 'subnodes'. | |
current_depth (int): The current depth in the graph hierarchy (0 for central node). | |
base_color (str): The hexadecimal base color for the deepest nodes. | |
""" | |
# Calculate color for current depth, making it lighter | |
# This factor determines how quickly the color lightens per level. | |
lightening_factor = 0.12 | |
# Convert base_color hex to RGB for interpolation | |
# Ensure base_color is a valid hex string before converting | |
if not isinstance(base_color, str) or not base_color.startswith('#') or len(base_color) != 7: | |
base_color = '#19191a' # Fallback to default dark if invalid | |
base_r = int(base_color[1:3], 16) | |
base_g = int(base_color[3:5], 16) | |
base_b = int(base_color[5:7], 16) | |
# Calculate current node color by blending towards white | |
current_r = base_r + int((255 - base_r) * current_depth * lightening_factor) | |
current_g = base_g + int((255 - base_g) * current_depth * lightening_factor) | |
current_b = base_b + int((255 - base_b) * current_depth * lightening_factor) | |
# Clamp values to 255 to stay within valid RGB range | |
current_r = min(255, current_r) | |
current_g = min(255, current_g) | |
current_b = min(255, current_b) | |
node_fill_color = f'#{current_r:02x}{current_g:02x}{current_b:02x}' | |
# Font color: white for dark nodes, black for very light nodes for readability | |
font_color = 'white' if current_depth * lightening_factor < 0.6 else 'black' | |
# Edge colors and font sizes | |
edge_color = '#4a4a4a' # Dark gray for lines | |
# Font size adjusts based on depth, ensuring a minimum size | |
font_size = max(9, 14 - (current_depth * 2)) | |
edge_font_size = max(7, 10 - (current_depth * 1)) | |
for node in nodes_list: | |
node_id = node.get('id') | |
label = node.get('label') | |
relationship = node.get('relationship') | |
# Basic validation for node data | |
if not all([node_id, label, relationship]): | |
raise ValueError(f"Invalid node: {node}") | |
# Add node with specified style | |
dot.node( | |
node_id, | |
label, | |
shape='box', # All nodes are rectangular | |
style='filled,rounded', # Filled and rounded corners | |
fillcolor=node_fill_color, | |
fontcolor=font_color, | |
fontsize=str(font_size) | |
) | |
# Add edge from parent to current node | |
dot.edge( | |
parent_id, | |
node_id, | |
label=relationship, | |
color=edge_color, | |
fontcolor=edge_color, # Edge label color also dark gray | |
fontsize=str(edge_font_size) | |
) | |
# Recursively call for subnodes | |
if 'subnodes' in node: | |
add_nodes_and_edges(dot, node_id, node['subnodes'], current_depth + 1, base_color) | |