1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/// A Axis represents a range of f32 values. The tick interval of that range is expressed
/// as a step. The axis also has an orientation describing which side of the axis should be used
/// to convey its optional title.
///
/// The component takes a "name" property field so that it may be easily referenced when styled.
///
/// The following styling properties are available:
///
/// * axis - the axis as a whole
/// * line - the axis line
/// * tick - the axis tick line
/// * text - the axis text
use std::{marker::PhantomData, rc::Rc};

use gloo_events::EventListener;
use wasm_bindgen::JsCast;
use web_sys::{Element, SvgElement};
use yew::prelude::*;

use crate::series::Scalar;

/// Axis scaled value, expected to be between 0 and 1
/// except in the case where the value is outside of the axis range
#[derive(Debug, PartialEq)]
pub struct NormalisedValue(pub f32);

/// Specifies a generic scale on which axes and data can be rendered
pub trait Scale {
    type Scalar: Scalar;

    /// Provides the list of [ticks](AxisTick) that should be rendered along the axis
    fn ticks(&self) -> Vec<Tick>;

    /// Normalises a value within the axis scale to a number between 0 and 1,
    /// where 0 represents the minimum value of the scale, and 1 the maximum
    ///
    /// For example, for a linear scale between 50 and 100:
    /// - normalise(50)  -> 0
    /// - normalise(60)  -> 0.2
    /// - normalise(75)  -> 0.5
    /// - normalise(100) -> 1
    fn normalise(&self, value: Self::Scalar) -> NormalisedValue;
}

/// An axis tick, specifying a label to be displayed at some normalised
/// position along the axis
#[derive(Debug, PartialEq)]
pub struct Tick {
    /// normalised location between zero and one along the axis specifying
    /// the position at which the tick should be rendered
    pub location: NormalisedValue,

    /// An optional label that should be rendered alongside the tick
    pub label: Option<String>,
}

pub enum Msg {
    Resize,
}

#[derive(Clone, PartialEq)]
pub enum Orientation {
    Left,
    Right,
    Bottom,
    Top,
}

#[derive(Properties, Clone)]
pub struct Props<S: Scalar> {
    /// A name given to the axis that will be used for CSS classes
    pub name: String,
    /// How the axis will be positioned in relation to other elements
    pub orientation: Orientation,
    /// The start position
    pub x1: f32,
    /// The start position
    pub y1: f32,
    /// The target position as x or y depending on orientation - x for left
    /// and right, y for bottom and top
    pub xy2: f32,
    /// The length of ticks
    pub tick_len: f32,
    /// Any title to be drawn and associated with the axis
    #[prop_or_default]
    pub title: Option<String>,
    /// The scaling conversion to be used with the axis
    pub scale: Rc<dyn Scale<Scalar = S>>,
}

impl<S: Scalar> PartialEq for Props<S> {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
            && self.orientation == other.orientation
            && self.x1 == other.x1
            && self.y1 == other.y1
            && self.xy2 == other.xy2
            && self.tick_len == other.tick_len
            && self.title == other.title
            && std::ptr::eq(
                // test reference equality, avoiding issues with vtables discussed in
                // https://github.com/rust-lang/rust/issues/46139
                &*self.scale as *const _ as *const u8,
                &*other.scale as *const _ as *const u8,
            )
    }
}

pub struct Axis<S: Scalar> {
    phantom: PhantomData<S>,
    _resize_listener: EventListener,
    svg: NodeRef,
}

