plotkit_core/charts/bar.rs
1//! Bar chart builder methods.
2//!
3//! Provides a fluent builder API for configuring [`BarArtist`] instances.
4//! Each method returns `&mut Self`, allowing calls to be chained together
5//! for concise, readable chart construction.
6
7use crate::artist::BarArtist;
8use crate::primitives::Color;
9
10impl BarArtist {
11 /// Sets the bar color.
12 ///
13 /// Applies the given [`Color`] to every bar rendered by this artist.
14 ///
15 /// # Arguments
16 ///
17 /// * `color` - The [`Color`] to fill each bar with.
18 pub fn color(&mut self, color: Color) -> &mut Self {
19 self.color = color;
20 self
21 }
22
23 /// Sets the legend label.
24 ///
25 /// When a legend is displayed on the figure, this label will appear
26 /// next to the color swatch for this bar series. Passing a new value
27 /// overwrites any previously set label.
28 ///
29 /// # Arguments
30 ///
31 /// * `label` - A string slice that will be stored as the legend entry.
32 pub fn label(&mut self, label: &str) -> &mut Self {
33 self.label = Some(label.to_string());
34 self
35 }
36
37 /// Sets the opacity.
38 ///
39 /// The value is clamped to the range `[0.0, 1.0]`, where `0.0` is fully
40 /// transparent and `1.0` is fully opaque.
41 ///
42 /// # Arguments
43 ///
44 /// * `alpha` - The desired opacity level.
45 pub fn alpha(&mut self, alpha: f64) -> &mut Self {
46 self.alpha = alpha.clamp(0.0, 1.0);
47 self
48 }
49
50 /// Sets the bar width as a fraction of the category spacing (0.0 to 1.0).
51 ///
52 /// Smaller values produce thinner bars with more whitespace between them,
53 /// while larger values make the bars wider. The value is clamped to the
54 /// range `[0.1, 1.0]` so that bars are never invisibly thin nor overlap
55 /// their neighbours.
56 ///
57 /// # Arguments
58 ///
59 /// * `width` - The fraction of available category space each bar should occupy.
60 pub fn bar_width(&mut self, width: f64) -> &mut Self {
61 self.bar_width = width.clamp(0.1, 1.0);
62 self
63 }
64}