diff --git a/charming/src/element/formatter.rs b/charming/src/element/formatter.rs index 3b55904..c9e6f55 100644 --- a/charming/src/element/formatter.rs +++ b/charming/src/element/formatter.rs @@ -1,12 +1,63 @@ +use super::RawString; use serde::Serialize; -use super::RawString; +#[derive(Serialize)] +#[serde(transparent)] +pub struct FormatterFunction { + #[cfg(not(target_arch = "wasm32"))] + pub value: RawString, + #[cfg(target_arch = "wasm32")] + pub value: web_sys::js_sys::Function, +} + +impl FormatterFunction { + #[cfg(not(target_arch = "wasm32"))] + pub fn new_no_args(body: &str) -> FormatterFunction { + FormatterFunction { + value: RawString::from(format!("function() {{ {} }}", body)), + } + } + + #[cfg(target_arch = "wasm32")] + pub fn new_no_args(body: &str) -> FormatterFunction { + FormatterFunction { + value: web_sys::js_sys::Function::new_no_args(body), + } + } + + #[cfg(not(target_arch = "wasm32"))] + pub fn new_with_args(args: &str, body: &str) -> FormatterFunction { + FormatterFunction { + value: RawString::from(format!("function({}) {{ {} }}", args, body)), + } + } + + #[cfg(target_arch = "wasm32")] + pub fn new_with_args(args: &str, body: &str) -> FormatterFunction { + FormatterFunction { + value: web_sys::js_sys::Function::new_with_args(args, body), + } + } +} + +#[cfg(not(target_arch = "wasm32"))] + +impl From for FormatterFunction +where + S: Into, +{ + fn from(s: S) -> Self { + FormatterFunction { + value: RawString::from(s), + } + } +} #[derive(Serialize)] #[serde(untagged)] pub enum Formatter { String(String), - Function(RawString), + Function(FormatterFunction), } impl From<&str> for Formatter { @@ -15,8 +66,15 @@ impl From<&str> for Formatter { } } +#[cfg(not(target_arch = "wasm32"))] impl From for Formatter { fn from(s: RawString) -> Self { - Formatter::Function(s) + Formatter::Function(FormatterFunction { value: s }) + } +} + +impl From for Formatter { + fn from(f: FormatterFunction) -> Self { + Formatter::Function(f) } } diff --git a/charming/src/element/symbol_size.rs b/charming/src/element/symbol_size.rs index 242c72a..27f540b 100644 --- a/charming/src/element/symbol_size.rs +++ b/charming/src/element/symbol_size.rs @@ -1,12 +1,12 @@ use serde::Serialize; -use super::RawString; +use super::{FormatterFunction, RawString}; #[derive(Serialize)] #[serde(untagged)] pub enum SymbolSize { Number(f64), - Function(RawString), + Function(FormatterFunction), } impl From for SymbolSize { @@ -21,8 +21,17 @@ impl From for SymbolSize { } } +#[cfg(not(target_arch = "wasm32"))] impl From<&str> for SymbolSize { fn from(s: &str) -> Self { - SymbolSize::Function(RawString::from(s)) + SymbolSize::Function(FormatterFunction { + value: RawString::from(s), + }) + } +} + +impl From for SymbolSize { + fn from(f: FormatterFunction) -> Self { + SymbolSize::Function(f) } } diff --git a/charming/src/element/tooltip.rs b/charming/src/element/tooltip.rs index 7102386..a006a1d 100644 --- a/charming/src/element/tooltip.rs +++ b/charming/src/element/tooltip.rs @@ -36,6 +36,9 @@ pub struct Tooltip { #[serde(skip_serializing_if = "Option::is_none")] formatter: Option, + #[serde(skip_serializing_if = "Option::is_none")] + value_formatter: Option, + #[serde(skip_serializing_if = "Option::is_none")] position: Option, @@ -65,6 +68,7 @@ impl Tooltip { trigger_on: None, axis_pointer: None, formatter: None, + value_formatter: None, position: None, padding: None, background_color: None, @@ -93,6 +97,11 @@ impl Tooltip { self } + pub fn value_formatter>(mut self, value_formatter: F) -> Self { + self.value_formatter = Some(value_formatter.into()); + self + } + pub fn position>(mut self, position: S) -> Self { self.position = Some(position.into()); self