# Introduction
## What is Charton? (The Core Idea)
Charton is a modern Rust visualization library designed around a simple, declarative framework for data visualization.
- Declarative API: It offers an API similar to Python's Altair/Vega-Lite, allowing users to define "what to visualize" rather than "how to draw it.". That is, "the Grammar of Graphics".
- Native Polars Support: Charton is tightly integrated with the high-performance Rust DataFrame library Polars, enabling efficient, zero-copy data plotting.
- Dual Rendering Capability: You can utilize its pure Rust SVG renderer for dependency-free plotting, or leverage its IPC mechanism to seamlessly connect with external Python visualization ecosystems like Altair and Matplotlib.
## Design Philosophy and Key Advantages
Charton is engineered to be an efficient, safe, and flexible solution, built on the principle that visualization should be declarative.
- π Performance and Safety: It leverages Rust's strong type system to achieve compile-time safety and utilizes Polars' integration for superior data handling performance.
- π‘ Layered and Expressive: It features a multi-layer plotting architecture that easily combines various marks (e.g., points, lines, bars, boxplots, error bars) within a shared coordinate system to create complex composite visualizations.
- π Frontend Ready: It can generate standard Vega-Lite JSON specifications, making it ready for easy integration into modern web applications using libraries like React-Vega or Vega-Embed.
- π Efficient Integration: Through Inter-Process Communication (IPC), it efficiently communicates with external Python libraries, avoiding slow, temporary file operations and maintaining compatibility with environments like Conda in Jupyter.
- π Jupyter Interactivity: It offers native support for the evcxr Jupyter Notebook environment, enabling interactive and real-time exploratory data analysis.
## System Architecture
Charton adopts a modern, decoupled architecture designed for high-performance data processing and cross-language interoperability.
```text
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Input Layer β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββββββββββββββββββ β
β β Rust Polars β β External β β Jupyter/evcxr Interactive β β
β β DataFrame/ β β Datasets β β Input β β
β β LazyFrame β β (CSV/Parquet)β β (Notebook cell data/commands)β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββββββββββββββββββ β
ββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββ
β
ββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββ
β Core Layer β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Charton Core Engine β β
β β ββββββββββββββββ βββββββββββββββββ ββββββββββββββββββββββββ β β
β β β Declarative β β Layered β β Cross-backend Data β β β
β β β API (Altair- β β Chart β β Converter β β β
β β β style) β β Management β β (Rust β Python/JSON) β β β
β β ββββββββββββββββ β (LayeredChart)β ββββββββββββββββββββββββ β β
β β βββββββββββββββββ β β
β β ββββββββββββββββ βββββββββββββββββ ββββββββββββββββββββββββ β β
β β β Data β β IPC β β Vega-Lite Spec β β β
β β β Validation/ β β Communication β β Generator β β β
β β β Mapping β β Module β β β β β
β β ββββββββββββββββ βββββββββββββββββ ββββββββββββββββββββββββ β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
ββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββ
β
ββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββ
β Render Backends β
β ββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββββββββββββ β
β β Rust Native Backend β β External Cross-Language Backends β β
β β β β β β
β β ββββββββββββββββββ β β βββββββββββββββ ββββββββββββββββββββ β β
β β β Pure Rust SVG β β β β Altair β β Matplotlib β β β
β β β Renderer β β β β Backend β β Backend β β β
β β ββββββββββββββββββ β β β (Python IPC)β β (Python IPC) β β β
β β β β βββββββββββββββ ββββββββββββββββββββ β β
β β ββββββββββββββββββ β β β β
β β β Wasm Renderer β β β ββββββββββββββ ββββββββββββββββββββ β β
β β β (Partial β β β β Other β β Extended Backendsβ β β
β β β Support) β β β β Python β β (Future) β β β
β β ββββββββββββββββββ β β β Viz Libs β β (R/Julia, etc.) β β β
β β β β ββββββββββββββ ββββββββββββββββββββ β β
β ββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββββββββββββ β
ββββββββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββ
β
ββββββββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββ
β Output Layer β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β SVG Vector β β Vega-Lite β β PNG Bitmap β β Jupyter β β
β β Graphics β β JSON β β Image β β Inline β β
β β (Native/Wasm)β β (for Web) β β (via Ext.) β β Rendering β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
**1. Input Layer (Data Orchestration)**
- **Polars-Native**: Unlike other libraries that require heavy data cloning, Charton is built on **Apache Arrow** (via Polars), enabling efficient, zero-copy data access.
- **Versatile Sourcing**: It supports `DataFrame` and `LazyFrame`, allowing for out-of-core data processing before visualization.
**2. Core Layer (The Grammar Engine)**
- **Declarative DSL**: A type-safe implementation of the **Grammar of Graphics**, allowing users to compose complex visualizations using intuitive tuples (e.g., `.encode((x, y, color))`).
- **Universal Data Bridge**: This is the core innovation of Charton. It utilizes **Parquet-serialized bytes** as an intermediate format to exchange data between different Polars versions and languages, effectively bypassing Rust's orphan rules and dependency conflicts.
- **Vega-Lite Spec Generator**: A high-level compiler that transforms Rust structures into standard Vega-Lite JSON for seamless frontend integration.
**3. Render Backends (Multi-Engine)**
- **Rust Native Backend**: A **zero-dependency**, pure Rust implementation. It uses a custom SVG renderer for ultra-fast, server-side batch generation and provides partial support for WebAssembly (Wasm).
- **IPC Bridge (External)**: For features not yet in the native engine, Charton provides a high-speed Inter-Process Communication (IPC) bridge to Pythonβs mature ecosystem (**Altair/Matplotlib**), eliminating the need for slow temporary disk I/O.
**4. Output Layer (Multi-Format Delivery)**
- **Vector & Raster**: Support for SVG and high-resolution PNG (via `resvg`).
- **Web & Notebook**: Direct JSON output for **React/Vue** integration and inline rendering for **evcxr Jupyter** notebooks.
## Why This Architecture Matters
π **Solving the "Version Hell"**
In the Rust ecosystem, if your project depends on Polars `v0.50` and a plotting library depends on `v0.40`, your code won't compile. Chartonβs **Parquet-encoded IPC** bypasses this entirely, making it the most robust visualization tool for production Rust environments.
π **Hot-Swappable Backends**
You can develop interactively using the **Altair backend** to leverage its rich feature set, and then switch to the **Native SVG backend** for deployment to achieve maximum performance and minimum container size.
π **Frontend-First Design**
By generating standard **Vega-Lite JSON**, Charton allows you to handle heavy data lifting in Rust while letting the browserβs GPU handle the final rendering via `Vega-Embed` or `React-Vega`.