Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVG canvas scaling and .replace method #2645

Closed
hyanwong opened this issue Nov 23, 2022 · 2 comments · Fixed by #2646
Closed

SVG canvas scaling and .replace method #2645

hyanwong opened this issue Nov 23, 2022 · 2 comments · Fixed by #2646
Labels
Visualisation This issue addresses visualisation

Comments

@hyanwong
Copy link
Member

A few times now, myself and others (e.g. @gtsambos) have wanted to make the SVG canvas a little bigger, e.g. to accommodate long or rotated labels, etc.

There's a quick hack to do this as follows:

import tskit
from IPython.display import display, SVG
w = 300
h = 200
tree = tskit.Tree.generate_balanced(10)
svg = tree.draw_svg(
    size=(w, h),
    style=".leaf > .lab {text-anchor: start; transform: rotate(90deg) translate(6px)}",
    node_labels={u: "abcedfghij"[u] * 10 for u in ts.samples()},
)
display(svg) # labels cut off

# Hack to make room for labels
taller_svg = svg.replace(f'height="{h}"', f'height="{h+50}"', 1)
display(SVG(taller_svg))

So two things here:

  1. should we implement the .replace method on the tskit.SVGstring class, so ti returns another SVGstring? That would save a small amount of hassle, but I don't want to be implementing all the other standard string methods that return a string. I guess this would be just
    def replace(self, *args, **kwargs):
        return tskit.SVGString(self.replace(*args, **kwargs))
    
  2. should we be able to specify a different canvas size from the size() passed to draw_svg to avoid having to hack it altogether?
@hyanwong hyanwong added the Visualisation This issue addresses visualisation label Nov 23, 2022
@jeromekelleher
Copy link
Member

Adding a canvas_size to draw_svg seems a reasonable thing to do - a lot of this layout stuff is pretty arbitrary so adding options to tweak is good.

@hyanwong
Copy link
Member Author

SGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Visualisation This issue addresses visualisation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants