From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 871B61FF0E7 for ; Fri, 27 Nov 2026 17:14:16 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id CF7DD21738; Fri, 27 Nov 2026 17:13:22 +0100 (CET) From: Dominik Csapak 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 Message-ID: <20261127161241.3713494-31-d.csapak@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261127161241.3713494-1-d.csapak@proxmox.com> References: <20261127161241.3713494-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -2.258 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) KAM_SOMETLD_ARE_BAD_TLD 5 .bar, .beauty, .buzz, .cam, .casa, .cfd, .club, .date, .guru, .link, .live, .monster, .press, .pw, .quest, .rest, .sbs, .shop, .stream, .top, .trade, .wiki, .work, .xyz TLD abuse RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_PDS_OTHER_BAD_TLD 0.01 Untrustworthy TLDs Message-ID-Hash: VFOTEF3I2MK7UXUUNXTRH4YPIRICJ2VG X-Message-ID-Hash: VFOTEF3I2MK7UXUUNXTRH4YPIRICJ2VG X-MailFrom: d.csapak@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox Datacenter Manager development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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) -> Option { + 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)], + pixel_size: f64, + width: f64, + height: f64, +) -> Vec { + let reach = |radius: f64| radius * POINT_REACH; + let mut taken: Vec = 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 { #[builder] /// Additional buttons for the panel on the map, shown before the zoom buttons. tools: Vec, + + #[prop_or_default] + #[builder] + /// Show the labels of the points next to them, where there is room for it. + show_labels: bool, } impl Map { @@ -987,6 +1100,8 @@ impl yew::Component for MapComp { 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 yew::Component for MapComp { 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 yew::Component for MapComp { } } + 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 yew::Component for MapComp { 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 { /// Where to remember what the user changed on the map. state_id: Option, + #[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, @@ -535,6 +540,7 @@ impl yew::Component for WorldMapComp { .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