From: Dominik Csapak <d.csapak@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [PATCH yew-widget-toolkit 30/38] widget: charts: map: allow showing labels next to the points
Date: Fri, 27 Nov 2026 17:12:27 +0100 [thread overview]
Message-ID: <20261127161241.3713494-31-d.csapak@proxmox.com> (raw)
In-Reply-To: <20261127161241.3713494-1-d.csapak@proxmox.com>
with the new 'show_labels' option, so one can see what is where without
hovering each point. The WorldMap forwards it.
The label of a point comes from the new `render_label` of its data. By
default that is the title of a single point, and nothing for a cluster.
A label is drawn to the right of its point. If it would cover another
point or label there, or reach out of the map, it goes to the left,
below or above the point instead. If there is no room at all, it is
left out, the tooltip still has the names then. How much room a label
needs is estimated from the number of its characters.
The look is defined in the style sheet with the new
'pwt-map-location-label' class.
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
src/widget/charts/map/map_point.rs | 10 ++
src/widget/charts/map/mod.rs | 176 +++++++++++++++++++++++++++++
src/widget/charts/world_map.rs | 6 +
3 files changed, 192 insertions(+)
diff --git a/src/widget/charts/map/map_point.rs b/src/widget/charts/map/map_point.rs
index 0a27c9b..2b358fe 100644
--- a/src/widget/charts/map/map_point.rs
+++ b/src/widget/charts/map/map_point.rs
@@ -135,6 +135,16 @@ pub trait MapPointData: PartialEq + Clone {
render_point_default(args)
}
+ /// The label that is shown next to a cluster of points, if there is room for it.
+ ///
+ /// By default that is the title of a single point, clusters of several points have none.
+ fn render_label(args: &PointsRenderArgs<Self>) -> Option<AttrValue> {
+ match args.points {
+ [point] => Some(point.data.render_title()),
+ _ => None,
+ }
+ }
+
/// Render the info box for a cluster of points.
///
/// Uses [render_info_default] by default.
diff --git a/src/widget/charts/map/mod.rs b/src/widget/charts/map/mod.rs
index b35eb5a..431cc45 100644
--- a/src/widget/charts/map/mod.rs
+++ b/src/widget/charts/map/mod.rs
@@ -142,6 +142,14 @@ fn connection_label_fits(label: &str, length: f64) -> bool {
length >= width * CONNECTION_LABEL_MIN_LENGTH_FACTOR
}
+// The room a label of a point needs is estimated from the number of its characters and these
+// sizes of a character in screen pixels.
+const POINT_LABEL_CHAR_WIDTH: f64 = 6.5;
+const POINT_LABEL_HEIGHT: f64 = 14.0;
+
+// how far the drawing of a point reaches, relative to its radius
+const POINT_REACH: f64 = 1.6;
+
// How fast the shown view follows a change of the view: after this time in milliseconds it has
// covered about two thirds of the way, after three times as long nearly all of it.
const VIEW_ANIMATION_TIME_CONSTANT: f64 = 70.0;
@@ -270,6 +278,106 @@ impl ConnectionCurve {
}
}
+/// An area on the map that something is drawn in.
+#[derive(Clone, Copy)]
+struct Area {
+ left: f64,
+ top: f64,
+ right: f64,
+ bottom: f64,
+}
+
+impl Area {
+ fn around(center: Coordinates, half_width: f64, half_height: f64) -> Self {
+ Self {
+ left: center.x - half_width,
+ top: center.y - half_height,
+ right: center.x + half_width,
+ bottom: center.y + half_height,
+ }
+ }
+
+ fn overlaps(&self, other: &Area) -> bool {
+ self.left < other.right
+ && other.left < self.right
+ && self.top < other.bottom
+ && other.top < self.bottom
+ }
+}
+
+/// Where the label of a point is drawn.
+#[derive(Debug, PartialEq)]
+struct PointLabel {
+ text: AttrValue,
+ /// The `text-anchor` of the label, so which part of it is at the position.
+ anchor: &'static str,
+ position: Coordinates,
+}
+
+/// Finds a place for the labels of points, each given as its center, radius and label.
+///
+/// A label is next to its point, where it does not cover a point or another label and is inside
+/// of the map with the given size. Labels without such a place are left out. `pixel_size` is the
+/// size of a screen pixel on the map.
+fn place_point_labels(
+ points: &[(Coordinates, f64, Option<AttrValue>)],
+ pixel_size: f64,
+ width: f64,
+ height: f64,
+) -> Vec<PointLabel> {
+ let reach = |radius: f64| radius * POINT_REACH;
+ let mut taken: Vec<Area> = points
+ .iter()
+ .map(|&(center, radius, _)| Area::around(center, reach(radius), reach(radius)))
+ .collect();
+
+ let mut labels = Vec::new();
+ for (center, radius, text) in points {
+ let Some(text) = text else {
+ continue;
+ };
+ let half_width = text.chars().count() as f64 * POINT_LABEL_CHAR_WIDTH * pixel_size / 2.0;
+ let half_height = POINT_LABEL_HEIGHT * pixel_size / 2.0;
+ let distance = reach(*radius) + 2.0 * pixel_size;
+
+ // to the right, to the left, below and above the point
+ let candidates = [
+ ("start", distance, 0.0, half_width),
+ ("end", -distance, 0.0, -half_width),
+ ("middle", 0.0, distance + half_height, 0.0),
+ ("middle", 0.0, -distance - half_height, 0.0),
+ ];
+ let place = candidates
+ .into_iter()
+ .find_map(|(anchor, dx, dy, to_middle)| {
+ let position = Coordinates {
+ x: center.x + dx,
+ y: center.y + dy,
+ };
+ let middle = Coordinates {
+ x: position.x + to_middle,
+ y: position.y,
+ };
+ let area = Area::around(middle, half_width, half_height);
+ let inside = area.left >= 0.0
+ && area.top >= 0.0
+ && area.right <= width
+ && area.bottom <= height;
+ let free = inside && !taken.iter().any(|other| area.overlaps(other));
+ free.then_some((anchor, position, area))
+ });
+ if let Some((anchor, position, area)) = place {
+ taken.push(area);
+ labels.push(PointLabel {
+ text: text.clone(),
+ anchor,
+ position,
+ });
+ }
+ }
+ labels
+}
+
/// An interactive Map that handles interaction (zooming, panning) and draws
/// [MapPoint]s on top of the given SVG element.
///
@@ -318,6 +426,11 @@ pub struct Map<T: MapPointData + 'static = AttrValue> {
#[builder]
/// Additional buttons for the panel on the map, shown before the zoom buttons.
tools: Vec<Html>,
+
+ #[prop_or_default]
+ #[builder]
+ /// Show the labels of the points next to them, where there is room for it.
+ show_labels: bool,
}
impl<T: MapPointData> Map<T> {
@@ -987,6 +1100,8 @@ impl<T: MapPointData + 'static> yew::Component for MapComp<T> {
let (connections, mut tooltip) = self.render_connections(ctx, effective_radius);
let mut points = Group::new();
let mut info = None;
+ // what the labels of the points have to make room for
+ let mut label_points = Vec::new();
for (index, cluster) in self.clusters.iter().enumerate() {
let radius_factor = cluster.radius_factor();
@@ -1011,6 +1126,10 @@ impl<T: MapPointData + 'static> yew::Component for MapComp<T> {
suggested_radius: effective_radius * radius_factor,
};
+ if props.show_labels {
+ label_points.push((center, args.suggested_radius, T::render_label(&args)));
+ }
+
let point = T::render_point(&args)
.onpointermove(link.callback(move |event: PointerEvent| {
let x = event.client_x();
@@ -1049,6 +1168,16 @@ impl<T: MapPointData + 'static> yew::Component for MapComp<T> {
}
}
+ let pixel_size = 1.0 / self.fit_scale;
+ for label in place_point_labels(&label_points, pixel_size, width, height) {
+ points.add_child(
+ Text::new(label.text)
+ .class("pwt-map-location-label")
+ .attribute("text-anchor", label.anchor)
+ .position(label.position.x as f32, label.position.y as f32),
+ );
+ }
+
Container::new()
.with_std_props(&props.std_props)
.listeners(&props.listeners)
@@ -1152,6 +1281,7 @@ impl<T: MapPointData + 'static> yew::Component for MapComp<T> {
mod test {
use super::{
ConnectionCurve, Coordinates, MapConnection, connection_label, connection_label_fits,
+ place_point_labels,
};
#[test]
@@ -1225,4 +1355,50 @@ mod test {
assert!(connection_label_fits("Migration", 126.0));
assert!(!connection_label_fits("Migration", 125.0));
}
+
+ #[test]
+ fn test_place_point_labels() {
+ let point = |x: f64, y: f64, label: &'static str| {
+ (
+ Coordinates { x, y },
+ 10.0,
+ Some(yew::AttrValue::from(label)),
+ )
+ };
+ let place = |points: &[_]| -> Vec<(&'static str, f64, f64)> {
+ place_point_labels(points, 1.0, 1000.0, 500.0)
+ .into_iter()
+ .map(|label| (label.anchor, label.position.x, label.position.y))
+ .collect()
+ };
+
+ // to the right of the point, or to the left if the map ends there
+ assert_eq!(
+ place(&[point(100.0, 100.0, "one")]),
+ [("start", 118.0, 100.0)]
+ );
+ assert_eq!(
+ place(&[point(990.0, 100.0, "one")]),
+ [("end", 972.0, 100.0)]
+ );
+
+ // below if there are points on both sides, and above if also below is no room
+ let neighbors = [point(60.0, 100.0, "left"), point(140.0, 100.0, "right")];
+ let mut points = vec![point(100.0, 100.0, "one")];
+ points.extend(neighbors.clone());
+ assert_eq!(place(&points)[0], ("middle", 100.0, 125.0));
+ points[0] = point(100.0, 480.0, "one");
+ points[1].0.y = 480.0;
+ points[2].0.y = 480.0;
+ assert_eq!(place(&points)[0], ("middle", 100.0, 455.0));
+
+ // a label without room is left out
+ let points = [
+ point(30.0, 100.0, "one"),
+ point(30.0, 200.0, "far too long for this map"),
+ ];
+ let labels = place_point_labels(&points, 1.0, 80.0, 500.0);
+ assert_eq!(labels.len(), 1);
+ assert_eq!(labels[0].text, "one");
+ }
}
diff --git a/src/widget/charts/world_map.rs b/src/widget/charts/world_map.rs
index 0996d5d..0c4dfcb 100644
--- a/src/widget/charts/world_map.rs
+++ b/src/widget/charts/world_map.rs
@@ -128,6 +128,11 @@ pub struct WorldMap<T: MapPointData + 'static> {
/// Where to remember what the user changed on the map.
state_id: Option<StorageLocation>,
+ #[prop_or_default]
+ #[builder]
+ /// Show the labels of the points next to them. Forwarded to the inner [Map].
+ show_labels: bool,
+
// the map is big, and it would be compared for every render of the parent otherwise
#[derivative(PartialEq(compare_with = "Rc::ptr_eq"))]
map_data: Rc<GeoJson>,
@@ -535,6 +540,7 @@ impl<T: MapPointData + 'static> yew::Component for WorldMapComp<T> {
.listeners(&props.listeners)
.class("pwt-world-map")
.tools(vec![night_button.into()])
+ .show_labels(props.show_labels)
.width(WIDTH)
.height(HEIGHT)
.max_zoom_level(props.max_zoom_level)
--
2.47.3
next prev parent reply other threads:[~2026-11-27 16:14 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 16:11 [PATCH datacenter-manager/proxmox-geojson-data/yew-widget-toolkit/yew-widget-toolkit-assets 00/38] map: show running remote migrations and rework the world map Dominik Csapak
2026-11-27 16:11 ` [PATCH proxmox-geojson-data 01/38] generate the borders between countries as lines without duplicates Dominik Csapak
2026-11-28 19:46 ` Thomas Lamprecht
2026-11-27 16:11 ` [PATCH yew-widget-toolkit-assets 02/38] charts: world map: add classes for the map and its features Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit-assets 03/38] charts: map: stack the map and its points in two layers Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit-assets 04/38] charts: map: show locations as a dot with a halo Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit-assets 05/38] charts: map: show the buttons as one panel Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit-assets 06/38] charts: map: add classes for connections Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit-assets 07/38] charts: world map: add class for the shade of the night Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit-assets 08/38] charts: map: add classes for the shares and the ping of a location Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit-assets 09/38] charts: map: add class for the shadow of a connection Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit-assets 10/38] charts: map: add class for the labels of locations Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit-assets 11/38] charts: map: add class for the status of a map Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit 12/38] widget: charts: map: factor out the radius factor of a cluster Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit 13/38] widget: charts: map: close the info card with the first click after a drag Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit 14/38] widget: menu: open the menu of a button above it if there is no room below Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit 15/38] widget: charts: world map: compare the map data by reference Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit 16/38] widget: charts: world map: draw polygons as areas and line strings as lines Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit 17/38] widget: charts: world map: split the map into one path per region Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit 18/38] widget: charts: map: draw the points in an svg element of their own Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit 19/38] widget: charts: map: don't render if the pointer moves over the same point Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit 20/38] widget: charts: map: keep the tooltip on its points when clustering anew Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit 21/38] widget: charts: world map: add a graticule Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit 22/38] widget: charts: map: animate changes of the view Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit 23/38] widget: charts: map: draw points as a dot with a halo and their count Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit 24/38] widget: charts: map: let the style sheet color the zoom buttons Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit 25/38] widget: charts: map: allow drawing connections between points Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit 26/38] widget: charts: map: allow additional buttons in the panel Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit 27/38] widget: charts: world map: allow shading the night Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit 28/38] widget: charts: map: add helpers to show shares and to ping a point Dominik Csapak
2026-11-27 16:12 ` [PATCH yew-widget-toolkit 29/38] widget: charts: map: draw a shadow below connections Dominik Csapak
2026-11-27 16:12 ` Dominik Csapak [this message]
2026-11-27 16:12 ` [PATCH yew-widget-toolkit 31/38] widget: charts: map: allow showing a status in a corner of the map Dominik Csapak
2026-11-27 16:12 ` [PATCH datacenter-manager 32/38] ui: views: map: only load and parse the world map once Dominik Csapak
2026-11-27 16:12 ` [PATCH datacenter-manager 33/38] ui: views: map: only format the titles once when sorting Dominik Csapak
2026-11-27 16:12 ` [PATCH datacenter-manager 34/38] ui: views: map: show running remote migrations Dominik Csapak
2026-11-27 16:12 ` [PATCH datacenter-manager 35/38] ui: views: map: shade the night on the map Dominik Csapak
2026-11-27 16:12 ` [PATCH datacenter-manager 36/38] ui: views: map: show the states of a cluster and ping problems Dominik Csapak
2026-11-27 16:12 ` [PATCH datacenter-manager 37/38] ui: views: map: show the names of the locations Dominik Csapak
2026-11-27 16:12 ` [PATCH datacenter-manager 38/38] ui: views: map: show a summary of the remotes and migrations Dominik Csapak
2026-11-28 23:48 ` [PATCH datacenter-manager/proxmox-geojson-data/yew-widget-toolkit/yew-widget-toolkit-assets 00/38] map: show running remote migrations and rework the world map Thomas Lamprecht
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261127161241.3713494-31-d.csapak@proxmox.com \
--to=d.csapak@proxmox.com \
--cc=pdm-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.