Skip to content

new functionality: set focus on creation #13

Description

@pchtsp

I work with networkx and I use the layout produced by it. The problem is that when the graph is produced in my notebook, the image is not centered and I cannot control where I want the zoom/view to start. I figure out that it tries to show the origin of coordinates (0, 0) and so now I modify the positions coming from networkx so that they're positioned around the (0, 0) coordinate.

Instead of this hack, maybe it could be useful to pass an argument to the gv.d3 function with the coordinates to display by default on the creation of the graph?

I guess this is only useful when someone is setting the coordinates manually as I'm doing.

Example:

G = my_instance.create_scenario_tree()
pos = nx.drawing.nx_pydot.graphviz_layout(G, prog="dot")

def prepare_graph(g):
  g = g.copy()
  x_coord = {v[1] for v in pos.values()}
  y_coord = {v[0] for v in pos.values()}
  max_y, min_y = max(y_coord), min(y_coord)
  max_x, min_x = max(x_coord), min(x_coord)

  for node_id in g.nodes:
    node = g.nodes[node_id]
    node['x'] = (-pos[node_id][1] + (max_x-min_x)/2)*4
    node['y'] = (pos[node_id][0] - (max_y-min_y)/2)/3
  return g


gv.d3(prepare_graph(G), zoom_factor=0.2, node_label_size_factor=3, show_node_label_border=True, graph_height=500, layout_algorithm_active=False)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions