Charton - A versatile plotting library for Rust
Altair-style declarative plotting for Rust. High-performance, Polars-native, and Wasm-ready.
"Really nice project. ... This works perfectly as an ecosystem." — Ritchie Vink, Creator of Polars
Charton is a high-performance Rust plotting library featuring a declarative API inspired by Altair. It provides native Polars support and bridges the gap to the Python visualization ecosystem (Altair/Matplotlib). Integrated with evcxr_jupyter, it enables seamless interactive data exploration in notebooks.
Installation
Add to Cargo.toml:
[]
= "0.5" # Standard (Parallel enabled)
= { = "0.5", = false } # For WASM / Single-thread
= { = "0.5", = ["resvg"] } # With PNG support
= { = "0.5", = ["bridge"] } # With Altair/Matplotlib interop
Quick Start
Charton provides a high-level, declarative API. Standard visualizations can be generated using a concise one-liner syntax:
use *;
// Data: Physical measurements (Height vs. Weight)
let height = vec!;
let weight = vec!;
// One-liner plotting
chart!?.mark_point?.encode?.save?;
From Macros to Production API
While the chart! macro is a convenient syntactic sugar for rapid prototyping and simple scripts, the underlying Chart::build API is recommended for production environments where explicit data handling is required.
1. Professional Build API
For complex applications, use Chart::build to gain full control over the Dataset lifecycle.
let ds = new
.with_column?
.with_column?;
build? // Equivalent to chart!(ds)?
.mark_point?
.encode?
.save?;
Tip: Use
add_columninstead if you need to add columns dynamically within loops or conditional logic.
2. Polars Integration
For Polars users, Charton provides the load_polars_df! macro to seamlessly convert a DataFrame into a Charton-ready Dataset.
use *;
// Efficiently convert Polars DataFrame to Charton Dataset
let df = df!?;
let ds = load_polars_df!?;
build? // Equivalent to chart!(ds)?
.mark_point?
.encode?
.save?;
Compatibility Note: Charton uses versioned macros to handle Polars' rapid API evolutions. Versions below 0.42 are no longer supported.
| Polars Version | Macro to Use | Status |
|---|---|---|
| 0.53+ | load_polars_df!(df)? |
Latest (Standard) |
| 0.42 - 0.52 | load_polars_v42_52!(df)? |
Legacy Support |
| < 0.42 | N/A | Unsupported |
Layered Grammar
Inspired by the Grammar of Graphics (as seen in ggplot2 and Altair), Charton replaces rigid templates with a modular, layer-based system. Visualizations are constructed by stacking atomic marks, offering infinite flexibility beyond fixed chart types.
// Create individual layers
let line = chart!?
.mark_line?
.encode?;
let point = chart!?
.mark_point?
.encode?;
// Combine into a composite chart
line.and.save?;
Charton can also leverages Rust’s functional paradigms, enabling infinite layer composition via fluent chaining or iterator folding. This allows for effortless, dynamic generation of complex multi-layered plots.
let layers: = vec!;
// Equivalent to line.and(point).and(bar)...
let lc = layers.into_iter
.reduce
.expect;
Interactive Notebooks (Jupyter)
Charton integrates with evcxr_jupyter for interactive data exploration. Replacing .save() with .show() renders SVGs directly within notebook cells:

WebAssembly and Frontend
Charton supports WebAssembly and modern web frontend; please refer to Charton Docs for details.
Leveraging External Plotting Power
Charton bridges Rust with mature visualization ecosystems like Altair and Matplotlib via a high-speed IPC, enabling users to leverage diverse, professional-grade plotting tools within a unified workflow. please refer to Charton Docs for details.
Industrial-Grade Visualization
Charton scales the Grammar of Graphics to heavy-duty production. By adopting the same proven philosophy as ggplot2, Altair, and the evolving ECharts, it validates its architecture as the industry standard, delivering strict type safety and zero-copy Polars integration for robust pipelines under extreme loads. This is powered by a rigorous Scale Arbitration engine that consolidates data domains into a "Single Source of Truth," ensuring absolute mathematical consistency and seamless cross-plot mapping while eliminating the fragile, hard-coded patches and silent overrides common in template-based tools.
This figure demonstrates semantic synchronization in Charton. Heterogeneous samples (point layer) are anchored to a global background (bar layer). By enforcing a single mathematical truth across all layers, Charton maintains absolute color consistency, ensuring samples are accurately contextualized within the global background.
Publish Quality
Designed for precision, Charton provides pixel-perfect control over complex marks. Whether it is a multi-layered ErrorBar for biomedical research or a high-density scatter plot for finance, Charton delivers the aesthetic rigor required by top-tier journals.
A reproduction of Figure 1A from the 2021 NEJM landmark study on once-weekly semaglutide for weight management, implemented using Charton.
Documentation
Please go to the Charton Docs for full documentation.
License
Charton is licensed under the Apache License 2.0.