Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
93128e0
fix(layout): draw a legend's points with the map's marks
jgregoriorincon Nov 4, 2026
60a8913
fix(layout): apply transparency to a legend's points
jgregoriorincon Nov 4, 2026
bd60ec8
fix(layout): grow a legend's rows to hold large points
jgregoriorincon Nov 4, 2026
309c3eb
fix(layout): scale a legend's points with the reference scale
jgregoriorincon Nov 4, 2026
f32ed45
fix(layout): draw a legend class's own stacked marks
jgregoriorincon Nov 4, 2026
2c295e7
docs(layouts): say how a legend draws point symbols
jgregoriorincon Nov 4, 2026
d9b46cd
test(layout): cover a legend's widened column and class rows
jgregoriorincon Nov 4, 2026
27275aa
style(layout): wrap a legend comment
jgregoriorincon Nov 4, 2026
2880524
fix(layout): keep an oversized legend point inside its row
jgregoriorincon Nov 4, 2026
f56a483
docs(layouts): rewrap the legend paragraph
jgregoriorincon Nov 4, 2026
9842484
test(layout): allow for rounding in a legend's size
jgregoriorincon Nov 4, 2026
b148f80
docs(layouts): say what a legend does with large points
jgregoriorincon Nov 4, 2026
9acf472
fix(layout): draw a legend's plain point with the map's marker
jgregoriorincon Nov 5, 2026
5a8f965
fix(layout): keep a see-through point's picture on the canvas
jgregoriorincon Nov 5, 2026
d292773
refactor(layout): read a legend row's symbol only for a point
jgregoriorincon Nov 5, 2026
3fcf22e
refactor(symbology): take a legend row's class strokes from one place
jgregoriorincon Nov 5, 2026
162e33d
docs: describe the legend's functions
jgregoriorincon Nov 5, 2026
a6feefe
test(layout): check a recorded legend's point, its cut and its opacity
jgregoriorincon Nov 5, 2026
fd3e7ff
test(symbology): check a class's strokes with its classes stored in a…
jgregoriorincon Nov 5, 2026
a40f9c4
docs(layouts): rewrap the legend paragraph
jgregoriorincon Nov 5, 2026
f577e47
docs(layouts): say what a legend's symbol leaves out
jgregoriorincon Nov 5, 2026
651ecc4
fix(render): reach the corners of a mark with its own angle
jgregoriorincon Nov 5, 2026
3d1faaa
refactor(layout): name a legend row's strokes apart from a renderer's
jgregoriorincon Nov 5, 2026
6498793
docs(layout): say what else limits a legend's side
jgregoriorincon Nov 5, 2026
ab56d8b
test(layout): compare a see-through plain point with the map's
jgregoriorincon Nov 5, 2026
7571363
docs: describe the legend's tests
jgregoriorincon Nov 5, 2026
c74b39f
docs(layouts): split the legend's paragraph into its parts
jgregoriorincon Nov 5, 2026
dea3680
test(layout): drop a needless struct update
jgregoriorincon Nov 5, 2026
eb8b46d
Merge branch 'main' into fix/legend-symbol-fidelity
jgregoriorincon Nov 5, 2026
8a3d21c
fix(map): give a diagram's legend rows their own mark
jgregoriorincon Nov 5, 2026
a2c061b
fix(layout): cut a recorded point at its box
jgregoriorincon Nov 5, 2026
ba8d710
test(layout): check an upright mark stays in its row
jgregoriorincon Nov 5, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion crates/map/src/diagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustgis_data::Dataset;
use serde::{Deserialize, Serialize};

use crate::color::Rgba;
use crate::symbology::{LegendEntry, Marker, Symbol};
use crate::symbology::{LegendEntry, LineMark, LineMarkers, LineStroke, Marker, Symbol};

/// The most categories one diagram lists (each is a slice or a bar).
pub const MAX_CATEGORIES: usize = 16;
Expand Down Expand Up @@ -108,6 +108,18 @@ pub struct DiagramCategory {
pub colour: Rgba,
}

/// A category's mark in a point layer's legend: its own, or the row would take the layer's stacked
/// marks and every category would look the same.
fn category_mark(colour: Rgba) -> LineStroke {
let markers = LineMarkers {
mark: LineMark::Marker(Marker::Circle),
size: 7.0,
outline: Some(Rgba::rgb(0xFF, 0xFF, 0xFF)),
..LineMarkers::default()
};
LineStroke { color: Some(colour), width: Some(1.0), markers: Some(markers), ..LineStroke::default() }
}

