A charts library for rust
charts-rs is a charting library for rust. It's simple and fast.
[![Crates.io][crates-badge]][crates-url] [![Apache licensed][apache-badge]][apache-url] [crates-badge]: https://img.shields.io/crates/v/charts-rs.svg [crates-url]: https://crates.io/crates/charts-rs [apache-badge]: https://img.shields.io/badge/license-apache2-blue.svg [apache-url]: https://github.com/vicanso/charts-rs/blob/main/LICENSE
charts-rs provides a straightforward approach to generating charts with support for multiple output formats including svg, png, jpeg, webp, and avif. The library offers ten distinct themes: light, dark, grafana, ant, vintage, walden, westeros, chalk, shine, and shadcn, with light as the default theme.
The library supports twenty-two chart types: Bar, HorizontalBar, Line, Pie, Radar, Scatter, Candlestick, Table, Heatmap, Funnel, Waterfall, MultiChart, Calendar, Gauge, Treemap, BoxPlot, Sunburst, Sankey, Tree, Graph, Parallel, and ThemeRiver. Drawing inspiration from Apache ECharts, charts-rs enables developers to create charts with similar functionality and appearance.
add_theme()round rect, circle, and recty_axis_configs and series.y_axis_index"log", "log2", or {"type":"log","base":N})Fill::LinearGradient)Option (null in JSON; legacy NIL_VALUE still accepted)svg_to_png_with_size and equivalent functionsAdd charts-rs to your Cargo.toml:
[dependencies]
charts-rs = "0.7"SVG output works with the default build. Raster export (png, jpeg, webp,
avif) — the svg_to_png, svg_to_png_with_size, and related functions used
below — requires the image-encoder feature:
[dependencies]
charts-rs = { version = "0.7", features = ["image-encoder"] }You can try to use the web demo page, it's simple and useful.
Charts Web Demo Page: https://charts.npmtrend.com/
Charts Web Source: https://github.com/vicanso/charts-rs-web
Runnable examples live in examples/; each writes an svg file:
cargo run --example bar # basic bar chart
cargo run --example line # smooth line with area fill and mark line / points
cargo run --example pie # nightingale (rose) chart
cargo run --example sunburst # sunburst with label formatter, ring thickness, animation
cargo run --example sankey # sankey flow diagram (nodes auto-derived from links)
cargo run --example tree # node-link tree with curved links (LR layout)……use charts_rs::{BarChart, svg_to_png_with_size};
let chart = BarChart::from_json(r###"{ ... }"###).unwrap();
let svg = chart.svg().unwrap();
// Scale to exactly 800×400
let png = svg_to_png_with_size(&svg, Some(800), Some(400)).unwrap();
// Scale to width 800, preserve aspect ratio
let png = svg_to_png_with_size(&svg, Some(800), None).unwrap();Use null in JSON arrays to represent missing data; the chart skips rendering for those positions.
{
"series_list": [{
"name": "Sales",
"data": [120.0, null, 101.0, null, 90.0]
}]
}In Rust, build the series with Option values (None is a missing point):
use charts_rs::Series;
let series = Series::new_nullable(
"Sales".to_string(),
vec![Some(120.0), None, Some(101.0), None, Some(90.0)],
);
// or via the tuple conversion
let series: Series = ("Sales", vec![Some(120.0), None, Some(101.0)]).into();The legacy NIL_VALUE sentinel (Series::new / Vec) still works and is treated as None.
Formatters are supported in series_label_formatter, axis_formatter, and value_formatter (GaugeChart).
| Placeholder | Meaning |
|---|---|
{c} |
Data value |
{a} |
Series name |
{b} |
Category (x-axis label) |
{d} |
Percentage (pie / funnel) |
{t} |
Thousands notation (1.2K, 5.6M) |
let buf = fs::read(file).unwrap();
add_fonts(&[&buf]).unwrap();This project is licensed under the Apache-2.0 license.
No open issues yet, or sync has not completed.