plotlars/components/
cell.rs

1use plotly::common::Font;
2use plotly::traces::table::Cells as CellsPlotly;
3
4use crate::components::Rgb;
5
6/// A structure representing cell formatting for tables.
7///
8/// The `Cell` struct allows customization of table cells including height,
9/// alignment, font, and fill color.
10///
11/// # Example
12///
13/// ```rust
14/// use plotlars::{Table, Cell, Plot, Text, Rgb};
15/// use polars::prelude::*;
16///
17/// let dataset = df![
18///     "product" => &["Laptop", "Mouse", "Keyboard", "Monitor"],
19///     "price" => &[999.99, 29.99, 79.99, 299.99],
20///     "stock" => &[15, 250, 87, 42]
21/// ]
22/// .unwrap();
23///
24/// let cell = Cell::new()
25///     .height(30.0)
26///     .align("left")
27///     .font("Arial")
28///     .fill(Rgb(240, 240, 240));
29///
30/// Table::builder()
31///     .data(&dataset)
32///     .columns(vec!["product", "price", "stock"])
33///     .cell(&cell)
34///     .plot_title(Text::from("Product Inventory"))
35///     .build()
36///     .plot();
37/// ```
38///
39/// ![Example](https://imgur.com/FYYcWRH.png)
40#[derive(Clone, Default)]
41pub struct Cell {
42    pub(crate) height: Option<f64>,
43    pub(crate) align: Option<String>,
44    pub(crate) font: Option<String>,
45    pub(crate) fill: Option<Rgb>,
46}
47
48impl Cell {
49    /// Creates a new `Cell` instance with default values.
50    pub fn new() -> Self {
51        Self::default()
52    }
53
54    /// Sets the height of the cells.
55    ///
56    /// # Argument
57    ///
58    /// * `height` - A `f64` value specifying the cell height.
59    pub fn height(mut self, height: f64) -> Self {
60        self.height = Some(height);
61        self
62    }
63
64    /// Sets the alignment of the cell text.
65    ///
66    /// # Argument
67    ///
68    /// * `align` - A string specifying the alignment (left, center, right).
69    pub fn align(mut self, align: impl Into<String>) -> Self {
70        self.align = Some(align.into());
71        self
72    }
73
74    /// Sets the font family of the cell text.
75    ///
76    /// # Argument
77    ///
78    /// * `font` - A string slice specifying the font family name.
79    pub fn font(mut self, font: &str) -> Self {
80        self.font = Some(font.to_string());
81        self
82    }
83
84    /// Sets the fill color of the cells.
85    ///
86    /// # Argument
87    ///
88    /// * `fill` - An `Rgb` value specifying the background color.
89    pub fn fill(mut self, fill: Rgb) -> Self {
90        self.fill = Some(fill);
91        self
92    }
93
94    pub(crate) fn to_plotly<T>(&self, values: Vec<Vec<T>>) -> CellsPlotly<T>
95    where
96        T: serde::Serialize + Clone + Default + 'static,
97    {
98        let mut cells = CellsPlotly::new(values);
99
100        if let Some(height) = self.height {
101            cells = cells.height(height);
102        }
103
104        if let Some(align) = &self.align {
105            cells = cells.align(align.as_str());
106        }
107
108        if let Some(font) = &self.font {
109            cells = cells.font(Font::new().family(font.as_str()));
110        }
111
112        if let Some(fill) = &self.fill {
113            cells = cells.fill(plotly::traces::table::Fill::new().color(fill.to_plotly()));
114        }
115
116        cells
117    }
118}