/// How big a diagram is: one size for every feature, or one grown with the categories' sum, as
/// [`crate::Renderer::Proportional`] grows a marker (area-proportional square root scaling).
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -258,6 +270,7 @@ impl DiagramSettings {
size: 7.0,
marker: Marker::Circle,
},
strokes: vec![category_mark(c.colour)],
fallback: false,
})
.collect()
Expand Down Expand Up @@ -432,6 +445,7 @@ mod tests {
assert_eq!(l[0].symbol.fill, CATEGORY_COLORS[0]);
assert_eq!(l[0].symbol.stroke, Rgba::rgb(0xFF, 0xFF, 0xFF), "a white outline on a polygon or point layer");
assert!(l.iter().all(|e| !e.fallback));
assert_eq!(l[0].strokes[0].color, Some(CATEGORY_COLORS[0]), "its own mark, not the layer's stacked ones");
let line = settings().legend(true);
assert_eq!(line[0].symbol.stroke, CATEGORY_COLORS[0], "a line's swatch draws its stroke, so the colour is there");
}
Expand Down
106 changes: 94 additions & 12 deletions crates/map/src/symbology.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,11 +759,20 @@ pub const MAX_LEGEND_STEPS: usize = 2_000_000;
pub struct LegendEntry {
pub label: String,
pub symbol: Symbol,
/// The stacked strokes its class has of its own; empty, it takes the layer's `line_strokes`.
pub strokes: Vec<LineStroke>,
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
/// The Unique Values fallback row (`<all other values>`), told apart from a class whatever
/// its label: a legend can leave it out without touching a class that bears the same name.
pub fallback: bool,
}

impl LegendEntry {
/// A class's row, with the stacked strokes it has of its own, if any.
fn class(label: String, symbol: Symbol, strokes: Option<&Vec<LineStroke>>) -> Self {
LegendEntry { label, symbol, strokes: strokes.cloned().unwrap_or_default(), fallback: false }
}
}

