Skip to content

Commit aa1f79a

Browse files
lisanhu38
authored andcommitted
adding bitmapbackend support
1 parent 840e6e4 commit aa1f79a

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
[workspace]
22
members = ["plotters", "plotters-backend", "plotters-bitmap", "plotters-svg"]
33
default-members = ["plotters"]
4-

plotters/src/evcxr.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::coord::Shift;
22
use crate::drawing::{DrawingArea, IntoDrawingArea};
3+
use base64;
34
use plotters_svg::SVGBackend;
5+
use plotters_bitmap::BitMapBackend;
46

57
/// The wrapper for the generated SVG
68
pub struct SVGWrapper(String, String);
@@ -40,3 +42,53 @@ pub fn evcxr_figure<
4042
draw(root).expect("Drawing failure");
4143
SVGWrapper(buffer, "".to_string())
4244
}
45+
46+
pub struct BitmapWrapper(String, String);
47+
48+
impl BitmapWrapper {
49+
pub fn evcxr_display(&self) {
50+
println!("{:?}", self);
51+
}
52+
53+
pub fn style<S: Into<String>>(mut self, style: S) -> Self {
54+
self.1 = style.into();
55+
self
56+
}
57+
}
58+
59+
impl std::fmt::Debug for BitmapWrapper {
60+
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
61+
let svg = self.0.as_str();
62+
write!(
63+
formatter,
64+
"EVCXR_BEGIN_CONTENT text/html\n<img style=\"{}\" src=\"data:image/png;base64,{}\"/>\nEVCXR_END_CONTENT",
65+
self.1, svg
66+
)
67+
}
68+
}
69+
70+
/// Start drawing an evcxr figure
71+
pub fn evcxr_bitmap_figure<
72+
Draw: FnOnce(DrawingArea<BitMapBackend, Shift>) -> Result<(), Box<dyn std::error::Error>>,
73+
>(
74+
size: (u32, u32),
75+
draw: Draw,
76+
) -> SVGWrapper {
77+
let pixel_size = plotters_bitmap::bitmap_pixel::RGBPixel::PIXEL_SIZE;
78+
let mut buf = [u8; (size.0 as usize) * (size.1 as usize) * pixel_size];
79+
let root = BitMapBackend::with_buffer(&buf, size).into_drawing_area();
80+
let buffer = base64::encode(&buf);
81+
draw(root).expect("Drawing failure");
82+
BitmapWrapper(buffer, "".to_string())
83+
}
84+
85+
pub fn evcxr_animation<
86+
Draw: FnOnce(DrawingArea<SVGBackend, Shift>) -> Result<(), Box<dyn std::error::Error>>,
87+
>(
88+
drawing_area: &DrawingArea<SVGBackend, Shift>,
89+
draws: Draw,
90+
frames: usize,
91+
interval: usize,
92+
) -> SVGWrapper {
93+
todo!();
94+
}

0 commit comments

Comments
 (0)