charcoal 0.1.1

Declarative, DataFrame-native chart library for Polars. No browser required.
Documentation
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
//! Chart builder entry points and shared output types.
//!
//! This module wires together all chart-type submodules and exposes the
//! [`Chart`] struct (the fully-rendered output of any `.build()` call) along
//! with the shared option enums ([`NullPolicy`], [`DashStyle`], [`Orientation`])
//! that appear across multiple chart types.
//!
//! Each chart type lives in its own submodule (`scatter`, `line`, `bar`, …) and
//! is reached through the static entry points on `Chart` (e.g. `Chart::scatter`).
//! Contributors adding a new chart type should follow the typestate pattern
//! established in `scatter.rs` — see `CONTRIBUTING.md` for the full checklist.

#![allow(dead_code)]

pub mod area;
pub mod bar;
pub mod box_plot;
pub mod heatmap;
pub mod histogram;
pub mod line;
pub mod scatter;

use std::fs;
use polars::frame::DataFrame;
use crate::error::{CharcoalError, CharcoalWarning};

impl Chart {
    /// Begin building a scatter chart from `df`.
    ///
    /// Required columns: `.x()` (Numeric or Temporal) and `.y()` (Numeric).
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use charcoal::Chart;
    /// # let df = polars::frame::DataFrame::empty();
    /// let chart = Chart::scatter(&df)
    ///     .x("sepal_length")
    ///     .y("sepal_width")
    ///     .color_by("species")
    ///     .build()?;
    /// # Ok::<(), charcoal::CharcoalError>(())
    /// ```
    pub fn scatter(df: &DataFrame) -> scatter::ScatterBuilder<'_> {
        scatter::ScatterBuilder::new(df)
    }

    /// Begin building a line chart from `df`.
    ///
    /// Required columns: `.x()` (Numeric or Temporal) and `.y()` (Numeric).
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use charcoal::{Chart, NullPolicy};
    /// # let df = polars::frame::DataFrame::empty();
    /// let chart = Chart::line(&df)
    ///     .x("date")
    ///     .y("value")
    ///     .null_policy(NullPolicy::Skip)
    ///     .build()?;
    /// # Ok::<(), charcoal::CharcoalError>(())
    /// ```
    pub fn line(df: &DataFrame) -> line::LineBuilder<'_> {
        line::LineBuilder::new(df)
    }

    /// Begin building a bar chart from `df`.
    ///
    /// Required columns: `.x()` (Categorical) and `.y()` (Numeric).
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use charcoal::Chart;
    /// # let df = polars::frame::DataFrame::empty();
    /// let chart = Chart::bar(&df)
    ///     .x("category")
    ///     .y("count")
    ///     .build()?;
    /// # Ok::<(), charcoal::CharcoalError>(())
    /// ```
    pub fn bar(df: &DataFrame) -> bar::BarBuilder<'_> {
        bar::BarBuilder::new(df)
    }

    /// Begin building a histogram from `df`.
    ///
    /// Required column: `.x()` (Numeric). Bin counts are computed automatically.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use charcoal::Chart;
    /// # let df = polars::frame::DataFrame::empty();
    /// let chart = Chart::histogram(&df)
    ///     .x("value")
    ///     .bins(20)
    ///     .build()?;
    /// # Ok::<(), charcoal::CharcoalError>(())
    /// ```
    pub fn histogram(df: &DataFrame) -> histogram::HistogramBuilder<'_> {
        histogram::HistogramBuilder::new(df)
    }

    /// Begin building a heatmap from `df`.
    ///
    /// Required columns: `.x()` (Categorical), `.y()` (Categorical),
    /// `.z()` (Numeric).
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use charcoal::{Chart, ColorScale};
    /// # let df = polars::frame::DataFrame::empty();
    /// let chart = Chart::heatmap(&df)
    ///     .x("col_label")
    ///     .y("row_label")
    ///     .z("value")
    ///     .color_scale(ColorScale::Viridis)
    ///     .build()?;
    /// # Ok::<(), charcoal::CharcoalError>(())
    /// ```
    pub fn heatmap(df: &DataFrame) -> heatmap::HeatmapBuilder<'_> {
        heatmap::HeatmapBuilder::new(df)
    }

    /// Begin building a box plot from `df`.
    ///
    /// Required columns: `.x()` (Categorical, group labels) and `.y()` (Numeric,
    /// values to summarise).
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use charcoal::Chart;
    /// # let df = polars::frame::DataFrame::empty();
    /// let chart = Chart::box_plot(&df)
    ///     .x("group")
    ///     .y("measurement")
    ///     .notched(true)
    ///     .build()?;
    /// # Ok::<(), charcoal::CharcoalError>(())
    /// ```
    pub fn box_plot(df: &DataFrame) -> box_plot::BoxPlotBuilder<'_> {
        box_plot::BoxPlotBuilder::new(df)
    }

    /// Begin building an area chart from `df`.
    ///
    /// Required columns: `.x()` (Numeric or Temporal) and `.y()` (Numeric).
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use charcoal::{Chart};
    /// use charcoal::FillMode;
    /// # let df = polars::frame::DataFrame::empty();
    /// let chart = Chart::area(&df)
    ///     .x("week")
    ///     .y("downloads")
    ///     .fill_mode(FillMode::ToZero)
    ///     .build()?;
    /// # Ok::<(), charcoal::CharcoalError>(())
    /// ```
    pub fn area(df: &DataFrame) -> area::AreaBuilder<'_> {
        area::AreaBuilder::new(df)
    }
}

