| 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 |
use crate::form::escape_into; |
| 29 |
use crate::{Depth, Emit, class, depth_rule, gated, hover_condition, push_class}; |
| 30 |
use makeover_layout::{Bar, Chart, Intent, Tone}; |
| 31 |
use std::fmt::Write as _; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
pub const CHART_CLASSES: &[&str] = &[ |
| 38 |
"chart", |
| 39 |
"chart-bars", |
| 40 |
"chart-bar-col", |
| 41 |
"chart-bar", |
| 42 |
"chart-bar-label", |
| 43 |
]; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
#[must_use] |
| 56 |
pub fn bar_text(bar: &Bar<'_>) -> Option<String> { |
| 57 |
match (bar.reading, bar.note) { |
| 58 |
(Some(reading), Some(note)) => Some(format!("{reading} / {note}")), |
| 59 |
(Some(only), None) | (None, Some(only)) => Some(only.to_string()), |
| 60 |
(None, None) => None, |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
pub fn chart_html_into<'a>( |
| 75 |
chart: &Chart<'_>, |
| 76 |
bars: impl IntoIterator<Item = Bar<'a>>, |
| 77 |
opts: &Emit, |
| 78 |
out: &mut String, |
| 79 |
) { |
| 80 |
emit_chart(chart, bars, opts, out, None); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
pub fn chart_html_placed<'a>( |
| 92 |
chart: &Chart<'_>, |
| 93 |
bars: impl IntoIterator<Item = Bar<'a>>, |
| 94 |
opts: &Emit, |
| 95 |
out: &mut String, |
| 96 |
placed: &mut Vec<core::ops::Range<usize>>, |
| 97 |
) { |
| 98 |
emit_chart(chart, bars, opts, out, Some(placed)); |
| 99 |
} |
| 100 |
|
| 101 |
fn emit_chart<'a>( |
| 102 |
chart: &Chart<'_>, |
| 103 |
bars: impl IntoIterator<Item = Bar<'a>>, |
| 104 |
opts: &Emit, |
| 105 |
out: &mut String, |
| 106 |
mut placed: Option<&mut Vec<core::ops::Range<usize>>>, |
| 107 |
) { |
| 108 |
out.push_str("<div class=\""); |
| 109 |
push_class(out, "chart", opts); |
| 110 |
out.push('"'); |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
let _ = write!(out, " style=\"--most: {}\"", chart.most); |
| 115 |
if chart.tone != Tone::Neutral { |
| 116 |
let _ = write!(out, " data-tone=\"{}\"", chart.tone.token()); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
if let Some(label) = chart.label { |
| 125 |
out.push_str(" role=\"img\" aria-label=\""); |
| 126 |
escape_into(label, out); |
| 127 |
out.push('"'); |
| 128 |
} |
| 129 |
out.push('>'); |
| 130 |
|
| 131 |
out.push_str("<div class=\""); |
| 132 |
push_class(out, "chart-bars", opts); |
| 133 |
out.push_str("\">"); |
| 134 |
|
| 135 |
for bar in bars { |
| 136 |
let at = out.len(); |
| 137 |
bar_html_into(&bar, opts, out); |
| 138 |
if let Some(placed) = placed.as_deref_mut() { |
| 139 |
placed.push(at..out.len()); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
out.push_str("</div></div>"); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
fn bar_html_into(bar: &Bar<'_>, opts: &Emit, out: &mut String) { |
| 151 |
out.push_str("<div class=\""); |
| 152 |
push_class(out, "chart-bar-col", opts); |
| 153 |
out.push('"'); |
| 154 |
if let Some(text) = bar_text(bar) { |
| 155 |
out.push_str(" data-tooltip=\""); |
| 156 |
escape_into(&text, out); |
| 157 |
out.push('"'); |
| 158 |
} |
| 159 |
out.push('>'); |
| 160 |
|
| 161 |
out.push_str("<div class=\""); |
| 162 |
push_class(out, "chart-bar", opts); |
| 163 |
|
| 164 |
|
| 165 |
let _ = write!(out, "\" style=\"--value: {}\"></div>", bar.value); |
| 166 |
|
| 167 |
out.push_str("<div class=\""); |
| 168 |
push_class(out, "chart-bar-label", opts); |
| 169 |
out.push_str("\">"); |
| 170 |
escape_into(bar.at, out); |
| 171 |
out.push_str("</div></div>"); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
#[must_use] |
| 176 |
pub fn chart_html<'a>( |
| 177 |
chart: &Chart<'_>, |
| 178 |
bars: impl IntoIterator<Item = Bar<'a>>, |
| 179 |
opts: &Emit, |
| 180 |
) -> String { |
| 181 |
let mut html = String::new(); |
| 182 |
chart_html_into(chart, bars, opts, &mut html); |
| 183 |
html |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
fn chart_rules(opts: &Emit) -> String { |
| 206 |
let chart = class("chart", opts); |
| 207 |
let bars = class("chart-bars", opts); |
| 208 |
let col = class("chart-bar-col", opts); |
| 209 |
let bar = class("chart-bar", opts); |
| 210 |
let label = class("chart-bar-label", opts); |
| 211 |
|
| 212 |
let mut css = depth_rule(&chart, Depth::Well); |
| 213 |
|
| 214 |
let _ = writeln!( |
| 215 |
css, |
| 216 |
".{bars} {{\n display: flex;\n align-items: flex-end;\n \ |
| 217 |
gap: var(--chart-gap, 2px);\n height: var(--chart-height, 200px);\n}}" |
| 218 |
); |
| 219 |
let _ = writeln!( |
| 220 |
css, |
| 221 |
".{col} {{\n flex: 1;\n display: flex;\n flex-direction: column;\n \ |
| 222 |
align-items: center;\n min-width: 0;\n position: relative;\n}}" |
| 223 |
); |
| 224 |
let _ = writeln!( |
| 225 |
css, |
| 226 |
".{bar} {{\n width: 100%;\n background: var(--action);\n \ |
| 227 |
min-height: var(--chart-bar-least, 2px);\n \ |
| 228 |
height: calc(var(--value, 0) * 100% / max(var(--most, 1), 1));\n}}" |
| 229 |
); |
| 230 |
|
| 231 |
|
| 232 |
for tone in [Tone::Info, Tone::Success, Tone::Warning, Tone::Danger] { |
| 233 |
let _ = writeln!( |
| 234 |
css, |
| 235 |
".{chart}[data-tone=\"{0}\"] .{bar} {{\n background: var(--{0});\n}}", |
| 236 |
tone.token() |
| 237 |
); |
| 238 |
} |
| 239 |
let _ = writeln!( |
| 240 |
css, |
| 241 |
".{label} {{\n color: var(--content-muted);\n max-width: 100%;\n \ |
| 242 |
white-space: nowrap;\n overflow: hidden;\n text-overflow: ellipsis;\n}}" |
| 243 |
); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
css.push_str(&gated( |
| 256 |
hover_condition(), |
| 257 |
&format!( |
| 258 |
".{col}[data-tooltip]:hover::before {{\n content: attr(data-tooltip);\n \ |
| 259 |
position: absolute;\n bottom: 100%;\n inset-inline: 0;\n \ |
| 260 |
margin-inline: auto;\n width: max-content;\n \ |
| 261 |
background: var(--surface-raised);\n color: var(--content);\n \ |
| 262 |
border: var(--border);\n box-shadow: var(--elevation-overlay);\n \ |
| 263 |
padding: var(--chart-readout-padding, 0.25em 0.5em);\n \ |
| 264 |
white-space: nowrap;\n pointer-events: none;\n}}\n" |
| 265 |
), |
| 266 |
)); |
| 267 |
css |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
#[must_use] |
| 272 |
pub fn rules(opts: &Emit) -> String { |
| 273 |
chart_rules(opts) |
| 274 |
} |
| 275 |
|
| 276 |
#[cfg(test)] |
| 277 |
mod tests { |
| 278 |
use super::*; |
| 279 |
|
| 280 |
fn axis() -> Chart<'static> { |
| 281 |
Chart::new(6740).label("revenue") |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
#[test] |
| 289 |
fn both_numbers_are_printed_and_neither_is_divided() { |
| 290 |
let html = chart_html( |
| 291 |
&axis(), |
| 292 |
[Bar::at("Mar 3").of(4210).reading("$42.10").note("3 sales")], |
| 293 |
&Emit::default(), |
| 294 |
); |
| 295 |
assert!(html.contains("--most: 6740"), "{html}"); |
| 296 |
assert!(html.contains("--value: 4210"), "{html}"); |
| 297 |
assert!( |
| 298 |
!html.contains('%'), |
| 299 |
"a percentage reached the markup: {html}" |
| 300 |
); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
#[test] |
| 307 |
fn an_unlabelled_chart_claims_no_role() { |
| 308 |
let named = chart_html(&axis(), [Bar::at("Mar 3").of(1)], &Emit::default()); |
| 309 |
assert!( |
| 310 |
named.contains(r#"role="img" aria-label="revenue""#), |
| 311 |
"{named}" |
| 312 |
); |
| 313 |
|
| 314 |
let bare = chart_html(&Chart::new(10), [Bar::at("Mar 3").of(1)], &Emit::default()); |
| 315 |
assert!(!bare.contains("role="), "{bare}"); |
| 316 |
assert!(!bare.contains("aria-label"), "{bare}"); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
#[test] |
| 322 |
fn a_readout_is_what_the_bar_was_given() { |
| 323 |
assert_eq!( |
| 324 |
bar_text(&Bar::at("a").of(1).reading("$1").note("2 sales")), |
| 325 |
Some("$1 / 2 sales".to_string()) |
| 326 |
); |
| 327 |
assert_eq!( |
| 328 |
bar_text(&Bar::at("a").of(1).reading("$1")), |
| 329 |
Some("$1".to_string()) |
| 330 |
); |
| 331 |
assert_eq!( |
| 332 |
bar_text(&Bar::at("a").of(1).note("2 sales")), |
| 333 |
Some("2 sales".to_string()) |
| 334 |
); |
| 335 |
assert_eq!(bar_text(&Bar::at("a").of(1)), None); |
| 336 |
|
| 337 |
let bare = chart_html(&axis(), [Bar::at("Mar 3").of(1)], &Emit::default()); |
| 338 |
assert!(!bare.contains("data-tooltip"), "{bare}"); |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
#[test] |
| 344 |
fn a_label_and_a_readout_are_escaped() { |
| 345 |
let html = chart_html( |
| 346 |
&Chart::new(10).label("a & b"), |
| 347 |
[Bar::at("<script>").of(5).reading("\"x\"")], |
| 348 |
&Emit::default(), |
| 349 |
); |
| 350 |
assert!(!html.contains("<script>"), "{html}"); |
| 351 |
assert!(html.contains("<script>"), "{html}"); |
| 352 |
assert!(html.contains("a & b"), "{html}"); |
| 353 |
assert!(html.contains(""x""), "{html}"); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
#[test] |
| 361 |
fn an_empty_axis_draws_rather_than_dividing() { |
| 362 |
let html = chart_html(&Chart::new(0), [Bar::at("Mar 3").of(0)], &Emit::default()); |
| 363 |
assert!(html.contains("--most: 0"), "{html}"); |
| 364 |
assert!(html.contains("--value: 0"), "{html}"); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
#[test] |
| 370 |
fn the_streamed_form_is_the_returned_one() { |
| 371 |
let opts = Emit { |
| 372 |
class_prefix: "mk-", |
| 373 |
..Emit::default() |
| 374 |
}; |
| 375 |
let bars = [Bar::at("Mar 3").of(4210).reading("$42.10")]; |
| 376 |
let mut streamed = String::new(); |
| 377 |
chart_html_into(&axis(), bars, &opts, &mut streamed); |
| 378 |
assert_eq!(streamed, chart_html(&axis(), bars, &opts)); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
#[test] |
| 384 |
fn every_class_this_module_writes_has_a_rule() { |
| 385 |
let css = chart_rules(&Emit::default()); |
| 386 |
for name in CHART_CLASSES { |
| 387 |
assert!(css.contains(&format!(".{name}")), "{name} has no rule"); |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
|