Commit 627015f1 authored by Tiago Peixoto's avatar Tiago Peixoto

graph_draw(): honor float values passed to 'fit_view' as described in documentation

parent 8664f7f2
...@@ -504,6 +504,7 @@ def cairo_draw(g, pos, cr, vprops=None, eprops=None, vorder=None, eorder=None, ...@@ -504,6 +504,7 @@ def cairo_draw(g, pos, cr, vprops=None, eprops=None, vorder=None, eorder=None,
cr.scale(output_size[0] / w, output_size[1] / h) cr.scale(output_size[0] / w, output_size[1] / h)
cr.translate(-x, -y) cr.translate(-x, -y)
except TypeError: except TypeError:
pad = fit_view if fit_view != True else 0.95
offset, zoom = fit_to_view(g, pos, output_size, offset, zoom = fit_to_view(g, pos, output_size,
vprops.get("size", _vdefaults["size"]), vprops.get("size", _vdefaults["size"]),
vprops.get("pen_width", _vdefaults["pen_width"]), vprops.get("pen_width", _vdefaults["pen_width"]),
...@@ -512,7 +513,7 @@ def cairo_draw(g, pos, cr, vprops=None, eprops=None, vorder=None, eorder=None, ...@@ -512,7 +513,7 @@ def cairo_draw(g, pos, cr, vprops=None, eprops=None, vorder=None, eorder=None,
_vdefaults["font_family"]), _vdefaults["font_family"]),
vprops.get("font_size", vprops.get("font_size",
_vdefaults["font_size"]), _vdefaults["font_size"]),
cr) pad, cr)
cr.translate(offset[0], offset[1]) cr.translate(offset[0], offset[1])
if not isinstance(fit_view, bool): if not isinstance(fit_view, bool):
zoom /= fit_view zoom /= fit_view
...@@ -590,7 +591,8 @@ def auto_colors(g, bg, pos, back): ...@@ -590,7 +591,8 @@ def auto_colors(g, bg, pos, back):
def graph_draw(g, pos=None, vprops=None, eprops=None, vorder=None, eorder=None, def graph_draw(g, pos=None, vprops=None, eprops=None, vorder=None, eorder=None,
nodesfirst=False, output_size=(600, 600), fit_view=True, nodesfirst=False, output_size=(600, 600), fit_view=True,
inline=is_draw_inline, mplfig=None, output=None, fmt="auto", **kwargs): inline=is_draw_inline, mplfig=None, output=None, fmt="auto",
**kwargs):
r"""Draw a graph to screen or to a file using :mod:`cairo`. r"""Draw a graph to screen or to a file using :mod:`cairo`.
Parameters Parameters
...@@ -935,6 +937,9 @@ def graph_draw(g, pos=None, vprops=None, eprops=None, vorder=None, eorder=None, ...@@ -935,6 +937,9 @@ def graph_draw(g, pos=None, vprops=None, eprops=None, vorder=None, eorder=None,
b, t = y.a.min(), y.a.max() b, t = y.a.min(), y.a.max()
w = r - l w = r - l
h = t - b h = t - b
if fit_view != True:
w *= float(fit_view)
h *= float(fit_view)
ax.set_xlim(l - w * .1, r + w * .1) ax.set_xlim(l - w * .1, r + w * .1)
ax.set_ylim(b - h * .1, t + h * .1) ax.set_ylim(b - h * .1, t + h * .1)
...@@ -958,8 +963,9 @@ def graph_draw(g, pos=None, vprops=None, eprops=None, vorder=None, eorder=None, ...@@ -958,8 +963,9 @@ def graph_draw(g, pos=None, vprops=None, eprops=None, vorder=None, eorder=None,
if isinstance(val, PropertyMap): if isinstance(val, PropertyMap):
eprops[p] = _convert(edge_attrs.__dict__[p], val, eprops[p] = _convert(edge_attrs.__dict__[p], val,
kwargs.get("ecmap", default_cm)) kwargs.get("ecmap", default_cm))
fit_area = fit_view if fit_view != True else 0.95
return interactive_window(g, pos, vprops, eprops, vorder, eorder, return interactive_window(g, pos, vprops, eprops, vorder, eorder,
nodesfirst, **kwargs) nodesfirst, fit_area=fit_area, **kwargs)
else: else:
if isinstance(output, str): if isinstance(output, str):
out, auto_fmt = open_file(output, mode="wb") out, auto_fmt = open_file(output, mode="wb")
...@@ -993,6 +999,7 @@ def graph_draw(g, pos=None, vprops=None, eprops=None, vorder=None, eorder=None, ...@@ -993,6 +999,7 @@ def graph_draw(g, pos=None, vprops=None, eprops=None, vorder=None, eorder=None,
x, y, w, h = fit_view x, y, w, h = fit_view
offset, zoom = [0, 0], 1 offset, zoom = [0, 0], 1
except TypeError: except TypeError:
pad = fit_view if fit_view != True else 0.95
offset, zoom = fit_to_view(g, pos, output_size, vprops["size"], offset, zoom = fit_to_view(g, pos, output_size, vprops["size"],
vprops["pen_width"], None, vprops["pen_width"], None,
vprops.get("text", None), vprops.get("text", None),
...@@ -1000,7 +1007,7 @@ def graph_draw(g, pos=None, vprops=None, eprops=None, vorder=None, eorder=None, ...@@ -1000,7 +1007,7 @@ def graph_draw(g, pos=None, vprops=None, eprops=None, vorder=None, eorder=None,
_vdefaults["font_family"]), _vdefaults["font_family"]),
vprops.get("font_size", vprops.get("font_size",
_vdefaults["font_size"]), _vdefaults["font_size"]),
cr) pad, cr)
fit_view = False fit_view = False
else: else:
offset, zoom = [0, 0], 1 offset, zoom = [0, 0], 1
...@@ -1155,7 +1162,7 @@ def get_bb(g, pos, size, pen_width, size_scale=1, text=None, font_family=None, ...@@ -1155,7 +1162,7 @@ def get_bb(g, pos, size, pen_width, size_scale=1, text=None, font_family=None,
def fit_to_view(g, pos, geometry, size, pen_width, M=None, text=None, def fit_to_view(g, pos, geometry, size, pen_width, M=None, text=None,
font_family=None, font_size=None, cr=None): font_family=None, font_size=None, pad=0.95, cr=None):
if g.num_vertices() == 0: if g.num_vertices() == 0:
return [0, 0], 1 return [0, 0], 1
if M is not None: if M is not None:
...@@ -1187,7 +1194,6 @@ def fit_to_view(g, pos, geometry, size, pen_width, M=None, text=None, ...@@ -1187,7 +1194,6 @@ def fit_to_view(g, pos, geometry, size, pen_width, M=None, text=None,
zoom_x = 1 zoom_x = 1
if np.isnan(zoom_y) or np.isinf(zoom_y) or zoom_y == 0: if np.isnan(zoom_y) or np.isinf(zoom_y) or zoom_y == 0:
zoom_y = 1 zoom_y = 1
pad = 0.95
zoom = min(zoom_x, zoom_y) * pad zoom = min(zoom_x, zoom_y) * pad
empty_x = (geometry[0] - sum(x_delta)) - dx * zoom empty_x = (geometry[0] - sum(x_delta)) - dx * zoom
empty_y = (geometry[1] - sum(y_delta)) - dy * zoom empty_y = (geometry[1] - sum(y_delta)) - dy * zoom
......
...@@ -130,8 +130,8 @@ class GraphWidget(Gtk.DrawingArea): ...@@ -130,8 +130,8 @@ class GraphWidget(Gtk.DrawingArea):
def __init__(self, g, pos, vprops=None, eprops=None, vorder=None, def __init__(self, g, pos, vprops=None, eprops=None, vorder=None,
eorder=None, nodesfirst=False, update_layout=False, eorder=None, nodesfirst=False, update_layout=False,
layout_K=1., multilevel=False, display_props=None, layout_K=1., multilevel=False, display_props=None,
display_props_size=11, bg_color=None, layout_callback=None, display_props_size=11, fit_area=0.95, bg_color=None,
key_press_callback=None, **kwargs): layout_callback=None, key_press_callback=None, **kwargs):
r"""Interactive GTK+ widget displaying a given graph. r"""Interactive GTK+ widget displaying a given graph.
Parameters Parameters
...@@ -163,8 +163,10 @@ class GraphWidget(Gtk.DrawingArea): ...@@ -163,8 +163,10 @@ class GraphWidget(Gtk.DrawingArea):
Parameter ``multilevel`` passed to :func:`~graph_tool.draw.sfdp_layout`. Parameter ``multilevel`` passed to :func:`~graph_tool.draw.sfdp_layout`.
display_props : list of :class:`~graph_tool.PropertyMap` instances (optional, default: ``None``) display_props : list of :class:`~graph_tool.PropertyMap` instances (optional, default: ``None``)
List of properties to be displayed when the mouse passes over a vertex. List of properties to be displayed when the mouse passes over a vertex.
display_props_size : float (optional, default: ``11``) display_props_size : float (optional, default: ``11.``)
Font size used to display the vertex properties. Font size used to display the vertex properties.
fit_area : float (optional, default: ``.95``)
Fraction of the drawing area to fit the graph initially.
bg_color : str or sequence (optional, default: ``None``) bg_color : str or sequence (optional, default: ``None``)
Background color. The default is white. Background color. The default is white.
layout_callback : function (optional, default: ``Node``) layout_callback : function (optional, default: ``Node``)
...@@ -262,6 +264,7 @@ class GraphWidget(Gtk.DrawingArea): ...@@ -262,6 +264,7 @@ class GraphWidget(Gtk.DrawingArea):
self.drag_begin = None self.drag_begin = None
self.moved_picked = False self.moved_picked = False
self.vertex_matrix = None self.vertex_matrix = None
self.pad = fit_area
self.display_prop = g.vertex_index if display_props is None \ self.display_prop = g.vertex_index if display_props is None \
else display_props else display_props
...@@ -620,6 +623,7 @@ class GraphWidget(Gtk.DrawingArea): ...@@ -620,6 +623,7 @@ class GraphWidget(Gtk.DrawingArea):
_vdefaults["font_family"]), _vdefaults["font_family"]),
self.vprops.get("font_size", self.vprops.get("font_size",
_vdefaults["font_size"]), _vdefaults["font_size"]),
self.pad,
cr) cr)
m = cairo.Matrix() m = cairo.Matrix()
m.translate(offset[0] + ox, offset[1] + oy) m.translate(offset[0] + ox, offset[1] + oy)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment