hellogauges/gauges/
arc.rs

1/*
2HELLOGAUGES
3Copyright (C) 2022 Adrián Romero
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 3 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program.  If not, see <http://www.gnu.org/licenses/>.
16*/
17
18use yew::prelude::*;
19
20use super::svgdraw;
21
22#[derive(Clone, PartialEq)]
23pub struct ArcContext {
24    pub min: f64,
25    pub max: f64,
26    pub startangle: f64,
27    pub endangle: f64,
28    pub centerx: i32,
29    pub centery: i32,
30    pub r: f64,
31    pub class: &'static str,
32}
33
34#[derive(Properties, PartialEq)]
35pub struct ArcProps {
36    pub start: f64,
37    pub end: f64,
38    #[prop_or(1.0)]
39    pub r: f64,
40    #[prop_or_default]
41    pub style: String,
42}
43
44#[function_component(Arc)]
45pub fn arc(props: &ArcProps) -> Html {
46    let ctx = use_context::<ArcContext>().expect("no ctx found");
47
48    let arctotal = ctx.endangle - ctx.startangle;
49    let arcstart = svgdraw::padvalue(ctx.min, ctx.max, arctotal, props.start) + ctx.startangle;
50    let arcend = svgdraw::padvalue(ctx.min, ctx.max, arctotal, props.end) + ctx.startangle;
51
52    html! {
53    <path
54        d={svgdraw::arcpath(
55            ctx.centerx,
56            ctx.centery,
57            ctx.r * props.r,
58            arcstart,
59            arcend,
60            arcend - arcstart > 180.0,
61            1)}
62        class={ctx.class}
63        style={props.style.clone()}
64    />}
65}