/// Most unique-value classes created automatically.
pub const MAX_UNIQUE: usize = 64;
/// The most classes by an expression: a QGIS rule-based style's rules (one class each), kept when
Expand Down Expand Up @@ -1408,7 +1417,19 @@ impl Renderer {
/// Values by an expression reading `@map_scale` class by. One whose expression would take
/// more than [`MAX_LEGEND_STEPS`] to class every kept feature lists all its classes.
pub fn legend_at(&self, ds: &Dataset, rows: &[bool], map_scale: Option<f64>) -> Vec<LegendEntry> {
let Renderer::Unique { field, classes, other, class_labels, other_label, expression, other_fills, other_strokes, .. } = self else {
let Renderer::Unique {
field,
classes,
other,
class_labels,
other_label,
expression,
other_fills,
other_strokes,
class_strokes,
..
} = self
else {
return self.legend();
};
// A fallback that draws nothing has nothing to stand for in a legend: its own symbol as
Expand Down Expand Up @@ -1469,10 +1490,15 @@ impl Renderer {
.into_iter()
.filter(|i| used.get(*i).copied().unwrap_or(false))
.filter_map(|i| classes.get(i))
.map(|(k, s)| LegendEntry { label: unique_label(class_labels, k), symbol: *s, fallback: false })
.map(|(k, s)| LegendEntry::class(unique_label(class_labels, k), *s, class_strokes.get(k)))
.collect();
if other_used && visible {
v.push(LegendEntry { label: other_label.clone().unwrap_or_else(|| OTHER_VALUES.into()), symbol: *other, fallback: true });
v.push(LegendEntry {
label: other_label.clone().unwrap_or_else(|| OTHER_VALUES.into()),
symbol: *other,
strokes: other_strokes.clone(),
fallback: true,
});
}
v
}
Expand Down Expand Up @@ -1502,21 +1528,28 @@ impl Renderer {
rustgis_data::value::numeric_classes(classes.iter().map(|(k, _)| k.as_str()))
}

/// Every row of its legend, whatever the features: the classes, then a Unique Values'
/// `<all other values>`; none for a single symbol.
pub fn legend(&self) -> Vec<LegendEntry> {
match self {
Renderer::Single(_) => Vec::new(),
Renderer::Unique { classes, other, class_labels, other_label, .. } => {
Renderer::Unique { classes, other, class_labels, other_label, class_strokes, other_strokes, .. } => {
// Keep symbols attached to their classes in `class_order` and the fallback at the end.
let mut v: Vec<LegendEntry> = self
.class_order()
.into_iter()
.filter_map(|i| classes.get(i))
.map(|(k, s)| LegendEntry { label: unique_label(class_labels, k), symbol: *s, fallback: false })
.map(|(k, s)| LegendEntry::class(unique_label(class_labels, k), *s, class_strokes.get(k)))
.collect();
v.push(LegendEntry { label: other_label.clone().unwrap_or_else(|| OTHER_VALUES.into()), symbol: *other, fallback: true });
v.push(LegendEntry {
label: other_label.clone().unwrap_or_else(|| OTHER_VALUES.into()),
symbol: *other,
strokes: other_strokes.clone(),
fallback: true,
});
v
}
Renderer::Graduated { breaks, symbols, min, class_labels, .. } => {
Renderer::Graduated { breaks, symbols, min, class_labels, class_strokes, .. } => {
let mut lo = *min;
breaks
.iter()
Expand All @@ -1525,18 +1558,19 @@ impl Renderer {
.map(|(i, (b, s))| {
let label = class_labels.get(&i).cloned().unwrap_or_else(|| range_label(lo, *b));
lo = *b;
LegendEntry { label, symbol: *s, fallback: false }
LegendEntry::class(label, *s, class_strokes.get(&i))
})
.collect()
}
Renderer::Proportional { base, min_size, max_size, min, max, .. } => vec![
LegendEntry { label: fmt_num(*min), symbol: Symbol { size: *min_size, ..*base }, fallback: false },
LegendEntry { label: fmt_num(*min), symbol: Symbol { size: *min_size, ..*base }, strokes: Vec::new(), fallback: false },
LegendEntry {
label: fmt_num((min + max) / 2.0),
symbol: Symbol { size: min_size + (max_size - min_size) * 0.707, ..*base },
strokes: Vec::new(),
fallback: false,
},
LegendEntry { label: fmt_num(*max), symbol: Symbol { size: *max_size, ..*base }, fallback: false },
LegendEntry { label: fmt_num(*max), symbol: Symbol { size: *max_size, ..*base }, strokes: Vec::new(), fallback: false },
],
}
}
Expand Down Expand Up @@ -1582,7 +1616,7 @@ impl Renderer {
// The class's row in `legend()`, which lists the classes and then `other`. Unique Values
// rows are sorted by label, so that class's row is built here rather than looked up.
let row = match (&mut *self, class) {
(Renderer::Unique { classes, class_labels, .. }, ClassRef::Value(v)) => {
(Renderer::Unique { classes, class_labels, class_strokes, .. }, ClassRef::Value(v)) => {
let i = classes.iter().position(|(k, _)| k == v).ok_or_else(|| format!("no class with the value '{v}'"))?;
if let Some((k, s)) = classes.get_mut(i) {
restyle(s);
Expand All @@ -1599,7 +1633,7 @@ impl Renderer {
}
return classes
.get(i)
.map(|(k, s)| LegendEntry { label: unique_label(class_labels, k), symbol: *s, fallback: false })
.map(|(k, s)| LegendEntry::class(unique_label(class_labels, k), *s, class_strokes.get(k)))
.ok_or_else(|| "the class has no legend row".into());
}
(Renderer::Unique { classes, other, other_label, .. }, ClassRef::Other) => {
Expand Down Expand Up @@ -3259,6 +3293,54 @@ mod tests {
assert_eq!(r.legend().len(), 2);
}

/// Each row keeps its class's strokes, its class stored in either order, in both renderers
/// with classes.
#[test]
fn a_legend_row_has_its_class_own_strokes() {
let mut d = Dataset::new("t", GeometryType::Point, vec![Field::new("g", FieldType::Text)]);
for g in ["a", "b", "z"] {
d.push(None, vec![Value::Text(g.into())]);
}
let s = |c| Symbol::default_for(GeometryType::Point, c);
let stroke = |width| LineStroke { width: Some(width), ..LineStroke::default() };
// Stored in either order, the rows come by label and each keeps its class's strokes.
let (a, b) = (("a".to_string(), s(0)), ("b".to_string(), s(1)));
for classes in [vec![a.clone(), b.clone()], vec![b, a]] {
let r = Renderer::Unique {
field: "g".into(),
classes,
other: s(2),
ramp: Ramp::Ember,
class_labels: Default::default(),
other_label: None,
class_strokes: [("a".to_string(), vec![stroke(1.0)])].into_iter().collect(),
other_strokes: vec![stroke(2.0)],
class_fills: Default::default(),
other_fills: Vec::new(),
expression: None,
};
// The whole list and the classes of the features kept.
for rows in [r.legend(), r.legend_for(&d, &[])] {
let strokes: Vec<_> = rows.iter().map(|e| e.strokes.clone()).collect();
assert_eq!(strokes, [vec![stroke(1.0)], vec![], vec![stroke(2.0)]]);
}
}
let g = Renderer::Graduated {
field: "v".into(),
breaks: vec![1.0, 2.0],
symbols: vec![s(0), s(1)],
ramp: Ramp::Ember,
method: ClassMethod::Quantile,
min: 0.0,
classes: 2,
class_labels: Default::default(),
class_strokes: [(1, vec![stroke(1.0)])].into_iter().collect(),
class_fills: Default::default(),
};
let strokes: Vec<_> = g.legend().into_iter().map(|e| e.strokes).collect();
assert_eq!(strokes, [vec![], vec![stroke(1.0)]]);
}

#[test]
fn categorical_children_sort_case_insensitively_without_changing_symbols() {
let names = ["Zulu", "alpha", "Bravo", "ALPHA", "éclair", "École"];
Expand Down
Loading
Loading