Background: I have some data that I want to present in a table. Each column in the table has a header. Some of these headers in turn, have common headers. In other words, I have a tree of headers that I want to show in the table cells.
Problem: How to nicely lay out a tree in the form of table by means of merging cells (cf merged cells in Excel or rowspan / colspan in HTML tables)?
Some requirements:
Given a tree like the following:
+ Header 1
|---+ Header 2
| |---- Header 4
| '---+ Header 5
| |---- Header 8
| '---- Header 9
'---+ Header 3
|---- Header 6
'---- Header 7
-
The resulting table should always be rectangular, i.e. this is not acceptable:
.-------------------------------------------. | Header 1 | +--------------------------+----------------+ | Header 2 | Header 3 | +----------+---------------+--------+-------+ | Header 4 | Header 5 | Hdr 6 | Hdr 7 | '----------+-------+-------+--------+-------' | Hdr 8 | Hdr 9 | '-------+-------' -
The height of the cells should be as evenly distributed as possible. (There should be no unecessary height constraints between siblings.) For instance solving the above situation by simply letting the leaves grow downwards like this is not acceptable:
.-------------------------------------------. | Header 1 | +--------------------------+----------------+ | Header 2 | Header 3 | +----------+---------------+--------+-------+ <-- Height of Header 3 | | Header 5 | | | constrained by | Header 4 +-------+-------+ Hdr 6 | Hdr 7 | height of Header 2 | | Hdr 8 | Hdr 9 | | | '----------+-------+-------+--------+-------' -
The correct output should look something like this:
.-------------------------------------------. | Header 1 | +--------------------------+----------------+ | Header 2 | Header 3 | +----------+---------------+ | | Header 4 | Header 5 |--------+-------+ <-- constraint relaxed. | +-------+-------+ Hdr 6 | Hdr 7 | | | Hdr 8 | Hdr 9 | | | '----------+-------+-------+--------+-------' -
The number of rows used should be minimized. In other words, the right version is preferred over the left version below:
.--------------------------. .--------------------------. | Rowspan 3 | | Rowspan 1 | +-------------+------------+ +-------------+------------+ | Rowspan 4 | Rowspan 6 | --> | Rowspan 2 | Rowspan 3 | +-------------+ | +-------------+ | | Rowspan 6 +------------+ | Rowspan 3 +------------+ | | Rowspan 4 | | | Rowspan 2 | '-------------+------------' '-------------+------------' Unecessarily large rowspans. Minimized rowspans. (actual height: 13 rows) (actual height: 6 rows)
As usual, explaining the problem carefully seem to have helped. I believe I figured it out. The key idea is to traverse the tree recursively and (among a few other things) compute the least common multiple of the depths of all subtrees in each step.
The answer is written in Java but it should be trivial to rewrite into PHP, C# or what-have-you. It refers to the following two auxiliary classes and targets HTML-tables.
The solution is broken up in two parts:
Conversion from
TreetoList<Cell>.Layout of
List<Cell>to a proper<table>...</table>.(Presumably not required if targetting for instance a spread-sheet.)
Conversion from
TreetoList<Cell>This is done using the methods
rowsToUseandgetCellsdefined below. The former computes the total number of rows required to lay out a given tree, and the latter generates the actualCells. The arguments denote the following:tis the root of the tree for whichCells should be generated.rowandcoldenotes the current row and column of the top-most (root) cell.rowsLeftspecifies how many rows the current tree should be distributed on.Here are the two methods:
The methods
depth,widthandlcmare straight forward. See full source at the bottom if you like.Layout of
List<Cell>to a proper<table>...</table>Full source and demo.
Here’s the full source. Given the tree
it produces the following table body:
which looks like
Full source: