This is an implementation of an Iced backend for Plotters, for both native and wasm applications.
This backend has been optimized as for speed. Note that some specific plotting features supported in the Bitmap backend may not be implemented there, though.
Plotters is an extensible Rust drawing library that can be used to plot data on nice-looking graphs, rendering them through a plotting backend (eg. to a Bitmap image raw buffer, to your GUI backend, to an SVG file, etc.).
For more details on Plotters, please check the following links:
Include plotters-iced
in your Cargo.toml
dependencies:
[dependencies]
plotters-iced = "0.11"
iced = { version = "0.13", features = ["canvas", "tokio"] }
plotters="0.3"
First, import Chart
and ChartWidget
:
use plotters_iced::{Chart, ChartWidget, DrawingBackend, ChartBuilder};
Then, derive Chart
trait and build your chart, and let plotters-iced
takes care the rest:
struct MyChart;
impl Chart<Message> for MyChart {
type State = ();
fn build_chart<DB:DrawingBackend>(&self, state: &Self::State, builder: ChartBuilder<DB>) {
//build your chart here, please refer to plotters for more details
}
}
Finally, render your chart view:
impl MyChart {
fn view(&mut self)->Element<Message> {
ChartWidget::new(self)
.width(Length::Fixed(200))
.height(Length::Fixed(200))
.into()
}
}
If you are looking for a full example of an implementation, please check cpu-monitor.rs.
cpu-monitor
This example samples your CPU load every second, and renders it in a real-time chart:
cargo run --release --example cpu-monitor
From this example, you'll learn:
plotters-iced
split-chart
This example shows you how to split drawing area.
cargo run --release --example split-chart
cd examples/split-chart
trunk serve
No image rendering for native and wasm applications.
Only TTF font family are supported for text rendering, which is a limitation of Iced
, please look at cpu-monitor.rs. As well, font transforms are not supported,which is also a limitation of Iced
.