| 1 | use std::collections::HashSet; |
| 2 | use std::f32::consts::{FRAC_PI_2, PI, TAU}; |
| 3 | |
| 4 | use gpui_kit::component::{h_flex, v_flex}; |
| 5 | use gpui_kit::prelude::FluentBuilder as _; |
| 6 | use gpui_kit::*; |
| 7 | use teams_core::{ |
| 8 | BarDisplayMode, CardChart, ChartColor, ChartGauge, ChartKind, ChartPoint, ChartSeries, |
| 9 | GaugeValueFormat, format_chart_value, label_stride, nice_scale, series_categories, |
| 10 | series_maximum, series_value, slice_angles, unit_fraction, |
| 11 | }; |
| 12 | |
| 13 | use app_base::theme; |
| 14 | |
| 15 | const CHART_HEIGHT: f32 = 200.; |
| 16 | const GAUGE_HEIGHT: f32 = 136.; |
| 17 | const GAUGE_MAX_RADIUS: f32 = 100.; |
| 18 | const GAUGE_RING_RATIO: f32 = 0.72; |
| 19 | const GAUGE_VALUE_SIZE: f32 = 22.; |
| 20 | const GAUGE_MARKER_RADIUS: f32 = 6.; |
| 21 | const DONUT_HOLE_RATIO: f32 = 0.58; |
| 22 | const SLICE_GAP: f32 = 2.; |
| 23 | const SLICE_LABEL_MIN_SWEEP: f32 = 0.32; |
| 24 | const ARC_STEP: f32 = 0.05; |
| 25 | const TITLE_SIZE: f32 = 13.5; |
| 26 | const LABEL_SIZE: f32 = 11.; |
| 27 | const LABEL_LINE_HEIGHT: f32 = 14.; |
| 28 | const LEGEND_SIZE: f32 = 12.; |
| 29 | const LEGEND_DOT: f32 = 8.; |
| 30 | const LEGEND_GAP: f32 = 12.; |
| 31 | const SECTION_GAP: f32 = 8.; |
| 32 | const AXIS_TICK_TARGET: usize = 4; |
| 33 | const AXIS_LABEL_GAP: f32 = 6.; |
| 34 | const AXIS_TITLE_HEIGHT: f32 = 16.; |
| 35 | const CATEGORY_LABEL_HEIGHT: f32 = 20.; |
| 36 | const FRAME_PADDING: f32 = 8.; |
| 37 | const VALUE_GUTTER: f32 = 40.; |
| 38 | const LABEL_COLUMN_MAX_RATIO: f32 = 0.4; |
| 39 | const BAR_SLOT_RATIO: f32 = 0.64; |
| 40 | const BAR_MAX_WIDTH: f32 = 56.; |
| 41 | const BAR_RADIUS: f32 = 2.; |
| 42 | const GROUP_GAP: f32 = 2.; |
| 43 | const HORIZONTAL_ROW_HEIGHT: f32 = 30.; |
| 44 | const HORIZONTAL_BAR_HEIGHT: f32 = 18.; |
| 45 | const WHOLE_ROW_HEIGHT: f32 = 46.; |
| 46 | const WHOLE_BAR_HEIGHT: f32 = 10.; |
| 47 | const SEGMENT_LABEL_MIN_WIDTH: f32 = 26.; |
| 48 | const LINE_WIDTH: f32 = 2.; |
| 49 | const MARKER_RADIUS: f32 = 3.5; |
| 50 | const GRID_WIDTH: f32 = 1.; |
| 51 | const ELLIPSIS: &str = "\u{2026}"; |
| 52 | const LIGHT_FILL_LUMINANCE: f32 = 0.62; |
| 53 | const DARK_LABEL: u32 = 0x161617; |
| 54 | |
| 55 | #[derive(Clone, Copy)] |
| 56 | struct Rect { |
| 57 | left: f32, |
| 58 | top: f32, |
| 59 | right: f32, |
| 60 | bottom: f32, |
| 61 | } |
| 62 | |
| 63 | impl Rect { |
| 64 | fn from_bounds(bounds: Bounds<Pixels>) -> Rect { |
| 65 | Rect { |
| 66 | left: f32::from(bounds.origin.x), |
| 67 | top: f32::from(bounds.origin.y), |
| 68 | right: f32::from(bounds.origin.x + bounds.size.width), |
| 69 | bottom: f32::from(bounds.origin.y + bounds.size.height), |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | fn width(&self) -> f32 { |
| 74 | self.right - self.left |
| 75 | } |
| 76 | |
| 77 | fn height(&self) -> f32 { |
| 78 | self.bottom - self.top |
| 79 | } |
| 80 | |
| 81 | fn center_x(&self) -> f32 { |
| 82 | (self.left + self.right) / 2. |
| 83 | } |
| 84 | |
| 85 | fn center_y(&self) -> f32 { |
| 86 | (self.top + self.bottom) / 2. |
| 87 | } |
| 88 | |
| 89 | fn bounds(&self) -> Bounds<Pixels> { |
| 90 | Bounds::new( |
| 91 | point(px(self.left), px(self.top)), |
| 92 | size(px(self.width().max(0.)), px(self.height().max(0.))), |
| 93 | ) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | #[derive(Clone, Copy)] |
| 98 | enum LabelAlign { |
| 99 | Left, |
| 100 | Center, |
| 101 | Right, |
| 102 | } |
| 103 | |
| 104 | struct Pen<'a> { |
| 105 | window: &'a mut Window, |
| 106 | cx: &'a mut App, |
| 107 | } |
| 108 | |
| 109 | impl Pen<'_> { |
| 110 | fn shape(&self, text: &str, size_pixels: f32, color: Hsla, weight: FontWeight) -> ShapedLine { |
| 111 | let mut font = self.window.text_style().font(); |
| 112 | font.weight = weight; |
| 113 | let run = TextRun { |
| 114 | len: text.len(), |
| 115 | font, |
| 116 | color, |
| 117 | background_color: None, |
| 118 | underline: None, |
| 119 | strikethrough: None, |
| 120 | }; |
| 121 | self.window |
| 122 | .text_system() |
| 123 | .shape_line(text.to_owned().into(), px(size_pixels), &[run], None) |
| 124 | } |
| 125 | |
| 126 | fn text_width(&self, text: &str) -> f32 { |
| 127 | f32::from( |
| 128 | self.shape(text, LABEL_SIZE, theme::text_muted(), FontWeight::NORMAL) |
| 129 | .width, |
| 130 | ) |
| 131 | } |
| 132 | |
| 133 | fn text(&mut self, text: &str, x: f32, y: f32, align: LabelAlign, color: Hsla) { |
| 134 | self.sized_text(text, (x, y), align, color, LABEL_SIZE, FontWeight::NORMAL); |
| 135 | } |
| 136 | |
| 137 | fn sized_text( |
| 138 | &mut self, |
| 139 | text: &str, |
| 140 | (x, y): (f32, f32), |
| 141 | align: LabelAlign, |
| 142 | color: Hsla, |
| 143 | size_pixels: f32, |
| 144 | weight: FontWeight, |
| 145 | ) { |
| 146 | if text.is_empty() { |
| 147 | return; |
| 148 | } |
| 149 | let line = self.shape(text, size_pixels, color, weight); |
| 150 | let width = f32::from(line.width); |
| 151 | let left = match align { |
| 152 | LabelAlign::Left => x, |
| 153 | LabelAlign::Center => x - width / 2., |
| 154 | LabelAlign::Right => x - width, |
| 155 | }; |
| 156 | let _ = line.paint( |
| 157 | point(px(left), px(y)), |
| 158 | px(size_pixels * 1.25), |
| 159 | TextAlign::Left, |
| 160 | None, |
| 161 | self.window, |
| 162 | self.cx, |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | fn fitted(&self, text: &str, max_width: f32) -> String { |
| 167 | if self.text_width(text) <= max_width { |
| 168 | return text.to_owned(); |
| 169 | } |
| 170 | let characters: Vec<char> = text.chars().collect(); |
| 171 | (1..characters.len()) |
| 172 | .rev() |
| 173 | .map(|length| { |
| 174 | format!( |
| 175 | "{}{ELLIPSIS}", |
| 176 | characters[..length].iter().collect::<String>() |
| 177 | ) |
| 178 | }) |
| 179 | .find(|candidate| self.text_width(candidate) <= max_width) |
| 180 | .unwrap_or_else(|| ELLIPSIS.to_owned()) |
| 181 | } |
| 182 | |
| 183 | fn rect(&mut self, rect: Rect, color: Hsla, radius: f32) { |
| 184 | if rect.width() <= 0. || rect.height() <= 0. { |
| 185 | return; |
| 186 | } |
| 187 | self.window.paint_quad(quad( |
| 188 | rect.bounds(), |
| 189 | px(radius), |
| 190 | color, |
| 191 | px(0.), |
| 192 | transparent_black(), |
| 193 | BorderStyle::default(), |
| 194 | )); |
| 195 | } |
| 196 | |
| 197 | fn circle(&mut self, x: f32, y: f32, radius: f32, color: Hsla) { |
| 198 | self.rect( |
| 199 | Rect { |
| 200 | left: x - radius, |
| 201 | top: y - radius, |
| 202 | right: x + radius, |
| 203 | bottom: y + radius, |
| 204 | }, |
| 205 | color, |
| 206 | radius, |
| 207 | ); |
| 208 | } |
| 209 | |
| 210 | fn horizontal_line(&mut self, left: f32, right: f32, y: f32, color: Hsla) { |
| 211 | self.rect( |
| 212 | Rect { |
| 213 | left, |
| 214 | top: y - GRID_WIDTH / 2., |
| 215 | right, |
| 216 | bottom: y + GRID_WIDTH / 2., |
| 217 | }, |
| 218 | color, |
| 219 | 0., |
| 220 | ); |
| 221 | } |
| 222 | |
| 223 | fn vertical_line(&mut self, x: f32, top: f32, bottom: f32, color: Hsla) { |
| 224 | self.rect( |
| 225 | Rect { |
| 226 | left: x - GRID_WIDTH / 2., |
| 227 | top, |
| 228 | right: x + GRID_WIDTH / 2., |
| 229 | bottom, |
| 230 | }, |
| 231 | color, |
| 232 | 0., |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | fn polyline(&mut self, points: &[(f32, f32)], color: Hsla) { |
| 237 | let Some(((first_x, first_y), rest)) = points.split_first() else { |
| 238 | return; |
| 239 | }; |
| 240 | let mut builder = PathBuilder::stroke(px(LINE_WIDTH)); |
| 241 | builder.move_to(point(px(*first_x), px(*first_y))); |
| 242 | for (x, y) in rest { |
| 243 | builder.line_to(point(px(*x), px(*y))); |
| 244 | } |
| 245 | if let Ok(path) = builder.build() { |
| 246 | self.window.paint_path(path, color); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | fn sector(&mut self, center: (f32, f32), radii: (f32, f32), angles: (f32, f32), color: Hsla) { |
| 251 | let (inner, outer) = radii; |
| 252 | let (start, end) = angles; |
| 253 | let steps = ((end - start).abs() / ARC_STEP).ceil().max(1.) as usize; |
| 254 | let arc_point = |radius: f32, step: usize| { |
| 255 | let angle = start + (end - start) * step as f32 / steps as f32; |
| 256 | point( |
| 257 | px(center.0 + radius * angle.cos()), |
| 258 | px(center.1 + radius * angle.sin()), |
| 259 | ) |
| 260 | }; |
| 261 | let mut builder = PathBuilder::fill(); |
| 262 | builder.move_to(arc_point(outer, 0)); |
| 263 | for step in 1..=steps { |
| 264 | builder.line_to(arc_point(outer, step)); |
| 265 | } |
| 266 | if inner > 0. { |
| 267 | for step in (0..=steps).rev() { |
| 268 | builder.line_to(arc_point(inner, step)); |
| 269 | } |
| 270 | } else { |
| 271 | builder.line_to(point(px(center.0), px(center.1))); |
| 272 | } |
| 273 | builder.close(); |
| 274 | if let Ok(path) = builder.build() { |
| 275 | self.window.paint_path(path, color); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | pub fn chart_view(chart: &CardChart) -> AnyElement { |
| 281 | let legend_entries = if chart.show_legend { |
| 282 | chart.legend_entries() |
| 283 | } else { |
| 284 | Vec::new() |
| 285 | }; |
| 286 | let height = chart_height(chart); |
| 287 | let drawing_chart = chart.clone(); |
| 288 | let drawing = canvas( |
| 289 | |_, _, _| (), |
| 290 | move |bounds, (), window, cx| { |
| 291 | paint_chart(&drawing_chart, bounds, &mut Pen { window, cx }); |
| 292 | }, |
| 293 | ) |
| 294 | .h(px(height)); |
| 295 | let side_legend = matches!(chart.kind, ChartKind::Donut(_) | ChartKind::Pie(_)); |
| 296 | let body = if side_legend { |
| 297 | h_flex() |
| 298 | .w_full() |
| 299 | .items_center() |
| 300 | .gap(px(LEGEND_GAP)) |
| 301 | .child(drawing.flex_1().min_w(px(0.))) |
| 302 | .child(legend_view(&legend_entries, true)) |
| 303 | .into_any_element() |
| 304 | } else { |
| 305 | v_flex() |
| 306 | .w_full() |
| 307 | .gap(px(SECTION_GAP)) |
| 308 | .child(drawing.w_full()) |
| 309 | .child(legend_view(&legend_entries, false)) |
| 310 | .into_any_element() |
| 311 | }; |
| 312 | v_flex() |
| 313 | .w_full() |
| 314 | .gap(px(SECTION_GAP)) |
| 315 | .when_some(chart.title.clone(), |column, title| { |
| 316 | column.child( |
| 317 | div() |
| 318 | .text_size(px(TITLE_SIZE)) |
| 319 | .font_weight(FontWeight::SEMIBOLD) |
| 320 | .text_color(theme::text_strong()) |
| 321 | .child(title), |
| 322 | ) |
| 323 | }) |
| 324 | .child(body) |
| 325 | .into_any_element() |
| 326 | } |
| 327 | |
| 328 | fn legend_view(entries: &[(String, ChartColor)], vertical: bool) -> Div { |
| 329 | let container = if vertical { |
| 330 | v_flex().flex_none().gap(px(6.)) |
| 331 | } else { |
| 332 | h_flex().flex_wrap().gap_x(px(LEGEND_GAP)).gap_y(px(4.)) |
| 333 | }; |
| 334 | container.children(entries.iter().map(|(label, color)| { |
| 335 | h_flex() |
| 336 | .gap(px(6.)) |
| 337 | .items_center() |
| 338 | .text_size(px(LEGEND_SIZE)) |
| 339 | .text_color(theme::text_muted()) |
| 340 | .child( |
| 341 | div() |
| 342 | .flex_none() |
| 343 | .size(px(LEGEND_DOT)) |
| 344 | .rounded(px(LEGEND_DOT / 2.)) |
| 345 | .bg(palette(*color)), |
| 346 | ) |
| 347 | .child(label.clone()) |
| 348 | })) |
| 349 | } |
| 350 | |
| 351 | fn chart_height(chart: &CardChart) -> f32 { |
| 352 | let title_rows = |has_axis_title: bool| { |
| 353 | if has_axis_title { |
| 354 | AXIS_TITLE_HEIGHT |
| 355 | } else { |
| 356 | 0. |
| 357 | } |
| 358 | }; |
| 359 | match &chart.kind { |
| 360 | ChartKind::Gauge(_) => GAUGE_HEIGHT, |
| 361 | ChartKind::HorizontalBar { bars, mode } => match mode { |
| 362 | BarDisplayMode::AbsoluteWithAxis => { |
| 363 | bars.len() as f32 * HORIZONTAL_ROW_HEIGHT |
| 364 | + CATEGORY_LABEL_HEIGHT |
| 365 | + title_rows(chart.y_axis_title.is_some()) |
| 366 | + title_rows(chart.x_axis_title.is_some()) |
| 367 | + FRAME_PADDING |
| 368 | } |
| 369 | BarDisplayMode::AbsoluteNoAxis => { |
| 370 | bars.len() as f32 * HORIZONTAL_ROW_HEIGHT |
| 371 | + title_rows(chart.x_axis_title.is_some()) |
| 372 | + FRAME_PADDING |
| 373 | } |
| 374 | BarDisplayMode::PartToWhole => bars.len() as f32 * WHOLE_ROW_HEIGHT, |
| 375 | }, |
| 376 | ChartKind::StackedHorizontalBar(rows) => { |
| 377 | rows.len() as f32 * HORIZONTAL_ROW_HEIGHT |
| 378 | + CATEGORY_LABEL_HEIGHT |
| 379 | + title_rows(chart.y_axis_title.is_some()) |
| 380 | + title_rows(chart.x_axis_title.is_some()) |
| 381 | + FRAME_PADDING |
| 382 | } |
| 383 | _ => CHART_HEIGHT, |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | fn paint_chart(chart: &CardChart, bounds: Bounds<Pixels>, pen: &mut Pen) { |
| 388 | let area = Rect::from_bounds(bounds); |
| 389 | match &chart.kind { |
| 390 | ChartKind::Donut(slices) => paint_slices(pen, slices, area, DONUT_HOLE_RATIO), |
| 391 | ChartKind::Pie(slices) => paint_slices(pen, slices, area, 0.), |
| 392 | ChartKind::Gauge(gauge) => paint_gauge(pen, gauge, area), |
| 393 | ChartKind::Line(series) => paint_lines(pen, chart, series, area), |
| 394 | ChartKind::VerticalBar(bars) => paint_vertical_bars(pen, chart, bars, area), |
| 395 | ChartKind::GroupedBar { series, stacked } => { |
| 396 | paint_grouped_bars(pen, chart, series, *stacked, area) |
| 397 | } |
| 398 | ChartKind::HorizontalBar { bars, mode } => match mode { |
| 399 | BarDisplayMode::PartToWhole => paint_part_to_whole(pen, bars, area), |
| 400 | _ => paint_horizontal_bars(pen, chart, bars, *mode, area), |
| 401 | }, |
| 402 | ChartKind::StackedHorizontalBar(rows) => paint_stacked_rows(pen, chart, rows, area), |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | fn paint_slices(pen: &mut Pen, slices: &[ChartPoint], area: Rect, hole_ratio: f32) { |
| 407 | let radius = area.width().min(area.height()) / 2. - FRAME_PADDING / 2.; |
| 408 | if radius <= 0. { |
| 409 | return; |
| 410 | } |
| 411 | let center = (area.center_x(), area.center_y()); |
| 412 | let inner = radius * hole_ratio; |
| 413 | let values: Vec<f64> = slices.iter().map(|slice| slice.value).collect(); |
| 414 | let visible = values.iter().filter(|value| **value > 0.).count(); |
| 415 | let gap = if visible > 1 { SLICE_GAP / radius } else { 0. }; |
| 416 | for (slice, (start, end)) in slices.iter().zip(slice_angles(&values)) { |
| 417 | let sweep = end - start; |
| 418 | if sweep <= 0. { |
| 419 | continue; |
| 420 | } |
| 421 | let fill = palette(slice.color); |
| 422 | let trim = gap.min(sweep / 4.); |
| 423 | pen.sector( |
| 424 | center, |
| 425 | (inner, radius), |
| 426 | (start + trim / 2., end - trim / 2.), |
| 427 | fill, |
| 428 | ); |
| 429 | if sweep >= SLICE_LABEL_MIN_SWEEP { |
| 430 | let middle = (start + end) / 2.; |
| 431 | let label_radius = (inner + radius) / 2.; |
| 432 | pen.sized_text( |
| 433 | &format_chart_value(slice.value), |
| 434 | ( |
| 435 | center.0 + label_radius * middle.cos(), |
| 436 | center.1 + label_radius * middle.sin() - LABEL_LINE_HEIGHT / 2., |
| 437 | ), |
| 438 | LabelAlign::Center, |
| 439 | label_color_on(fill), |
| 440 | LABEL_SIZE, |
| 441 | FontWeight::SEMIBOLD, |
| 442 | ); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | fn paint_gauge(pen: &mut Pen, gauge: &ChartGauge, area: Rect) { |
| 448 | let radius = (area.width() / 2. - FRAME_PADDING * 1.5).min(GAUGE_MAX_RADIUS); |
| 449 | if radius <= 0. { |
| 450 | return; |
| 451 | } |
| 452 | let center = (area.center_x(), area.top + FRAME_PADDING / 2. + radius); |
| 453 | let inner = radius * GAUGE_RING_RATIO; |
| 454 | let segment_total: f64 = gauge.segments.iter().map(|segment| segment.size).sum(); |
| 455 | let fraction = unit_fraction(gauge.value, gauge.minimum, gauge.maximum); |
| 456 | if segment_total > 0. { |
| 457 | let sizes: Vec<f64> = gauge.segments.iter().map(|segment| segment.size).collect(); |
| 458 | let gap = SLICE_GAP / radius; |
| 459 | for (segment, (start, end)) in gauge.segments.iter().zip(slice_angles(&sizes)) { |
| 460 | let (start, end) = (half_arc_angle(start), half_arc_angle(end)); |
| 461 | let trim = gap.min((end - start) / 4.); |
| 462 | pen.sector( |
| 463 | center, |
| 464 | (inner, radius), |
| 465 | (start + trim / 2., end - trim / 2.), |
| 466 | palette(segment.color), |
| 467 | ); |
| 468 | } |
| 469 | } else { |
| 470 | pen.sector( |
| 471 | center, |
| 472 | (inner, radius), |
| 473 | (PI, TAU), |
| 474 | palette(ChartColor::Neutral), |
| 475 | ); |
| 476 | pen.sector( |
| 477 | center, |
| 478 | (inner, radius), |
| 479 | (PI, PI + PI * fraction), |
| 480 | palette(ChartColor::CategoricalBlue), |
| 481 | ); |
| 482 | } |
| 483 | let marker_angle = PI + PI * fraction; |
| 484 | let marker_radius = (inner + radius) / 2.; |
| 485 | let marker = ( |
| 486 | center.0 + marker_radius * marker_angle.cos(), |
| 487 | center.1 + marker_radius * marker_angle.sin(), |
| 488 | ); |
| 489 | pen.circle( |
| 490 | marker.0, |
| 491 | marker.1, |
| 492 | GAUGE_MARKER_RADIUS, |
| 493 | theme::text_strong(), |
| 494 | ); |
| 495 | pen.circle( |
| 496 | marker.0, |
| 497 | marker.1, |
| 498 | GAUGE_MARKER_RADIUS - 2., |
| 499 | theme::surface(), |
| 500 | ); |
| 501 | let value_text = match gauge.format { |
| 502 | GaugeValueFormat::Percentage => { |
| 503 | format!("{}%", format_chart_value((fraction * 100.).round() as f64)) |
| 504 | } |
| 505 | GaugeValueFormat::Fraction => format!( |
| 506 | "{}/{}", |
| 507 | format_chart_value(gauge.value), |
| 508 | format_chart_value(gauge.maximum) |
| 509 | ), |
| 510 | }; |
| 511 | pen.sized_text( |
| 512 | &value_text, |
| 513 | (center.0, center.1 - GAUGE_VALUE_SIZE * 1.1), |
| 514 | LabelAlign::Center, |
| 515 | theme::text_strong(), |
| 516 | GAUGE_VALUE_SIZE, |
| 517 | FontWeight::SEMIBOLD, |
| 518 | ); |
| 519 | if let Some(sub_label) = &gauge.sub_label { |
| 520 | pen.text( |
| 521 | sub_label, |
| 522 | center.0, |
| 523 | center.1 + 4., |
| 524 | LabelAlign::Center, |
| 525 | theme::text_muted(), |
| 526 | ); |
| 527 | } |
| 528 | if gauge.show_min_max { |
| 529 | let end_offset = marker_radius; |
| 530 | let y = center.1 + 4.; |
| 531 | pen.text( |
| 532 | &format_chart_value(gauge.minimum), |
| 533 | center.0 - end_offset, |
| 534 | y, |
| 535 | LabelAlign::Center, |
| 536 | theme::text_muted(), |
| 537 | ); |
| 538 | pen.text( |
| 539 | &format_chart_value(gauge.maximum), |
| 540 | center.0 + end_offset, |
| 541 | y, |
| 542 | LabelAlign::Center, |
| 543 | theme::text_muted(), |
| 544 | ); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | struct VerticalFrame { |
| 549 | plot: Rect, |
| 550 | slot_width: f32, |
| 551 | maximum: f64, |
| 552 | } |
| 553 | |
| 554 | impl VerticalFrame { |
| 555 | fn slot_center(&self, index: usize) -> f32 { |
| 556 | self.plot.left + self.slot_width * (index as f32 + 0.5) |
| 557 | } |
| 558 | |
| 559 | fn y(&self, value: f64) -> f32 { |
| 560 | self.plot.bottom - self.plot.height() * unit_fraction(value, 0., self.maximum) |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | fn vertical_frame( |
| 565 | pen: &mut Pen, |
| 566 | chart: &CardChart, |
| 567 | area: Rect, |
| 568 | categories: &[String], |
| 569 | maximum_value: f64, |
| 570 | ) -> VerticalFrame { |
| 571 | let scale = nice_scale(maximum_value, AXIS_TICK_TARGET); |
| 572 | let ticks = scale.ticks(); |
| 573 | let tick_labels: Vec<String> = ticks.iter().map(|tick| format_chart_value(*tick)).collect(); |
| 574 | let label_width = tick_labels |
| 575 | .iter() |
| 576 | .map(|label| pen.text_width(label)) |
| 577 | .fold(0., f32::max); |
| 578 | let top_padding = if chart.y_axis_title.is_some() { |
| 579 | AXIS_TITLE_HEIGHT + FRAME_PADDING |
| 580 | } else { |
| 581 | FRAME_PADDING |
| 582 | }; |
| 583 | let bottom_padding = CATEGORY_LABEL_HEIGHT |
| 584 | + if chart.x_axis_title.is_some() { |
| 585 | AXIS_TITLE_HEIGHT |
| 586 | } else { |
| 587 | 0. |
| 588 | }; |
| 589 | let plot = Rect { |
| 590 | left: area.left + label_width + AXIS_LABEL_GAP + FRAME_PADDING / 2., |
| 591 | top: area.top + top_padding, |
| 592 | right: area.right - FRAME_PADDING, |
| 593 | bottom: area.bottom - bottom_padding, |
| 594 | }; |
| 595 | let frame = VerticalFrame { |
| 596 | plot, |
| 597 | slot_width: plot.width() / categories.len().max(1) as f32, |
| 598 | maximum: scale.maximum, |
| 599 | }; |
| 600 | if let Some(title) = &chart.y_axis_title { |
| 601 | pen.text( |
| 602 | title, |
| 603 | area.left, |
| 604 | area.top, |
| 605 | LabelAlign::Left, |
| 606 | theme::text_muted(), |
| 607 | ); |
| 608 | } |
| 609 | for (tick, label) in ticks.iter().zip(&tick_labels) { |
| 610 | let y = frame.y(*tick); |
| 611 | let line_color = if *tick == 0. { |
| 612 | theme::border_strong() |
| 613 | } else { |
| 614 | theme::border() |
| 615 | }; |
| 616 | pen.horizontal_line(plot.left, plot.right, y, line_color); |
| 617 | pen.text( |
| 618 | label, |
| 619 | plot.left - AXIS_LABEL_GAP, |
| 620 | y - LABEL_LINE_HEIGHT / 2., |
| 621 | LabelAlign::Right, |
| 622 | theme::text_muted(), |
| 623 | ); |
| 624 | } |
| 625 | let widest_category = categories |
| 626 | .iter() |
| 627 | .map(|category| pen.text_width(category)) |
| 628 | .fold(0., f32::max); |
| 629 | let stride = label_stride( |
| 630 | categories.len(), |
| 631 | frame.slot_width, |
| 632 | widest_category + AXIS_LABEL_GAP, |
| 633 | ); |
| 634 | let fitted: Vec<String> = categories |
| 635 | .iter() |
| 636 | .map(|category| pen.fitted(category, frame.slot_width - AXIS_LABEL_GAP)) |
| 637 | .collect(); |
| 638 | let fitted_are_unique = fitted.iter().collect::<HashSet<_>>().len() == categories.len(); |
| 639 | let (labels, stride) = if stride > 1 && fitted_are_unique { |
| 640 | (fitted.as_slice(), 1) |
| 641 | } else { |
| 642 | (categories, stride) |
| 643 | }; |
| 644 | for (index, label) in labels.iter().enumerate().step_by(stride) { |
| 645 | pen.text( |
| 646 | label, |
| 647 | frame.slot_center(index), |
| 648 | plot.bottom + 4., |
| 649 | LabelAlign::Center, |
| 650 | theme::text_muted(), |
| 651 | ); |
| 652 | } |
| 653 | if let Some(title) = &chart.x_axis_title { |
| 654 | pen.text( |
| 655 | title, |
| 656 | plot.center_x(), |
| 657 | area.bottom - AXIS_TITLE_HEIGHT + 2., |
| 658 | LabelAlign::Center, |
| 659 | theme::text_muted(), |
| 660 | ); |
| 661 | } |
| 662 | frame |
| 663 | } |
| 664 | |
| 665 | fn paint_bar_value(pen: &mut Pen, value: f64, x: f32, top: f32) { |
| 666 | pen.text( |
| 667 | &format_chart_value(value), |
| 668 | x, |
| 669 | top - LABEL_LINE_HEIGHT - 1., |
| 670 | LabelAlign::Center, |
| 671 | theme::text_strong(), |
| 672 | ); |
| 673 | } |
| 674 | |
| 675 | fn paint_vertical_bars(pen: &mut Pen, chart: &CardChart, bars: &[ChartPoint], area: Rect) { |
| 676 | let categories: Vec<String> = bars.iter().map(|bar| bar.label.clone()).collect(); |
| 677 | let maximum = bars.iter().map(|bar| bar.value).fold(0., f64::max); |
| 678 | let frame = vertical_frame(pen, chart, area, &categories, maximum); |
| 679 | let bar_width = (frame.slot_width * BAR_SLOT_RATIO).min(BAR_MAX_WIDTH); |
| 680 | for (index, bar) in bars.iter().enumerate() { |
| 681 | let center = frame.slot_center(index); |
| 682 | let top = frame.y(bar.value); |
| 683 | pen.rect( |
| 684 | Rect { |
| 685 | left: center - bar_width / 2., |
| 686 | top, |
| 687 | right: center + bar_width / 2., |
| 688 | bottom: frame.plot.bottom, |
| 689 | }, |
| 690 | palette(bar.color), |
| 691 | BAR_RADIUS, |
| 692 | ); |
| 693 | if chart.show_values { |
| 694 | paint_bar_value(pen, bar.value, center, top); |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | fn paint_grouped_bars( |
| 700 | pen: &mut Pen, |
| 701 | chart: &CardChart, |
| 702 | series: &[ChartSeries], |
| 703 | stacked: bool, |
| 704 | area: Rect, |
| 705 | ) { |
| 706 | let categories = series_categories(series); |
| 707 | let frame = vertical_frame( |
| 708 | pen, |
| 709 | chart, |
| 710 | area, |
| 711 | &categories, |
| 712 | series_maximum(series, stacked), |
| 713 | ); |
| 714 | let group_width = |
| 715 | (frame.slot_width * BAR_SLOT_RATIO).min(BAR_MAX_WIDTH * series.len().max(1) as f32); |
| 716 | let bar_width = if stacked { |
| 717 | group_width.min(BAR_MAX_WIDTH) |
| 718 | } else { |
| 719 | group_width / series.len().max(1) as f32 |
| 720 | }; |
| 721 | for (index, category) in categories.iter().enumerate() { |
| 722 | let center = frame.slot_center(index); |
| 723 | let mut stack_total = 0.; |
| 724 | for (series_index, entry) in series.iter().enumerate() { |
| 725 | let value = series_value(entry, category).max(0.); |
| 726 | let (left, bottom_value, top_value) = if stacked { |
| 727 | let base = stack_total; |
| 728 | stack_total += value; |
| 729 | (center - bar_width / 2., base, stack_total) |
| 730 | } else { |
| 731 | let left = center - group_width / 2. + bar_width * series_index as f32; |
| 732 | (left, 0., value) |
| 733 | }; |
| 734 | let gap = if stacked { 0. } else { GROUP_GAP / 2. }; |
| 735 | pen.rect( |
| 736 | Rect { |
| 737 | left: left + gap, |
| 738 | top: frame.y(top_value), |
| 739 | right: left + bar_width - gap, |
| 740 | bottom: frame.y(bottom_value), |
| 741 | }, |
| 742 | palette(entry.color), |
| 743 | if stacked { 0. } else { BAR_RADIUS }, |
| 744 | ); |
| 745 | if chart.show_values && !stacked { |
| 746 | paint_bar_value(pen, value, left + bar_width / 2., frame.y(value)); |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | fn paint_lines(pen: &mut Pen, chart: &CardChart, series: &[ChartSeries], area: Rect) { |
| 753 | let categories = series_categories(series); |
| 754 | let frame = vertical_frame(pen, chart, area, &categories, series_maximum(series, false)); |
| 755 | for entry in series { |
| 756 | let color = palette(entry.color); |
| 757 | let points: Vec<(f32, f32)> = categories |
| 758 | .iter() |
| 759 | .enumerate() |
| 760 | .filter(|(_, category)| entry.points.iter().any(|point| point.label == **category)) |
| 761 | .map(|(index, category)| { |
| 762 | ( |
| 763 | frame.slot_center(index), |
| 764 | frame.y(series_value(entry, category)), |
| 765 | ) |
| 766 | }) |
| 767 | .collect(); |
| 768 | pen.polyline(&points, color); |
| 769 | for (x, y) in &points { |
| 770 | pen.circle(*x, *y, MARKER_RADIUS + 1.5, theme::surface()); |
| 771 | pen.circle(*x, *y, MARKER_RADIUS, color); |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | struct HorizontalFrame { |
| 777 | plot: Rect, |
| 778 | row_height: f32, |
| 779 | maximum: f64, |
| 780 | } |
| 781 | |
| 782 | impl HorizontalFrame { |
| 783 | fn row_center(&self, index: usize) -> f32 { |
| 784 | self.plot.top + self.row_height * (index as f32 + 0.5) |
| 785 | } |
| 786 | |
| 787 | fn x(&self, value: f64) -> f32 { |
| 788 | self.plot.left + self.plot.width() * unit_fraction(value, 0., self.maximum) |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | fn horizontal_frame( |
| 793 | pen: &mut Pen, |
| 794 | chart: &CardChart, |
| 795 | area: Rect, |
| 796 | labels: &[String], |
| 797 | maximum_value: f64, |
| 798 | with_axis: bool, |
| 799 | ) -> HorizontalFrame { |
| 800 | let scale = nice_scale(maximum_value, AXIS_TICK_TARGET); |
| 801 | let widest_label = labels |
| 802 | .iter() |
| 803 | .map(|label| pen.text_width(label)) |
| 804 | .fold(0., f32::max); |
| 805 | let label_width = widest_label.min(area.width() * LABEL_COLUMN_MAX_RATIO); |
| 806 | let top_padding = if chart.x_axis_title.is_some() { |
| 807 | AXIS_TITLE_HEIGHT |
| 808 | } else { |
| 809 | FRAME_PADDING / 2. |
| 810 | }; |
| 811 | let bottom_padding = if with_axis { |
| 812 | CATEGORY_LABEL_HEIGHT |
| 813 | + if chart.y_axis_title.is_some() { |
| 814 | AXIS_TITLE_HEIGHT |
| 815 | } else { |
| 816 | 0. |
| 817 | } |
| 818 | } else { |
| 819 | FRAME_PADDING / 2. |
| 820 | }; |
| 821 | let plot = Rect { |
| 822 | left: area.left + label_width + AXIS_LABEL_GAP * 2., |
| 823 | top: area.top + top_padding, |
| 824 | right: area.right - VALUE_GUTTER, |
| 825 | bottom: area.bottom - bottom_padding, |
| 826 | }; |
| 827 | let frame = HorizontalFrame { |
| 828 | plot, |
| 829 | row_height: plot.height() / labels.len().max(1) as f32, |
| 830 | maximum: scale.maximum, |
| 831 | }; |
| 832 | if let Some(title) = &chart.x_axis_title { |
| 833 | pen.text( |
| 834 | title, |
| 835 | area.left, |
| 836 | area.top, |
| 837 | LabelAlign::Left, |
| 838 | theme::text_muted(), |
| 839 | ); |
| 840 | } |
| 841 | if with_axis { |
| 842 | for tick in scale.ticks() { |
| 843 | let x = frame.x(tick); |
| 844 | let line_color = if tick == 0. { |
| 845 | theme::border_strong() |
| 846 | } else { |
| 847 | theme::border() |
| 848 | }; |
| 849 | pen.vertical_line(x, plot.top, plot.bottom, line_color); |
| 850 | pen.text( |
| 851 | &format_chart_value(tick), |
| 852 | x, |
| 853 | plot.bottom + 4., |
| 854 | LabelAlign::Center, |
| 855 | theme::text_muted(), |
| 856 | ); |
| 857 | } |
| 858 | if let Some(title) = &chart.y_axis_title { |
| 859 | pen.text( |
| 860 | title, |
| 861 | plot.center_x(), |
| 862 | area.bottom - AXIS_TITLE_HEIGHT + 2., |
| 863 | LabelAlign::Center, |
| 864 | theme::text_muted(), |
| 865 | ); |
| 866 | } |
| 867 | } |
| 868 | for (index, label) in labels.iter().enumerate() { |
| 869 | let fitted = pen.fitted(label, label_width); |
| 870 | pen.text( |
| 871 | &fitted, |
| 872 | plot.left - AXIS_LABEL_GAP, |
| 873 | frame.row_center(index) - LABEL_LINE_HEIGHT / 2., |
| 874 | LabelAlign::Right, |
| 875 | theme::text_soft(), |
| 876 | ); |
| 877 | } |
| 878 | frame |
| 879 | } |
| 880 | |
| 881 | fn paint_horizontal_bars( |
| 882 | pen: &mut Pen, |
| 883 | chart: &CardChart, |
| 884 | bars: &[ChartPoint], |
| 885 | mode: BarDisplayMode, |
| 886 | area: Rect, |
| 887 | ) { |
| 888 | let labels: Vec<String> = bars.iter().map(|bar| bar.label.clone()).collect(); |
| 889 | let maximum = bars.iter().map(|bar| bar.value).fold(0., f64::max); |
| 890 | let with_axis = mode == BarDisplayMode::AbsoluteWithAxis; |
| 891 | let frame = horizontal_frame(pen, chart, area, &labels, maximum, with_axis); |
| 892 | let bar_height = HORIZONTAL_BAR_HEIGHT.min(frame.row_height * 0.7); |
| 893 | for (index, bar) in bars.iter().enumerate() { |
| 894 | let center = frame.row_center(index); |
| 895 | let right = frame.x(bar.value); |
| 896 | pen.rect( |
| 897 | Rect { |
| 898 | left: frame.plot.left, |
| 899 | top: center - bar_height / 2., |
| 900 | right, |
| 901 | bottom: center + bar_height / 2., |
| 902 | }, |
| 903 | palette(bar.color), |
| 904 | BAR_RADIUS, |
| 905 | ); |
| 906 | if chart.show_values || !with_axis { |
| 907 | pen.text( |
| 908 | &format_chart_value(bar.value), |
| 909 | right + 6., |
| 910 | center - LABEL_LINE_HEIGHT / 2., |
| 911 | LabelAlign::Left, |
| 912 | theme::text_strong(), |
| 913 | ); |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | fn paint_part_to_whole(pen: &mut Pen, bars: &[ChartPoint], area: Rect) { |
| 919 | let total: f64 = bars.iter().map(|bar| bar.value.max(0.)).sum(); |
| 920 | for (index, bar) in bars.iter().enumerate() { |
| 921 | let top = area.top + WHOLE_ROW_HEIGHT * index as f32; |
| 922 | let value_text = format_chart_value(bar.value); |
| 923 | let value_width = pen.text_width(&value_text); |
| 924 | let label = pen.fitted(&bar.label, area.width() - value_width - LEGEND_GAP); |
| 925 | pen.text(&label, area.left, top, LabelAlign::Left, theme::text_soft()); |
| 926 | pen.text( |
| 927 | &value_text, |
| 928 | area.right, |
| 929 | top, |
| 930 | LabelAlign::Right, |
| 931 | theme::text_strong(), |
| 932 | ); |
| 933 | let track = Rect { |
| 934 | left: area.left, |
| 935 | top: top + LABEL_LINE_HEIGHT + 6., |
| 936 | right: area.right, |
| 937 | bottom: top + LABEL_LINE_HEIGHT + 6. + WHOLE_BAR_HEIGHT, |
| 938 | }; |
| 939 | pen.rect(track, theme::surface_raised(), WHOLE_BAR_HEIGHT / 2.); |
| 940 | let share = unit_fraction(bar.value, 0., total); |
| 941 | pen.rect( |
| 942 | Rect { |
| 943 | right: track.left + track.width() * share, |
| 944 | ..track |
| 945 | }, |
| 946 | palette(bar.color), |
| 947 | WHOLE_BAR_HEIGHT / 2., |
| 948 | ); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | fn paint_stacked_rows(pen: &mut Pen, chart: &CardChart, rows: &[ChartSeries], area: Rect) { |
| 953 | let labels: Vec<String> = rows.iter().map(|row| row.legend.clone()).collect(); |
| 954 | let maximum = rows |
| 955 | .iter() |
| 956 | .map(|row| { |
| 957 | row.points |
| 958 | .iter() |
| 959 | .map(|point| point.value.max(0.)) |
| 960 | .sum::<f64>() |
| 961 | }) |
| 962 | .fold(0., f64::max); |
| 963 | let frame = horizontal_frame(pen, chart, area, &labels, maximum, true); |
| 964 | let bar_height = HORIZONTAL_BAR_HEIGHT.min(frame.row_height * 0.7); |
| 965 | for (index, row) in rows.iter().enumerate() { |
| 966 | let center = frame.row_center(index); |
| 967 | let mut consumed = 0.; |
| 968 | for point in &row.points { |
| 969 | let start = frame.x(consumed); |
| 970 | consumed += point.value.max(0.); |
| 971 | let end = frame.x(consumed); |
| 972 | let fill = palette(point.color); |
| 973 | pen.rect( |
| 974 | Rect { |
| 975 | left: start, |
| 976 | top: center - bar_height / 2., |
| 977 | right: (end - 1.).max(start), |
| 978 | bottom: center + bar_height / 2., |
| 979 | }, |
| 980 | fill, |
| 981 | 0., |
| 982 | ); |
| 983 | if chart.show_values && end - start >= SEGMENT_LABEL_MIN_WIDTH { |
| 984 | pen.text( |
| 985 | &format_chart_value(point.value), |
| 986 | (start + end) / 2., |
| 987 | center - LABEL_LINE_HEIGHT / 2., |
| 988 | LabelAlign::Center, |
| 989 | label_color_on(fill), |
| 990 | ); |
| 991 | } |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | fn half_arc_angle(full_circle_angle: f32) -> f32 { |
| 997 | PI + (full_circle_angle + FRAC_PI_2) / 2. |
| 998 | } |
| 999 | |
| 1000 | fn label_color_on(fill: Hsla) -> Hsla { |
| 1001 | if fill.l > LIGHT_FILL_LUMINANCE { |
| 1002 | theme::color(DARK_LABEL) |
| 1003 | } else { |
| 1004 | theme::white() |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | fn palette(color: ChartColor) -> Hsla { |
| 1009 | theme::color(match color { |
| 1010 | ChartColor::Good => 0x3fb56f, |
| 1011 | ChartColor::Warning => 0xf2b33d, |
| 1012 | ChartColor::Attention => 0xe5484d, |
| 1013 | ChartColor::Neutral => 0x4a4a50, |
| 1014 | ChartColor::CategoricalRed => 0xe5484d, |
| 1015 | ChartColor::CategoricalPurple => 0x9b6bdc, |
| 1016 | ChartColor::CategoricalLavender => 0xb4a7f5, |
| 1017 | ChartColor::CategoricalBlue => 0x4f8cff, |
| 1018 | ChartColor::CategoricalLightBlue => 0x7cc4f7, |
| 1019 | ChartColor::CategoricalTeal => 0x2fb8a6, |
| 1020 | ChartColor::CategoricalGreen => 0x4cb86a, |
| 1021 | ChartColor::CategoricalLime => 0xa5d24a, |
| 1022 | ChartColor::CategoricalMarigold => 0xf0a830, |
| 1023 | ChartColor::Sequential1 => 0xc7dbff, |
| 1024 | ChartColor::Sequential2 => 0xa3c4fb, |
| 1025 | ChartColor::Sequential3 => 0x7eabf6, |
| 1026 | ChartColor::Sequential4 => 0x5b92ee, |
| 1027 | ChartColor::Sequential5 => 0x3f79e0, |
| 1028 | ChartColor::Sequential6 => 0x2e61c8, |
| 1029 | ChartColor::Sequential7 => 0x2a4fa3, |
| 1030 | ChartColor::Sequential8 => 0x253f80, |
| 1031 | ChartColor::DivergingBlue => 0x3b6fd6, |
| 1032 | ChartColor::DivergingLightBlue => 0x7fb2f0, |
| 1033 | ChartColor::DivergingCyan => 0x4cc3d9, |
| 1034 | ChartColor::DivergingTeal => 0x2fa89a, |
| 1035 | ChartColor::DivergingYellow => 0xe8d44d, |
| 1036 | ChartColor::DivergingPeach => 0xf2a679, |
| 1037 | ChartColor::DivergingLightRed => 0xea7a72, |
| 1038 | ChartColor::DivergingRed => 0xd64545, |
| 1039 | ChartColor::DivergingMaroon => 0x962b3a, |
| 1040 | ChartColor::DivergingGray => 0x8a8a92, |
| 1041 | }) |
| 1042 | } |