|
| 1 | +use crate::emath::NumExt; |
| 2 | +use crate::epaint::{Color32, RectShape, Shape, Stroke}; |
| 3 | + |
| 4 | +use super::{add_rulers_and_text, highlighted_color, Orientation, PlotConfig, RectElement}; |
| 5 | +use crate::plot::{BarChart, ScreenTransform, Value}; |
| 6 | + |
| 7 | +/// One bar in a [`BarChart`]. Potentially floating, allowing stacked bar charts. |
| 8 | +/// Width can be changed to allow variable-width histograms. |
| 9 | +#[derive(Clone, Debug, PartialEq)] |
| 10 | +pub struct Bar { |
| 11 | + /// Name of plot element in the diagram (annotated by default formatter) |
| 12 | + pub name: String, |
| 13 | + |
| 14 | + /// Which direction the bar faces in the diagram |
| 15 | + pub orientation: Orientation, |
| 16 | + |
| 17 | + /// Position on the argument (input) axis -- X if vertical, Y if horizontal |
| 18 | + pub argument: f64, |
| 19 | + |
| 20 | + /// Position on the value (output) axis -- Y if vertical, X if horizontal |
| 21 | + pub value: f64, |
| 22 | + |
| 23 | + /// For stacked bars, this denotes where the bar starts. None if base axis |
| 24 | + pub base_offset: Option<f64>, |
| 25 | + |
| 26 | + /// Thickness of the bar |
| 27 | + pub bar_width: f64, |
| 28 | + |
| 29 | + /// Line width and color |
| 30 | + pub stroke: Stroke, |
| 31 | + |
| 32 | + /// Fill color |
| 33 | + pub fill: Color32, |
| 34 | +} |
| 35 | + |
| 36 | +impl Bar { |
| 37 | + /// Create a bar. Its `orientation` is set by its [`BarChart`] parent. |
| 38 | + /// |
| 39 | + /// - `argument`: Position on the argument axis (X if vertical, Y if horizontal). |
| 40 | + /// - `value`: Height of the bar (if vertical). |
| 41 | + /// |
| 42 | + /// By default the bar is vertical and its base is at zero. |
| 43 | + pub fn new(argument: f64, height: f64) -> Bar { |
| 44 | + Bar { |
| 45 | + argument, |
| 46 | + value: height, |
| 47 | + orientation: Orientation::default(), |
| 48 | + name: Default::default(), |
| 49 | + base_offset: None, |
| 50 | + bar_width: 0.5, |
| 51 | + stroke: Stroke::new(1.0, Color32::TRANSPARENT), |
| 52 | + fill: Color32::TRANSPARENT, |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /// Name of this bar chart element. |
| 57 | + #[allow(clippy::needless_pass_by_value)] |
| 58 | + pub fn name(mut self, name: impl ToString) -> Self { |
| 59 | + self.name = name.to_string(); |
| 60 | + self |
| 61 | + } |
| 62 | + |
| 63 | + /// Add a custom stroke. |
| 64 | + pub fn stroke(mut self, stroke: impl Into<Stroke>) -> Self { |
| 65 | + self.stroke = stroke.into(); |
| 66 | + self |
| 67 | + } |
| 68 | + |
| 69 | + /// Add a custom fill color. |
| 70 | + pub fn fill(mut self, color: impl Into<Color32>) -> Self { |
| 71 | + self.fill = color.into(); |
| 72 | + self |
| 73 | + } |
| 74 | + |
| 75 | + /// Offset the base of the bar. |
| 76 | + /// This offset is on the Y axis for a vertical bar |
| 77 | + /// and on the X axis for a horizontal bar. |
| 78 | + pub fn base_offset(mut self, offset: f64) -> Self { |
| 79 | + self.base_offset = Some(offset); |
| 80 | + self |
| 81 | + } |
| 82 | + |
| 83 | + /// Set the bar width. |
| 84 | + pub fn width(mut self, width: f64) -> Self { |
| 85 | + self.bar_width = width; |
| 86 | + self |
| 87 | + } |
| 88 | + |
| 89 | + /// Set orientation of the element as vertical. Argument axis is X. |
| 90 | + pub fn vertical(mut self) -> Self { |
| 91 | + self.orientation = Orientation::Vertical; |
| 92 | + self |
| 93 | + } |
| 94 | + |
| 95 | + /// Set orientation of the element as horizontal. Argument axis is Y. |
| 96 | + pub fn horizontal(mut self) -> Self { |
| 97 | + self.orientation = Orientation::Horizontal; |
| 98 | + self |
| 99 | + } |
| 100 | + |
| 101 | + pub(super) fn lower(&self) -> f64 { |
| 102 | + if self.value.is_sign_positive() { |
| 103 | + self.base_offset.unwrap_or(0.0) |
| 104 | + } else { |
| 105 | + self.base_offset.map_or(self.value, |o| o + self.value) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + pub(super) fn upper(&self) -> f64 { |
| 110 | + if self.value.is_sign_positive() { |
| 111 | + self.base_offset.map_or(self.value, |o| o + self.value) |
| 112 | + } else { |
| 113 | + self.base_offset.unwrap_or(0.0) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + pub(super) fn add_shapes( |
| 118 | + &self, |
| 119 | + transform: &ScreenTransform, |
| 120 | + highlighted: bool, |
| 121 | + shapes: &mut Vec<Shape>, |
| 122 | + ) { |
| 123 | + let (stroke, fill) = if highlighted { |
| 124 | + highlighted_color(self.stroke, self.fill) |
| 125 | + } else { |
| 126 | + (self.stroke, self.fill) |
| 127 | + }; |
| 128 | + |
| 129 | + let rect = transform.rect_from_values(&self.bounds_min(), &self.bounds_max()); |
| 130 | + let rect = Shape::Rect(RectShape { |
| 131 | + rect, |
| 132 | + corner_radius: 0.0, |
| 133 | + fill, |
| 134 | + stroke, |
| 135 | + }); |
| 136 | + |
| 137 | + shapes.push(rect); |
| 138 | + } |
| 139 | + |
| 140 | + pub(super) fn add_rulers_and_text( |
| 141 | + &self, |
| 142 | + parent: &BarChart, |
| 143 | + plot: &PlotConfig<'_>, |
| 144 | + shapes: &mut Vec<Shape>, |
| 145 | + ) { |
| 146 | + let text: Option<String> = parent |
| 147 | + .element_formatter |
| 148 | + .as_ref() |
| 149 | + .map(|fmt| fmt(self, parent)); |
| 150 | + |
| 151 | + add_rulers_and_text(self, plot, text, shapes); |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +impl RectElement for Bar { |
| 156 | + fn name(&self) -> &str { |
| 157 | + self.name.as_str() |
| 158 | + } |
| 159 | + |
| 160 | + fn bounds_min(&self) -> Value { |
| 161 | + self.point_at(self.argument - self.bar_width / 2.0, self.lower()) |
| 162 | + } |
| 163 | + |
| 164 | + fn bounds_max(&self) -> Value { |
| 165 | + self.point_at(self.argument + self.bar_width / 2.0, self.upper()) |
| 166 | + } |
| 167 | + |
| 168 | + fn values_with_ruler(&self) -> Vec<Value> { |
| 169 | + let base = self.base_offset.unwrap_or(0.0); |
| 170 | + let value_center = self.point_at(self.argument, base + self.value); |
| 171 | + |
| 172 | + let mut ruler_positions = vec![value_center]; |
| 173 | + |
| 174 | + if let Some(offset) = self.base_offset { |
| 175 | + ruler_positions.push(self.point_at(self.argument, offset)); |
| 176 | + } |
| 177 | + |
| 178 | + ruler_positions |
| 179 | + } |
| 180 | + |
| 181 | + fn orientation(&self) -> Orientation { |
| 182 | + self.orientation |
| 183 | + } |
| 184 | + |
| 185 | + fn default_values_format(&self, transform: &ScreenTransform) -> String { |
| 186 | + let scale = transform.dvalue_dpos(); |
| 187 | + let y_decimals = ((-scale[1].abs().log10()).ceil().at_least(0.0) as usize).at_most(6); |
| 188 | + format!("\n{:.*}", y_decimals, self.value) |
| 189 | + } |
| 190 | +} |
0 commit comments