pub struct BarArtist {
pub categories: Categories,
pub heights: Series,
pub color: Color,
pub label: Option<String>,
pub alpha: f64,
pub horizontal: bool,
pub bar_width: f64,
pub bottom: Option<Vec<f64>>,
pub offset: Option<Vec<f64>>,
}Expand description
A bar chart rendering vertical or horizontal bars over categorical data.
Categories are placed at integer positions 0, 1, 2, ... on the
category axis, with each bar centered on its position. The bar_width
field controls the fraction of the inter-category spacing that the bar
occupies (1.0 = bars touching, 0.5 = half-width with gaps).
For stacked bars, set bottom to offset each bar from a baseline other
than zero. For grouped (side-by-side) bars, adjust category positions and
bar_width for each series.
Fields§
§categories: CategoriesCategory labels for the bar axis.
heights: SeriesBar heights (or lengths, for horizontal bars).
color: ColorFill color of the bars.
label: Option<String>Optional legend label. When Some, the bar series appears in the legend.
alpha: f64Opacity from 0.0 (fully transparent) to 1.0 (fully opaque).
horizontal: boolWhen true, bars extend horizontally (categories on the y-axis).
bar_width: f64Bar width as a fraction of the category spacing (0.0, 1.0].
bottom: Option<Vec<f64>>Optional per-bar base offset for stacking.
When Some, each bar starts at bottom[i] instead of 0.0 and extends
to bottom[i] + heights[i]. The length must equal heights.len().
offset: Option<Vec<f64>>Optional per-bar x-position offset for grouped (side-by-side) bars.
When Some, each bar’s category center is shifted by offset[i] data
units. The length must equal heights.len().
Implementations§
Source§impl BarArtist
impl BarArtist
Sourcepub fn data_bounds(&self) -> (f64, f64, f64, f64)
pub fn data_bounds(&self) -> (f64, f64, f64, f64)
Computes the data-space bounding box (xmin, xmax, ymin, ymax).
For vertical bars, the x-axis spans from -0.5 to n - 0.5 (where
n is the number of categories) so that bars are centered on integer
positions. The y-axis spans from 0.0 to the tallest bar, with a
fallback of (0.0, 1.0) when the heights series is empty.
When bottom is set, the value axis includes both the bottom offsets
and bottom + height values. When offset is set, the category axis
is expanded to accommodate shifted bar positions.
For horizontal bars the axes are transposed: the y-axis holds the category positions and the x-axis holds the bar lengths.
Source§impl BarArtist
impl BarArtist
Sourcepub fn label(&mut self, label: &str) -> &mut Self
pub fn label(&mut self, label: &str) -> &mut Self
Sets the legend label.
When a legend is displayed on the figure, this label will appear next to the color swatch for this bar series. Passing a new value overwrites any previously set label.
§Arguments
label- A string slice that will be stored as the legend entry.
Sourcepub fn alpha(&mut self, alpha: f64) -> &mut Self
pub fn alpha(&mut self, alpha: f64) -> &mut Self
Sets the opacity.
The value is clamped to the range [0.0, 1.0], where 0.0 is fully
transparent and 1.0 is fully opaque.
§Arguments
alpha- The desired opacity level.
Sourcepub fn bar_width(&mut self, width: f64) -> &mut Self
pub fn bar_width(&mut self, width: f64) -> &mut Self
Sets the bar width as a fraction of the category spacing (0.0 to 1.0).
Smaller values produce thinner bars with more whitespace between them,
while larger values make the bars wider. The value is clamped to the
range [0.1, 1.0] so that bars are never invisibly thin nor overlap
their neighbours.
§Arguments
width- The fraction of available category space each bar should occupy.
Sourcepub fn bottom(&mut self, bottom: Vec<f64>) -> &mut Self
pub fn bottom(&mut self, bottom: Vec<f64>) -> &mut Self
Sets the bottom offset for each bar (for stacking).
When stacking multiple bar series, set bottom for each subsequent
series to the cumulative heights of the series below it. Each bar
then starts at bottom[i] instead of 0.0 and extends to
bottom[i] + height[i].
§Arguments
bottom- A vector of base offsets, one per bar. Must have the same length as the heights vector.
§Example
// Stack series B on top of series A:
ax.bar(cats, &heights_a)?.label("A");
ax.bar(cats, &heights_b)?.bottom(heights_a.clone()).label("B");Sourcepub fn offset(&mut self, offset: Vec<f64>) -> &mut Self
pub fn offset(&mut self, offset: Vec<f64>) -> &mut Self
Sets the per-bar position offset for grouped (side-by-side) bars.
Each bar’s category center is shifted by offset[i] units along the
category axis. This is useful for placing multiple bar series next to
each other within the same categories.
For most grouped-bar use cases, prefer [Axes::bar_group] which
computes offsets automatically.
§Arguments
offset- A vector of position offsets, one per bar.