Skip to content

Commit

Permalink
Add optional color component to BarChart archetype (#4372)
Browse files Browse the repository at this point in the history
### What
- Resolves: #1355

```
"""Create and log a bar chart."""

import rerun as rr

rr.init("rerun_example_bar_chart", spawn=True)
rr.log("bar_chart", rr.BarChart([8, 4, 0, 9, 1, 4, 1, 6, 9, 0], color=(255, 0, 0)))
```


![image](https://github.com/rerun-io/rerun/assets/3312232/6af1ad96-0683-401c-ac82-3f7cf0ade57d)


### 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 [app.rerun.io](https://app.rerun.io/pr/4372) (if
applicable)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG

- [PR Build Summary](https://build.rerun.io/pr/4372)
- [Docs
preview](https://rerun.io/preview/44d44da7cf0e5ce66a48273fdcd24695bf965331/docs)
<!--DOCS-PREVIEW-->
- [Examples
preview](https://rerun.io/preview/44d44da7cf0e5ce66a48273fdcd24695bf965331/examples)
<!--EXAMPLES-PREVIEW-->
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)
  • Loading branch information
jleibs committed Nov 28, 2023
1 parent 35e0083 commit a521a06
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 26 deletions.
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.

0 comments on commit a521a06

Please sign in to comment.