re_types/archetypes/
bar_chart.rs

1// DO NOT EDIT! This file was auto-generated by crates/build/re_types_builder/src/codegen/rust/api.rs
2// Based on "crates/store/re_types/definitions/rerun/archetypes/bar_chart.fbs".
3
4#![allow(unused_braces)]
5#![allow(unused_imports)]
6#![allow(unused_parens)]
7#![allow(clippy::clone_on_copy)]
8#![allow(clippy::cloned_instead_of_copied)]
9#![allow(clippy::map_flatten)]
10#![allow(clippy::needless_question_mark)]
11#![allow(clippy::new_without_default)]
12#![allow(clippy::redundant_closure)]
13#![allow(clippy::too_many_arguments)]
14#![allow(clippy::too_many_lines)]
15
16use ::re_types_core::try_serialize_field;
17use ::re_types_core::SerializationResult;
18use ::re_types_core::{ComponentBatch as _, SerializedComponentBatch};
19use ::re_types_core::{ComponentDescriptor, ComponentType};
20use ::re_types_core::{DeserializationError, DeserializationResult};
21
22/// **Archetype**: A bar chart.
23///
24/// The x values will be the indices of the array, and the bar heights will be the provided values.
25///
26/// ## Example
27///
28/// ### Simple bar chart
29/// ```ignore
30/// fn main() -> Result<(), Box<dyn std::error::Error>> {
31///     let rec = rerun::RecordingStreamBuilder::new("rerun_example_bar_chart").spawn()?;
32///
33///     rec.log(
34///         "bar_chart",
35///         &rerun::BarChart::new([8_i64, 4, 0, 9, 1, 4, 1, 6, 9, 0].as_slice()),
36///     )?;
37///
38///     Ok(())
39/// }
40/// ```
41/// <center>
42/// <picture>
43///   <source media="(max-width: 480px)" srcset="https://static.rerun.io/barchart_simple/cf6014b18265edfcaa562c06526c0716b296b193/480w.png">
44///   <source media="(max-width: 768px)" srcset="https://static.rerun.io/barchart_simple/cf6014b18265edfcaa562c06526c0716b296b193/768w.png">
45///   <source media="(max-width: 1024px)" srcset="https://static.rerun.io/barchart_simple/cf6014b18265edfcaa562c06526c0716b296b193/1024w.png">
46///   <source media="(max-width: 1200px)" srcset="https://static.rerun.io/barchart_simple/cf6014b18265edfcaa562c06526c0716b296b193/1200w.png">
47///   <img src="https://static.rerun.io/barchart_simple/cf6014b18265edfcaa562c06526c0716b296b193/full.png" width="640">
48/// </picture>
49/// </center>
50#[derive(Clone, Debug, PartialEq, Default)]
51pub struct BarChart {
52    /// The values. Should always be a 1-dimensional tensor (i.e. a vector).
53    pub values: Option<SerializedComponentBatch>,
54
55    /// The color of the bar chart
56    pub color: Option<SerializedComponentBatch>,
57}
58
59impl BarChart {
60    /// Returns the [`ComponentDescriptor`] for [`Self::values`].
61    ///
62    /// The corresponding component is [`crate::components::TensorData`].
63    #[inline]
64    pub fn descriptor_values() -> ComponentDescriptor {
65        ComponentDescriptor {
66            archetype: Some("rerun.archetypes.BarChart".into()),
67            component: "BarChart:values".into(),
68            component_type: Some("rerun.components.TensorData".into()),
69        }
70    }
71
72    /// Returns the [`ComponentDescriptor`] for [`Self::color`].
73    ///
74    /// The corresponding component is [`crate::components::Color`].
75    #[inline]
76    pub fn descriptor_color() -> ComponentDescriptor {
77        ComponentDescriptor {
78            archetype: Some("rerun.archetypes.BarChart".into()),
79            component: "BarChart:color".into(),
80            component_type: Some("rerun.components.Color".into()),
81        }
82    }
83}
84
85static REQUIRED_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; 1usize]> =
86    once_cell::sync::Lazy::new(|| [BarChart::descriptor_values()]);
87
88static RECOMMENDED_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; 0usize]> =
89    once_cell::sync::Lazy::new(|| []);
90
91static OPTIONAL_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; 1usize]> =
92    once_cell::sync::Lazy::new(|| [BarChart::descriptor_color()]);
93
94static ALL_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; 2usize]> =
95    once_cell::sync::Lazy::new(|| [BarChart::descriptor_values(), BarChart::descriptor_color()]);
96
97impl BarChart {
98    /// The total number of components in the archetype: 1 required, 0 recommended, 1 optional
99    pub const NUM_COMPONENTS: usize = 2usize;
100}
101
102impl ::re_types_core::Archetype for BarChart {
103    #[inline]
104    fn name() -> ::re_types_core::ArchetypeName {
105        "rerun.archetypes.BarChart".into()
106    }
107
108    #[inline]
109    fn display_name() -> &'static str {
110        "Bar chart"
111    }
112
113    #[inline]
114    fn required_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
115        REQUIRED_COMPONENTS.as_slice().into()
116    }
117
118    #[inline]
119    fn recommended_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
120        RECOMMENDED_COMPONENTS.as_slice().into()
121    }
122
123    #[inline]
124    fn optional_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
125        OPTIONAL_COMPONENTS.as_slice().into()
126    }
127
128    #[inline]
129    fn all_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
130        ALL_COMPONENTS.as_slice().into()
131    }
132
133    #[inline]
134    fn from_arrow_components(
135        arrow_data: impl IntoIterator<Item = (ComponentDescriptor, arrow::array::ArrayRef)>,
136    ) -> DeserializationResult<Self> {
137        re_tracing::profile_function!();
138        use ::re_types_core::{Loggable as _, ResultExt as _};
139        let arrays_by_descr: ::nohash_hasher::IntMap<_, _> = arrow_data.into_iter().collect();
140        let values = arrays_by_descr
141            .get(&Self::descriptor_values())
142            .map(|array| SerializedComponentBatch::new(array.clone(), Self::descriptor_values()));
143        let color = arrays_by_descr
144            .get(&Self::descriptor_color())
145            .map(|array| SerializedComponentBatch::new(array.clone(), Self::descriptor_color()));
146        Ok(Self { values, color })
147    }
148}
149
150impl ::re_types_core::AsComponents for BarChart {
151    #[inline]
152    fn as_serialized_batches(&self) -> Vec<SerializedComponentBatch> {
153        use ::re_types_core::Archetype as _;
154        [self.values.clone(), self.color.clone()]
155            .into_iter()
156            .flatten()
157            .collect()
158    }
159}
160
161impl ::re_types_core::ArchetypeReflectionMarker for BarChart {}
162
163impl BarChart {
164    /// Create a new `BarChart`.
165    #[inline]
166    pub fn new(values: impl Into<crate::components::TensorData>) -> Self {
167        Self {
168            values: try_serialize_field(Self::descriptor_values(), [values]),
169            color: None,
170        }
171    }
172
173    /// Update only some specific fields of a `BarChart`.
174    #[inline]
175    pub fn update_fields() -> Self {
176        Self::default()
177    }
178
179    /// Clear all the fields of a `BarChart`.
180    #[inline]
181    pub fn clear_fields() -> Self {
182        use ::re_types_core::Loggable as _;
183        Self {
184            values: Some(SerializedComponentBatch::new(
185                crate::components::TensorData::arrow_empty(),
186                Self::descriptor_values(),
187            )),
188            color: Some(SerializedComponentBatch::new(
189                crate::components::Color::arrow_empty(),
190                Self::descriptor_color(),
191            )),
192        }
193    }
194
195    /// Partitions the component data into multiple sub-batches.
196    ///
197    /// Specifically, this transforms the existing [`SerializedComponentBatch`]es data into [`SerializedComponentColumn`]s
198    /// instead, via [`SerializedComponentBatch::partitioned`].
199    ///
200    /// This makes it possible to use `RecordingStream::send_columns` to send columnar data directly into Rerun.
201    ///
202    /// The specified `lengths` must sum to the total length of the component batch.
203    ///
204    /// [`SerializedComponentColumn`]: [::re_types_core::SerializedComponentColumn]
205    #[inline]
206    pub fn columns<I>(
207        self,
208        _lengths: I,
209    ) -> SerializationResult<impl Iterator<Item = ::re_types_core::SerializedComponentColumn>>
210    where
211        I: IntoIterator<Item = usize> + Clone,
212    {
213        let columns = [
214            self.values
215                .map(|values| values.partitioned(_lengths.clone()))
216                .transpose()?,
217            self.color
218                .map(|color| color.partitioned(_lengths.clone()))
219                .transpose()?,
220        ];
221        Ok(columns.into_iter().flatten())
222    }
223
224    /// Helper to partition the component data into unit-length sub-batches.
225    ///
226    /// This is semantically similar to calling [`Self::columns`] with `std::iter::take(1).repeat(n)`,
227    /// where `n` is automatically guessed.
228    #[inline]
229    pub fn columns_of_unit_batches(
230        self,
231    ) -> SerializationResult<impl Iterator<Item = ::re_types_core::SerializedComponentColumn>> {
232        let len_values = self.values.as_ref().map(|b| b.array.len());
233        let len_color = self.color.as_ref().map(|b| b.array.len());
234        let len = None.or(len_values).or(len_color).unwrap_or(0);
235        self.columns(std::iter::repeat(1).take(len))
236    }
237
238    /// The values. Should always be a 1-dimensional tensor (i.e. a vector).
239    #[inline]
240    pub fn with_values(mut self, values: impl Into<crate::components::TensorData>) -> Self {
241        self.values = try_serialize_field(Self::descriptor_values(), [values]);
242        self
243    }
244
245    /// This method makes it possible to pack multiple [`crate::components::TensorData`] in a single component batch.
246    ///
247    /// This only makes sense when used in conjunction with [`Self::columns`]. [`Self::with_values`] should
248    /// be used when logging a single row's worth of data.
249    #[inline]
250    pub fn with_many_values(
251        mut self,
252        values: impl IntoIterator<Item = impl Into<crate::components::TensorData>>,
253    ) -> Self {
254        self.values = try_serialize_field(Self::descriptor_values(), values);
255        self
256    }
257
258    /// The color of the bar chart
259    #[inline]
260    pub fn with_color(mut self, color: impl Into<crate::components::Color>) -> Self {
261        self.color = try_serialize_field(Self::descriptor_color(), [color]);
262        self
263    }
264
265    /// This method makes it possible to pack multiple [`crate::components::Color`] in a single component batch.
266    ///
267    /// This only makes sense when used in conjunction with [`Self::columns`]. [`Self::with_color`] should
268    /// be used when logging a single row's worth of data.
269    #[inline]
270    pub fn with_many_color(
271        mut self,
272        color: impl IntoIterator<Item = impl Into<crate::components::Color>>,
273    ) -> Self {
274        self.color = try_serialize_field(Self::descriptor_color(), color);
275        self
276    }
277}
278
279impl ::re_byte_size::SizeBytes for BarChart {
280    #[inline]
281    fn heap_size_bytes(&self) -> u64 {
282        self.values.heap_size_bytes() + self.color.heap_size_bytes()
283    }
284}