plotly_fork/traces/
table.rs

1//! Table trace
2
3use plotly_derive::FieldSetter;
4use serde::Serialize;
5
6use crate::{
7    color::Color,
8    common::{Font, Line, PlotType, Visible},
9    Trace,
10};
11
12#[serde_with::skip_serializing_none]
13#[derive(Serialize, Clone, Debug, FieldSetter)]
14#[field_setter(box_self, kind = "trace")]
15pub struct Table<T, N>
16where
17    T: Serialize + Clone + 'static,
18    N: Serialize + Clone + 'static,
19{
20    #[field_setter(default = "PlotType::Table")]
21    r#type: PlotType,
22    /// Sets the trace name. The trace name appear as the legend item and on
23    /// hover.
24    name: Option<String>,
25    #[serde(rename = "columnorder")]
26    /// Determines whether or not this trace is visible. If
27    /// `Visible::LegendOnly`, the trace is not drawn, but can appear as a
28    /// legend item (provided that the legend itself is visible).
29    visible: Option<Visible>,
30    ///Specifies the rendered order of the data columns; for example, a value `2` at position `0`,
31    ///means that column index `0` in the data will be rendered as the,
32    ///third column, as columns have an index base of zero.
33    column_order: Option<Vec<usize>>,
34    #[serde(rename = "columnwidth")]
35    ///The width of columns expressed as a ratio. Columns fill the available width,
36    ///in proportion of their specified column widths.
37    column_width: Option<f64>,
38    ///Header cell values. `values[m][n]` represents the value of the `n`th point in column `m`,,
39    ///therefore the `values[m]` vector length for all columns must be the same (longer vectors,
40    ///will be truncated). Each value must be a finite number or a string.
41    header: Option<Header<T>>,
42    ///Cell values. `values[m][n]` represents the value of the `n`th point in column `m`,,
43    ///therefore the `values[m]` vector length for all columns must be the same (longer vectors,
44    ///will be truncated). Each value must be a finite number or a string.
45    cells: Option<Cells<N>>,
46}
47
48impl<T, N> Table<T, N>
49where
50    T: Serialize + Clone + Default + 'static,
51    N: Serialize + Clone + Default + 'static,
52{
53    pub fn new(header: Vec<T>, cells: Vec<Vec<N>>) -> Box<Self> {
54        Box::new(Table {
55            header: Header::new().values(header).into(),
56            cells: Cells::new().values(cells).into(),
57            ..Default::default()
58        })
59    }
60}
61
62impl<T, N> Trace for Table<T, N>
63where
64    T: Serialize + Clone + 'static,
65    N: Serialize + Clone + 'static,
66{
67    fn to_json(&self) -> String {
68        serde_json::to_string(self).unwrap()
69    }
70}
71
72#[serde_with::skip_serializing_none]
73#[derive(Serialize, Clone, Debug, FieldSetter)]
74pub struct Cells<N> {
75    ///Cell values. `values[m][n]` represents the value of the `n`th point in column `m`,
76    ///therefore the `values[m]` vector length for all columns must be the same (longer vectors,
77    ///will be truncated). Each value must be a finite number or a string
78    values: Option<Vec<Vec<N>>>,
79    ///Prefix for cell values.
80    prefix: Option<String>,
81    ///Suffix for cell values.
82    suffix: Option<String>,
83    height: Option<f64>,
84    align: Option<String>,
85    line: Option<Line>,
86    ///Sets the cell fill color. It accepts either a specific color,
87    ///or an array of colors or a 2D array of colors
88    fill: Option<Fill>,
89    font: Option<Font>,
90}
91
92impl<N> Cells<N>
93where
94    N: Serialize + Clone + Default + 'static,
95{
96    pub fn new() -> Self {
97        Default::default()
98    }
99}
100
101#[serde_with::skip_serializing_none]
102#[derive(Serialize, Clone, Debug, FieldSetter)]
103pub struct Header<T> {
104    ///Header cell values. `values[m][n]` represents the value of the `n`th point in column `m`,
105    ///therefore the `values[m]` vector length for all columns must be the same (longer vectors,
106    ///will be truncated). Each value must be a finite number or a string.
107    values: Option<Vec<T>>,
108    ///Prefix for cell values.
109    prefix: Option<String>,
110    ///Suffix for cell values.
111    suffix: Option<String>,
112    height: Option<f64>,
113    align: Option<String>,
114    line: Option<Line>,
115    ///Sets the cell fill color. It accepts either a specific color,
116    ///or an array of colors or a 2D array of colors
117    fill: Option<Fill>,
118    font: Option<Font>,
119}
120
121impl<T> Header<T>
122where
123    T: Serialize + Clone + Default + 'static,
124{
125    pub fn new() -> Self {
126        Default::default()
127    }
128}
129
130#[serde_with::skip_serializing_none]
131#[derive(Serialize, Clone, Debug, FieldSetter)]
132pub struct Fill {
133    color: Option<Box<dyn Color>>,
134}
135
136impl Fill {
137    pub fn new() -> Self {
138        Default::default()
139    }
140}
141
142#[cfg(test)]
143mod tests {
144    use serde_json::{json, to_value};
145
146    use super::*;
147
148    #[test]
149    fn test_serialize_table() {
150        let columns = vec![String::from("col1"), String::from("col2")];
151        let values = vec![vec![1, 2], vec![2, 3]];
152        let trace = Table::new(columns.clone(), values);
153
154        let expected = json!({
155            "type": "table",
156            "cells": {
157                "values": [[1, 2], [2, 3]],
158            },
159            "header": {
160                "values": ["col1", "col2"],
161            },
162        });
163
164        assert_eq!(to_value(trace).unwrap(), expected);
165    }
166}