From 39bdd251bfcb03885c77daf4cf5b1588536755c7 Mon Sep 17 00:00:00 2001 From: Ralf Grubenmann Date: Thu, 28 Dec 2023 14:21:34 +0100 Subject: [PATCH] move axis definition to separate file --- src/axis.rs | 49 ++++++++++++++ src/bar.rs | 187 +++++++++++++++++++++------------------------------- src/lib.rs | 2 + src/pie.rs | 4 +- 4 files changed, 129 insertions(+), 113 deletions(-) create mode 100644 src/axis.rs diff --git a/src/axis.rs b/src/axis.rs new file mode 100644 index 0000000..fa1b87f --- /dev/null +++ b/src/axis.rs @@ -0,0 +1,49 @@ +use leptos::component; +use leptos::{svg::*, *}; + +#[component] +pub fn YAxis(ticks: Memo>) -> impl IntoView { + view! { + + + {move || { + ticks + .get() + .into_iter() + .map(|(t, s)| { + view! { + + + {s} + + } + }) + .collect_view() + }} + + + } +} diff --git a/src/bar.rs b/src/bar.rs index 292d606..64a5ed4 100644 --- a/src/bar.rs +++ b/src/bar.rs @@ -1,4 +1,4 @@ -use crate::{ChartColor, Palette, CATPPUCCIN_COLORS}; +use crate::{axis::YAxis, ChartColor, Palette, CATPPUCCIN_COLORS}; use leptos::{svg::*, *}; use leptos_use::*; use num_traits::ToPrimitive; @@ -150,128 +150,91 @@ where view! { - - - {move || { - ticks - .get() - .into_iter() - .map(|(t, s)| { - view! { - - - {s} - - } - }) - .collect_view() - }} + - {move || { - values - .get() - .into_iter() - .map(|(i, v)| { - let el = create_node_ref::(); - let is_hovered = use_element_hover(el); - let color = String::from(options.color.color_for_index(i, num_bars.get())); - view! { - - - 0.0 { - 100.0 * -tick_config.get().min_point - / (tick_config.get().max_point - - tick_config.get().min_point) - } else { - 100.0 * (v - tick_config.get().min_point) - / (tick_config.get().max_point - - tick_config.get().min_point) - } - } - - width=move || (80.0 / num_bars.get() as f64) - height=move || { - 100.0 * v.abs() + {move || { + values + .get() + .into_iter() + .map(|(i, v)| { + let el = create_node_ref::(); + let is_hovered = use_element_hover(el); + let color = String::from(options.color.color_for_index(i, num_bars.get())); + view! { + + + 0.0 { + 100.0 * -tick_config.get().min_point + / (tick_config.get().max_point + - tick_config.get().min_point) + } else { + 100.0 * (v - tick_config.get().min_point) / (tick_config.get().max_point - tick_config.get().min_point) } + } - fill=color.clone() - fill-opacity=move || { - if is_hovered.get() { "0.8" } else { "0.6" } - } - - stroke=color - stroke-width=move || { - if is_hovered.get() { "3px" } else { "1px" } - } + width=move || (80.0 / num_bars.get() as f64) + height=move || { + 100.0 * v.abs() + / (tick_config.get().max_point + - tick_config.get().min_point) + } - vector-effect="non-scaling-stroke" - > - - - - 0.0 { "-5" } else { "15" } } - dx="-9" - > - {v} - - - } - }) - .collect_view() - }} + vector-effect="non-scaling-stroke" + > + + + + 0.0 { "-5" } else { "15" } } + dx="-9" + > + {v} + + + } + }) + .collect_view() + }} - } } diff --git a/src/lib.rs b/src/lib.rs index 304104f..8a467be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,9 @@ #![feature(iter_map_windows)] +pub mod axis; pub mod bar; pub mod color; +pub mod legend; pub mod pie; pub mod point; diff --git a/src/pie.rs b/src/pie.rs index da752dc..b2a7b28 100644 --- a/src/pie.rs +++ b/src/pie.rs @@ -170,7 +170,9 @@ where let text_el = create_node_ref::(); let is_path_hovered = use_element_hover(path_el); let is_text_hovered = use_element_hover(text_el); - let is_hovered = create_memo(move |_| is_path_hovered.with(|&h| is_text_hovered.with(|&t| h || t))); + let is_hovered = create_memo(move |_| { + is_path_hovered.with(|&h| is_text_hovered.with(|&t| h || t)) + }); let label_pos = segment.get_center_unit_vector(); let color = String::from(options.color.color_for_index(i, num_pies.get())); view! {