From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 5AD3C1FF140 for ; Fri, 10 Apr 2026 08:48:05 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id CF42414C0B; Fri, 10 Apr 2026 08:48:49 +0200 (CEST) From: Dominik Csapak To: yew-devel@lists.proxmox.com Subject: [PATCH yew-widget-toolkit] widget: pie chart: improve constructor interface Date: Fri, 10 Apr 2026 08:47:19 +0200 Message-ID: <20260410064844.337358-1-d.csapak@proxmox.com> X-Mailer: git-send-email 2.47.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.050 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment 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: YWQDTFJDMNAXLIPEZ6T3WU73QWK34XIA X-Message-ID-Hash: YWQDTFJDMNAXLIPEZ6T3WU73QWK34XIA 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: Yew framework devel list at Proxmox List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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, value: f64) -> Self { + pub fn gauge(title: impl Into, 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, f64)>) -> Self { + /// Creates a new pie chart with multiple relative values. + pub fn pie(values: Vec<(impl Into, f64)>) -> Self { yew::props!(Self { values: values .into_iter() .map(|(title, value)| (title.into(), value)) - .collect::>() + .collect::>(), + variant: ChartVariant::Pie, + thickness_ratio: 1.0, + }) + } + + /// Creates a new donut chart with multiple relative values. + pub fn donut(values: Vec<(impl Into, f64)>) -> Self { + yew::props!(Self { + values: values + .into_iter() + .map(|(title, value)| (title.into(), value)) + .collect::>(), + 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