# 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**](https://github.com/pola-rs/polars/issues/25941), Creator of Polars
[](https://crates.io/crates/charton)
[](https://docs.rs/charton)
[](https://github.com/wangjiawen2013/charton/actions)
[](LICENSE)
**Charton** is a high-performance Rust plotting library featuring a declarative API inspired by [Altair](https://altair-viz.github.io/). It provides native [Polars](https://github.com/pola-rs/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.
<table>
<tr>
<td><img src="docs/src/images/altair.svg" alt="Altair" /><p align="center">Altair</p></td>
<td><img src="docs/src/images/matplotlib.png" alt="Matplotlib" /><p align="center">Matplotlib</p></td>
<td><img src="docs/src/images/stacked_bar.svg" alt="Stacked Bar Chart" /><p align="center">Stacked Bar Chart</p></td>
<td><img src="docs/src/images/grouped_bar_with_errorbar_2.svg" alt="Grouped Bar Chart with Errorbar" /><p align="center">Grouped Bar With Errorbar</p></td>
<td><img src="docs/src/images/density.svg" alt="Density" /><p align="center">Density</p></td>
</tr>
<tr>
<td><img src="docs/src/images/histogram.svg" alt="Histogram" /><p align="center">Histogram</p></td>
<td><img src="docs/src/images/2d_density.svg" alt="2d Density" /><p align="center">2d Density Chart</p></td>
<td><img src="docs/src/images/heatmap.svg" alt="Heatmap" /><p align="center">Heatmap</p></td>
<td><img src="docs/src/images/grouped_boxplot.svg" alt="Grouped Boxplot" /><p align="center">Grouped Boxplot</p></td>
<td><img src="docs/src/images/cumulative_frequency.svg" alt="Cumulative Frequency" /><p align="center">Cumulative Frequency</p></td>
</tr>
<tr>
<td><img src="docs/src/images/distribution.svg" alt="Distribution" /><p align="center">Distribution</p></td>
<td><img src="docs/src/images/pie.svg" alt="Pie" /><p align="center">Pie</p></td>
<td><img src="docs/src/images/donut.svg" alt="Donut" /><p align="center">Donut</p></td>
<td><img src="docs/src/images/rose.svg" alt="Rose" /><p align="center">Rose</p></td>
<td><img src="docs/src/images/nightingale.svg" alt="Nightingale" /><p align="center">Nightingale</p></td>
</tr>
<tr>
<td><img src="docs/src/images/simple_stacked_area.svg" alt="Simple Stacked Area" /><p align="center">Simple Stack Area</p></td>
<td><img src="docs/src/images/normalized_stacked_area.svg" alt="Normalized Stacked Area" /><p align="center">Normalized Stacked Area</p></td>
<td><img src="docs/src/images/steamgraph.svg" alt="Steamgraph" /><p align="center">Steamgraph</p></td>
<td><img src="docs/src/images/rule.svg" alt="Rule" /><p align="center">Rule</p></td>
<td><img src="docs/src/images/strip.svg" alt="Strip" /><p align="center">Strip</p></td>
</tr>
</table>
## Installation
Add to `Cargo.toml`:
```toml
[dependencies]
charton = "0.4"
polars = "0.49"
```
## Quick Start
Charton provides a high-level, declarative API for Polars. Standard visualizations can be generated using a concise one-liner syntax:
```rust
use charton::prelude::*;
use polars::prelude::*;
use std::error::Error;
// Data preparation: Hooke's Law (Force vs. Extension)
let df = df![
"force" => [1.0, 2.0, 3.0, 4.0, 5.0],
"extension" => [1.2, 2.3, 3.1, 4.4, 5.2]
]?;
// One-liner plotting
Chart::build(&df)?.mark_point()?.encode((x("force"), y("extension")))?.save("output.svg")?;
```
## 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.
```rust
// Create individual layers
let line = Chart::build(&df)?
.mark_line()?
.encode((x("force"), y("extension")))?;
let point = Chart::build(&df)?
.mark_point()?
.encode((x("force"), y("extension")))?;
// Combine into a composite chart
line.and(point).save("layered.svg")?;
```
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.
```rust
let layers: Vec<LayeredChart> = vec![line.into(), point.into(), bar.into() /* , ... etc */];
// Equivalent to line.and(point).and(bar)...
let lc = layers.into_iter()
.reduce(|acc, layer| acc.and(layer))
.expect("Failed to fold layers");
```
## 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](https://wangjiawen2013.github.io/charton) 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.
<table>
<tr>
<td><img src="docs/src/images/weight_loss_curve_NEJM.png" alt="NEJM" /><p align="center">NEJM</p></td>
<td><img src="docs/src/images/weight_loss_curve.svg" alt="Charton" /><p align="center">Charton</p></td>
</tr>
</table>
*A reproduction of Figure 1A from the 2021 NEJM landmark study on once-weekly semaglutide for weight management, implemented using Charton.*
## Performance Benchmark
*Benchmarks are currently in progress.*
## Documentation
Please go to the [Charton Docs](https://wangjiawen2013.github.io/charton) for full documentation.
## License
Charton is licensed under the **Apache License 2.0**.