/// How null y-values are handled in connected series (Line, Area).
///
/// # Examples
///
/// ```rust,no_run
/// use charcoal::{Chart, NullPolicy};
/// # let df = polars::frame::DataFrame::empty();
/// let chart = Chart::line(&df)
///     .x("date")
///     .y("value")
///     .null_policy(NullPolicy::Interpolate)
///     .build()?;
/// # Ok::<(), charcoal::CharcoalError>(())
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum NullPolicy {
    /// Break the line at nulls, leaving a gap. Default.
    #[default]
    Skip,
    /// Fill gaps by linear interpolation between surrounding non-null values.
    Interpolate,
}

/// Dash style for line and area series.
///
/// # Examples
///
/// ```rust,no_run
/// use charcoal::{Chart, DashStyle};
/// # let df = polars::frame::DataFrame::empty();
/// let chart = Chart::line(&df)
///     .x("x")
///     .y("y")
///     .dash_style(DashStyle::Dashed)
///     .build()?;
/// # Ok::<(), charcoal::CharcoalError>(())
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum DashStyle {
    /// Unbroken line. Default.
    #[default]
    Solid,
    /// `stroke-dasharray="6 3"`
    Dashed,
    /// `stroke-dasharray="2 2"`
    Dotted,
}

/// Bar chart orientation.
///
/// # Examples
///
/// ```rust,no_run
/// use charcoal::{Chart, Orientation};
/// # let df = polars::frame::DataFrame::empty();
/// let chart = Chart::bar(&df)
///     .x("category")
///     .y("value")
///     .orientation(Orientation::Horizontal)
///     .build()?;
/// # Ok::<(), charcoal::CharcoalError>(())
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Orientation {
    /// Categories on the x-axis, bar height on the y-axis. **Default.**
    Vertical,
    /// Categories on the y-axis, bar width on the x-axis.
    Horizontal,
}

impl DashStyle {
    /// SVG `stroke-dasharray` value, or `None` for solid lines.
    pub(crate) fn stroke_dasharray(self) -> Option<&'static str> {
        match self {
            DashStyle::Solid => None,
            DashStyle::Dashed => Some("6 3"),
            DashStyle::Dotted => Some("2 2"),
        }
    }
}

/// The fully-rendered output of a `.build()` call.
///
/// A `Chart` is produced by calling `.build()` at the end of any builder chain.
/// From it you can retrieve the SVG string, write output files, or inspect
/// warnings emitted during rendering.
///
/// # Examples
///
/// ```rust,no_run
/// use charcoal::Chart;
/// # let df = polars::frame::DataFrame::empty();
/// let chart = Chart::scatter(&df)
///     .x("sepal_length")
///     .y("sepal_width")
///     .build()?;
/// chart.save_svg("iris.svg")?;
/// chart.save_html("iris.html")?;
/// # Ok::<(), charcoal::CharcoalError>(())
/// ```
#[derive(Debug, Clone)]
pub struct Chart {
    pub(crate) svg: String,
    pub(crate) warnings: Vec<CharcoalWarning>,
    pub(crate) title: String,
    pub(crate) width: u32,
    pub(crate) height: u32,
}

impl Chart {
    /// The rendered SVG string.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use charcoal::Chart;
    /// # let df = polars::frame::DataFrame::empty();
    /// let chart = Chart::scatter(&df).x("x").y("y").build()?;
    /// let svg: &str = chart.svg();
    /// assert!(svg.starts_with("<svg"));
    /// # Ok::<(), charcoal::CharcoalError>(())
    /// ```
    pub fn svg(&self) -> &str {
        &self.svg
    }

