Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional color component to BarChart archetype #4372

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 37 additions & 15 deletions crates/re_space_view_bar_chart/src/space_view_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,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 @@ -183,25 +185,45 @@ 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()),
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::U8(data) => {
create_bar_chart(ent_path, data.iter().copied(), color)
}
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::U16(data) => {
create_bar_chart(ent_path, data.iter().copied(), color)
}
TensorBuffer::U32(data) => {
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(), color)
}
TensorBuffer::I16(data) => {
create_bar_chart(ent_path, data.iter().copied(), color)
}
TensorBuffer::I32(data) => {
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(), color)
}
TensorBuffer::F64(data) => {
create_bar_chart(ent_path, data.iter().copied(), color)
}
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 ({:?})",
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.