Skip to content

Commit

Permalink
Make barchart legend settable via blueprint (#6514)
Browse files Browse the repository at this point in the history
### What
Also gets rid of more EntityProperties.

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested the web demo (if applicable):
* Using examples from latest `main` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/6514?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/6514?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG
* [x] If applicable, add a new check to the [release
checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)!

- [PR Build Summary](https://build.rerun.io/pr/6514)
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)

To run all checks from `main`, comment on the PR with `@rerun-bot
full-check`.
  • Loading branch information
jleibs authored Jun 7, 2024
1 parent fdf659e commit 9cec07c
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 89 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4879,6 +4879,7 @@ dependencies = [
"re_types",
"re_ui",
"re_viewer_context",
"re_viewport_blueprint",
]

[[package]]
Expand Down
19 changes: 0 additions & 19 deletions crates/re_entity_db/src/entity_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,6 @@ pub struct EntityProperties {
/// Used to scale the radii of the points in the resulting point cloud.
pub backproject_radius_scale: EditableAutoValue<f32>, // TODO(andreas): should be a component on the DepthImage archetype.

/// Should the legend be shown (for plot space views).
pub show_legend: EditableAutoValue<bool>, // TODO(andreas): BarChart is still using it, we already have the legend archteype!

/// The location of the legend (for plot space views).
///
/// This is an Option instead of an EditableAutoValue to let each space view class decide on
/// what's the best default.
pub legend_location: Option<LegendCorner>, // TODO(andreas): BarChart is still using it, we already have the legend archteype!

/// What kind of data aggregation to perform (for plot space views).
pub time_series_aggregator: EditableAutoValue<TimeSeriesAggregator>, // TODO(andreas): Should be a component probably on SeriesLine, but today it would become a view property.
}
Expand All @@ -136,8 +127,6 @@ impl Default for EntityProperties {
backproject_depth: EditableAutoValue::Auto(true),
depth_from_world_scale: EditableAutoValue::Auto(1.0),
backproject_radius_scale: EditableAutoValue::Auto(1.0),
show_legend: EditableAutoValue::Auto(true),
legend_location: None,
time_series_aggregator: EditableAutoValue::Auto(TimeSeriesAggregator::default()),
}
}
Expand All @@ -162,8 +151,6 @@ impl EntityProperties {
.or(&child.backproject_radius_scale)
.clone(),

show_legend: self.show_legend.or(&child.show_legend).clone(),
legend_location: self.legend_location.or(child.legend_location),
time_series_aggregator: self
.time_series_aggregator
.or(&child.time_series_aggregator)
Expand Down Expand Up @@ -194,8 +181,6 @@ impl EntityProperties {
.or(&self.backproject_radius_scale)
.clone(),

show_legend: other.show_legend.or(&self.show_legend).clone(),
legend_location: other.legend_location.or(self.legend_location),
time_series_aggregator: other
.time_series_aggregator
.or(&self.time_series_aggregator)
Expand All @@ -211,8 +196,6 @@ impl EntityProperties {
backproject_depth,
depth_from_world_scale,
backproject_radius_scale,
show_legend,
legend_location,
time_series_aggregator,
} = self;

Expand All @@ -221,8 +204,6 @@ impl EntityProperties {
|| backproject_depth.has_edits(&other.backproject_depth)
|| depth_from_world_scale.has_edits(&other.depth_from_world_scale)
|| backproject_radius_scale.has_edits(&other.backproject_radius_scale)
|| show_legend.has_edits(&other.show_legend)
|| *legend_location != other.legend_location
|| time_series_aggregator.has_edits(&other.time_series_aggregator)
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/re_space_view_bar_chart/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ re_tracing.workspace = true
re_types.workspace = true
re_ui.workspace = true
re_viewer_context.workspace = true
re_viewport_blueprint.workspace = true

egui_plot.workspace = true
egui.workspace = true
102 changes: 36 additions & 66 deletions crates/re_space_view_bar_chart/src/space_view_class.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
use egui::{ahash::HashMap, util::hash};
use re_entity_db::{EditableAutoValue, EntityProperties, LegendCorner};
use re_entity_db::EntityProperties;
use re_log_types::EntityPath;
use re_space_view::{controls, suggest_space_view_for_each_entity};
use re_space_view::{controls, suggest_space_view_for_each_entity, view_property_ui};
use re_types::blueprint::archetypes::PlotLegend;
use re_types::blueprint::components::{Corner2D, Visible};
use re_types::View;
use re_types::{datatypes::TensorBuffer, SpaceViewClassIdentifier};
use re_ui::UiExt as _;
use re_ui::list_item;
use re_viewer_context::{
auto_color, IdentifiedViewSystem as _, IndicatedEntities, PerVisualizer, SpaceViewClass,
SpaceViewClassRegistryError, SpaceViewId, SpaceViewState, SpaceViewSystemExecutionError,
ViewQuery, ViewerContext, VisualizableEntities,
SpaceViewClassRegistryError, SpaceViewId, SpaceViewState, SpaceViewStateExt,
SpaceViewSystemExecutionError, TypedComponentFallbackProvider, ViewQuery, ViewerContext,
VisualizableEntities,
};
use re_viewport_blueprint::ViewProperty;

use super::visualizer_system::BarChartVisualizerSystem;

Expand Down Expand Up @@ -106,54 +110,15 @@ impl SpaceViewClass for BarChartSpaceView {

fn selection_ui(
&self,
_ctx: &ViewerContext<'_>,
ctx: &ViewerContext<'_>,
ui: &mut egui::Ui,
_state: &mut dyn SpaceViewState,
state: &mut dyn SpaceViewState,
_space_origin: &EntityPath,
_space_view_id: SpaceViewId,
root_entity_properties: &mut EntityProperties,
space_view_id: SpaceViewId,
_root_entity_properties: &mut EntityProperties,
) -> Result<(), SpaceViewSystemExecutionError> {
ui.selection_grid("bar_chart_selection_ui").show(ui, |ui| {
ui.grid_left_hand_label("Legend");

ui.vertical(|ui| {
let mut selected = *root_entity_properties.show_legend.get();
if ui.re_checkbox(&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.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();
list_item::list_item_scope(ui, "time_series_selection_ui", |ui| {
view_property_ui::<PlotLegend>(ctx, ui, space_view_id, self, state);
});

Ok(())
Expand All @@ -163,33 +128,37 @@ impl SpaceViewClass for BarChartSpaceView {
&self,
ctx: &ViewerContext<'_>,
ui: &mut egui::Ui,
_state: &mut dyn SpaceViewState,
root_entity_properties: &EntityProperties,
state: &mut dyn SpaceViewState,
_root_entity_properties: &EntityProperties,
query: &ViewQuery<'_>,
system_output: re_viewer_context::SystemExecutionOutput,
) -> Result<(), SpaceViewSystemExecutionError> {
use egui_plot::{Bar, BarChart, Legend, Plot};

let state = state.downcast_mut::<()>()?;

let blueprint_db = ctx.blueprint_db();
let view_id = query.space_view_id;

let charts = &system_output
.view_systems
.get::<BarChartVisualizerSystem>()?
.charts;

let zoom_both_axis = !ui.input(|i| i.modifiers.contains(controls::ASPECT_SCROLL_MODIFIER));

let plot_legend =
ViewProperty::from_archetype::<PlotLegend>(blueprint_db, ctx.blueprint_query, view_id);
let legend_visible = plot_legend.component_or_fallback::<Visible>(ctx, self, state)?;
let legend_corner = plot_legend.component_or_fallback::<Corner2D>(ctx, self, state)?;

ui.scope(|ui| {
let mut plot = Plot::new("bar_chart_plot")
.clamp_grid(true)
.allow_zoom([true, zoom_both_axis]);

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

let mut plot_item_id_to_entity_path = HashMap::default();
Expand Down Expand Up @@ -311,11 +280,12 @@ impl SpaceViewClass for BarChartSpaceView {
}
}

fn to_egui_plot_corner(value: LegendCorner) -> egui_plot::Corner {
match value {
LegendCorner::LeftTop => egui_plot::Corner::LeftTop,
LegendCorner::RightTop => egui_plot::Corner::RightTop,
LegendCorner::LeftBottom => egui_plot::Corner::LeftBottom,
LegendCorner::RightBottom => egui_plot::Corner::RightBottom,
impl TypedComponentFallbackProvider<Corner2D> for BarChartSpaceView {
fn fallback_for(&self, _ctx: &re_viewer_context::QueryContext<'_>) -> Corner2D {
// Explicitly pick RightCorner2D::RightTop, we don't want to make this dependent on the (arbitrary)
// default of Corner2D
Corner2D::RightTop
}
}

re_viewer_context::impl_component_fallback_provider!(BarChartSpaceView => [Corner2D]);
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ namespace rerun.blueprint.views;
table BarChartView (
"attr.rerun.view_identifier": "BarChart"
) {
/// Configures the legend of the plot.
plot_legend: rerun.blueprint.archetypes.PlotLegend (order: 2000);
}
40 changes: 37 additions & 3 deletions crates/re_types/src/blueprint/views/bar_chart_view.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions docs/content/reference/types/views/bar_chart_view.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion rerun_py/rerun_sdk/rerun/blueprint/views/bar_chart_view.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9cec07c

Please sign in to comment.