pub struct BarChart<C: PixelColor> { /* private fields */ }Expand description
Bar chart implementation for displaying categorical data.
A bar chart displays data using rectangular bars with lengths proportional to the values they represent. This implementation supports both vertical and horizontal orientations, making it suitable for various data visualization needs on embedded displays.
§Features
- Configurable bar orientation (vertical/horizontal)
- Flexible bar width options (fixed, proportional, automatic)
- Multi-color support with automatic color cycling
- Customizable spacing and borders
- Support for stacked bars (feature-gated)
- Memory-efficient static allocation
§Memory Usage
The bar chart uses static allocation with:
- Up to 16 bar colors stored in a heapless vector
- Screen coordinate calculations for bar positioning
- Efficient rendering with minimal temporary storage
§Examples
Basic vertical bar chart:
use embedded_charts::prelude::*;
use embedded_graphics::pixelcolor::Rgb565;
let chart: BarChart<Rgb565> = BarChart::new();
let mut data: StaticDataSeries<Point2D, 256> = StaticDataSeries::new();
data.push(Point2D::new(0.0, 10.0))?;
data.push(Point2D::new(1.0, 20.0))?;Horizontal bar chart with custom styling:
use embedded_charts::prelude::*;
use embedded_graphics::pixelcolor::Rgb565;
let chart = BarChart::builder()
.orientation(BarOrientation::Horizontal)
.bar_width(BarWidth::Fixed(25))
.spacing(3)
.colors(&[Rgb565::GREEN])
.build()?;Implementations§
Source§impl<C> BarChart<C>
impl<C> BarChart<C>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new bar chart with default styling.
This creates a bar chart with:
- Vertical orientation
- Single blue bar color
- Automatic bar width
- 5-pixel spacing between bars
- No borders
- Default margins (10 pixels on all sides)
§Examples
use embedded_charts::prelude::*;
use embedded_graphics::pixelcolor::Rgb565;
let chart: BarChart<Rgb565> = BarChart::new();Sourcepub fn builder() -> BarChartBuilder<C>
pub fn builder() -> BarChartBuilder<C>
Create a builder for configuring the bar chart.
The builder pattern provides a fluent interface for configuring all aspects of the bar chart before creation.
§Examples
use embedded_charts::prelude::*;
use embedded_graphics::pixelcolor::Rgb565;
let chart = BarChart::builder()
.orientation(BarOrientation::Horizontal)
.bar_width(BarWidth::Fixed(30))
.spacing(8)
.colors(&[Rgb565::GREEN])
.build()?;Sourcepub fn set_style(&mut self, style: BarChartStyle<C>)
pub fn set_style(&mut self, style: BarChartStyle<C>)
Set the bar chart style configuration.
This replaces the entire style configuration with the provided one. Use the builder pattern for more granular control.
§Arguments
style- The new bar chart style configuration
§Examples
use embedded_charts::prelude::*;
use embedded_graphics::pixelcolor::Rgb565;
let mut chart: BarChart<Rgb565> = BarChart::new();
let mut colors = heapless::Vec::new();
colors.push(Rgb565::BLUE).unwrap();
colors.push(Rgb565::RED).unwrap();
let style = BarChartStyle {
bar_colors: colors,
bar_width: BarWidth::Fixed(25),
spacing: 3,
border: None,
stacked: false,
};
chart.set_style(style);Sourcepub fn style(&self) -> &BarChartStyle<C>
pub fn style(&self) -> &BarChartStyle<C>
Get the current bar chart style configuration.
Returns a reference to the current style configuration, allowing inspection of current settings.
§Returns
A reference to the current BarChartStyle
Sourcepub fn set_config(&mut self, config: ChartConfig<C>)
pub fn set_config(&mut self, config: ChartConfig<C>)
Set the chart configuration.
This includes general chart settings like title, background color, margins, and grid visibility.
§Arguments
config- The new chart configuration
Sourcepub fn config(&self) -> &ChartConfig<C>
pub fn config(&self) -> &ChartConfig<C>
Get the chart configuration
Sourcepub fn set_orientation(&mut self, orientation: BarOrientation)
pub fn set_orientation(&mut self, orientation: BarOrientation)
Set the bar orientation
Sourcepub fn orientation(&self) -> BarOrientation
pub fn orientation(&self) -> BarOrientation
Get the bar orientation