use crate::chart::Chart;
use crate::coordinate::CoordSystem;
use crate::core::composite::LayeredChart;
use crate::core::layer::Layer;
use crate::error::ChartonError;
use crate::mark::Mark;
use crate::scale::{Expansion, IntoExplicitTicks, ScaleDomain};
use crate::theme::Theme;
pub trait IntoLayered: Into<LayeredChart> + Clone {
fn and<L: Into<LayeredChart>>(self, other: L) -> LayeredChart {
let mut lc: LayeredChart = self.into();
let mut other_lc: LayeredChart = other.into();
lc.layers.append(&mut other_lc.layers);
if lc.title.is_none() {
lc.title = other_lc.title;
}
if lc.x_label.is_none() {
lc.x_label = other_lc.x_label;
}
if lc.y_label.is_none() {
lc.y_label = other_lc.y_label;
}
lc
}
fn with_size(self, width: u32, height: u32) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.width = width;
lc.height = height;
lc
}
fn with_top_margin(self, margin: f64) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.top_margin = Some(margin);
lc
}
fn with_right_margin(self, margin: f64) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.right_margin = Some(margin);
lc
}
fn with_bottom_margin(self, margin: f64) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.bottom_margin = Some(margin);
lc
}
fn with_left_margin(self, margin: f64) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.left_margin = Some(margin);
lc
}
fn with_margins(self, top: f64, right: f64, bottom: f64, left: f64) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.top_margin = Some(top);
lc.right_margin = Some(right);
lc.bottom_margin = Some(bottom);
lc.left_margin = Some(left);
lc
}
fn with_theme(self, theme: Theme) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.theme = theme;
lc
}
fn configure_theme<F>(self, f: F) -> LayeredChart
where
F: FnOnce(Theme) -> Theme,
{
let mut lc: LayeredChart = self.into();
lc.theme = f(lc.theme);
lc
}
fn with_title<S: Into<String>>(self, title: S) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.title = Some(title.into());
lc
}
fn with_x_domain(self, min: f64, max: f64) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.x_domain = Some(ScaleDomain::Continuous(min, max));
lc
}
fn with_x_expand(self, expand: Expansion) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.x_expand = Some(expand);
lc
}
fn with_x_label<S: Into<String>>(self, label: S) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.x_label = Some(label.into());
lc
}
fn with_x_ticks<T: IntoExplicitTicks>(self, ticks: T) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.x_ticks = Some(ticks.into_explicit_ticks());
lc
}
fn with_y_domain(self, min: f64, max: f64) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.y_domain = Some(ScaleDomain::Continuous(min, max));
lc
}
fn with_y_expand(self, expand: Expansion) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.y_expand = Some(expand);
lc
}
fn with_y_label<S: Into<String>>(self, label: S) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.y_label = Some(label.into());
lc
}
fn with_y_ticks<T: IntoExplicitTicks>(self, ticks: T) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.y_ticks = Some(ticks.into_explicit_ticks());
lc
}
fn with_shape_label<S: Into<String>>(self, label: S) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.shape_label = Some(label.into());
lc
}
fn with_size_label<S: Into<String>>(self, label: S) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.size_label = Some(label.into());
lc
}
fn coord_flip(self) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.flipped = true;
lc
}
fn with_coord(self, coord: CoordSystem) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.coord_system = coord;
lc
}
fn with_start_angle(self, angle: f64) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.polar_start_angle = Some(angle);
lc
}
fn with_end_angle(self, angle: f64) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.polar_end_angle = Some(angle);
lc
}
fn with_inner_radius(self, radius: f64) -> LayeredChart {
let mut lc: LayeredChart = self.into();
lc.polar_inner_radius = Some(radius);
lc
}
fn to_svg(&self) -> Result<String, ChartonError> {
let lc: LayeredChart = self.clone().into();
lc.to_svg()
}
fn show(&self) -> Result<(), ChartonError> {
let lc: LayeredChart = self.clone().into();
lc.show()
}
fn save<P: AsRef<std::path::Path>>(&self, path: P) -> Result<(), ChartonError> {
let lc: LayeredChart = self.clone().into();
lc.save(path)
}
}
impl<T: crate::mark::Mark + 'static> IntoLayered for crate::chart::Chart<T> where
crate::chart::Chart<T>: crate::core::layer::Layer + Clone
{
}
impl IntoLayered for LayeredChart {}
impl<T: Mark + 'static> From<Chart<T>> for LayeredChart
where
Chart<T>: Layer + Clone,
{
fn from(val: Chart<T>) -> Self {
LayeredChart::new().add_layer(val)
}
}