charton 0.5.7

A high-performance, layered charting system for Rust, featuring a flexible data core and multi-backend rendering.
Documentation
# Publication-Ready Themes


In enterprise-grade data analysis and professional publishing workflows, visual consistency is non-negotiable. Graphs produced by different teams or across different project cycles must strictly adhere to a unified visual identity. Manual adjustments of font sizes, line widths, and padding for every single plot are not only inefficient but also highly prone to error.

Charton addresses this requirement through a fully decoupled, semantic Theme System. By separating data-driven graphic specifications from non-data aesthetics, this system enables the automated enforcement of standardized visual guidelines, ensuring that every chart—from internal dashboards to formal reports—maintains a professional and cohesive appearance.

## Architectural Foundation of Themes


As defined in `theme.rs`, a `Theme` is an immutable, comprehensive configuration container. Unlike traditional plotting libraries that might hard-code layout margins or text properties within geometric renderers, Charton’s architecture treats styles as externalized constants.

These themes are injected into the rendering pipeline through `PanelContext`, allowing the same data layer to be styled differently simply by swapping the theme reference. The system governs three critical stylistic dimensions:

- Global Canvas & Spatial Margins: Controlled by parameters such as `top_margin`, `right_margin`, `bottom_margin`, and `left_margin`. These define the protective buffers around the canvas, ensuring that legends, titles, and axis elements do not bleed into the boundaries of the export surface.
- Typographic Hierarchy: The system utilizes a font-stack strategy, governing specific layers—from `title_size` to `tick_label_size` and `legend_label_size`. This ensures clear, readable visual hierarchy across different output resolutions.
- Visual Anchor Metrics: Geometric constraints such as `axis_width`, `tick_length`, `legend_block_gap`, and `legend_marker_text_gap`. By formalizing these metrics, the library maintains consistent layout density across varying chart complexities.

```rust
// Global theme injection ensures visual uniformity across all chart components.
let styled_chart = Chart::new(df)
    .mark_point()
    .encode((x("dosage"), y("efficacy")))
    .theme(Theme::corporate_standard()); // Enforces pre-defined visual guidelines
```

## Layout-Safe Dynamic Adjustments


A primary challenge with static themes is that a layout ruleset might function perfectly for a simple scatter plot but fail in a dense, multi-panel (faceted) grid. Charton’s layout engine treats theme metrics as elastic constraints rather than fixed pixel values:

- The Axis Reserve Buffer: Defined in `theme.rs` as `axis_reserve_buffer`, this creates a defensive boundary around the plotting panel. When text labels rotate (via `x_tick_label_angle`), the layout engine dynamically calculates the required space based on the rotated bounding box, preventing text truncation or boundary collisions.
- Panel Defense Ratio: To prevent oversized legends or long categorical labels from shrinking the core visualization area to a sliver, the system enforces a `panel_defense_ratio` (defaulting to 0.2). This rule guarantees that no matter how many guide blocks are added to the outer regions, the central plotting area retains a significant percentage of the available canvas real estate.

## Perceptually Uniform Palette Integration


Professional styling requires safeguarding the mathematical fidelity of continuous data. The theme engine integrates high-fidelity continuous mapping engines that prevent the artifacts common in naive RGB interpolation.

By utilizing predefined `ColorMap` strategies (such as Viridis or Magma), the theme ensures that data magnitude changes correspond linearly to human visual perception. This eliminates "false clustering"—a common visual bias in standard palettes—and ensures that every pixel accurately reflects the underlying data distribution.

## Key Takeaways


- Complete Separation of Concerns: Data encodings (via `Chart<T>`) define what is visualized, while the `Theme` architecture dictates how it is physically presented.
- Stateless Rendering: Renderers do not maintain internal style states; they query theme constants via `PanelContext`, ensuring that visual updates are immediate and globally consistent.
- Defensive Layout Guardrails: Mechanisms like `panel_defense_ratio` and `axis_reserve_buffer` ensure that chart structural integrity is maintained automatically, even under complex or crowded layout conditions.