Skip to content

Commit

Permalink
Add optional color component to BarChart archetype
Browse files Browse the repository at this point in the history
  • Loading branch information
jleibs committed Nov 28, 2023
1 parent f5291c9 commit ec370b0
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 28 deletions.
40 changes: 23 additions & 17 deletions crates/re_space_view_bar_chart/src/space_view_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ impl SpaceViewClass for BarChartSpaceView {
fn create_bar_chart<N: Into<f64>>(
ent_path: &EntityPath,
values: impl Iterator<Item = N>,
color: &Option<re_types::components::Color>,
) -> BarChart {
let color = auto_color(hash(ent_path) as _);
let color = color
.map_or_else(|| auto_color(hash(ent_path) as _), |color| color.into());
let fill = color.gamma_multiply(0.75).additive(); // make sure overlapping bars are obvious
BarChart::new(
values
Expand All @@ -121,40 +123,44 @@ impl SpaceViewClass for BarChartSpaceView {
.color(color)
}

for (ent_path, tensor) in charts {
for (ent_path, (tensor, color)) in charts {
let chart = match &tensor.buffer {
TensorBuffer::U8(data) => {
create_bar_chart(ent_path, data.iter().copied())
create_bar_chart(ent_path, data.iter().copied(), color)
}
TensorBuffer::U16(data) => {
create_bar_chart(ent_path, data.iter().copied())
create_bar_chart(ent_path, data.iter().copied(), color)
}
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))
create_bar_chart(ent_path, data.iter().copied(), color)
}
TensorBuffer::U64(data) => create_bar_chart(
ent_path,
data.iter().copied().map(|v| v as f64),
color,
),
TensorBuffer::I8(data) => {
create_bar_chart(ent_path, data.iter().copied())
create_bar_chart(ent_path, data.iter().copied(), color)
}
TensorBuffer::I16(data) => {
create_bar_chart(ent_path, data.iter().copied())
create_bar_chart(ent_path, data.iter().copied(), color)
}
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))
create_bar_chart(ent_path, data.iter().copied(), color)
}
TensorBuffer::I64(data) => create_bar_chart(
ent_path,
data.iter().copied().map(|v| v as f64),
color,
),
TensorBuffer::F16(data) => {
create_bar_chart(ent_path, data.iter().map(|f| f.to_f32()))
create_bar_chart(ent_path, data.iter().map(|f| f.to_f32()), color)
}
TensorBuffer::F32(data) => {
create_bar_chart(ent_path, data.iter().copied())
create_bar_chart(ent_path, data.iter().copied(), color)
}
TensorBuffer::F64(data) => {
create_bar_chart(ent_path, data.iter().copied())
create_bar_chart(ent_path, data.iter().copied(), color)
}
TensorBuffer::Jpeg(_) => {
re_log::warn_once!(
Expand Down
14 changes: 11 additions & 3 deletions crates/re_space_view_bar_chart/src/view_part_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use re_arrow_store::LatestAtQuery;
use re_data_store::EntityPath;
use re_types::{
archetypes::{BarChart, Tensor},
components::Color,
datatypes::TensorData,
Archetype, ComponentNameSet,
};
Expand All @@ -15,7 +16,7 @@ use re_viewer_context::{
/// A bar chart system, with everything needed to render it.
#[derive(Default)]
pub struct BarChartViewPartSystem {
pub charts: BTreeMap<EntityPath, TensorData>,
pub charts: BTreeMap<EntityPath, (TensorData, Option<Color>)>,
}

impl NamedViewSystem for BarChartViewPartSystem {
Expand Down Expand Up @@ -82,10 +83,17 @@ impl ViewPartSystem for BarChartViewPartSystem {
&query,
);

let color = store.query_latest_component::<re_types::components::Color>(
&data_result.entity_path,
&query,
);

if let Some(tensor) = tensor {
if tensor.is_vector() {
self.charts
.insert(data_result.entity_path.clone(), tensor.value.0.clone());
self.charts.insert(
data_result.entity_path.clone(),
(tensor.value.0.clone(), color.map(|c| c.value)),
);
// shallow clones
}
}
Expand Down
5 changes: 5 additions & 0 deletions crates/re_types/definitions/rerun/archetypes/bar_chart.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ table BarChart (

/// The values. Should always be a rank-1 tensor.
values: rerun.components.TensorData ("attr.rerun.component_required", required, order: 1000);

// --- Optional ---

/// The color of the bar chart
color: rerun.components.Color ("attr.rerun.component_optional", nullable, order: 2000);
}
41 changes: 36 additions & 5 deletions crates/re_types/src/archetypes/bar_chart.rs

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

2 changes: 2 additions & 0 deletions docs/content/reference/types/archetypes/bar_chart.md

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

1 change: 1 addition & 0 deletions docs/content/reference/types/components/color.md

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

7 changes: 6 additions & 1 deletion rerun_cpp/src/rerun/archetypes/bar_chart.cpp

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

13 changes: 13 additions & 0 deletions rerun_cpp/src/rerun/archetypes/bar_chart.hpp

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

16 changes: 14 additions & 2 deletions rerun_py/rerun_sdk/rerun/archetypes/bar_chart.py

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

0 comments on commit ec370b0

Please sign in to comment.