Murali-group / GraphSpace

The interactive graph sharing website.
http://graphspace.org
GNU General Public License v2.0
30 stars 41 forks source link

Store x and y corrd. and only changed style attributes for layout #290

Open jlaw9 opened 7 years ago

jlaw9 commented 7 years ago

Currently when you save a layout, the entire style JSON file is stored. This is presumably because the layout editor now supports changing node and edge styles instead of just moving nodes around. This has an unwanted side-effect of losing your layout when you update the graph through the REST API (the layout still exists, but if you select the layout, the updates you made to the style JSON are not used because the layout's stored style JSON is loaded).

When saving a layout, is it possible to store the x and y coordinates and then only the style attributes that were modified while in the layout editor?

Here is an example workflow:

  1. Upload graph
  2. Layout graph
  3. Change name of a node
  4. Modify border size of all nodes (through REST API)

Currently the user would have to repeat steps 2 and 3 after step 4. The change I'm proposing would eliminate the need to repeat steps 2 and 3 (i.e. re-layout the graph).

jlaw9 commented 7 years ago

I figured out a simple work-around for the tutorial to keep node positions after updating a graph. Here's the code I used:

gs = GraphSpace(username, password)
gs_graph = gs.get_graph(graph_name, owner_email=username)
print("\nGraph '%s' already exists. Updating it\n" % (graph_name))
# this can take a while if your graph has a lot of nodes and edges 
gs_graph = gs.update_graph(G, graph_name=graph_name, owner_email=username)

# Currently layouts store style attributes as well as x and y positions
# so if you layed out the graph how you wanted it but now want to update the style (such as edge width), 
# you will need to copy the x and y positions of the layout you made to the updated graph
# I created the layout 'layout1', so I'll use that
layout_name = 'layout1'
layout = gs.get_graph_layout(graph=gs_graph, layout_name=layout_name)
# set the x and y position of each node in the updated graph to the x and y positions of the layout you created
print("Setting the x and y coordinates of each node to the positions in %s" % layout_name)
for node, positions in layout.positions_json.items():
    G.set_node_position(node_name=node, x=positions['x'], y=positions['y'])

# now re-post the graph and the positions should be set
print("Updating graph", graph_name)
gs_graph = gs.update_graph(G, graph_name=graph_name, owner_email=username)
# have to publish/share again after updating
#gs_graph = gs.publish_graph(graph=G)
print("Done")
print(gs_graph.url)