Skip to main content

plotkit_core/charts/
fill_between.rs

1//! Fill-between chart builder methods.
2//!
3//! This module extends [`FillBetweenArtist`] with a fluent API for configuring
4//! the visual properties of a filled region between two curves. Since
5//! [`Axes::fill_between`] returns `Result<&mut FillBetweenArtist>`, these
6//! builder methods can be chained directly on the return value:
7//!
8//! ```ignore
9//! ax.fill_between(&x, &y1, &y2)?
10//!     .color(Color::rgb(78, 121, 167))
11//!     .label("Confidence interval")
12//!     .alpha(0.3);
13//! ```
14
15use crate::artist::FillBetweenArtist;
16use crate::primitives::Color;
17
18impl FillBetweenArtist {
19    /// Sets the fill color.
20    ///
21    /// Applies the given [`Color`] to the entire shaded region between the
22    /// two boundary curves. Any previously set color is replaced.
23    ///
24    /// # Arguments
25    ///
26    /// * `color` - The [`Color`] to fill the region with.
27    ///
28    /// # Examples
29    ///
30    /// ```ignore
31    /// artist.color(Color::rgb(78, 121, 167));
32    /// ```
33    pub fn color(&mut self, color: Color) -> &mut Self {
34        self.color = color;
35        self
36    }
37
38    /// Sets the legend label.
39    ///
40    /// When a legend is displayed on the figure, this label will appear
41    /// next to the color swatch for this fill region. Passing a new value
42    /// overwrites any previously set label.
43    ///
44    /// # Arguments
45    ///
46    /// * `label` - A string slice that will be stored as the legend entry.
47    ///
48    /// # Examples
49    ///
50    /// ```ignore
51    /// artist.label("Confidence interval");
52    /// ```
53    pub fn label(&mut self, label: &str) -> &mut Self {
54        self.label = Some(label.to_string());
55        self
56    }
57
58    /// Sets the opacity (0.0 = fully transparent, 1.0 = fully opaque).
59    ///
60    /// The value is clamped to the `[0.0, 1.0]` range. The default opacity
61    /// is `1.0`. Lower values are useful for layering multiple fill regions
62    /// so that underlying data remains visible.
63    ///
64    /// # Arguments
65    ///
66    /// * `alpha` - The desired opacity level.
67    ///
68    /// # Examples
69    ///
70    /// ```ignore
71    /// artist.alpha(0.3); // 30% opaque, letting background show through
72    /// ```
73    pub fn alpha(&mut self, alpha: f64) -> &mut Self {
74        self.alpha = alpha.clamp(0.0, 1.0);
75        self
76    }
77}