From 86c41a1465a32db5f93d73979f2b0bb3ac9c775b Mon Sep 17 00:00:00 2001 From: humphreylee <66808535+humphreylee@users.noreply.github.com> Date: Wed, 31 Jul 2024 20:07:22 +0800 Subject: [PATCH] Added macros "df_transposed" to transpose vectors (mixed data type) into Echarts dataframe format. --- charming/src/datatype/dataframe.rs | 40 ++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/charming/src/datatype/dataframe.rs b/charming/src/datatype/dataframe.rs index 640f2e4..bd87fd7 100644 --- a/charming/src/datatype/dataframe.rs +++ b/charming/src/datatype/dataframe.rs @@ -69,3 +69,43 @@ macro_rules! df { ] }; } + +#[macro_export] +macro_rules! df_transposed { + ($([$($x:expr),*]),* $(,)?) => { + + // Determine the length of the vectors (assuming they are all the same length) + let len = $crate::vec_len!($($col),*); + + // Create a vector to store the resulting rows + let mut rows = Vec::with_capacity(len); + + // Iterate over the length of the vectors + for i in 0..len { + vec![ + $( + $crate::datatype::DataPoint::from($crate::datatype::CompositeValue::from(vec![ + $( + $crate::datatype::CompositeValue::from($x[i].clone()) + ),* + ])) + ),* + ] + } + }; + ($($x:expr),* $(,)?) => { + vec![ + $( + $crate::datatype::DataPoint::from($x) + ),* + ] + }; +} + +#[macro_export] +macro_rules! vec_len { + ($first:expr $(, $rest:expr)*) => { + $first.len() + }; +} +