Skip to main content

plotkit_core/charts/
line.rs

1//! Line chart builder methods.
2//!
3//! This module extends [`LineArtist`] with a fluent API for configuring
4//! line chart properties. Since [`Axes::plot`] returns `Result<&mut LineArtist>`,
5//! these builder methods can be chained directly on the return value:
6//!
7//! ```ignore
8//! ax.plot(&x, &y)?
9//!     .color(Color::rgb(0.2, 0.4, 0.8))
10//!     .width(2.0)
11//!     .style(LineStyle::Dashed)
12//!     .label("Series A")
13//!     .alpha(0.9);
14//! ```
15
16use crate::artist::LineArtist;
17use crate::primitives::Color;
18use crate::theme::LineStyle;
19
20impl LineArtist {
21    /// Sets the line color.
22    ///
23    /// Accepts any [`Color`] value, which can be constructed from RGB components,
24    /// hex strings, or named color constants.
25    ///
26    /// # Examples
27    ///
28    /// ```ignore
29    /// artist.color(Color::rgb(1.0, 0.0, 0.0)); // red
30    /// ```
31    pub fn color(&mut self, color: Color) -> &mut Self {
32        self.color = color;
33        self
34    }
35
36    /// Sets the line width in pixels.
37    ///
38    /// A width of `1.0` is the default hairline width. Values below `1.0` may
39    /// produce sub-pixel rendering depending on the backend.
40    ///
41    /// # Examples
42    ///
43    /// ```ignore
44    /// artist.width(2.5);
45    /// ```
46    pub fn width(&mut self, width: f64) -> &mut Self {
47        self.width = width;
48        self
49    }
50
51    /// Sets the line style (solid, dashed, dotted, dash-dot).
52    ///
53    /// The [`LineStyle`] enum defines the available stroke patterns. The default
54    /// is [`LineStyle::Solid`].
55    ///
56    /// # Examples
57    ///
58    /// ```ignore
59    /// artist.style(LineStyle::Dashed);
60    /// ```
61    pub fn style(&mut self, style: LineStyle) -> &mut Self {
62        self.style = style;
63        self
64    }
65
66    /// Sets the legend label for this line.
67    ///
68    /// When a label is set, the line will appear in the legend if one is
69    /// displayed on the axes. Pass an empty string or omit this call to
70    /// exclude the line from the legend.
71    ///
72    /// # Examples
73    ///
74    /// ```ignore
75    /// artist.label("Temperature");
76    /// ```
77    pub fn label(&mut self, label: &str) -> &mut Self {
78        self.label = Some(label.to_string());
79        self
80    }
81
82    /// Sets the opacity (0.0 = fully transparent, 1.0 = fully opaque).
83    ///
84    /// The value is clamped to the `[0.0, 1.0]` range. The default opacity
85    /// is `1.0`.
86    ///
87    /// # Examples
88    ///
89    /// ```ignore
90    /// artist.alpha(0.5); // 50% transparent
91    /// ```
92    pub fn alpha(&mut self, alpha: f64) -> &mut Self {
93        self.alpha = alpha.clamp(0.0, 1.0);
94        self
95    }
96}