    /// Warnings accumulated during rendering. Empty slice means a clean build.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use charcoal::Chart;
    /// # let df = polars::frame::DataFrame::empty();
    /// let chart = Chart::scatter(&df).x("x").y("y").build()?;
    /// for w in chart.warnings() {
    ///     eprintln!("warning: {w}");
    /// }
    /// # Ok::<(), charcoal::CharcoalError>(())
    /// ```
    pub fn warnings(&self) -> &[CharcoalWarning] {
        &self.warnings
    }

    /// Chart width in pixels.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use charcoal::Chart;
    /// # let df = polars::frame::DataFrame::empty();
    /// let chart = Chart::scatter(&df).x("x").y("y").build()?;
    /// println!("{}×{}", chart.width(), chart.height());
    /// # Ok::<(), charcoal::CharcoalError>(())
    /// ```
    pub fn width(&self) -> u32 {
        self.width
    }

    /// Chart height in pixels.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use charcoal::Chart;
    /// # let df = polars::frame::DataFrame::empty();
    /// let chart = Chart::scatter(&df).x("x").y("y").build()?;
    /// println!("{}×{}", chart.width(), chart.height());
    /// # Ok::<(), charcoal::CharcoalError>(())
    /// ```
    pub fn height(&self) -> u32 {
        self.height
    }

    /// Write the SVG to `path` (UTF-8, overwrites if exists).
    ///
    /// # Errors
    ///
    /// [`CharcoalError::Io`] if the path is not writable or its parent directory does not exist.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use charcoal::Chart;
    /// # let df = polars::frame::DataFrame::empty();
    /// let chart = Chart::scatter(&df).x("x").y("y").build()?;
    /// chart.save_svg("output.svg")?;
    /// # Ok::<(), charcoal::CharcoalError>(())
    /// ```
    pub fn save_svg(&self, path: &str) -> Result<(), CharcoalError> {
        fs::write(path, &self.svg)?;
        Ok(())
    }

    /// Write an HTML document embedding the SVG inline to `path`.
    ///
    /// The file has no external dependencies and can be opened directly in any browser.
    ///
    /// # Errors
    ///
    /// [`CharcoalError::Io`] if the path is not writable or its parent directory does not exist.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use charcoal::Chart;
    /// # let df = polars::frame::DataFrame::empty();
    /// let chart = Chart::scatter(&df).x("x").y("y").build()?;
    /// chart.save_html("output.html")?;
    /// # Ok::<(), charcoal::CharcoalError>(())
    /// ```
    pub fn save_html(&self, path: &str) -> Result<(), CharcoalError> {
        fs::write(path, to_inline_html(&self.svg, &self.title))?;
        Ok(())
    }

    /// Write a PNG raster image to `path`.
    ///
    /// Requires the `static` feature. The SVG is rendered via `resvg` + `tiny-skia`
    /// entirely in-process — no browser, headless Chrome, or external binary required.
    ///
    /// # Errors
    /// - [`CharcoalError::RenderError`] if the SVG cannot be parsed or encoded.
    /// - [`CharcoalError::Io`] if `path` is not writable or its parent directory does not exist.
    ///
    /// # Examples
    /// ```rust,no_run
    /// # #[cfg(feature = "static")]
    /// # fn example() -> Result<(), charcoal::CharcoalError> {
    /// # use polars::prelude::*;
    /// # let df = DataFrame::empty();
    /// let chart = charcoal::Chart::scatter(&df).x("x").y("y").build()?;
    /// chart.save_png("output.png")?;
    /// # Ok(()) }
    /// ```
    #[cfg(feature = "static")]
    pub fn save_png(&self, path: &str) -> Result<(), CharcoalError> {
        let bytes = crate::render::raster::render_png(&self.svg, self.width, self.height)?;
        std::fs::write(path, bytes)?;
        Ok(())
    }

    /// `save_png` is only available with the `static` feature.
    ///
    /// Add `features = ["static"]` to your charcoal dependency in `Cargo.toml`.
    #[cfg(not(feature = "static"))]
    pub fn save_png(&self, _path: &str) -> Result<(), CharcoalError> {
        Err(CharcoalError::RenderError(
            "PNG export requires the `static` feature. \
             Add `features = [\"static\"]` to your charcoal dependency in Cargo.toml."
                .to_string(),
        ))
    }

