Unfortunately, it is not possible for the treemap to display all elements in squares for mathematical reasons.
Here I will just give you a counterexample. Suppose that at the end of your processing you will receive 4 categories (A, B, C, D) with the indicated surface area.
A=2 B=2 C=2 D=1
Regardless of the position you give them, it is impossible to get the final treemap with a square or even rectangular shape.
Think: in each of the following patterns you always have a missing corner
AB AC AD BA BC BD CA CB CD DA DB DC CD BD BC CD AD AC BD AD AB CB AC AB ..there are more
because one of the squares has a surface area = 1.
Thus, it is impossible to have all the squares
From this document , as JSuar is mentioned, we learn that there is an optimized almost square representation that you can have by implementing the algorithm described in the article.
But this is an approximation.
I think that all you can do if you really need ONLY squares is:
But in this latter case, you may have empty spaces or other crashes.
Daniele b
source share