Repository navigation
Description
Before you start
- I searched the existing issues and the roadmap and did not find a duplicate request.
What problem does this solve?
On a map of classes (soils, land cover, geology), the legend says what each colour is, and the reader also wants to know how much of the area each class takes. A treemap answers both in one element: boxes in the classes' colours, each box's area the class's share. It fills a compact block, so it fits a column beside the map.
Layouts can't draw one. The closest is the chart frame of #2487, which does the data part well. This is the chart of a soil map (Minas Gerais, Brazil, 3,386 polygons in 15 classes) in RustGIS 0.8.0, grouped by class, with classes and values on, 85 mm wide:
- A bar's length tells the order well. The share of the whole has to be worked out by the reader: nothing shows what 180,032 km² is out of.
- The smallest classes come out as lines a hair wide, with no room for their colour, so the chart can't stand in for the legend.
- Three names are cut in the labels' column (
Latossolo Vermelho-A,Argissolo Vermelho-A,Neossolo Quartzarêni).
This is the page I made instead, in RustGIS 0.8.0 (the text is in Portuguese; soil data: IDE-Sisema, by UFV, Cetec, UFLA and Feam, 2010):
The treemap is made of layout rectangles and texts: a Python script works out the boxes and writes 15 rectangles, their names and a key into an .rgislayout file. It exports well, but it doesn't follow the project: after a colour, a class or the data changes, the script has to run again, and nobody can make one from the window.
Proposed solution
A form for the chart frame: "form": "bars" (the default) or "treemap". It is a new way to draw the bars the chart already works out (ChartData: values, labels, class colours, the Other bar), so grouping, statistics, limits and colours stay as they are.
layout.addElement={"kind":"chart","layer":"solos","field":"km2","group":"grupo","form":"treemap","classes":true,"values":true,"percent":true}
- Boxes: one per bar, its area in proportion to its value, laid out squarified (Bruls, Huizing and van Wijk, 2000), the largest at the top left and an Other box last. A thin gap separates them.
- Colours:
classesandbarColor, as for bars. - Text: each box has its name, and its value when
valuesis on, in black or white, whichever contrasts more with the box. A name wraps at spaces. - Share:
"percent": truewrites each value as a percentage of the total, with the chart'sdecimalsand the layout's decimal mark. A share above zero that would round to zero is written as less than the smallest one shown (< 0.1%). - Key: a box too small for its text keeps its colour, and a key under the boxes gives its swatch, name and value, in as many columns as fit. A box too short for the key draws the chart's placeholder and says so, as a stacked chart does today.
- Format pane: a Form row (Bars, Treemap). The rows a treemap doesn't read (Order, Orientation, Labels, Mirror, Maximum, Stack by) are hidden while it is one.
- Project: its own capability,
layout-chart-treemap, required only by a project that has one, so an older reader refuses it instead of drawing bars. Charts saved before keep their bytes and their pixels.
It is drawn where bars are, so the layout view, PNG, PDF and SVG exports all get it.
In two pull requests:
- The form: the boxes (a pure function in
crates/map, with tests), colours, and names and values inside them. Until part 2, boxes without room for their text are counted in a warning. "percent"and the key.
I can do both, after part 4b of #2487 (the bar outline) is in, as it touches the same code. @jgregoriorincon, if you would rather do it as part of that series, I'm glad to leave it to you.
Choices in this proposal
- A form of the chart, not an element of its own. An element would repeat the grouping, the limits, the class colours and their commands.
- Only a sum or a count, when grouped. The boxes have to add up to the whole, so
"form": "treemap"with a mean, least or greatest is refused, with the reason, asstackis. Switching a treemap's statistic to one of those turns it back to bars, and the chart says so. - Values of zero and below get no box. They have no area. The chart says how many are left out.
- Always the largest first. A squarified layout needs it to keep boxes near square.
- The percentage is of every value with a box to draw, those past the limit included. Leaving bars out then doesn't inflate the others' shares, and the chart already says how many it leaves out.
- Names are the group field's values, as for bars. Using the label a class has in the symbology would help bars too, and can come separately.
- The area comes from a field, not from what the map draws. Overlapping polygons count twice: in this map the polygons add up to 609,470 km², about 4% more than the 584,922 km² they cover (on a 200 m grid). My script counts each cell once, the smaller polygon on top, so its shares differ a little from the bars'.
- Vector layers only, like the chart today. Raster classes are left for another issue.
- No new dependency: squarifying is some 40 lines.
Any of these can change if you prefer another way.
Estimated scope
Medium: a broader feature that is feasible in a reasonable timeframe
Alternatives considered
- The bar chart beside the legend. It works today and reads lengths better, but takes two elements and shows no share of the whole.
- One stacked bar.
stackby the class shows parts of a whole, but its key stops at 8 values, and narrow stretches have no room for a name. - A pie. It takes more room for the same number of classes, and 15 slices are hard to compare.
- A picture made outside RustGIS. It doesn't follow the data and loses the layout's fonts.
- A generated
.rgislayout, as above.
Activity
ro9er117911 commentedon Dec 3, 2026
Thanks for the detailed proposal. A form for the chart frame would also fit a pie and a donut, which layouts can't draw today. A pie is hard to read with 15 classes, as your soil map shows, but for a few classes (a two- to five-way split) it is what many readers expect. Would you welcome
"form": "pie"and"form": "donut"beside"treemap"?They would follow the same rules you set for the treemap:
ChartData), so grouping, limits, class colours and the Other bar stay as they are.startAngleas inlayer.diagram."percent"and the key from your part 2, so a slice too thin for its name keeps its colour and is named in the key."hole": 0.5of the radius (the name is a first idea).layout-chart-pie), so an older reader refuses it instead of drawing bars.#2861 (part 4b of #2487) is in now. @elpidioassinaturas, are you starting part 1? I'll wait for it and add the pie and donut as a separate pull request on top of the
formfield, so it doesn't get in your way. @jgregoriorincon, does that work for you, or would you rather fold it into the series?elpidioassinaturas commentedon Dec 3, 2026
@ro9er117911 yes, part 1 is under way, on the branch
feat/layout-chart-treemap, and its pull request follows shortly. A pie and a donut on top of it are welcome, and the field is laid out for them:ChartForm(crates/map/src/layout/chart.rs) is the savedform("bars", the default and left out of the file, or"treemap"). A new form is a variant there, with itscode,labelandcapability.ChartForm::is_wholemarks a form whose shapes are parts of a whole.chart_form_refusal(form, group, statistic)refuses those grouped with a mean, least or greatest, andElementKind::chart_formdraws a stored one that can't take it as bars. The commands shareform_or_refuse: a form given is refused with the reason; one kept from before is turned back to bars when the statistic changes, and the chart says so.chart_data_with,chart_stack).ChartData::without_areacounts the values of zero and below, which the chart's notices say.ChartForm::capabilitygiveslayout-chart-treemap, andproject_capabilities.rshas a genericcommand_uses_chart_formto which a form adds a thin wrapper and a row.Generated by Claude Code