use crate::axes::Axes;
use crate::error::Result;
use crate::render::render_figure;
use crate::theme::Theme;
pub struct Figure {
pub width: f64,
pub height: f64,
pub title: Option<String>,
pub theme: Theme,
pub(crate) axes: Vec<Axes>,
}
impl Figure {
pub fn new() -> Self {
Self {
width: 800.0,
height: 600.0,
title: None,
theme: Theme::default(),
axes: Vec::new(),
}
}
pub fn size(mut self, width: f64, height: f64) -> Self {
self.width = width;
self.height = height;
self
}
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
pub fn theme(mut self, theme: Theme) -> Self {
self.theme = theme;
self
}
pub fn add_axes(&mut self) -> &mut Axes {
self.axes.push(Axes::new());
self.axes.last_mut().unwrap()
}
pub fn to_svg(&self) -> Result<String> {
let canvas = render_figure(self)?;
let svg = esoc_gfx::backend::svg::render_svg(&canvas)?;
Ok(svg)
}
pub fn save_svg(&self, path: &str) -> Result<()> {
let svg = self.to_svg()?;
std::fs::write(path, svg)?;
Ok(())
}
#[cfg(feature = "png")]
pub fn to_png(&self) -> Result<Vec<u8>> {
use esoc_gfx::backend::RenderBackend;
let canvas = render_figure(self)?;
let bytes = esoc_gfx::backend::png::PngBackend
.render(&canvas)
.map_err(crate::error::ChartError::Gfx)?;
Ok(bytes)
}
#[cfg(feature = "png")]
pub fn save_png(&self, path: &str) -> Result<()> {
let bytes = self.to_png()?;
std::fs::write(path, bytes)?;
Ok(())
}
}
impl Default for Figure {
fn default() -> Self {
Self::new()
}
}