impl<S: Scalar + 'static> Component for Axis<S> {
    type Message = Msg;

    type Properties = Props<S>;

    fn create(ctx: &Context<Self>) -> Self {
        let on_resize = ctx.link().callback(|_: Event| Msg::Resize);
        Axis {
            phantom: PhantomData,
            _resize_listener: EventListener::new(&gloo_utils::window(), "resize", move |e| {
                on_resize.emit(e.clone())
            }),
            svg: NodeRef::default(),
        }
    }

    fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
        match msg {
            Msg::Resize => true,
        }
    }

    fn changed(&mut self, _ctx: &Context<Self>, _old_props: &Self::Properties) -> bool {
        true
    }

    fn view(&self, ctx: &Context<Self>) -> Html {
        let p = ctx.props();

        fn title(x: f32, y: f32, baseline: &str, title: &str) -> Html {
            html! {
                <text
                    x={x.to_string()} y={y.to_string()}
                    dominant-baseline={baseline.to_string()}
                    text-anchor={"middle"}
                    transform-origin={format!("{} {}", x, y)}
                    class="title" >
                    {title}
                </text>
            }
        }

        let class = match p.orientation {
            Orientation::Left => "left",
            Orientation::Right => "right",
            Orientation::Bottom => "bottom",
            Orientation::Top => "top",
        };

        if p.orientation == Orientation::Left || p.orientation == Orientation::Right {
            let scale = p.xy2 - p.y1;
            let x = p.x1;
            let to_x = if p.orientation == Orientation::Left {
                x - p.tick_len
            } else {
                x + p.tick_len
            };

            html! {
                <svg ref={self.svg.clone()} class={classes!("axis", class, p.name.to_owned())}>
                    <line x1={p.x1.to_string()} y1={p.y1.to_string()} x2={p.x1.to_string()} y2={p.xy2.to_string()} class="line" />
                    { for (p.scale.ticks().iter()).map(|Tick { location: NormalisedValue(normalised_location), label }| {
                        let y = (p.xy2 - (normalised_location * scale)) as u32;
                        html! {
                        <>
                            <line x1={x.to_string()} y1={y.to_string()} x2={to_x.to_string()} y2={y.to_string()} class="tick" />
                            if let Some(l) = label {
                                <text x={to_x.to_string()} y={y.to_string()} text-anchor={if p.orientation == Orientation::Left {"end"} else {"start"}} class="text">{l.to_string()}</text>
                            }
                        </>
                        }
                    }) }
                    { for p.title.as_ref().map(|t| {
                        let title_distance = p.tick_len * 2.0;
                        let x = if p.orientation == Orientation::Left {
                            p.x1 - title_distance
                        } else {
                            p.x1 + title_distance
                        };
                        let y = p.y1 + ((p.xy2 - p.y1) * 0.5);
                        title(x, y, "auto",t)
                    })}
                </svg>
            }
        } else {
            let scale = p.xy2 - p.x1;
            let y = p.y1;
            let (to_y, baseline) = if p.orientation == Orientation::Top {
                (y - p.tick_len, "auto")
            } else {
                (y + p.tick_len, "hanging")
            };

            html! {
                <svg ref={self.svg.clone()} class={classes!("axis", class, p.name.to_owned())}>
                    <line x1={p.x1.to_string()} y1={p.y1.to_string()} x2={p.xy2.to_string()} y2={p.y1.to_string()} class="line" />
                    { for(p.scale.ticks().iter()).map(|Tick { location: NormalisedValue(normalised_location), label }| {
                        let x = p.x1 + normalised_location * scale;
                        html! {
                        <>
                            <line x1={x.to_string()} y1={y.to_string()} x2={x.to_string()} y2={to_y.to_string()} class="tick" />
                            if let Some(l) = label {
                                <text x={x.to_string()} y={to_y.to_string()} text-anchor="middle" transform-origin={format!("{} {}", x, to_y)} dominant-baseline={baseline.to_string()} class="text">{l.to_string()}</text>
                            }
                        </>
                        }
                    }) }
                    { for p.title.as_ref().map(|t| {
                        let title_distance = p.tick_len * 2.0;
                        let y = if p.orientation == Orientation::Top {
                            p.y1 - title_distance
                        } else {
                            p.y1 + title_distance
                        };
                        let x = p.x1 + ((p.xy2 - p.x1) * 0.5);
                        title(x, y, baseline, t)
                    })}
                </svg>
            }
        }
    }

    fn rendered(&mut self, ctx: &Context<Self>, _first_render: bool) {
        let p = ctx.props();

        let element = self.svg.cast::<Element>().unwrap();
        if let Some(svg_element) = element
            .first_child()
            .and_then(|n| n.dyn_into::<SvgElement>().ok())
        {
            let bounding_rect = svg_element.get_bounding_client_rect();
            let scale = if p.orientation == Orientation::Left || p.orientation == Orientation::Right
            {
                let height = bounding_rect.height() as f32;
                (p.xy2 - p.y1) / height
            } else {
                let width = bounding_rect.width() as f32;
                (p.xy2 - p.x1) / width
            };
            let font_size = scale * 100.0;
            let _ = element.set_attribute("font-size", &format!("{}%", &font_size));
            let _ = element.set_attribute("style", &format!("stroke-width: {}", scale));
        }
    }
}