    /// Renders the chart inline in an evcxr Jupyter notebook cell.
    ///
    /// Emits the SVG as `image/svg+xml` MIME data that evcxr captures and displays
    /// in the cell output. Has no visible effect when called outside of an evcxr context.
    ///
    /// Requires the `notebook` feature flag.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # #[cfg(feature = "notebook")]
    /// # fn example() -> Result<(), charcoal::CharcoalError> {
    /// # let df = polars::frame::DataFrame::empty();
    /// let chart = charcoal::Chart::scatter(&df).x("x").y("y").build()?;
    /// chart.display(); // emits SVG to the notebook cell output
    /// # Ok(()) }
    /// ```
    #[cfg(feature = "notebook")]
    pub fn display(&self) {
        evcxr_runtime::Display::evcxr_display(self);
    }
}

/// Minimal self-contained HTML document with the SVG embedded inline.
///
/// `&`, `<`, and `>` in `title` are escaped. The SVG body is embedded verbatim.
pub(crate) fn to_inline_html(svg: &str, title: &str) -> String {
    let safe_title = title
        .replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;");

    format!(
        r#"<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>{safe_title}</title>
  <style>
    body {{
      margin: 0;
      display: flex;
      justify-content: center;
      align-items: flex-start;
      background: #ffffff;
      padding: 1rem;
      box-sizing: border-box;
    }}
    svg {{ max-width: 100%; height: auto; }}
  </style>
</head>
<body>
{svg}
</body>
</html>
"#
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_chart(svg: &str, title: &str) -> Chart {
        Chart {
            svg: svg.to_string(),
            warnings: vec![],
            title: title.to_string(),
            width: 800,
            height: 500,
        }
    }

    fn make_chart_with_warnings(svg: &str, title: &str, warnings: Vec<CharcoalWarning>) -> Chart {
        Chart { svg: svg.to_string(), warnings, title: title.to_string(), width: 800, height: 500 }
    }

    #[test]
    fn svg_returns_stored_string() {
        assert_eq!(make_chart("<svg/>", "T").svg(), "<svg/>");
    }

    #[test]
    fn svg_is_unmodified() {
        let raw = r#"<svg xmlns="http://www.w3.org/2000/svg" width="800" height="500"></svg>"#;
        assert_eq!(make_chart(raw, "T").svg(), raw);
    }

    #[test]
    fn warnings_empty_by_default() {
        assert!(make_chart("<svg/>", "T").warnings().is_empty());
    }

    #[test]
    fn warnings_nulls_skipped() {
        let chart = make_chart_with_warnings(
            "<svg/>", "T",
            vec![CharcoalWarning::NullsSkipped { col: "y".to_string(), count: 3 }],
        );
        assert_eq!(chart.warnings().len(), 1);
        match &chart.warnings()[0] {
            CharcoalWarning::NullsSkipped { col, count } => {
                assert_eq!(col, "y");
                assert_eq!(*count, 3);
            }
            other => panic!("unexpected variant: {other:?}"),
        }
    }

    #[test]
    fn warnings_rows_subsampled() {
        let chart = make_chart_with_warnings(
            "<svg/>", "T",
            vec![CharcoalWarning::RowsSubsampled { original: 1_000_000, rendered: 500_000 }],
        );
        match &chart.warnings()[0] {
            CharcoalWarning::RowsSubsampled { original, rendered } => {
                assert_eq!(*original, 1_000_000);
                assert_eq!(*rendered, 500_000);
            }
            other => panic!("unexpected variant: {other:?}"),
        }
    }

    #[test]
    fn warnings_multiple() {
        let chart = make_chart_with_warnings(
            "<svg/>", "T",
            vec![
                CharcoalWarning::NullsSkipped { col: "x".to_string(), count: 1 },
                CharcoalWarning::NullsSkipped { col: "y".to_string(), count: 2 },
                CharcoalWarning::RowsSubsampled { original: 600_000, rendered: 500_000 },
            ],
        );
        assert_eq!(chart.warnings().len(), 3);
    }

    #[test]
    fn dimensions() {
        let chart = make_chart("<svg/>", "T");
        assert_eq!(chart.width(), 800);
        assert_eq!(chart.height(), 500);
    }

    #[test]
    fn dimensions_custom() {
        let chart = Chart {
            svg: "<svg/>".to_string(), warnings: vec![],
            title: "T".to_string(), width: 1200, height: 900,
        };
        assert_eq!(chart.width(), 1200);
        assert_eq!(chart.height(), 900);
    }

    #[test]
    fn save_svg_writes_exact_contents() {
        let svg = "<svg><text>hello</text></svg>";
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("charcoal_test_save_svg.svg");
        make_chart(svg, "T").save_svg(path.to_str().unwrap()).unwrap();
        assert_eq!(std::fs::read_to_string(&path).unwrap(), svg);
    }

    #[test]
    fn save_svg_overwrites() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("charcoal_test_save_svg_overwrite.svg");
        std::fs::write(&path, "old content").unwrap();
        make_chart("<svg/>", "T").save_svg(path.to_str().unwrap()).unwrap();
        assert_eq!(std::fs::read_to_string(&path).unwrap(), "<svg/>");
    }

    #[test]
    fn save_svg_bad_path_returns_io_error() {
        let result = make_chart("<svg/>", "T").save_svg("/no/such/dir/chart.svg");
        assert!(matches!(result.unwrap_err(), CharcoalError::Io(_)));
    }

    #[test]
    fn save_html_writes_valid_document() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("charcoal_test_save_html.html");
        make_chart("<svg/>", "My Chart").save_html(path.to_str().unwrap()).unwrap();
        let contents = std::fs::read_to_string(&path).unwrap();
        assert!(contents.contains("<!DOCTYPE html>"));
        assert!(contents.contains("<title>My Chart</title>"));
        assert!(contents.contains("<svg/>"));
        assert!(contents.contains(r#"charset="UTF-8""#));
    }

    #[test]
    fn save_html_bad_path_returns_io_error() {
        let result = make_chart("<svg/>", "T").save_html("/no/such/dir/chart.html");
        assert!(matches!(result.unwrap_err(), CharcoalError::Io(_)));
    }

    #[test]
    #[cfg(not(feature = "static"))]
    fn save_png_without_feature_returns_render_error() {
        let result = make_chart("<svg/>", "T").save_png("/tmp/should_not_exist.png");
        match result.unwrap_err() {
            CharcoalError::RenderError(msg) => assert!(msg.contains("static")),
            other => panic!("expected RenderError, got {other:?}"),
        }
        assert!(!std::path::Path::new("/tmp/should_not_exist.png").exists());
    }

    #[test]
    #[cfg(feature = "static")]
    fn save_png_writes_valid_png() {
        let svg = r#"<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><rect width="100" height="100" fill="blue"/></svg>"#;
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("test.png");
        make_chart(svg, "T").save_png(path.to_str().unwrap()).unwrap();
        let bytes = std::fs::read(&path).unwrap();
        assert_eq!(&bytes[..4], &[0x89, 0x50, 0x4E, 0x47]);
    }

    #[test]
    #[cfg(feature = "static")]
    fn save_png_bad_path_returns_io_error() {
        let svg = r#"<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"></svg>"#;
        let result = make_chart(svg, "T").save_png("/no/such/dir/chart.png");
        assert!(matches!(result.unwrap_err(), CharcoalError::Io(_)));
    }

    #[test]
    fn html_escapes_title_special_chars() {
        let html = to_inline_html("<svg/>", "A & B <Chart>");
        assert!(html.contains("A &amp; B &lt;Chart&gt;"));
        assert!(!html.contains("A & B <Chart>"));
    }

    #[test]
    fn html_escapes_ampersand_not_in_title_element() {
        let html = to_inline_html("<svg/>", "Cats & Dogs");
        let title_content = &html[html.find("<title>").unwrap()..html.find("</title>").unwrap()];
        assert!(!title_content.contains("Cats & Dogs"));
    }

    #[test]
    fn html_embeds_svg_verbatim() {
        let svg = r#"<svg width="800" height="500"><rect/></svg>"#;
        assert!(to_inline_html(svg, "T").contains(svg));
    }

    #[test]
    fn html_structure() {
        let html = to_inline_html("<svg/>", "T");
        assert!(html.contains("<!DOCTYPE html>"));
        assert!(html.contains("<html"));
        assert!(html.contains(r#"charset="UTF-8""#));
        assert!(html.contains("<body>"));
        assert!(html.contains("</html>"));
    }

    #[test]
    fn html_plain_title_unchanged() {
        assert!(to_inline_html("<svg/>", "Simple Title").contains("<title>Simple Title</title>"));
    }

    #[test]
    fn dash_style_defaults_and_dasharray() {
        assert_eq!(DashStyle::default(), DashStyle::Solid);
        assert_eq!(DashStyle::Solid.stroke_dasharray(), None);
        assert_eq!(DashStyle::Dashed.stroke_dasharray(), Some("6 3"));
        assert_eq!(DashStyle::Dotted.stroke_dasharray(), Some("2 2"));
    }

    #[test]
    fn null_policy_default_is_skip() {
        assert_eq!(NullPolicy::default(), NullPolicy::Skip);
    }
}