Skip to content

Commit

Permalink
Same for bar chart space view
Browse files Browse the repository at this point in the history
  • Loading branch information
abey79 committed Nov 28, 2023
1 parent 8108ce2 commit 071a456
Showing 1 changed file with 131 additions and 88 deletions.
219 changes: 131 additions & 88 deletions crates/re_space_view_bar_chart/src/space_view_class.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use egui::util::hash;
use re_data_store::EntityProperties;
use re_data_store::{EditableAutoValue, EntityProperties, LegendCorner};
use re_log_types::EntityPath;
use re_space_view::controls;
use re_types::datatypes::TensorBuffer;
Expand Down Expand Up @@ -67,21 +67,69 @@ impl SpaceViewClass for BarChartSpaceView {

fn selection_ui(
&self,
_ctx: &mut ViewerContext<'_>,
_ui: &mut egui::Ui,
ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
_state: &mut Self::State,
_space_origin: &EntityPath,
_space_view_id: SpaceViewId,
_root_entity_properties: &mut EntityProperties,
root_entity_properties: &mut EntityProperties,
) {
ctx.re_ui
.selection_grid(ui, "bar_chart_selection_ui")
.show(ui, |ui| {
ctx.re_ui.grid_left_hand_label(ui, "Legend");

ui.vertical(|ui| {
let mut selected = *root_entity_properties.show_legend.get();
if ctx.re_ui.checkbox(ui, &mut selected, "Visible").changed() {
root_entity_properties.show_legend =
EditableAutoValue::UserEdited(selected);
}

let mut corner = root_entity_properties
.legend_location
.unwrap_or(LegendCorner::RightTop);

egui::ComboBox::from_id_source("legend_corner")
.selected_text(corner.to_string())
.show_ui(ui, |ui| {
ui.style_mut().wrap = Some(false);
ui.set_min_width(64.0);

ui.selectable_value(
&mut corner,
LegendCorner::LeftTop,
LegendCorner::LeftTop.to_string(),
);
ui.selectable_value(
&mut corner,
LegendCorner::RightTop,
LegendCorner::RightTop.to_string(),
);
ui.selectable_value(
&mut corner,
LegendCorner::LeftBottom,
LegendCorner::LeftBottom.to_string(),
);
ui.selectable_value(
&mut corner,
LegendCorner::RightBottom,
LegendCorner::RightBottom.to_string(),
);
});

root_entity_properties.legend_location = Some(corner);
});
ui.end_row();
});
}

fn ui(
&self,
_ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
_state: &mut Self::State,
_root_entity_properties: &EntityProperties,
root_entity_properties: &EntityProperties,
_view_ctx: &ViewContextCollection,
parts: &ViewPartCollection,
_query: &ViewQuery<'_>,
Expand All @@ -94,90 +142,85 @@ impl SpaceViewClass for BarChartSpaceView {
let zoom_both_axis = !ui.input(|i| i.modifiers.contains(controls::ASPECT_SCROLL_MODIFIER));

ui.scope(|ui| {
Plot::new("bar_chart_plot")
.legend(Legend::default())
.clamp_grid(true)
.allow_zoom(egui_plot::AxisBools {
x: true,
y: zoom_both_axis,
})
.show(ui, |plot_ui| {
fn create_bar_chart<N: Into<f64>>(
ent_path: &EntityPath,
values: impl Iterator<Item = N>,
) -> BarChart {
let color = auto_color(hash(ent_path) as _);
let fill = color.gamma_multiply(0.75).additive(); // make sure overlapping bars are obvious
BarChart::new(
values
.enumerate()
.map(|(i, value)| {
Bar::new(i as f64 + 0.5, value.into())
.width(0.95)
.name(format!("{ent_path} #{i}"))
.fill(fill)
.stroke(egui::Stroke::NONE)
})
.collect(),
)
.name(ent_path.to_string())
.color(color)
}

for (ent_path, tensor) in charts {
let chart = match &tensor.buffer {
TensorBuffer::U8(data) => {
create_bar_chart(ent_path, data.iter().copied())
}
TensorBuffer::U16(data) => {
create_bar_chart(ent_path, data.iter().copied())
}
TensorBuffer::U32(data) => {
create_bar_chart(ent_path, data.iter().copied())
}
TensorBuffer::U64(data) => {
create_bar_chart(ent_path, data.iter().copied().map(|v| v as f64))
}
TensorBuffer::I8(data) => {
create_bar_chart(ent_path, data.iter().copied())
}
TensorBuffer::I16(data) => {
create_bar_chart(ent_path, data.iter().copied())
}
TensorBuffer::I32(data) => {
create_bar_chart(ent_path, data.iter().copied())
}
TensorBuffer::I64(data) => {
create_bar_chart(ent_path, data.iter().copied().map(|v| v as f64))
}
TensorBuffer::F16(data) => {
create_bar_chart(ent_path, data.iter().map(|f| f.to_f32()))
}
TensorBuffer::F32(data) => {
create_bar_chart(ent_path, data.iter().copied())
}
TensorBuffer::F64(data) => {
create_bar_chart(ent_path, data.iter().copied())
}
TensorBuffer::Jpeg(_) => {
re_log::warn_once!(
"trying to display JPEG data as a bar chart ({:?})",
ent_path
);
continue;
}
TensorBuffer::Nv12(_) => {
re_log::warn_once!(
"trying to display NV12 data as a bar chart ({:?})",
ent_path
);
continue;
}
};

plot_ui.bar_chart(chart);
}
let mut plot =
Plot::new("bar_chart_plot")
.clamp_grid(true)
.allow_zoom(egui_plot::AxisBools {
x: true,
y: zoom_both_axis,
});

if *root_entity_properties.show_legend {
plot = plot.legend(Legend {
position: root_entity_properties
.legend_location
.unwrap_or(LegendCorner::RightTop)
.into(),
..Default::default()
});
}

plot.show(ui, |plot_ui| {
fn create_bar_chart<N: Into<f64>>(
ent_path: &EntityPath,
values: impl Iterator<Item = N>,
) -> BarChart {
let color = auto_color(hash(ent_path) as _);
let fill = color.gamma_multiply(0.75).additive(); // make sure overlapping bars are obvious
BarChart::new(
values
.enumerate()
.map(|(i, value)| {
Bar::new(i as f64 + 0.5, value.into())
.width(0.95)
.name(format!("{ent_path} #{i}"))
.fill(fill)
.stroke(egui::Stroke::NONE)
})
.collect(),
)
.name(ent_path.to_string())
.color(color)
}

for (ent_path, tensor) in charts {
let chart = match &tensor.buffer {
TensorBuffer::U8(data) => create_bar_chart(ent_path, data.iter().copied()),
TensorBuffer::U16(data) => create_bar_chart(ent_path, data.iter().copied()),
TensorBuffer::U32(data) => create_bar_chart(ent_path, data.iter().copied()),
TensorBuffer::U64(data) => {
create_bar_chart(ent_path, data.iter().copied().map(|v| v as f64))
}
TensorBuffer::I8(data) => create_bar_chart(ent_path, data.iter().copied()),
TensorBuffer::I16(data) => create_bar_chart(ent_path, data.iter().copied()),
TensorBuffer::I32(data) => create_bar_chart(ent_path, data.iter().copied()),
TensorBuffer::I64(data) => {
create_bar_chart(ent_path, data.iter().copied().map(|v| v as f64))
}
TensorBuffer::F16(data) => {
create_bar_chart(ent_path, data.iter().map(|f| f.to_f32()))
}
TensorBuffer::F32(data) => create_bar_chart(ent_path, data.iter().copied()),
TensorBuffer::F64(data) => create_bar_chart(ent_path, data.iter().copied()),
TensorBuffer::Jpeg(_) => {
re_log::warn_once!(
"trying to display JPEG data as a bar chart ({:?})",
ent_path
);
continue;
}
TensorBuffer::Nv12(_) => {
re_log::warn_once!(
"trying to display NV12 data as a bar chart ({:?})",
ent_path
);
continue;
}
};

plot_ui.bar_chart(chart);
}
});
});

Ok(())
Expand Down

0 comments on commit 071a456

Please sign in to comment.