juba / pyobsplot

Observable Plot in Jupyter notebooks and Quarto documents
https://juba.github.io/pyobsplot/
MIT License
184 stars 8 forks source link

Using custom TreeLayout? #28

Closed miretchin closed 2 months ago

miretchin commented 2 months ago

Hello!

This is an amazing library.

I am interested in making a custom tree layout as shown here for an Indented Tree Plot. As described here: https://observablehq.com/@observablehq/plot-custom-tree-layout

indent = js("""
function indent() {
  return (root) => {
    root.eachBefore((node, i) => {
      node.y = node.depth;
      node.x = i;
    });
  };
}
""")

Plot.plot({
    'axis': None,
    'inset': 10,
    'insetRight': 120,
    'round': True,
    'width': 2000,
    'height': 3600,
    'margin': 200,
    'marks': Plot.tree([all_paths, {
        'strokeWidth': 1,
        'curve': "step-before",
        'treeLayout': indent,
    })
})

However as far as I can tell, this is just ignoring the treeLayout provided. Am I missing something? Thank you!

juba commented 2 months ago

Hi,

I think for this to work you have to define indent as an anonymous function, for example this way:

indent = js("""
() => {
  return (root) => {
    root.eachBefore((node, i) => {
      node.y = node.depth;
      node.x = i;
    });
  };
}
""")
miretchin commented 2 months ago

Worked! Thank you!