public inbox for yew-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [PATCH yew-widget-toolkit] widget: pie chart: improve constructor interface
@ 2026-04-10  6:47 Dominik Csapak
  2026-04-10  7:37 ` applied: " Dietmar Maurer
  0 siblings, 1 reply; 2+ messages in thread
From: Dominik Csapak @ 2026-04-10  6:47 UTC (permalink / raw)
  To: yew-devel

instead of determining the chart type by the number of values, use
a dedicated field for it and use different constructors to generate
the different types of chart.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
Note this makes the gauge panel series for PDM[0] not valid, since the
interface changes. I'll send a new version there as well.

0: https://lore.proxmox.com/pdm-devel/20260402132111.3221890-1-d.csapak@proxmox.com/

 src/widget/charts/pie.rs | 75 +++++++++++++++++++++++++---------------
 1 file changed, 47 insertions(+), 28 deletions(-)

diff --git a/src/widget/charts/pie.rs b/src/widget/charts/pie.rs
index 5273c7e..0cf6709 100644
--- a/src/widget/charts/pie.rs
+++ b/src/widget/charts/pie.rs
@@ -53,6 +53,12 @@ pub enum LegendPosition {
     Bottom,
 }
 
+#[derive(Debug, Clone, PartialEq)]
+enum ChartVariant {
+    Gauge,
+    Pie,
+}
+
 #[widget(pwt=crate, comp=PwtPieChart, @element)]
 #[derive(Properties, Clone, PartialEq)]
 #[builder]
@@ -63,6 +69,7 @@ pub struct PieChart {
     /// The ratio of the ring to the inner radius of the pie chart, must be between 0.01 and 1.0.
     ///
     /// 1.0 results in a pie chart, values below in a donut shaped one.
+    /// Default is 1.0 for when using [PieChart::pie], 0.3 otherwise.
     thickness_ratio: f64,
 
     // the values to show. if only one, it's assumed to be between 0.0 and 1.0
@@ -123,29 +130,45 @@ pub struct PieChart {
     #[prop_or(360.0)]
     /// The ending angle of the chart in degrees, default is 360.0,
     angle_end: f64,
+
+    variant: ChartVariant,
 }
 
 impl PieChart {
     /// Creates a new chart with a single value from 0.0 to 1.0 (smaller and larger values will
     /// be clamped).
-    pub fn new(title: impl Into<AttrValue>, value: f64) -> Self {
+    pub fn gauge(title: impl Into<AttrValue>, value: f64) -> Self {
         yew::props!(Self {
-            values: vec![(title.into(), value.clamp(0.0, 1.0))]
+            values: vec![(title.into(), value.clamp(0.0, 1.0))],
+            variant: ChartVariant::Gauge,
         })
     }
 
-    /// Creates a new chart with multiple relative values.
-    pub fn with_values(values: Vec<(impl Into<AttrValue>, f64)>) -> Self {
+    /// Creates a new pie chart with multiple relative values.
+    pub fn pie(values: Vec<(impl Into<AttrValue>, f64)>) -> Self {
         yew::props!(Self {
             values: values
                 .into_iter()
                 .map(|(title, value)| (title.into(), value))
-                .collect::<Vec<_>>()
+                .collect::<Vec<_>>(),
+            variant: ChartVariant::Pie,
+            thickness_ratio: 1.0,
+        })
+    }
+
+    /// Creates a new donut chart with multiple relative values.
+    pub fn donut(values: Vec<(impl Into<AttrValue>, f64)>) -> Self {
+        yew::props!(Self {
+            values: values
+                .into_iter()
+                .map(|(title, value)| (title.into(), value))
+                .collect::<Vec<_>>(),
+            variant: ChartVariant::Pie,
         })
     }
 
     fn segment_get_color(&self, index: usize, value: f64) -> String {
-        if self.values.len() == 1 {
+        if self.variant == ChartVariant::Gauge {
             for (threshold, color) in GAUGE_COLORS {
                 if value >= *threshold {
                     return color.to_string();
@@ -171,12 +194,9 @@ impl PieChart {
     }
 
     fn effective_legend_position(&self) -> LegendPosition {
-        self.legend.unwrap_or({
-            if self.values.len() == 1 {
-                LegendPosition::Hidden
-            } else {
-                LegendPosition::End
-            }
+        self.legend.unwrap_or(match self.variant {
+            ChartVariant::Gauge => LegendPosition::Hidden,
+            ChartVariant::Pie => LegendPosition::End,
         })
     }
 }
@@ -279,21 +299,20 @@ impl PwtPieChart {
         let props = ctx.props();
         let mut segments = Vec::with_capacity(props.values.len());
 
-        let sum = if props.values.len() == 1 {
-            1.0
-        } else {
-            props
-                .values
-                .iter()
-                .enumerate()
-                .filter_map(|(index, (_, value))| (!self.hidden.contains(&index)).then_some(value))
-                .sum()
-        };
-
-        let allow_highlight = if props.values.len() == 1 {
-            false
-        } else {
-            props.allow_highlight.unwrap_or(true)
+        let (sum, allow_highlight) = match props.variant {
+            ChartVariant::Gauge => (1.0, false),
+            ChartVariant::Pie => {
+                let sum = props
+                    .values
+                    .iter()
+                    .enumerate()
+                    .filter_map(|(index, (_, value))| {
+                        (!self.hidden.contains(&index)).then_some(value)
+                    })
+                    .sum();
+
+                (sum, props.allow_highlight.unwrap_or(true))
+            }
         };
 
         let mut last_value = 0.0;
@@ -443,7 +462,7 @@ impl Component for PwtPieChart {
         let mut group = Group::new().style("transform", "rotate(90deg)");
 
         // background for the 'gauge' type chart
-        if props.values.len() == 1 {
+        if props.variant == ChartVariant::Gauge {
             group.add_child(
                 Circle::new()
                     .fill("none")
-- 
2.47.3





^ permalink raw reply	[flat|nested] 2+ messages in thread

* applied: [PATCH yew-widget-toolkit] widget: pie chart: improve constructor interface
  2026-04-10  6:47 [PATCH yew-widget-toolkit] widget: pie chart: improve constructor interface Dominik Csapak
@ 2026-04-10  7:37 ` Dietmar Maurer
  0 siblings, 0 replies; 2+ messages in thread
From: Dietmar Maurer @ 2026-04-10  7:37 UTC (permalink / raw)
  To: Dominik Csapak, yew-devel

applied




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-04-10  7:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-04-10  6:47 [PATCH yew-widget-toolkit] widget: pie chart: improve constructor interface Dominik Csapak
2026-04-10  7:37 ` applied: " Dietmar Maurer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal