Charming is a powerful and versatile chart rendering library for Rust that leverages the power of Apache ECharts to deliver high-quality data visualizations. Built with the Rust programming language, this library aims to provide the Rust ecosystem with an intuitive and effective way to generate and visualize charts, using a declarative and user-friendly API.
Highlights:
- Easy-to-use, declaritive API.
- Rich and customizable chart themes and styles.
- Rendering to multiple formats, including HTML, SVG, PNG, and JPEG.
- Abundant chart types.
Default |
Dark |
Vintage |
Vintage |
Essos |
Essos |
Walden |
Chalk |
Infographic |
Macarons |
Roma |
Shine |
Purple Passion |
Halloween |
Charming provides two types of renderers:
HtmlRenderer
- It generates HTML fragments and offloads the actual rendering to user's browser for an interactive, seamless experience.ImageRenderer
- It has the ability to generates static images. This renderer is disabled by default, to enable it, add thessr
feature in yourCargo.toml
.
// Use HtmlRenderer.
use charming::HtmlRenderer;
// Chart dimension 1000x800.
let renderer = HtmlRenderer::new("my charts", 1000, 800);
// Render the chart as HTML string.
let html_str = renderer.render(&chart).unwrap();
// Save the chart as HTML file.
renderer.save(&chart, "/tmp/chart.html").unwrap();
// Use ImageRenderer. The `ssr` feature needs to be enabled.
use charming::{ImageRenderer, ImageFormat};
// Chart dimension 1000x800.
let mut renderer = ImageRenderer::new(1000, 800);
// Render the chart as SVG string.
renderer.render(&chart).unwrap();
// Render the chart as PNG bytes.
renderer.render_format(ImageFormat::PNG, &chart).unwrap();
// Save the chart as SVG file.
renderer.save(&chart, "/tmp/chart.svg").unwrap();
// Save the chart as PNG file.
renderer.save_format(ImageFormat::PNG, &chart, "/tmp/chart.png");
Add charming as a dependency:
$ cargo add charming
Below is an example of drawing a simple pie chart.
use charming::image_renderer::ImageRenderer;
use charming::{
component::Legend,
element::ItemStyle,
series::{Pie, PieRoseType},
Chart,
};
fn main() {
let chart = Chart::new()
.legend(Legend::new().top("bottom"))
.series(
Pie::new()
.name("Nightingale Chart")
.rose_type(PieRoseType::Radius)
.radius(vec!["50", "250"])
.center(vec!["50%", "50%"])
.item_style(ItemStyle::new().border_radius(8))
.data(vec![
(40.0, "rose 1"),
(38.0, "rose 2"),
(32.0, "rose 3"),
(30.0, "rose 4"),
(28.0, "rose 5"),
(26.0, "rose 6"),
(22.0, "rose 7"),
(18.0, "rose 8"),
]),
);
let mut renderer = ImageRenderer::new(1000, 800);
renderer.save(&chart, "/tmp/nightingale.svg");
}
This code creates the following SVG file:
As another example, the code file gallery/src/dataset/encode_and_matrix.rs draws a complex chart with four sub-charts:
Here are some selected chart examples. Click on any single chart to view its source code file.
You can also clone the repo and run cargo run --bin gallery
to view the interactive charts on the rendered HTML page.