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
use leptos::prelude::*;
use leptos::{component, view, IntoView};
/// Axis component for X and Y axes with ticks and labels
use lodviz_core::core::scale::Scale;
/// Axis orientation
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AxisOrientation {
/// Horizontal axis (bottom)
Bottom,
/// Horizontal axis (top)
#[allow(dead_code)]
Top,
/// Vertical axis (left)
Left,
/// Vertical axis (right)
#[allow(dead_code)]
Right,
}
/// Axis component for rendering X/Y axes
///
/// Renders an axis line with ticks and labels based on the provided scale.
/// Optionally renders an axis label (e.g. "Time", "Amplitude").
#[component]
pub fn Axis<S: Scale + Clone + 'static>(
/// Orientation of the axis
orientation: AxisOrientation,
/// Scale for mapping domain to range
scale: S,
/// Number of ticks to display
#[prop(default = 5)]
tick_count: usize,
/// Width of the axis (for horizontal) or height (for vertical)
#[prop(default = 600.0)]
_dimension: f64,
/// Stroke color for axis lines and text
#[prop(default = "currentColor".to_string(), into)]
stroke: String,
/// Font size for axis text (overrides dynamic sizing if set)
#[prop(default = 12.0)]
font_size: f64,
/// Chart width in pixels — if provided, font_size is scaled dynamically
#[prop(optional)]
chart_width: Option<f64>,
/// Optional axis label text
#[prop(default = None)]
label: Option<String>,
) -> impl IntoView {
// Dynamic font sizing: scale based on chart dimensions
let font_size = match chart_width {
Some(w) if w < 300.0 => font_size.min(9.0),
Some(w) if w < 600.0 => font_size.min(11.0),
_ => font_size,
};
let (domain_min, domain_max) = scale.domain();
let (range_min, range_max) = scale.range();
// Generate tick values evenly spaced in the domain
let tick_values: Vec<f64> = (0..=tick_count)
.map(|i| {
let t = i as f64 / tick_count as f64;
domain_min + t * (domain_max - domain_min)
})
.collect();
// Map tick values to positions
let ticks: Vec<(f64, f64)> = tick_values
.iter()
.map(|&value| (value, scale.map(value)))
.collect();
let axis_center = (range_min + range_max) / 2.0;
match orientation {
AxisOrientation::Bottom => {
let label_y_offset = 35.0 + font_size * 1.2;
view! {
<g class="axis axis-bottom" pointer-events="none">
// Axis line
<line
x1=range_min
y1=0
x2=range_max
y2=0
stroke=stroke.clone()
stroke-width="1"
/>
// Ticks and labels
{ticks
.iter()
.map(|(value, pos)| {
view! {
<g key=*value>
// Tick mark
<line
x1=*pos
y1=0
x2=*pos
y2=6
stroke=stroke.clone()
stroke-width="1"
/>
// Label
<text
x=*pos
y=20
text-anchor="middle"
font-size=font_size
fill=stroke.clone()
>
{format!("{:.1}", value)}
</text>
</g>
}
})
.collect_view()}
// Axis label
{label
.clone()
.map(|text| {
view! {
<text
x=axis_center
y=label_y_offset
text-anchor="middle"
font-size=font_size + 1.0
fill=stroke.clone()
>
{text}
</text>
}
})}
</g>
}
.into_any()
}
AxisOrientation::Left => {
let center_y = axis_center;
let label_x_offset = -(45.0 + font_size * 1.2);
let ticks_view = ticks
.iter()
.map(|(value, pos)| {
view! {
<g key=*value>
<line
x1=0
y1=*pos
x2=-6
y2=*pos
stroke=stroke.clone()
stroke-width="1"
/>
<text
x=-10
y=*pos
text-anchor="end"
dominant-baseline="middle"
font-size=font_size
fill=stroke.clone()
>
{format!("{:.1}", value)}
</text>
</g>
}
})
.collect_view();
let label_view = label.clone().map(|text| {
view! {
<text
transform=format!("rotate(-90, {label_x_offset}, {center_y})")
x=label_x_offset
y=center_y
text-anchor="middle"
dominant-baseline="middle"
font-size=font_size + 2.0
fill=stroke.clone()
>
{text}
</text>
}
});
view! {
<g class="axis axis-left" pointer-events="none">
<line
x1=0
y1=range_min
x2=0
y2=range_max
stroke=stroke.clone()
stroke-width="1"
/>
{ticks_view}
{label_view}
</g>
}
.into_any()
}
AxisOrientation::Top => view! {
<g class="axis axis-top" pointer-events="none">
<line x1=range_min y1=0 x2=range_max y2=0 stroke=stroke.clone() stroke-width="1" />
</g>
}
.into_any(),
AxisOrientation::Right => view! {
<g class="axis axis-right" pointer-events="none">
<line x1=0 y1=range_min x2=0 y2=range_max stroke=stroke.clone() stroke-width="1" />
</g>
}
.into_any(),
}
}