Skip to main content

stack

Function stack 

Source
pub fn stack(a: impl Into<Element>, b: impl Into<Element>) -> Composition
Expand description

Stack a on top of b in a 2×1 composition.

Examples found in repository?
examples/aa_nondeterminism.rs (lines 53-56)
51fn build() -> PlotComposition {
52    let comp = || {
53        stack(
54            beside(Patch::new("a"), Patch::new("b")),
55            beside(Patch::new("c"), Patch::new("d")),
56        )
57    };
58
59    let xs: Vec<f64> = (0..40).map(|i| f64::from(i) * 0.5).collect();
60    let ys: Vec<f64> = xs.iter().map(|x| 10.0 + 8.0 * (x * 0.2).sin()).collect();
61
62    let mut view = PlotComposition::new(&comp())
63        // Removing this caption makes the output deterministic.
64        .caption("Composition caption")
65        .add_scale("t", scale::continuous(0.0..=20.0))
66        .add_scale("v", scale::continuous(0.0..=20.0));
67
68    for name in ["a", "b", "c", "d"] {
69        let mut plot = Plot::new(&comp(), name).bind("x", "t").bind("y", "v");
70        // Removing these geoms also makes the output deterministic.
71        plot.add_geom(
72            PointGeom::builder()
73                .set("x", xs.clone())
74                .set("y", ys.clone())
75                .set("fill", rgb8(200, 30, 30))
76                .set("size", 4.0_f64)
77                .build(),
78        );
79        view = view.with_plot(plot);
80    }
81    view
82}
More examples
Hide additional examples
examples/nesting_asymmetric.rs (line 63)
42fn main() {
43    let (w, h) = (1200u32, 700u32);
44    let dpi = 96.0;
45
46    let row_three = grid(
47        1,
48        3,
49        vec![
50            plot("a1", "Series A1", "0\n10\n20", "0  5  10").into(),
51            plot("a2", "Series A2", "0\n50\n100", "0  5  10").into(),
52            plot("a3", "Series A3", "0\n500\n1000", "0  5  10").into(),
53        ],
54    );
55    let row_two = grid(
56        1,
57        2,
58        vec![
59            plot("b1", "Series B1 (wider)", "0\n100\n200\n300", "0   25   50").into(),
60            plot("b2", "Series B2 (wider)", "0\n5000\n10000", "0   25   50").into(),
61        ],
62    );
63    let composed = stack(row_three, row_two);
64    let layout = composed.solve(hephaestus::Size::new(w as f64, h as f64), dpi);
65
66    let mut renderer = VelloRenderer::new().expect("vello renderer init");
67    {
68        let scene = renderer.scene();
69        let stroke = hephaestus::stroke::Stroke::new(1.0);
70        let text_brush: Brush = rgb8(20, 20, 30).into();
71
72        // Chrome rects, tinted background.
73        for (_id, region, rect) in layout.iter() {
74            if region == "panel" {
75                continue;
76            }
77            let c = color_for_region(region);
78            let tint = Color::new([c.components[0], c.components[1], c.components[2], 0.20]);
79            let path: Path = rect.to_path(0.1);
80            scene.fill(
81                FillRule::NonZero,
82                Affine::IDENTITY,
83                &Brush::Solid(tint),
84                None,
85                &path,
86                PickId::Skip,
87            );
88            scene.stroke(
89                &stroke,
90                Affine::IDENTITY,
91                &Brush::Solid(c),
92                None,
93                &path,
94                PickId::Skip,
95            );
96        }
97
98        // Panels solid.
99        for (_id, region, rect) in layout.iter() {
100            if region != "panel" {
101                continue;
102            }
103            let path: Path = rect.to_path(0.1);
104            scene.fill(
105                FillRule::NonZero,
106                Affine::IDENTITY,
107                &Brush::Solid(color_for_region(region)),
108                None,
109                &path,
110                PickId::Skip,
111            );
112            scene.stroke(
113                &stroke,
114                Affine::IDENTITY,
115                &Brush::Solid(rgb8(255, 255, 255)),
116                None,
117                &path,
118                PickId::Skip,
119            );
120        }
121
122        // Text labels in chrome.
123        for (id, region, rect) in layout.iter() {
124            if region == "panel" {
125                continue;
126            }
127            let text = match (id, region) {
128                ("a1", "title") => "Series A1",
129                ("a2", "title") => "Series A2",
130                ("a3", "title") => "Series A3",
131                ("b1", "title") => "Series B1 (wider)",
132                ("b2", "title") => "Series B2 (wider)",
133                ("a1", "axis_left") => "0\n10\n20",
134                ("a2", "axis_left") => "0\n50\n100",
135                ("a3", "axis_left") => "0\n500\n1000",
136                ("b1", "axis_left") => "0\n100\n200\n300",
137                ("b2", "axis_left") => "0\n5000\n10000",
138                (_, "axis_bottom") if id.starts_with('a') => "0  5  10",
139                (_, "axis_bottom") if id.starts_with('b') => "0   25   50",
140                _ => continue,
141            };
142            let size = if region == "title" { 16.0 } else { 11.0 };
143            let run = TextRun::new(text, &TextStyle::new(size), 96.0);
144            draw_text_in_rect(scene, &run, rect, &text_brush, PickId::Skip);
145        }
146    }
147
148    let mut pixels = vec![0u8; (w * h * 4) as usize];
149    let bg: Color = rgb8(248, 248, 252);
150    renderer
151        .render_to_buffer(w, h, bg, &mut pixels)
152        .expect("render");
153
154    let path = std::env::current_dir()
155        .unwrap()
156        .join("examples/nesting_asymmetric.png");
157    hephaestus::image::write_png(&path, w, h, &pixels).expect("write png");
158    println!("wrote {}", path.display());
159}