One d3 visualization I’ve been refining for a while now uses a flowing tree layout. I wanted to have a structure similar to the standard d3 tree layout which would position consistently-sized leaf nodes (children) in a recursively flowing (wrapping) arrangement. I also wanted to take a stab at building this layout as a library that followed the examples set forth in discussions of the ‘Reusable Charts’ pattern that has been such a hot topic lately. There are many great libraries out there that have attacked this problem in various fashions and it will take me a while to determine which one makes the most sense for my purposes. In the meantime, I wanted to share what I’ve come up with for a flowing tree chart:
Resizable Flow Tree (Expand/Collapse nodes & resize using space between chart and scrollbar)
Features:
Flow Layout
I’ve taken the logic from the d3 tree layout and adapted it to recursively size and space out the children of each container given the available width. What you end up with is a flowing version of the Indented Tree example.
Resizable Visualizations
The chart will update its layout when it is resized (Try it by putting the mouse cursor just the left of the scrollbar and resize the container and watch the layout do its thing). Most d3 examples that I have come across do not address resizing issues at all, something that is not surprising given the complexity involved. I’ve seen many developers asking about ways to create resizable charts in d3 and in most cases the replies suggest that they scale the SVG contents to fit the available space within the viewport. Personally, I do not find this very useful. Instead, I want to keep the size of the visualization constant (i.e. legible) and reapply the layout to make use of the available space and adjust the scrolling size as necessary.
Expanding and Collapsing Groups
The layout includes a variation of the expand/collapse logic found in the Indented Tree example. It also adjusts the container’s scroll size based on the updated chart height once expand/collapse actions have changed the size of the chart.
Recursive Layout
I’ve come across a few examples that feature wrapping style layouts, but what they did not address is recursive containment. They would not measure and layout containers within containers. This flow layout walks the hierarchy, sizing and positioning groups within groups as well as their children.
Possible Enhancements
There were a couple of ideas that I did not get time to implement yet. It might be nice to allow the children to vary in size. I may also look into justifying the children contained with each row rather than leaving the unused space tacked onto the right edge.
Related Links:
Stand alone demo of the visualization shown above
Gist showing the flow tree that resizes with the containing div. (same as shown in this blog post)
Flow Tree that resizes with the browser window
Gist showing the flow tree that resizes with the window