From: Dominik Csapak <d.csapak@proxmox.com>
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 [thread overview]
Message-ID: <20260410064844.337358-1-d.csapak@proxmox.com> (raw)
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
next reply other threads:[~2026-04-10 6:48 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-10 6:47 Dominik Csapak [this message]
2026-04-10 7:37 ` applied: " Dietmar Maurer
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=20260410064844.337358-1-d.csapak@proxmox.com \
--to=d.csapak@proxmox.com \
--cc=yew-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.