Skip to content
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

Added new dz! macros in dataframe.rs to create dataframe from vector of mixed data type vectors #70

Closed
wants to merge 8 commits into from
66 changes: 66 additions & 0 deletions charming/src/datatype/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,69 @@ macro_rules! df {
]
};
}

/// Code below is modified from ChatGPT generated code.
/// The main objective of macro dz! is to transpose mixed data type vectors,
/// aka columns or dimensions into ECharts dataframe format.

#[macro_export]
macro_rules! dz {
/*
Handle named vectors
let d1 = vec![44056, 13334];
let d2 = vec![81.8, 76.9];
let d3 = vec![23968973, 1376048943];
let d4 = vec!["Australia", "China"];
let d5 = vec![2015, 2015];
let df = dz!(d1, d2, d3, d4, d5);
>>df = [Value(Array([Number(Integer(44056)), Number(Float(81.8)),
Number(Integer(23968973)), String("Australia"), Number(Integer(2015))])),
Value(Array([Number(Integer(13334)), Number(Float(76.9)),
Number(Integer(1376048943)), String("China"), Number(Integer(2015))]))]
*/
($($v:expr),* $(,)?) => {{
let mut df = Vec::new();
let mut iterators: Vec<Box<dyn Iterator<Item = $crate::datatype::CompositeValue>>> = vec![
$(Box::new($v.into_iter().map(|x| $crate::datatype::CompositeValue::from(x))),)*
];

let len = iterators.first().map(|v| v.size_hint().0).unwrap_or(0);

for _ in 0..len {
let mut row = Vec::new();
for iter in iterators.iter_mut() {
if let Some(value) = iter.next() {
row.push(value);
}
}
let z = $crate::datatype::DataPoint::from(row);
df.push(z);
}
df
}};

/*
Handle direct input of vectors
let df = dz!([44056, 13334], [81.8, 76.9], [23968973, 1376048943], ["Australia", "China"], [2015, 2015]);
>>df = [Value(Array([Number(Integer(44056)), Number(Float(81.8)),
Number(Integer(23968973)), String("Australia"), Number(Integer(2015))])),
Value(Array([Number(Integer(13334)), Number(Float(76.9)),
Number(Integer(1376048943)), String("China"), Number(Integer(2015))]))]
*/
($([$($v:expr),*]),* $(,)?) => {{
let mut df = Vec::new();
let mut iterators: Vec<_> = vec![$(vec![$($v.into()),*].into_iter()),*];

let len = iterators.first().map(|v| v.len()).unwrap_or(0);
for _ in 0..len {
let mut row = Vec::new();
for iter in &mut iterators {
if let Some($crate::datatype::CompositeValue) = iter.next() {
row.push($crate::datatype::CompositeValue);
}
}
df.push(row);
}
df
}};
}
13 changes: 11 additions & 2 deletions charming/src/series/scatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::Serialize;
use crate::{
datatype::{DataFrame, DataPoint},
element::{
ColorBy, CoordinateSystem, DimensionEncode, Emphasis, ItemStyle, MarkArea, MarkLine,
ColorBy, CoordinateSystem, DimensionEncode, Emphasis, ItemStyle, Label, MarkArea, MarkLine,
Symbol, SymbolSize,
},
};
Expand All @@ -19,10 +19,13 @@ pub struct Scatter {

#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
color_by: Option<ColorBy>,

#[serde(skip_serializing_if = "Option::is_none")]
label: Option<Label>,

#[serde(skip_serializing_if = "Option::is_none")]
dataset_index: Option<f64>,

Expand Down Expand Up @@ -67,6 +70,7 @@ impl Scatter {
id: None,
name: None,
color_by: None,
label: None,
dataset_index: None,
coordinate_system: None,
x_axis_index: None,
Expand Down Expand Up @@ -97,6 +101,11 @@ impl Scatter {
self
}

pub fn label<L: Into<Label>>(mut self, label: L) -> Self {
self.label = Some(label.into());
self
}

pub fn dataset_index<F: Into<f64>>(mut self, dataset_index: F) -> Self {
self.dataset_index = Some(dataset_index.into());
self
Expand Down
Loading