DataViz 🚀
A modular, customizable, and feature-rich 2D plotting library written in Rust. With DataViz, you can create a wide variety of plots tailored to your needs, supporting both raster (PNG) and vector (SVG) outputs. You can save and display(interactive or real-time) your DataViz figures.
Features
Supported Plot Types
- Bar Charts: Create grouped horizontal and vertical bar charts.
- Scatter Graphs: Visualize data points with various shapes (circle, square, triangle, etc.).
- Pie Charts: Represent data proportions as slices of a circle.
- Area Charts: Highlight trends with filled areas under data lines.
- Histograms: Analyze frequency distributions with dynamic bin calculations.
- Cartesian Graphs: Plot mathematical functions or datasets on a coordinate plane.
- Quadrant 1 Graphs: Focused plotting in the first quadrant for non-negative data.
Customization Options
- Title: Add meaningful titles to your plots.
- Axes Labels: Define X-axis and Y-axis labels.
- Dynamic Scaling: Automatically fit data within the plot dimensions.
- Colors: Customize colors for the background, axes, and data elements.
- Margins: Add space around the plot for better visibility.
- Line and Dot Styles: Customize line types (solid, dashed, dotted) and dot shapes (circle, square, cross, triangle).
Output Formats
- Raster (PNG): Save high-quality images of your plots.
- Vector (SVG): Generate scalable vector graphics for precision and scalability.
Interactive Capabilities
- Hover effects and real-time updates.
Installation
Add the following dependencies to your Cargo.toml
file:
[dependencies]
dataviz = "0.1.5"
Examples
To see more examples you can visit: https://github.com/dataviz-rs/dataviz-examples
Usage Example
use dataviz::figure::{
canvas::pixelcanvas::PixelCanvas, configuration::figureconfig::FigureConfig, datasets::bardataset::BarDataset, display::winop::Winop, drawers::drawer::Drawer, figuretypes::groupbarchart::GroupBarChart, utilities::orientation::Orientation, };
use rand::Rng;
fn main() {
let figure_config = FigureConfig {
font_size_title: 20.0, font_size_label: 16.0, font_size_legend: 14.0, font_size_axis: 10.0, color_axis: [0, 0, 0], color_background: [0, 0, 0], color_grid: [220, 220, 220], color_title: [0, 0, 0], num_axis_ticks: 20, num_grid_horizontal: 20, num_grid_vertical: 20, font_label: Some("path/to/dataviz/resources/fonts/font.ttf"
.to_string()), font_title: Some("path/to/dataviz/resources/fonts/font.ttf"
.to_string()), };
let mut barchart = GroupBarChart::new(
"Grouped Bar Chart", "X Axis", "Y Axis", Orientation::Horizontal, figure_config, );
let mut canvas = PixelCanvas::new(800, 600, [255, 255, 255], 80);
let mut dataset1 = BarDataset::new("Company A", [220, 0, 0]); dataset1.add_data(2020.0, 100.0); dataset1.add_data(2021.0, 200.0); dataset1.add_data(2022.0, 150.0);
let mut dataset2 = BarDataset::new("Company B", [0, 220, 0]); dataset2.add_data(2020.0, 120.0); dataset2.add_data(2021.0, 180.0); dataset2.add_data(2022.0, 220.0);
let mut dataset3 = BarDataset::new("Company C", [0, 0, 220]); dataset3.add_data(2020.0, 150.0); dataset3.add_data(2021.0, 250.0); dataset3.add_data(2022.0, 400.0);
let mut dataset4 = BarDataset::new("Company D", [150, 100, 50]); dataset4.add_data(2020.0, 50.0); dataset4.add_data(2021.0, 256.0); dataset4.add_data(2022.0, 40.0);
barchart.add_dataset(dataset1);
barchart.add_dataset(dataset2);
barchart.add_dataset(dataset3);
barchart.add_dataset(dataset4);
barchart.draw(&mut canvas);
canvas.save_as_image("grouped_horizontal_bar_chart.png");
Winop::display_interactive(&mut canvas, &mut barchart, "Interactive BarChart");
let mut rng = rand::thread_rng();
let update_data = move |chart: &mut GroupBarChart| {
for i in 0..chart.datasets.len() {
for point in chart.datasets[i].data.iter_mut() {
point.1 += rng.gen_range(-20.0..30.0); }
}
};
Winop::display_real_time(
&mut canvas,
&mut barchart,
"Real-Time Bar Chart",
update_data,
30,
);
}
let mut figure = FigureFactory::create_figure(FigureType::GroupBarChartVertical);
if let Some(group_bar_chart) = figure.as_any().downcast_mut::<GroupBarChart>() {
group_bar_chart.config.set_font_paths(
"path/to/dataviz/resources/fonts/font.ttf".to_string(),
"path/to/dataviz/resources/fonts/font.ttf".to_string(),
);
let mut canvas = PixelCanvas::new(800, 600, [255, 255, 255], 80);
let mut dataset1 = BarDataset::new("Company A", [220, 0, 0]); dataset1.add_data(2020.0, 100.0); dataset1.add_data(2021.0, 200.0);
let mut dataset2 = BarDataset::new("Company B", [0, 220, 0]); dataset2.add_data(2020.0, 120.0); dataset2.add_data(2021.0, 180.0);
group_bar_chart.add_dataset(dataset1);
group_bar_chart.add_dataset(dataset2);
group_bar_chart.draw(&mut canvas);
canvas.save_as_image("grouped_vertical_bar_chart.png");
} else {
println!("Failed to downcast to GroupBarChart.");
}
use dataviz::figure::{
canvas::svgcanvas::SvgCanvas, configuration::figureconfig::FigureConfig, datasets::bardataset::BarDataset, drawers::drawer::Drawer, figuretypes::groupbarchart::GroupBarChart, utilities::orientation::Orientation, };
fn main() {
let figure_config = FigureConfig {
font_size_title: 20.0, font_size_label: 16.0, font_size_legend: 14.0, font_size_axis: 10.0, color_axis: [0, 0, 0], color_background: [0, 0, 0], color_grid: [220, 220, 220], color_title: [0, 0, 0], num_axis_ticks: 20, num_grid_horizontal: 20, num_grid_vertical: 20, font_label: Some("path/to/dataviz/resources/fonts/font.ttf"
.to_string()), font_title: Some("path/to/dataviz/resources/fonts/font.ttf"
.to_string()), };
let mut barchart = GroupBarChart::new(
"Grouped Bar Chart", "X Axis", "Y Axis", Orientation::Vertical, figure_config, );
let mut canvas = SvgCanvas::new(800, 600, "white", 80);
let mut dataset1 = BarDataset::new("Company A", [220, 0, 0]); dataset1.add_data(2020.0, 100.0); dataset1.add_data(2021.0, 200.0); dataset1.add_data(2022.0, 150.0);
let mut dataset2 = BarDataset::new("Company B", [0, 220, 0]); dataset2.add_data(2020.0, 120.0); dataset2.add_data(2021.0, 180.0); dataset2.add_data(2022.0, 220.0);
let mut dataset3 = BarDataset::new("Company C", [0, 0, 220]); dataset3.add_data(2020.0, 150.0); dataset3.add_data(2021.0, 250.0); dataset3.add_data(2022.0, 400.0);
let mut dataset4 = BarDataset::new("Company D", [150, 100, 50]); dataset4.add_data(2020.0, 50.0); dataset4.add_data(2021.0, 256.0); dataset4.add_data(2022.0, 40.0);
barchart.add_dataset(dataset1);
barchart.add_dataset(dataset2);
barchart.add_dataset(dataset3);
barchart.add_dataset(dataset4);
barchart.draw_svg(&mut canvas);
canvas.save("grouped_vertical_bar_chart.svg").unwrap();
}
Examples
PNG Outputs
SVG Outputs
License
This project is licensed under the MIT. See the LICENSE
file for details.
Contributing
We welcome contributions to make DataViz even better!
Let’s make data visualization in Rust easy and accessible!