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 87E4E1FF0B4 for ; Fri, 27 Nov 2026 17:13:52 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id AC4812189B; Fri, 27 Nov 2026 17:13:14 +0100 (CET) From: Dominik Csapak To: pdm-devel@lists.proxmox.com Subject: [PATCH yew-widget-toolkit 16/38] widget: charts: world map: draw polygons as areas and line strings as lines Date: Fri, 27 Nov 2026 17:12:13 +0100 Message-ID: <20261127161241.3713494-17-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 0.250 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) 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 Message-ID-Hash: IG7C7TMDDK2PCAG6FDBZV5GGFX4EGOVD X-Message-ID-Hash: IG7C7TMDDK2PCAG6FDBZV5GGFX4EGOVD 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: instead of filling all of them. That way a map can have the borders between countries as lines on top of the land, instead of as the outlines of all countries, where every border is drawn twice. Note that the land has to consist of polygons for that, as in the current world map of proxmox-geojson-data. Each feature gets a group with its 'kind' property as 'data-kind' attribute, and the look moves to the style sheet (the 'pwt-world-map' and 'pwt-world-map-feature' classes), so that the features can be styled differently. Signed-off-by: Dominik Csapak --- src/widget/charts/world_map.rs | 121 ++++++++++++++++++--------------- 1 file changed, 68 insertions(+), 53 deletions(-) diff --git a/src/widget/charts/world_map.rs b/src/widget/charts/world_map.rs index efe6099..c5a68e4 100644 --- a/src/widget/charts/world_map.rs +++ b/src/widget/charts/world_map.rs @@ -2,11 +2,11 @@ use std::marker::PhantomData; use std::rc::Rc; use derivative::Derivative; -use geojson::{GeoJson, Geometry}; +use geojson::{Feature, GeoJson, Geometry}; use yew::prelude::*; use crate::prelude::*; -use crate::widget::canvas::Path; +use crate::widget::canvas::{Group, Path}; use crate::widget::charts::{Coordinates, Map, MapPoint, MapPointData}; use pwt_macros::{builder, widget}; @@ -62,6 +62,9 @@ impl From> for MapPoint { } /// A world map using GeoJSON data to draw SVG lines and polygons. +/// +/// The features are drawn in their order, the polygons of a feature as areas and its line strings +/// as lines. Each feature has its `kind` property as `data-kind` attribute for the style sheet. #[widget(pwt=crate,comp=WorldMapComp, @element)] #[builder] #[derive(Properties, Derivative, Clone)] @@ -110,32 +113,47 @@ impl WorldMap { } pub struct WorldMapComp { - path: String, + shapes: Vec, _phantom_data: PhantomData, } -fn calculate_path(geojson: &GeoJson) -> String { - let mut paths = Vec::new(); - match geojson { - GeoJson::Geometry(geometry) => { - paths.append(&mut parse_geometry(geometry)); - } - GeoJson::Feature(feature) => { - if let Some(geometry) = &feature.geometry { - let mut new_paths = parse_geometry(geometry); - paths.append(&mut new_paths); - } - } - GeoJson::FeatureCollection(feature_collection) => { - for f in feature_collection { - if let Some(geometry) = &f.geometry { - let mut new_paths = parse_geometry(geometry); - paths.append(&mut new_paths); - } - } +/// The shapes of a GeoJSON feature as SVG path commands. +struct FeatureShapes { + /// The `kind` property of the feature, so that a style sheet can tell the features apart. + kind: Option, + /// The polygons of the feature. + areas: AttrValue, + /// The line strings of the feature, which are not filled. + lines: AttrValue, +} + +impl FeatureShapes { + fn new(kind: Option<&str>, geometry: &Geometry) -> Self { + let mut areas = Vec::new(); + let mut lines = Vec::new(); + parse_geometry(geometry, &mut areas, &mut lines); + Self { + kind: kind.map(|kind| kind.to_owned().into()), + areas: areas.join(" ").into(), + lines: lines.join(" ").into(), } } - paths.join(" ") + + fn from_feature(feature: &Feature) -> Option { + let kind = feature.property("kind").and_then(|kind| kind.as_str()); + Some(Self::new(kind, feature.geometry.as_ref()?)) + } +} + +fn calculate_shapes(geojson: &GeoJson) -> Vec { + match geojson { + GeoJson::Geometry(geometry) => vec![FeatureShapes::new(None, geometry)], + GeoJson::Feature(feature) => FeatureShapes::from_feature(feature).into_iter().collect(), + GeoJson::FeatureCollection(collection) => collection + .into_iter() + .filter_map(FeatureShapes::from_feature) + .collect(), + } } impl yew::Component for WorldMapComp { @@ -143,39 +161,39 @@ impl yew::Component for WorldMapComp { type Properties = WorldMap; fn create(ctx: &Context) -> Self { - let path = calculate_path(&ctx.props().map_data); Self { - path, + shapes: calculate_shapes(&ctx.props().map_data), _phantom_data: PhantomData::, } } fn view(&self, ctx: &Context) -> Html { let props = ctx.props(); - - Map::new( - Path::new() - .d(self.path.clone()) - .style("vector-effect", "non-scaling-stroke") - .style("stroke-width", "0.2px") - .style("fill", "var(--pwt-color-neutral)") - .style("stroke", "var(--pwt-color-on-neutral)"), - ) - .with_std_props(&props.std_props) - .listeners(&props.listeners) - .style("background-color", "var(--pwt-color-surface)") - .width(WIDTH) - .height(HEIGHT) - .max_zoom_level(props.max_zoom_level) - .info_point_radius(props.info_point_radius) - .points(props.points.clone()) - .into() + let features = self.shapes.iter().map(|feature| { + Group::new() + .class("pwt-world-map-feature") + .attribute("data-kind", feature.kind.clone()) + .with_child(Path::new().d(feature.areas.clone())) + .with_child(Path::new().d(feature.lines.clone()).fill("none")) + .into() + }); + + Map::new(Group::new().children(features)) + .with_std_props(&props.std_props) + .listeners(&props.listeners) + .class("pwt-world-map") + .width(WIDTH) + .height(HEIGHT) + .max_zoom_level(props.max_zoom_level) + .info_point_radius(props.info_point_radius) + .points(props.points.clone()) + .into() } fn changed(&mut self, ctx: &Context, old_props: &Self::Properties) -> bool { let props = ctx.props(); if !Rc::ptr_eq(&props.map_data, &old_props.map_data) { - self.path = calculate_path(&props.map_data); + self.shapes = calculate_shapes(&props.map_data); } true } @@ -210,29 +228,26 @@ fn line_to_path(coordinates: &[Vec]) -> String { path } -fn parse_geometry(geometry: &Geometry) -> Vec { - let mut paths = Vec::new(); - +fn parse_geometry(geometry: &Geometry, areas: &mut Vec, lines: &mut Vec) { match &geometry.value { geojson::Value::Polygon(polygon) => { - paths.append(&mut polygon.iter().map(|ring| ring_to_path(ring)).collect()); + areas.append(&mut polygon.iter().map(|ring| ring_to_path(ring)).collect()); } geojson::Value::MultiPolygon(items) => { for poly in items { - paths.append(&mut poly.iter().map(|ring| ring_to_path(ring)).collect()) + areas.append(&mut poly.iter().map(|ring| ring_to_path(ring)).collect()) } } - geojson::Value::LineString(line) => paths.push(line_to_path(line)), + geojson::Value::LineString(line) => lines.push(line_to_path(line)), geojson::Value::MultiLineString(line) => { - paths.append(&mut line.iter().map(|ring| line_to_path(ring)).collect()) + lines.append(&mut line.iter().map(|ring| line_to_path(ring)).collect()) } geojson::Value::GeometryCollection(items) => { for geom in items { - paths.append(&mut parse_geometry(geom)); + parse_geometry(geom, areas, lines); } } geojson::Value::Point(_) => {} geojson::Value::MultiPoint(_) => {} } - paths } -- 2.47.3