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