all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
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	[thread overview]
Message-ID: <20261127161241.3713494-17-d.csapak@proxmox.com> (raw)
In-Reply-To: <20261127161241.3713494-1-d.csapak@proxmox.com>

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 <d.csapak@proxmox.com>
---
 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<T: MapPointData> From<WorldPoint<T>> for MapPoint<T> {
 }
 
 /// 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<T>, @element)]
 #[builder]
 #[derive(Properties, Derivative, Clone)]
@@ -110,32 +113,47 @@ impl<T: MapPointData> WorldMap<T> {
 }
 
 pub struct WorldMapComp<T: MapPointData> {
-    path: String,
+    shapes: Vec<FeatureShapes>,
     _phantom_data: PhantomData<T>,
 }
 
-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<AttrValue>,
+    /// 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<Self> {
+        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<FeatureShapes> {
+    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<T: MapPointData + 'static> yew::Component for WorldMapComp<T> {
@@ -143,39 +161,39 @@ impl<T: MapPointData + 'static> yew::Component for WorldMapComp<T> {
     type Properties = WorldMap<T>;
 
     fn create(ctx: &Context<Self>) -> Self {
-        let path = calculate_path(&ctx.props().map_data);
         Self {
-            path,
+            shapes: calculate_shapes(&ctx.props().map_data),
             _phantom_data: PhantomData::<T>,
         }
     }
 
     fn view(&self, ctx: &Context<Self>) -> 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<Self>, 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<f64>]) -> String {
     path
 }
 
-fn parse_geometry(geometry: &Geometry) -> Vec<String> {
-    let mut paths = Vec::new();
-
+fn parse_geometry(geometry: &Geometry, areas: &mut Vec<String>, lines: &mut Vec<String>) {
     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





  parent reply	other threads:[~2026-11-27 16:13 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 ` Dominik Csapak [this message]
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 ` [PATCH yew-widget-toolkit 30/38] widget: charts: map: allow showing labels next to the points Dominik Csapak
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-17-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.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal