diff --git a/src/aesthetics.rs b/src/aesthetics.rs index d57ea94..f4e8ac4 100644 --- a/src/aesthetics.rs +++ b/src/aesthetics.rs @@ -675,7 +675,7 @@ fn plot_hover_hist( ..default() }; let fill = Fill::color(Color::Srgba(Srgba::hex("ffb73388").unwrap())); - let scales = plot_scales(this_dist, 600., font.clone(), 12.); + let scales = plot_scales::(this_dist, 600., font.clone(), 12.); commands .spawn(( HistTag { diff --git a/src/funcplot.rs b/src/funcplot.rs index d72a63c..998e006 100644 --- a/src/funcplot.rs +++ b/src/funcplot.rs @@ -1,6 +1,9 @@ //! Functions for plotting data. -use bevy::prelude::{Color, Component, Font, Handle, Text2d, TextColor, TextFont, Transform, Vec2}; +use bevy::{ + prelude::{Color, Component, Font, Handle, Text2d, TextColor, TextFont, Transform, Vec2}, + text::TextRoot, +}; use bevy_prototype_lyon::{ entity::ShapeBundle, prelude::{GeometryBuilder, Path, PathBuilder, Stroke}, @@ -178,17 +181,17 @@ pub fn plot_box_point(n_cond: usize, cond_index: usize) -> Path { path_builder.build() } -type TextBundle = (Text2d, TextFont, TextColor, Transform); +type TextBundle = (T, TextFont, TextColor, Transform); /// Three text tags to be used as Components to build a an axis scale. #[derive(Clone)] -pub struct ScaleBundle { - pub x_0: TextBundle, - pub y: TextBundle, - pub x_n: TextBundle, +pub struct ScaleBundle { + pub x_0: TextBundle, + pub y: TextBundle, + pub x_n: TextBundle, } -impl ScaleBundle { +impl ScaleBundle { /// Build text components from minimum, maximum and mean values. pub fn new( minimum: f32, @@ -202,21 +205,21 @@ impl ScaleBundle { ) -> Self { // build x component let x_0 = ( - Text2d::new(format!("{:+.3e}", minimum)), + T::from(format!("{:+.3e}", minimum)), TextFont::from_font(font.clone()).with_font_size(font_size), TextColor(color), // to the left so that it is centered Transform::from_xyz(-size / 2. - font_size * 2., 0., 0.2), ); let x_n = ( - Text2d::new(format!("{:+.3e}", maximum)), + T::from(format!("{:+.3e}", maximum)), TextFont::from_font(font.clone()).with_font_size(font_size), TextColor(color), // to the left so that it is centered Transform::from_xyz(size / 2., 0., 0.2), ); let y = ( - Text2d::new(format!("{:+.3e}", mean)), + T::from(format!("{:+.3e}", mean)), TextFont::from_font(font.clone()).with_font_size(font_size), TextColor(color), // to the left so that it is centered @@ -242,7 +245,12 @@ pub fn plot_line(size: f32, transform: Transform) -> (ShapeBundle, Stroke) { } /// Build and position text tags to indicate the scale of thethe x-axis. -pub fn plot_scales(samples: &[f32], size: f32, font: Handle, font_size: f32) -> ScaleBundle { +pub fn plot_scales( + samples: &[f32], + size: f32, + font: Handle, + font_size: f32, +) -> ScaleBundle { let mean: f32 = samples.iter().sum::() / samples.len() as f32; let min = min_f32(samples); let max = max_f32(samples); diff --git a/src/legend/mod.rs b/src/legend/mod.rs index 823c751..ff544b7 100644 --- a/src/legend/mod.rs +++ b/src/legend/mod.rs @@ -49,7 +49,7 @@ fn color_legend_arrow( mut img_query: Query<&ImageNode>, // these two queries are to filter Children of legend_query text_query: Query>, - text_max_query: Query>, + text_max_query: Query, With)>, point_query: Query<(&Point, &Aesthetics), (With, With)>, mut images: ResMut>, ) { @@ -121,7 +121,7 @@ fn color_legend_circle( mut legend_query: Query<(Entity, &mut Node, &Children), With>, mut img_query: Query<&ImageNode>, text_query: Query>, - text_max_query: Query>, + text_max_query: Query, With)>, point_query: Query<(&Point, &Aesthetics), (With, With)>, mut images: ResMut>, ) { @@ -195,7 +195,7 @@ fn color_legend_histograms( >, mut img_query: Query<(&ImageNode, &mut BackgroundColor)>, text_query: Query>, - text_max_query: Query>, + text_max_query: Query, With)>, ) { if !ui_state.is_changed() { // the ui_state always changes on the creation of histograms @@ -324,7 +324,7 @@ fn color_legend_box( mut legend_query: Query<(Entity, &mut Node, &Side, &Children), With>, mut img_query: Query<&ImageNode>, text_query: Query>, - text_max_query: Query>, + text_max_query: Query, With)>, point_query: Query<(&Point, &Aesthetics, &GeomHist), (With, Without)>, mut images: ResMut>, ) { @@ -357,7 +357,9 @@ fn color_legend_box( *writer.text(*child, 0) = format!("{:.2e}", max_val); } else if let Ok(img_legend) = img_query.get_mut(*child) { // modify the image inplace - let image = images.get_mut(&img_legend.image).unwrap(); + let image = images + .get_mut(&img_legend.image) + .expect("Image handles should have been initialized for legend."); let width = image.size().x as f64; let points = linspace(min_val, max_val, width as u32); diff --git a/src/legend/setup.rs b/src/legend/setup.rs index 122d83e..5215ede 100644 --- a/src/legend/setup.rs +++ b/src/legend/setup.rs @@ -65,7 +65,7 @@ fn build_image( /// - box legend, same as histogram but with Rects instead of images. pub fn spawn_legend(mut commands: Commands, asset_server: Res) { let font = asset_server.load("fonts/Assistant-Regular.ttf"); - let scales_arrow = ScaleBundle::new( + let scales_arrow = ScaleBundle::::new( 0., 0., 0., @@ -133,7 +133,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res) { // left box side .with_children(|p| { // TODO: check this works as expected - p.spawn((scales_right_box.x_0.0, Xmin)); + p.spawn(( + scales_right_box.x_0.0, + scales_right_box.x_0.1, + scales_right_box.x_0.2, + Xmin, + )); }) .with_children(|p| { p.spawn(build_image( @@ -143,7 +148,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res) { )); }) .with_children(|p| { - p.spawn((scales_right_box.x_n.0, Xmax)); + p.spawn(( + scales_right_box.x_n.0, + scales_right_box.x_n.1, + scales_right_box.x_n.2, + Xmax, + )); }); }) // container for right box side with text tags for axis @@ -163,7 +173,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res) { .insert(Side::Right) // right box side .with_children(|p| { - p.spawn((scales_left_box.x_0.0, Xmin)); + p.spawn(( + scales_left_box.x_0.0, + scales_left_box.x_0.1, + scales_left_box.x_0.2, + Xmin, + )); }) .with_children(|p| { p.spawn(build_image( @@ -173,7 +188,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res) { )); }) .with_children(|p| { - p.spawn((scales_left_box.x_n.0, Xmax)); + p.spawn(( + scales_left_box.x_n.0, + scales_left_box.x_n.1, + scales_left_box.x_n.2, + Xmax, + )); }); }); }) @@ -192,13 +212,23 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res) { )) .insert(LegendArrow) .with_children(|p| { - p.spawn((scales_arrow.x_0.0, Xmin)); + p.spawn(( + scales_arrow.x_0.0, + scales_arrow.x_0.1, + scales_arrow.x_0.2, + Xmin, + )); }) .with_children(|p| { p.spawn(build_image(arrow_handle.clone(), ARROW_WIDTH, ARROW_HEIGHT)); }) .with_children(|p| { - p.spawn((scales_arrow.x_n.0, Xmax)); + p.spawn(( + scales_arrow.x_n.0, + scales_arrow.x_n.1, + scales_arrow.x_n.2, + Xmax, + )); }); }) // metabolite legend @@ -216,7 +246,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res) { )) .insert(LegendCircle) .with_children(|p| { - p.spawn((scales_mets.x_0.0, Xmin)); + p.spawn(( + scales_mets.x_0.0, + scales_mets.x_0.1, + scales_mets.x_0.2, + Xmin, + )); }) .with_children(|p| { p.spawn(build_image( @@ -226,7 +261,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res) { )); }) .with_children(|p| { - p.spawn((scales_mets.x_n.0, Xmax)); + p.spawn(( + scales_mets.x_n.0, + scales_mets.x_n.1, + scales_mets.x_n.2, + Xmax, + )); }); }) // hist legend @@ -282,7 +322,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res) { .insert(Side::Left) // left histogram side .with_children(|p| { - p.spawn((scales_left.x_0.0, Xmin)); + p.spawn(( + scales_left.x_0.0, + scales_left.x_0.1, + scales_left.x_0.2, + Xmin, + )); }) .with_children(|p| { p.spawn(build_image( @@ -292,7 +337,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res) { )); }) .with_children(|p| { - p.spawn((scales_left.x_n.0, Xmax)); + p.spawn(( + scales_left.x_n.0, + scales_left.x_n.1, + scales_left.x_n.2, + Xmax, + )); }); }) // container for right histogram side with text tags for axis @@ -315,7 +365,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res) { .insert(Side::Right) // right histogram side .with_children(|p| { - p.spawn((scales_right.x_0.0, Xmin)); + p.spawn(( + scales_right.x_0.0, + scales_right.x_0.1, + scales_right.x_0.2, + Xmin, + )); }) .with_children(|p| { p.spawn(build_image( @@ -325,7 +380,12 @@ pub fn spawn_legend(mut commands: Commands, asset_server: Res) { )); }) .with_children(|p| { - p.spawn((scales_right.x_n.0, Xmax)); + p.spawn(( + scales_right.x_n.0, + scales_right.x_n.1, + scales_right.x_n.2, + Xmax, + )); }); }); }); diff --git a/src/main.rs b/src/main.rs index d8761a8..fdf1132 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,7 +47,7 @@ fn main() { .add_systems(Startup, setup_system) .add_plugins(aesthetics::AesPlugin) .add_plugins(scale::ZoomPlugin) - // .add_plugins(legend::LegendPlugin) + .add_plugins(legend::LegendPlugin) .run(); }