Skip to main content

rgpui_component/plot/
mod.rs

1mod axis;
2mod grid;
3pub mod label;
4pub mod scale;
5pub mod shape;
6pub mod tooltip;
7
8pub use rgpui_component_macros::IntoPlot;
9
10use std::{fmt::Debug, ops::Add};
11
12use rgpui::{App, Bounds, IntoElement, Path, PathBuilder, Pixels, Point, Window, point, px};
13
14pub use axis::{AXIS_GAP, AxisLabelSide, AxisText, PlotAxis};
15pub use grid::Grid;
16pub use label::PlotLabel;
17
18pub trait Plot: IntoElement {
19    fn paint(&mut self, bounds: Bounds<Pixels>, window: &mut Window, cx: &mut App);
20}
21
22#[derive(Clone, Copy, Default)]
23pub enum StrokeStyle {
24    #[default]
25    Natural,
26    Linear,
27    StepAfter,
28}
29
30pub fn origin_point<T>(x: T, y: T, origin: Point<T>) -> Point<T>
31where
32    T: Default + Clone + Debug + PartialEq + Add<Output = T>,
33{
34    point(x, y) + origin
35}
36
37pub fn polygon<T>(points: &[Point<T>], bounds: &Bounds<Pixels>) -> Option<Path<Pixels>>
38where
39    T: Default + Clone + Copy + Debug + Into<f32> + PartialEq,
40{
41    let mut path = PathBuilder::stroke(px(1.));
42    let points = &points
43        .iter()
44        .map(|p| {
45            point(
46                px(p.x.into() + bounds.origin.x.as_f32()),
47                px(p.y.into() + bounds.origin.y.as_f32()),
48            )
49        })
50        .collect::<Vec<_>>();
51    path.add_polygon(points, false);
52    path.build().ok()
53}