-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Table
trace.
#165
Closed
Closed
Add Table
trace.
#165
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bb6a633
Add `Table` trace
c7bf0d8
First commit
3949709
add
2af0eea
reductant_code
f5931fd
Fill_Color
f87377d
Fill
0066b8e
Tables now take a header and cells struct
kylemello 9da5c2a
Merge pull request #1 from kylemello/master
baiguoname File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,6 +201,7 @@ pub enum PlotType { | |
Ohlc, | ||
Sankey, | ||
Surface, | ||
Table, | ||
} | ||
|
||
#[derive(Serialize, Clone, Debug)] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
//! 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 test_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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@baiguoname There is a typo here. This should be
trace