Skip to content

Commit

Permalink
Improve README
Browse files Browse the repository at this point in the history
  • Loading branch information
dorian.pinaud committed Feb 22, 2024
1 parent 41e5d84 commit 07379eb
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 5 deletions.
86 changes: 85 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,87 @@
# Plotters-dioxus

Implementation of an [Dioxus](https://github.com/DioxusLabs/dioxus) backend for the rust library [Plotters](https://github.com/plotters-rs/plotters).
Implementation of an [Dioxus](https://github.com/DioxusLabs/dioxus) backend for the rust library [Plotters](https://github.com/plotters-rs/plotters).

![alt text](resources/scatter-plots.png)

## Install

To use plotters-dioxus, you can add the following line into your Cargo.toml:

```toml
[dependencies]
plotters-dioxus = "0.2.0"
```

## Usage

The following code allows you to draw a scatter plot

```rs

#![allow(non_snake_case)]
use dioxus::prelude::*;
use plotters::prelude::*;
use plotters_dioxus::{ Plotters, DioxusDrawingArea };

fn main() {
dioxus_desktop::launch(App);
}

fn App<'a>(cx: Scope<'a>) -> Element {
render!(Plotters {
size: (400, 400),
init: move |drawing_area: DioxusDrawingArea| {
drawing_area.fill(&WHITE).expect("The drawing area filling is expected");
let mut simple_ctx = ChartBuilder::on(&drawing_area)
.caption("Simple plot", ("sans-serif", 14, &BLACK))
.margin(10)
.x_label_area_size(40)
.y_label_area_size(40)
.build_cartesian_2d(0f64..1f64, 0f64..1f64)
.expect("Simple context creation is expected");

let original_style = ShapeStyle {
color: BLACK.mix(1.0),
filled: true,
stroke_width: 1,
};

simple_ctx
.configure_mesh()
.disable_x_mesh()
.disable_y_mesh()
.y_label_style(("sans-serif", 11, &BLACK).into_text_style(&drawing_area))
.x_label_style(("sans-serif", 11, &BLACK).into_text_style(&drawing_area))
.x_desc("Count")
.y_desc("Data")
.axis_style(original_style)
.axis_desc_style(("sans-serif", 11, &BLACK).into_text_style(&drawing_area))
.draw()
.expect("Simple configuration creation is expected");
simple_ctx
.draw_series(
vec![(0.1f64, 0.5f64), (0.5f64, 0.5f64), (0.5f64, 0.1f64)]
.iter()
.map(|e| Circle::new(*e, 3i32, BLUE))
)
.expect("Draw is expected");

drawing_area
.present()
.expect(
"Unable to write result to file, please make sure 'plotters-doc-data' dir exists under current dir"
);
},
})
}

```

Check the [demo](./demo/) project for an example more interactive.

### Credits

- [dioxus](https://github.com/DioxusLabs/dioxus)
- [plotters-rs](https://github.com/plotters-rs/)

8 changes: 4 additions & 4 deletions plotters-dioxus/src/plotter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ pub type DioxusDrawingArea<'a> = DrawingArea<BitMapBackend<'a>, Shift>;
pub struct PlottersProps<'a, F: Fn(DioxusDrawingArea)> {
pub size: (u32, u32),
pub init: F,
pub on_click: EventHandler<'a, Event<MouseData>>,
pub on_wheel: EventHandler<'a, Event<WheelData>>,
pub on_click: Option<EventHandler<'a, Event<MouseData>>>,
pub on_wheel: Option<EventHandler<'a, Event<WheelData>>>,
}

impl<'a, F: Fn(DioxusDrawingArea)> PartialEq for PlottersProps<'a, F> {
Expand Down Expand Up @@ -49,8 +49,8 @@ pub fn Plotters<'a, F: Fn(DioxusDrawingArea)>(cx: Scope<'a, PlottersProps<'a, F>
let buffer_base64 = BASE64_STANDARD.encode(data);

render!(img {
onclick: |e| cx.props.on_click.call(e),
onwheel: |e| cx.props.on_wheel.call(e),
onclick: |e| if cx.props.on_click.is_some() { cx.props.on_click.as_ref().unwrap().call(e)},
onwheel: |e| if cx.props.on_wheel.is_some() { cx.props.on_wheel.as_ref().unwrap().call(e)},
src: "data:image/png;base64,{buffer_base64}",
})
}
Binary file added resources/scatter-plots.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 07379eb

Please sign in to comment.