use plotine_core::{Cmap, Color, Norm};
use plotine_render::{TextAlign, TextBaseline};
use crate::recipes::{HistogramBins, StepMode, TableLoc};
use crate::series::{IntoSeries, Series};
use crate::style::{Hatch, LineStyle, MarkerStyle};
pub(crate) trait ArtistProps {
fn explicit_color(&self) -> Option<Color>;
fn color_index(&self) -> usize;
fn label(&self) -> Option<&str>;
fn legend_kind(&self) -> LegendKind;
fn resolved_color(&self, cycle: &[Color]) -> Color {
self.explicit_color()
.unwrap_or_else(|| cycle[self.color_index() % cycle.len()])
}
}
macro_rules! impl_artist_props {
($ty:ty, $kind:expr) => {
impl ArtistProps for $ty {
fn explicit_color(&self) -> Option<Color> {
self.color
}
fn color_index(&self) -> usize {
self.color_index
}
fn label(&self) -> Option<&str> {
self.label.as_deref()
}
fn legend_kind(&self) -> LegendKind {
$kind
}
}
};
}
#[derive(Debug, Clone)]
pub(crate) enum PlotElement {
Line(LinePlot),
Scatter(ScatterPlot),
Bar(BarPlot),
BarH(BarHPlot),
Hist(HistPlot),
Area(AreaPlot),
FillBetween(FillBetweenPlot),
FillBetweenX(FillBetweenXPlot),
Step(StepPlot),
Stairs(StairsPlot),
Stem(StemPlot),
HLines(HLinesPlot),
VLines(VLinesPlot),
AxHLine(AxHLinePlot),
AxVLine(AxVLinePlot),
AxHSpan(AxHSpanPlot),
AxVSpan(AxVSpanPlot),
Polygon(PolygonPlot),
Rectangle(RectanglePlot),
Circle(CirclePlot),
Ellipse(EllipsePlot),
Pie(PiePlot),
StackPlot(StackPlot),
EventPlot(EventPlot),
BrokenBarH(BrokenBarHPlot),
ErrorBar(ErrorBarPlot),
Heatmap(HeatmapPlot),
Hist2d(Hist2dPlot),
Hexbin(HexbinPlot),
Contour(ContourPlot),
Contourf(ContourfPlot),
Tripcolor(TripcolorPlot),
Tricontour(TricontourPlot),
Tricontourf(TricontourfPlot),
PcolorMesh(PcolorMeshPlot),
Spy(SpyPlot),
Quiver(QuiverPlot),
Barbs(BarbsPlot),
StreamPlot(StreamPlot),
PolarFrame(PolarFramePlot),
BoxPlot(BoxPlot),
Violin(ViolinPlot),
Text(TextPlot),
Annotate(AnnotatePlot),
Table(TablePlot),
AxLine(AxLinePlot),
}
#[derive(Debug, Clone)]
pub(crate) struct ColorbarSpec {
pub cmap: Cmap,
pub vmin: f64,
pub vmax: f64,
pub norm: Norm,
pub boundaries: Option<Vec<f64>>,
}
impl PlotElement {
pub(crate) fn resolved_color(&self, cycle: &[Color]) -> Color {
self.as_props().resolved_color(cycle)
}
pub(crate) fn label(&self) -> Option<&str> {
self.as_props().label()
}
pub(crate) fn legend_kind(&self) -> LegendKind {
self.as_props().legend_kind()
}
pub(crate) fn legend_items(&self, cycle: &[Color]) -> Vec<(String, Color, LegendKind)> {
match self {
Self::Pie(p) => {
if p.labels.is_empty() {
return self.single_legend(cycle);
}
p.labels
.iter()
.enumerate()
.map(|(i, label)| {
let color = cycle[(p.color_index + i) % cycle.len()];
(label.clone(), color, LegendKind::Patch)
})
.collect()
}
Self::StackPlot(p) => {
if p.labels.is_empty() {
return self.single_legend(cycle);
}
p.labels
.iter()
.enumerate()
.map(|(i, label)| {
let color = cycle[(p.color_index + i) % cycle.len()].with_alpha(p.alpha);
(label.clone(), color, LegendKind::Patch)
})
.collect()
}
Self::EventPlot(p) => {
if p.labels.is_empty() {
return self.single_legend(cycle);
}
p.labels
.iter()
.enumerate()
.map(|(i, label)| {
let color = cycle[(p.color_index + i) % cycle.len()];
(label.clone(), color, LegendKind::Line(LineStyle::Solid))
})
.collect()
}
_ => self.single_legend(cycle),
}
}
fn single_legend(&self, cycle: &[Color]) -> Vec<(String, Color, LegendKind)> {
match self.label() {
Some(label) => {
let kind = match self {
Self::Line(p) => LegendKind::Line(p.linestyle),
Self::Step(p) => LegendKind::Line(p.linestyle),
Self::AxHLine(p) => LegendKind::Line(p.linestyle),
Self::AxVLine(p) => LegendKind::Line(p.linestyle),
Self::AxLine(p) => LegendKind::Line(p.linestyle),
_ => self.legend_kind(),
};
let color = match self {
Self::Area(p) => self.resolved_color(cycle).with_alpha(p.alpha),
Self::FillBetween(p) => self.resolved_color(cycle).with_alpha(p.alpha),
Self::FillBetweenX(p) => self.resolved_color(cycle).with_alpha(p.alpha),
Self::AxHSpan(p) => self.resolved_color(cycle).with_alpha(p.alpha),
Self::AxVSpan(p) => self.resolved_color(cycle).with_alpha(p.alpha),
Self::StackPlot(p) => self.resolved_color(cycle).with_alpha(p.alpha),
Self::Polygon(p) => self.resolved_color(cycle).with_alpha(p.alpha),
Self::Rectangle(p) => self.resolved_color(cycle).with_alpha(p.alpha),
Self::Circle(p) => self.resolved_color(cycle).with_alpha(p.alpha),
Self::Ellipse(p) => self.resolved_color(cycle).with_alpha(p.alpha),
_ => self.resolved_color(cycle),
};
vec![(label.to_string(), color, kind)]
}
None => Vec::new(),
}
}
fn as_props(&self) -> &dyn ArtistProps {
match self {
Self::Line(p) => p,
Self::Scatter(p) => p,
Self::Bar(p) => p,
Self::BarH(p) => p,
Self::Hist(p) => p,
Self::Area(p) => p,
Self::FillBetween(p) => p,
Self::FillBetweenX(p) => p,
Self::Step(p) => p,
Self::Stairs(p) => p,
Self::Stem(p) => p,
Self::HLines(p) => p,
Self::VLines(p) => p,
Self::AxHLine(p) => p,
Self::AxVLine(p) => p,
Self::AxHSpan(p) => p,
Self::AxVSpan(p) => p,
Self::Polygon(p) => p,
Self::Rectangle(p) => p,
Self::Circle(p) => p,
Self::Ellipse(p) => p,
Self::Pie(p) => p,
Self::StackPlot(p) => p,
Self::EventPlot(p) => p,
Self::BrokenBarH(p) => p,
Self::ErrorBar(p) => p,
Self::Heatmap(p) => p,
Self::Hist2d(p) => p,
Self::Hexbin(p) => p,
Self::Contour(p) => p,
Self::Contourf(p) => p,
Self::Tripcolor(p) => p,
Self::Tricontour(p) => p,
Self::Tricontourf(p) => p,
Self::PcolorMesh(p) => p,
Self::Spy(p) => p,
Self::Quiver(p) => p,
Self::Barbs(p) => p,
Self::StreamPlot(p) => p,
Self::PolarFrame(p) => p,
Self::BoxPlot(p) => p,
Self::Violin(p) => p,
Self::Text(p) => p,
Self::Annotate(p) => p,
Self::Table(p) => p,
Self::AxLine(p) => p,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum LegendKind {
Line(LineStyle),
Marker,
ErrorBar,
Patch,
}
impl_artist_props!(LinePlot, LegendKind::Line(LineStyle::Solid));
impl_artist_props!(ScatterPlot, LegendKind::Marker);
impl_artist_props!(BarPlot, LegendKind::Patch);
impl_artist_props!(BarHPlot, LegendKind::Patch);
impl_artist_props!(HistPlot, LegendKind::Patch);
impl_artist_props!(AreaPlot, LegendKind::Patch);
impl_artist_props!(FillBetweenPlot, LegendKind::Patch);
impl_artist_props!(FillBetweenXPlot, LegendKind::Patch);
impl_artist_props!(StepPlot, LegendKind::Line(LineStyle::Solid));
impl_artist_props!(StairsPlot, LegendKind::Line(LineStyle::Solid));
impl_artist_props!(StemPlot, LegendKind::Marker);
impl_artist_props!(HLinesPlot, LegendKind::Line(LineStyle::Solid));
impl_artist_props!(VLinesPlot, LegendKind::Line(LineStyle::Solid));
impl_artist_props!(AxHLinePlot, LegendKind::Line(LineStyle::Solid));
impl_artist_props!(AxVLinePlot, LegendKind::Line(LineStyle::Solid));
impl_artist_props!(AxHSpanPlot, LegendKind::Patch);
impl_artist_props!(AxVSpanPlot, LegendKind::Patch);
impl_artist_props!(PolygonPlot, LegendKind::Patch);
impl_artist_props!(RectanglePlot, LegendKind::Patch);
impl_artist_props!(CirclePlot, LegendKind::Patch);
impl_artist_props!(EllipsePlot, LegendKind::Patch);
impl_artist_props!(PiePlot, LegendKind::Patch);
impl_artist_props!(StackPlot, LegendKind::Patch);
impl_artist_props!(EventPlot, LegendKind::Line(LineStyle::Solid));
impl_artist_props!(BrokenBarHPlot, LegendKind::Patch);
impl_artist_props!(ErrorBarPlot, LegendKind::ErrorBar);
impl_artist_props!(BoxPlot, LegendKind::Patch);
impl_artist_props!(ViolinPlot, LegendKind::Patch);
impl ArtistProps for HeatmapPlot {
fn explicit_color(&self) -> Option<Color> {
None
}
fn color_index(&self) -> usize {
0
}
fn label(&self) -> Option<&str> {
self.label.as_deref()
}
fn legend_kind(&self) -> LegendKind {
LegendKind::Patch
}
}
impl ArtistProps for Hist2dPlot {
fn explicit_color(&self) -> Option<Color> {
None
}
fn color_index(&self) -> usize {
0
}
fn label(&self) -> Option<&str> {
self.label.as_deref()
}
fn legend_kind(&self) -> LegendKind {
LegendKind::Patch
}
}
impl ArtistProps for HexbinPlot {
fn explicit_color(&self) -> Option<Color> {
None
}
fn color_index(&self) -> usize {
0
}
fn label(&self) -> Option<&str> {
self.label.as_deref()
}
fn legend_kind(&self) -> LegendKind {
LegendKind::Patch
}
}
impl ArtistProps for ContourfPlot {
fn explicit_color(&self) -> Option<Color> {
None
}
fn color_index(&self) -> usize {
0
}
fn label(&self) -> Option<&str> {
self.label.as_deref()
}
fn legend_kind(&self) -> LegendKind {
LegendKind::Patch
}
}
impl ArtistProps for TripcolorPlot {
fn explicit_color(&self) -> Option<Color> {
None
}
fn color_index(&self) -> usize {
0
}
fn label(&self) -> Option<&str> {
self.label.as_deref()
}
fn legend_kind(&self) -> LegendKind {
LegendKind::Patch
}
}
impl_artist_props!(TricontourPlot, LegendKind::Line(LineStyle::Solid));
impl ArtistProps for TricontourfPlot {
fn explicit_color(&self) -> Option<Color> {
None
}
fn color_index(&self) -> usize {
0
}
fn label(&self) -> Option<&str> {
self.label.as_deref()
}
fn legend_kind(&self) -> LegendKind {
LegendKind::Patch
}
}
impl ArtistProps for PcolorMeshPlot {
fn explicit_color(&self) -> Option<Color> {
None
}
fn color_index(&self) -> usize {
0
}
fn label(&self) -> Option<&str> {
self.label.as_deref()
}
fn legend_kind(&self) -> LegendKind {
LegendKind::Patch
}
}
impl_artist_props!(ContourPlot, LegendKind::Line(LineStyle::Solid));
impl_artist_props!(SpyPlot, LegendKind::Marker);
impl_artist_props!(QuiverPlot, LegendKind::Line(LineStyle::Solid));
impl_artist_props!(BarbsPlot, LegendKind::Line(LineStyle::Solid));
impl_artist_props!(StreamPlot, LegendKind::Line(LineStyle::Solid));
impl_artist_props!(PolarFramePlot, LegendKind::Line(LineStyle::Solid));
impl_artist_props!(TextPlot, LegendKind::Line(LineStyle::Solid));
impl_artist_props!(AnnotatePlot, LegendKind::Line(LineStyle::Solid));
impl_artist_props!(TablePlot, LegendKind::Patch);
#[derive(Debug, Clone)]
pub struct LinePlot {
pub(crate) x: Series,
pub(crate) y: Series,
pub(crate) color: Option<Color>,
pub(crate) width: f64,
pub(crate) linestyle: LineStyle,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl LinePlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn width(&mut self, width: f64) -> &mut Self {
self.width = width.max(0.1);
self
}
pub fn linestyle(&mut self, style: LineStyle) -> &mut Self {
self.linestyle = style;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
pub fn set_y<Y: crate::series::IntoSeries>(&mut self, y: Y) -> plotine_core::Result<&mut Self> {
let y = y.into_series();
if y.len() != self.x.len() {
return Err(plotine_core::PlotError::length_mismatch(
self.x.len(),
y.len(),
));
}
self.y = y;
Ok(self)
}
pub fn set_xy<X, Y>(&mut self, x: X, y: Y) -> plotine_core::Result<&mut Self>
where
X: crate::series::IntoSeries,
Y: crate::series::IntoSeries,
{
let x = x.into_series();
let y = y.into_series();
if x.len() != y.len() {
return Err(plotine_core::PlotError::length_mismatch(x.len(), y.len()));
}
self.x = x;
self.y = y;
Ok(self)
}
}
#[derive(Debug, Clone)]
pub struct ScatterPlot {
pub(crate) x: Series,
pub(crate) y: Series,
pub(crate) color: Option<Color>,
pub(crate) size: f64,
pub(crate) marker: MarkerStyle,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl ScatterPlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn size(&mut self, size: f64) -> &mut Self {
self.size = size.max(0.5);
self
}
pub fn marker(&mut self, style: MarkerStyle) -> &mut Self {
self.marker = style;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
pub fn set_y<Y: crate::series::IntoSeries>(&mut self, y: Y) -> plotine_core::Result<&mut Self> {
let y = y.into_series();
if y.len() != self.x.len() {
return Err(plotine_core::PlotError::length_mismatch(
self.x.len(),
y.len(),
));
}
self.y = y;
Ok(self)
}
pub fn set_xy<X, Y>(&mut self, x: X, y: Y) -> plotine_core::Result<&mut Self>
where
X: crate::series::IntoSeries,
Y: crate::series::IntoSeries,
{
let x = x.into_series();
let y = y.into_series();
if x.len() != y.len() {
return Err(plotine_core::PlotError::length_mismatch(x.len(), y.len()));
}
self.x = x;
self.y = y;
Ok(self)
}
}
#[derive(Debug, Clone)]
pub struct BarPlot {
pub(crate) x: Series,
pub(crate) heights: Series,
pub(crate) width: f64,
pub(crate) baseline: f64,
pub(crate) color: Option<Color>,
pub(crate) edgecolor: Option<Color>,
pub(crate) hatch: Hatch,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl BarPlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn edgecolor(&mut self, color: Color) -> &mut Self {
self.edgecolor = Some(color);
self
}
pub fn hatch(&mut self, hatch: Hatch) -> &mut Self {
self.hatch = hatch;
self
}
pub fn width(&mut self, width: f64) -> &mut Self {
self.width = width.clamp(0.05, 1.0);
self
}
pub fn baseline(&mut self, baseline: f64) -> &mut Self {
self.baseline = baseline;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct HistPlot {
pub(crate) data: Series,
pub(crate) bin_count: usize,
pub(crate) color: Option<Color>,
pub(crate) edgecolor: Option<Color>,
pub(crate) hatch: Hatch,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl HistPlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn edgecolor(&mut self, color: Color) -> &mut Self {
self.edgecolor = Some(color);
self
}
pub fn hatch(&mut self, hatch: Hatch) -> &mut Self {
self.hatch = hatch;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
pub fn bins(&mut self, n: usize) -> &mut Self {
self.bin_count = n.max(1);
self
}
pub(crate) fn compute_bins(&self) -> HistogramBins {
crate::recipes::histogram(self.data.as_slice(), self.bin_count)
}
}
#[derive(Debug, Clone)]
pub struct AreaPlot {
pub(crate) x: Series,
pub(crate) y: Series,
pub(crate) baseline: f64,
pub(crate) color: Option<Color>,
pub(crate) alpha: f64,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl AreaPlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn alpha(&mut self, alpha: f64) -> &mut Self {
self.alpha = alpha.clamp(0.0, 1.0);
self
}
pub fn baseline(&mut self, baseline: f64) -> &mut Self {
self.baseline = baseline;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct FillBetweenPlot {
pub(crate) x: Series,
pub(crate) y1: Series,
pub(crate) y2: Series,
pub(crate) color: Option<Color>,
pub(crate) alpha: f64,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl FillBetweenPlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn alpha(&mut self, alpha: f64) -> &mut Self {
self.alpha = alpha.clamp(0.0, 1.0);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct FillBetweenXPlot {
pub(crate) y: Series,
pub(crate) x1: Series,
pub(crate) x2: Series,
pub(crate) color: Option<Color>,
pub(crate) alpha: f64,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl FillBetweenXPlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn alpha(&mut self, alpha: f64) -> &mut Self {
self.alpha = alpha.clamp(0.0, 1.0);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct StepPlot {
pub(crate) x: Series,
pub(crate) y: Series,
pub(crate) mode: StepMode,
pub(crate) color: Option<Color>,
pub(crate) width: f64,
pub(crate) linestyle: LineStyle,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl StepPlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn width(&mut self, width: f64) -> &mut Self {
self.width = width.max(0.1);
self
}
pub fn linestyle(&mut self, style: LineStyle) -> &mut Self {
self.linestyle = style;
self
}
pub fn mode(&mut self, mode: StepMode) -> &mut Self {
self.mode = mode;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct StairsPlot {
pub(crate) edges: Series,
pub(crate) values: Series,
pub(crate) baseline: f64,
pub(crate) color: Option<Color>,
pub(crate) width: f64,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl StairsPlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn width(&mut self, width: f64) -> &mut Self {
self.width = width.max(0.1);
self
}
pub fn baseline(&mut self, baseline: f64) -> &mut Self {
self.baseline = baseline;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct StemPlot {
pub(crate) x: Series,
pub(crate) y: Series,
pub(crate) baseline: f64,
pub(crate) color: Option<Color>,
pub(crate) width: f64,
pub(crate) marker_size: f64,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl StemPlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn width(&mut self, width: f64) -> &mut Self {
self.width = width.max(0.1);
self
}
pub fn marker_size(&mut self, size: f64) -> &mut Self {
self.marker_size = size.max(0.5);
self
}
pub fn baseline(&mut self, baseline: f64) -> &mut Self {
self.baseline = baseline;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct HLinesPlot {
pub(crate) y: Series,
pub(crate) xmin: Series,
pub(crate) xmax: Series,
pub(crate) color: Option<Color>,
pub(crate) width: f64,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl HLinesPlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn width(&mut self, width: f64) -> &mut Self {
self.width = width.max(0.1);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct VLinesPlot {
pub(crate) x: Series,
pub(crate) ymin: Series,
pub(crate) ymax: Series,
pub(crate) color: Option<Color>,
pub(crate) width: f64,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl VLinesPlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn width(&mut self, width: f64) -> &mut Self {
self.width = width.max(0.1);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct AxHLinePlot {
pub(crate) y: f64,
pub(crate) color: Option<Color>,
pub(crate) width: f64,
pub(crate) linestyle: LineStyle,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl AxHLinePlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn width(&mut self, width: f64) -> &mut Self {
self.width = width.max(0.1);
self
}
pub fn linestyle(&mut self, style: LineStyle) -> &mut Self {
self.linestyle = style;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct AxVLinePlot {
pub(crate) x: f64,
pub(crate) color: Option<Color>,
pub(crate) width: f64,
pub(crate) linestyle: LineStyle,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl AxVLinePlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn width(&mut self, width: f64) -> &mut Self {
self.width = width.max(0.1);
self
}
pub fn linestyle(&mut self, style: LineStyle) -> &mut Self {
self.linestyle = style;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct BarHPlot {
pub(crate) y: Series,
pub(crate) widths: Series,
pub(crate) height: f64,
pub(crate) baseline: f64,
pub(crate) color: Option<Color>,
pub(crate) edgecolor: Option<Color>,
pub(crate) hatch: Hatch,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl BarHPlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn edgecolor(&mut self, color: Color) -> &mut Self {
self.edgecolor = Some(color);
self
}
pub fn hatch(&mut self, hatch: Hatch) -> &mut Self {
self.hatch = hatch;
self
}
pub fn height(&mut self, height: f64) -> &mut Self {
self.height = height.clamp(0.05, 1.0);
self
}
pub fn baseline(&mut self, baseline: f64) -> &mut Self {
self.baseline = baseline;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub enum ErrBars {
Symmetric(Series),
Asymmetric {
lower: Series,
upper: Series,
},
}
impl ErrBars {
pub(crate) fn len(&self) -> usize {
match self {
Self::Symmetric(s) => s.len(),
Self::Asymmetric { lower, .. } => lower.len(),
}
}
pub(crate) fn arms(&self) -> (&[f64], &[f64]) {
match self {
Self::Symmetric(s) => (s.as_slice(), s.as_slice()),
Self::Asymmetric { lower, upper } => (lower.as_slice(), upper.as_slice()),
}
}
}
#[derive(Debug, Clone)]
pub struct ErrorBarPlot {
pub(crate) x: Series,
pub(crate) y: Series,
pub(crate) yerr: ErrBars,
pub(crate) xerr: Option<ErrBars>,
pub(crate) color: Option<Color>,
pub(crate) width: f64,
pub(crate) capsize: f64,
pub(crate) connect: bool,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl ErrorBarPlot {
pub fn yerr_asym<L, U>(&mut self, lower: L, upper: U) -> &mut Self
where
L: IntoSeries,
U: IntoSeries,
{
self.yerr = ErrBars::Asymmetric {
lower: lower.into_series(),
upper: upper.into_series(),
};
self
}
pub fn xerr<E: IntoSeries>(&mut self, xerr: E) -> &mut Self {
self.xerr = Some(ErrBars::Symmetric(xerr.into_series()));
self
}
pub fn xerr_asym<L, U>(&mut self, lower: L, upper: U) -> &mut Self
where
L: IntoSeries,
U: IntoSeries,
{
self.xerr = Some(ErrBars::Asymmetric {
lower: lower.into_series(),
upper: upper.into_series(),
});
self
}
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn width(&mut self, width: f64) -> &mut Self {
self.width = width.max(0.1);
self
}
pub fn capsize(&mut self, capsize: f64) -> &mut Self {
self.capsize = capsize.max(0.0);
self
}
pub fn connect(&mut self, connect: bool) -> &mut Self {
self.connect = connect;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct HeatmapPlot {
pub(crate) nrows: usize,
pub(crate) ncols: usize,
pub(crate) values: Series,
pub(crate) cmap: Cmap,
pub(crate) vmin: Option<f64>,
pub(crate) vmax: Option<f64>,
pub(crate) norm: Norm,
pub(crate) origin: crate::recipes::HeatmapOrigin,
pub(crate) extent: Option<[f64; 4]>,
pub(crate) alpha: f64,
pub(crate) colorbar: bool,
pub(crate) label: Option<String>,
}
impl HeatmapPlot {
pub fn cmap(&mut self, cmap: impl Into<Cmap>) -> &mut Self {
self.cmap = cmap.into();
self
}
pub fn origin(&mut self, origin: crate::recipes::HeatmapOrigin) -> &mut Self {
self.origin = origin;
self
}
pub fn extent(&mut self, extent: [f64; 4]) -> &mut Self {
self.extent = Some(extent);
self
}
pub fn alpha(&mut self, alpha: f64) -> &mut Self {
self.alpha = alpha.clamp(0.0, 1.0);
self
}
pub fn norm(&mut self, norm: Norm) -> &mut Self {
self.norm = norm;
self
}
pub fn vmin(&mut self, vmin: f64) -> &mut Self {
self.vmin = Some(vmin);
self
}
pub fn vmax(&mut self, vmax: f64) -> &mut Self {
self.vmax = Some(vmax);
self
}
pub fn colorbar(&mut self, show: bool) -> &mut Self {
self.colorbar = show;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
pub(crate) fn value_limits(&self) -> (f64, f64) {
crate::recipes::heatmap_limits(self.values.as_slice(), self.vmin, self.vmax)
}
}
#[derive(Debug, Clone)]
pub struct Hist2dPlot {
pub(crate) x: Series,
pub(crate) y: Series,
pub(crate) bins_x: usize,
pub(crate) bins_y: usize,
pub(crate) cmap: Cmap,
pub(crate) vmin: Option<f64>,
pub(crate) vmax: Option<f64>,
pub(crate) norm: Norm,
pub(crate) colorbar: bool,
pub(crate) label: Option<String>,
}
impl Hist2dPlot {
pub fn bins(&mut self, n: usize) -> &mut Self {
let n = n.max(1);
self.bins_x = n;
self.bins_y = n;
self
}
pub fn bins_xy(&mut self, nx: usize, ny: usize) -> &mut Self {
self.bins_x = nx.max(1);
self.bins_y = ny.max(1);
self
}
pub fn cmap(&mut self, cmap: impl Into<Cmap>) -> &mut Self {
self.cmap = cmap.into();
self
}
pub fn norm(&mut self, norm: Norm) -> &mut Self {
self.norm = norm;
self
}
pub fn vmin(&mut self, vmin: f64) -> &mut Self {
self.vmin = Some(vmin);
self
}
pub fn vmax(&mut self, vmax: f64) -> &mut Self {
self.vmax = Some(vmax);
self
}
pub fn colorbar(&mut self, show: bool) -> &mut Self {
self.colorbar = show;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
pub(crate) fn value_limits(&self) -> (f64, f64) {
let bins = crate::recipes::hist2d_bins(
self.x.as_slice(),
self.y.as_slice(),
self.bins_x,
self.bins_y,
);
crate::recipes::hist2d_limits(&bins.counts, self.vmin, self.vmax)
}
}
#[derive(Debug, Clone)]
pub struct HexbinPlot {
pub(crate) x: Series,
pub(crate) y: Series,
pub(crate) gridsize: usize,
pub(crate) cmap: Cmap,
pub(crate) vmin: Option<f64>,
pub(crate) vmax: Option<f64>,
pub(crate) norm: Norm,
pub(crate) colorbar: bool,
pub(crate) label: Option<String>,
}
impl HexbinPlot {
pub fn gridsize(&mut self, n: usize) -> &mut Self {
self.gridsize = n.max(2);
self
}
pub fn cmap(&mut self, cmap: impl Into<Cmap>) -> &mut Self {
self.cmap = cmap.into();
self
}
pub fn norm(&mut self, norm: Norm) -> &mut Self {
self.norm = norm;
self
}
pub fn vmin(&mut self, vmin: f64) -> &mut Self {
self.vmin = Some(vmin);
self
}
pub fn vmax(&mut self, vmax: f64) -> &mut Self {
self.vmax = Some(vmax);
self
}
pub fn colorbar(&mut self, show: bool) -> &mut Self {
self.colorbar = show;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
pub(crate) fn value_limits(&self) -> (f64, f64) {
use plotine_core::{DataToPixel, LinearScale, Rect, ScaleKind};
let t = DataToPixel::new(
ScaleKind::Linear(LinearScale::new(0.0, 1.0).expect("unit")),
ScaleKind::Linear(LinearScale::new(0.0, 1.0).expect("unit")),
Rect::new(0.0, 0.0, 1.0, 1.0),
);
let (_, lo, hi) = crate::recipes::hexbin_cells(
self.x.as_slice(),
self.y.as_slice(),
self.gridsize,
&self.cmap,
self.vmin,
self.vmax,
self.norm,
&t,
);
(lo, hi)
}
}
#[derive(Debug, Clone)]
pub struct ContourPlot {
pub(crate) nrows: usize,
pub(crate) ncols: usize,
pub(crate) values: Series,
pub(crate) levels: Option<Vec<f64>>,
pub(crate) level_count: usize,
pub(crate) color: Option<Color>,
pub(crate) width: f64,
pub(crate) clabel: bool,
pub(crate) clabel_size: f64,
pub(crate) clabel_color: Option<Color>,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl ContourPlot {
pub fn level_values<I>(&mut self, levels: I) -> &mut Self
where
I: IntoIterator<Item = f64>,
{
self.levels = Some(levels.into_iter().filter(|v| v.is_finite()).collect());
self
}
pub fn levels(&mut self, n: usize) -> &mut Self {
self.level_count = n.max(1);
self
}
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn width(&mut self, width: f64) -> &mut Self {
self.width = width.max(0.1);
self
}
pub fn clabel(&mut self, on: bool) -> &mut Self {
self.clabel = on;
self
}
pub fn clabel_size(&mut self, size: f64) -> &mut Self {
self.clabel_size = size.max(1.0);
self
}
pub fn clabel_color(&mut self, color: Color) -> &mut Self {
self.clabel_color = Some(color);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
pub(crate) fn resolved_levels(&self) -> Vec<f64> {
if let Some(ref levels) = self.levels {
levels.clone()
} else {
crate::recipes::auto_levels(self.values.as_slice(), self.level_count)
}
}
}
#[derive(Debug, Clone)]
pub struct ContourfPlot {
pub(crate) nrows: usize,
pub(crate) ncols: usize,
pub(crate) values: Series,
pub(crate) levels: Option<Vec<f64>>,
pub(crate) level_count: usize,
pub(crate) cmap: Cmap,
pub(crate) norm: Norm,
pub(crate) colorbar: bool,
pub(crate) label: Option<String>,
}
impl ContourfPlot {
pub fn level_values<I>(&mut self, levels: I) -> &mut Self
where
I: IntoIterator<Item = f64>,
{
self.levels = Some(levels.into_iter().filter(|v| v.is_finite()).collect());
self
}
pub fn levels(&mut self, n: usize) -> &mut Self {
self.level_count = n.max(2);
self
}
pub fn cmap(&mut self, cmap: impl Into<Cmap>) -> &mut Self {
self.cmap = cmap.into();
self
}
pub fn norm(&mut self, norm: Norm) -> &mut Self {
self.norm = norm;
self
}
pub fn colorbar(&mut self, show: bool) -> &mut Self {
self.colorbar = show;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
pub(crate) fn resolved_levels(&self) -> Vec<f64> {
if let Some(ref levels) = self.levels {
levels.clone()
} else {
crate::recipes::auto_levels(self.values.as_slice(), self.level_count)
}
}
pub(crate) fn value_limits(&self) -> (f64, f64) {
let levels = self.resolved_levels();
if levels.len() >= 2 {
(
levels.iter().copied().fold(f64::INFINITY, f64::min),
levels.iter().copied().fold(f64::NEG_INFINITY, f64::max),
)
} else {
crate::recipes::heatmap_limits(self.values.as_slice(), None, None)
}
}
}
#[derive(Debug, Clone)]
pub struct PcolorMeshPlot {
pub(crate) x_edges: Series,
pub(crate) y_edges: Series,
pub(crate) values: Series,
pub(crate) cmap: Cmap,
pub(crate) vmin: Option<f64>,
pub(crate) vmax: Option<f64>,
pub(crate) norm: Norm,
pub(crate) colorbar: bool,
pub(crate) label: Option<String>,
}
impl PcolorMeshPlot {
pub fn cmap(&mut self, cmap: impl Into<Cmap>) -> &mut Self {
self.cmap = cmap.into();
self
}
pub fn norm(&mut self, norm: Norm) -> &mut Self {
self.norm = norm;
self
}
pub fn vmin(&mut self, vmin: f64) -> &mut Self {
self.vmin = Some(vmin);
self
}
pub fn vmax(&mut self, vmax: f64) -> &mut Self {
self.vmax = Some(vmax);
self
}
pub fn colorbar(&mut self, show: bool) -> &mut Self {
self.colorbar = show;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
pub(crate) fn value_limits(&self) -> (f64, f64) {
crate::recipes::pcolormesh_limits(self.values.as_slice(), self.vmin, self.vmax)
}
}
#[derive(Debug, Clone)]
pub struct SpyPlot {
pub(crate) nrows: usize,
pub(crate) ncols: usize,
pub(crate) values: Series,
pub(crate) precision: f64,
pub(crate) marker_size: f64,
pub(crate) color: Option<Color>,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl SpyPlot {
pub fn precision(&mut self, precision: f64) -> &mut Self {
self.precision = precision.abs();
self
}
pub fn marker_size(&mut self, size: f64) -> &mut Self {
self.marker_size = size.max(0.5);
self
}
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct QuiverPlot {
pub(crate) x: Series,
pub(crate) y: Series,
pub(crate) u: Series,
pub(crate) v: Series,
pub(crate) scale: Option<f64>,
pub(crate) width: f64,
pub(crate) key_length: Option<f64>,
pub(crate) key_label: Option<String>,
pub(crate) color: Option<Color>,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl QuiverPlot {
pub fn scale(&mut self, scale: f64) -> &mut Self {
self.scale = Some(scale.max(1e-12));
self
}
pub fn width(&mut self, width: f64) -> &mut Self {
self.width = width.max(0.1);
self
}
pub fn quiverkey(&mut self, length: f64, label: impl Into<String>) -> &mut Self {
self.key_length = Some(length.abs().max(1e-12));
self.key_label = Some(label.into());
self
}
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct BarbsPlot {
pub(crate) x: Series,
pub(crate) y: Series,
pub(crate) u: Series,
pub(crate) v: Series,
pub(crate) length: f64,
pub(crate) width: f64,
pub(crate) flip: bool,
pub(crate) half: f64,
pub(crate) full: f64,
pub(crate) flag: f64,
pub(crate) color: Option<Color>,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl BarbsPlot {
pub fn length(&mut self, length: f64) -> &mut Self {
self.length = length.max(2.0);
self
}
pub fn width(&mut self, width: f64) -> &mut Self {
self.width = width.max(0.1);
self
}
pub fn flip(&mut self, on: bool) -> &mut Self {
self.flip = on;
self
}
pub fn increments(&mut self, half: f64, full: f64, flag: f64) -> &mut Self {
self.half = half.max(1e-12);
self.full = full.max(self.half);
self.flag = flag.max(self.full);
self
}
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct StreamPlot {
pub(crate) u: Series,
pub(crate) v: Series,
pub(crate) nrows: usize,
pub(crate) ncols: usize,
pub(crate) density: f64,
pub(crate) width: f64,
pub(crate) arrow_size: f64,
pub(crate) color: Option<Color>,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl StreamPlot {
pub fn density(&mut self, density: f64) -> &mut Self {
self.density = density.clamp(0.1, 8.0);
self
}
pub fn width(&mut self, width: f64) -> &mut Self {
self.width = width.max(0.1);
self
}
pub fn arrow_size(&mut self, size: f64) -> &mut Self {
self.arrow_size = size.max(0.0);
self
}
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct PolarFramePlot {
pub(crate) rmax: f64,
pub(crate) rings: usize,
pub(crate) spokes: usize,
pub(crate) color: Option<Color>,
pub(crate) width: f64,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl PolarFramePlot {
pub fn rmax(&mut self, rmax: f64) -> &mut Self {
self.rmax = rmax.abs().max(1e-9);
self
}
pub fn rings(&mut self, rings: usize) -> &mut Self {
self.rings = rings.clamp(3, 12);
self
}
pub fn spokes(&mut self, spokes: usize) -> &mut Self {
self.spokes = spokes.clamp(4, 24);
self
}
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn width(&mut self, width: f64) -> &mut Self {
self.width = width.max(0.1);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct BoxPlot {
pub(crate) groups: Vec<Series>,
pub(crate) widths: f64,
pub(crate) show_fliers: bool,
pub(crate) color: Option<Color>,
pub(crate) edgecolor: Option<Color>,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl BoxPlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn edgecolor(&mut self, color: Color) -> &mut Self {
self.edgecolor = Some(color);
self
}
pub fn widths(&mut self, widths: f64) -> &mut Self {
self.widths = widths.clamp(0.1, 0.95);
self
}
pub fn show_fliers(&mut self, show: bool) -> &mut Self {
self.show_fliers = show;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct ViolinPlot {
pub(crate) groups: Vec<Series>,
pub(crate) widths: f64,
pub(crate) points: usize,
pub(crate) show_median: bool,
pub(crate) show_extrema: bool,
pub(crate) color: Option<Color>,
pub(crate) edgecolor: Option<Color>,
pub(crate) alpha: f64,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl ViolinPlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn edgecolor(&mut self, color: Color) -> &mut Self {
self.edgecolor = Some(color);
self
}
pub fn widths(&mut self, widths: f64) -> &mut Self {
self.widths = widths.clamp(0.1, 0.95);
self
}
pub fn points(&mut self, points: usize) -> &mut Self {
self.points = points.clamp(16, 256);
self
}
pub fn show_median(&mut self, show: bool) -> &mut Self {
self.show_median = show;
self
}
pub fn show_extrema(&mut self, show: bool) -> &mut Self {
self.show_extrema = show;
self
}
pub fn alpha(&mut self, alpha: f64) -> &mut Self {
self.alpha = alpha.clamp(0.0, 1.0);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct PiePlot {
pub(crate) sizes: Series,
pub(crate) labels: Vec<String>,
pub(crate) start_angle: f64,
pub(crate) counterclock: bool,
pub(crate) color: Option<Color>,
pub(crate) edgecolor: Option<Color>,
pub(crate) alpha: f64,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl PiePlot {
pub fn labels<I, S>(&mut self, labels: I) -> &mut Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.labels = labels.into_iter().map(Into::into).collect();
self
}
pub fn start_angle(&mut self, degrees: f64) -> &mut Self {
self.start_angle = degrees;
self
}
pub fn counterclock(&mut self, counterclock: bool) -> &mut Self {
self.counterclock = counterclock;
self
}
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn edgecolor(&mut self, color: Color) -> &mut Self {
self.edgecolor = Some(color);
self
}
pub fn alpha(&mut self, alpha: f64) -> &mut Self {
self.alpha = alpha.clamp(0.0, 1.0);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct StackPlot {
pub(crate) x: Series,
pub(crate) ys: Vec<Series>,
pub(crate) labels: Vec<String>,
pub(crate) alpha: f64,
pub(crate) color: Option<Color>,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl StackPlot {
pub fn labels<I, S>(&mut self, labels: I) -> &mut Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.labels = labels.into_iter().map(Into::into).collect();
self
}
pub fn alpha(&mut self, alpha: f64) -> &mut Self {
self.alpha = alpha.clamp(0.0, 1.0);
self
}
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct EventPlot {
pub(crate) positions: Vec<Series>,
pub(crate) labels: Vec<String>,
pub(crate) lineoffset: f64,
pub(crate) linewidth: f64,
pub(crate) color: Option<Color>,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl EventPlot {
pub fn labels<I, S>(&mut self, labels: I) -> &mut Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.labels = labels.into_iter().map(Into::into).collect();
self
}
pub fn lineoffset(&mut self, lineoffset: f64) -> &mut Self {
self.lineoffset = lineoffset.abs().max(0.05);
self
}
pub fn linewidth(&mut self, width: f64) -> &mut Self {
self.linewidth = width.max(0.1);
self
}
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct BrokenBarHPlot {
pub(crate) xranges: Vec<(f64, f64)>,
pub(crate) y: f64,
pub(crate) height: f64,
pub(crate) color: Option<Color>,
pub(crate) edgecolor: Option<Color>,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl BrokenBarHPlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn edgecolor(&mut self, color: Color) -> &mut Self {
self.edgecolor = Some(color);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct RectanglePlot {
pub(crate) x: f64,
pub(crate) y: f64,
pub(crate) width: f64,
pub(crate) height: f64,
pub(crate) color: Option<Color>,
pub(crate) edgecolor: Option<Color>,
pub(crate) alpha: f64,
pub(crate) hatch: Hatch,
pub(crate) linewidth: f64,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl RectanglePlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn edgecolor(&mut self, color: Color) -> &mut Self {
self.edgecolor = Some(color);
self
}
pub fn alpha(&mut self, alpha: f64) -> &mut Self {
self.alpha = alpha.clamp(0.0, 1.0);
self
}
pub fn hatch(&mut self, hatch: Hatch) -> &mut Self {
self.hatch = hatch;
self
}
pub fn linewidth(&mut self, width: f64) -> &mut Self {
self.linewidth = width.max(0.0);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct CirclePlot {
pub(crate) x: f64,
pub(crate) y: f64,
pub(crate) radius: f64,
pub(crate) color: Option<Color>,
pub(crate) edgecolor: Option<Color>,
pub(crate) alpha: f64,
pub(crate) linewidth: f64,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl CirclePlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn edgecolor(&mut self, color: Color) -> &mut Self {
self.edgecolor = Some(color);
self
}
pub fn alpha(&mut self, alpha: f64) -> &mut Self {
self.alpha = alpha.clamp(0.0, 1.0);
self
}
pub fn linewidth(&mut self, width: f64) -> &mut Self {
self.linewidth = width.max(0.0);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct EllipsePlot {
pub(crate) x: f64,
pub(crate) y: f64,
pub(crate) width: f64,
pub(crate) height: f64,
pub(crate) color: Option<Color>,
pub(crate) edgecolor: Option<Color>,
pub(crate) alpha: f64,
pub(crate) linewidth: f64,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl EllipsePlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn edgecolor(&mut self, color: Color) -> &mut Self {
self.edgecolor = Some(color);
self
}
pub fn alpha(&mut self, alpha: f64) -> &mut Self {
self.alpha = alpha.clamp(0.0, 1.0);
self
}
pub fn linewidth(&mut self, width: f64) -> &mut Self {
self.linewidth = width.max(0.0);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct PolygonPlot {
pub(crate) x: Series,
pub(crate) y: Series,
pub(crate) color: Option<Color>,
pub(crate) edgecolor: Option<Color>,
pub(crate) alpha: f64,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl PolygonPlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn edgecolor(&mut self, color: Color) -> &mut Self {
self.edgecolor = Some(color);
self
}
pub fn alpha(&mut self, alpha: f64) -> &mut Self {
self.alpha = alpha.clamp(0.0, 1.0);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct AxHSpanPlot {
pub(crate) ymin: f64,
pub(crate) ymax: f64,
pub(crate) color: Option<Color>,
pub(crate) alpha: f64,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl AxHSpanPlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn alpha(&mut self, alpha: f64) -> &mut Self {
self.alpha = alpha.clamp(0.0, 1.0);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct AxVSpanPlot {
pub(crate) xmin: f64,
pub(crate) xmax: f64,
pub(crate) color: Option<Color>,
pub(crate) alpha: f64,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl AxVSpanPlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn alpha(&mut self, alpha: f64) -> &mut Self {
self.alpha = alpha.clamp(0.0, 1.0);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct TablePlot {
pub(crate) cells: Vec<Vec<String>>,
pub(crate) col_labels: Vec<String>,
pub(crate) row_labels: Vec<String>,
pub(crate) loc: TableLoc,
pub(crate) fontsize: f64,
pub(crate) cell_pad: f64,
pub(crate) edgecolor: Color,
pub(crate) facecolor: Color,
pub(crate) header_facecolor: Color,
pub(crate) color: Option<Color>,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl TablePlot {
pub fn col_labels<I, S>(&mut self, labels: I) -> &mut Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.col_labels = labels.into_iter().map(Into::into).collect();
self
}
pub fn row_labels<I, S>(&mut self, labels: I) -> &mut Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.row_labels = labels.into_iter().map(Into::into).collect();
self
}
pub fn loc(&mut self, loc: TableLoc) -> &mut Self {
self.loc = loc;
self
}
pub fn fontsize(&mut self, pt: f64) -> &mut Self {
self.fontsize = pt.max(5.0);
self
}
pub fn cell_pad(&mut self, pt: f64) -> &mut Self {
self.cell_pad = pt.max(0.0);
self
}
pub fn edgecolor(&mut self, color: Color) -> &mut Self {
self.edgecolor = color;
self.color = Some(color);
self
}
pub fn facecolor(&mut self, color: Color) -> &mut Self {
self.facecolor = color;
self
}
pub fn header_facecolor(&mut self, color: Color) -> &mut Self {
self.header_facecolor = color;
self
}
}
#[derive(Debug, Clone)]
pub struct TextPlot {
pub(crate) x: f64,
pub(crate) y: f64,
pub(crate) text: String,
pub(crate) color: Option<Color>,
pub(crate) size: f64,
pub(crate) align: TextAlign,
pub(crate) baseline: TextBaseline,
pub(crate) rotation_deg: f64,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl TextPlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn size(&mut self, size: f64) -> &mut Self {
self.size = size.max(1.0);
self
}
pub fn ha(&mut self, align: TextAlign) -> &mut Self {
self.align = align;
self
}
pub fn va(&mut self, baseline: TextBaseline) -> &mut Self {
self.baseline = baseline;
self
}
pub fn rotation(&mut self, degrees: f64) -> &mut Self {
self.rotation_deg = degrees;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ArrowStyle {
#[default]
Triangle,
Simple,
Bracket,
BothEnds,
}
#[derive(Debug, Clone)]
pub struct AnnotatePlot {
pub(crate) xy: (f64, f64),
pub(crate) xytext: (f64, f64),
pub(crate) text: String,
pub(crate) arrow: bool,
pub(crate) arrow_style: ArrowStyle,
pub(crate) color: Option<Color>,
pub(crate) arrow_color: Option<Color>,
pub(crate) arrow_width: f64,
pub(crate) size: f64,
pub(crate) align: TextAlign,
pub(crate) baseline: TextBaseline,
pub(crate) rotation_deg: f64,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl AnnotatePlot {
pub fn arrow(&mut self, on: bool) -> &mut Self {
self.arrow = on;
self
}
pub fn arrow_style(&mut self, style: ArrowStyle) -> &mut Self {
self.arrow_style = style;
self.arrow = true;
self
}
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn arrow_color(&mut self, color: Color) -> &mut Self {
self.arrow_color = Some(color);
self
}
pub fn arrow_width(&mut self, width: f64) -> &mut Self {
self.arrow_width = width.max(0.1);
self
}
pub fn size(&mut self, size: f64) -> &mut Self {
self.size = size.max(1.0);
self
}
pub fn ha(&mut self, align: TextAlign) -> &mut Self {
self.align = align;
self
}
pub fn va(&mut self, baseline: TextBaseline) -> &mut Self {
self.baseline = baseline;
self
}
pub fn rotation(&mut self, degrees: f64) -> &mut Self {
self.rotation_deg = degrees;
self
}
}
#[derive(Debug, Clone)]
pub struct TripcolorPlot {
pub(crate) x: Series,
pub(crate) y: Series,
pub(crate) z: Series,
pub(crate) triangles: Vec<[usize; 3]>,
pub(crate) cmap: Cmap,
pub(crate) vmin: Option<f64>,
pub(crate) vmax: Option<f64>,
pub(crate) norm: Norm,
pub(crate) colorbar: bool,
pub(crate) label: Option<String>,
}
impl TripcolorPlot {
pub fn triangles<I>(&mut self, tris: I) -> &mut Self
where
I: IntoIterator<Item = [usize; 3]>,
{
self.triangles = tris.into_iter().collect();
self
}
pub fn cmap(&mut self, cmap: impl Into<Cmap>) -> &mut Self {
self.cmap = cmap.into();
self
}
pub fn norm(&mut self, norm: Norm) -> &mut Self {
self.norm = norm;
self
}
pub fn vmin(&mut self, vmin: f64) -> &mut Self {
self.vmin = Some(vmin);
self
}
pub fn vmax(&mut self, vmax: f64) -> &mut Self {
self.vmax = Some(vmax);
self
}
pub fn colorbar(&mut self, show: bool) -> &mut Self {
self.colorbar = show;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
pub(crate) fn value_limits(&self) -> (f64, f64) {
if let (Some(lo), Some(hi)) = (self.vmin, self.vmax) {
return (lo.min(hi), lo.max(hi));
}
let (zmin, zmax) =
crate::recipes::tripcolor_face_limits(self.z.as_slice(), &self.triangles)
.unwrap_or((0.0, 1.0));
let lo = self.vmin.unwrap_or(zmin);
let hi = self.vmax.unwrap_or(zmax);
(lo.min(hi), lo.max(hi))
}
}
#[derive(Debug, Clone)]
pub struct TricontourPlot {
pub(crate) x: Series,
pub(crate) y: Series,
pub(crate) z: Series,
pub(crate) triangles: Vec<[usize; 3]>,
pub(crate) levels: Option<Vec<f64>>,
pub(crate) level_count: usize,
pub(crate) color: Option<Color>,
pub(crate) width: f64,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl TricontourPlot {
pub fn triangles<I>(&mut self, tris: I) -> &mut Self
where
I: IntoIterator<Item = [usize; 3]>,
{
self.triangles = tris.into_iter().collect();
self
}
pub fn level_values<I>(&mut self, levels: I) -> &mut Self
where
I: IntoIterator<Item = f64>,
{
self.levels = Some(levels.into_iter().filter(|v| v.is_finite()).collect());
self
}
pub fn levels(&mut self, n: usize) -> &mut Self {
self.level_count = n.max(1);
self
}
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn width(&mut self, width: f64) -> &mut Self {
self.width = width.max(0.1);
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}
#[derive(Debug, Clone)]
pub struct TricontourfPlot {
pub(crate) x: Series,
pub(crate) y: Series,
pub(crate) z: Series,
pub(crate) triangles: Vec<[usize; 3]>,
pub(crate) levels: Option<Vec<f64>>,
pub(crate) level_count: usize,
pub(crate) cmap: Cmap,
pub(crate) norm: Norm,
pub(crate) colorbar: bool,
pub(crate) label: Option<String>,
}
impl TricontourfPlot {
pub fn triangles<I>(&mut self, tris: I) -> &mut Self
where
I: IntoIterator<Item = [usize; 3]>,
{
self.triangles = tris.into_iter().collect();
self
}
pub fn level_values<I>(&mut self, levels: I) -> &mut Self
where
I: IntoIterator<Item = f64>,
{
self.levels = Some(levels.into_iter().filter(|v| v.is_finite()).collect());
self
}
pub fn levels(&mut self, n: usize) -> &mut Self {
self.level_count = n.max(2);
self
}
pub fn cmap(&mut self, cmap: impl Into<Cmap>) -> &mut Self {
self.cmap = cmap.into();
self
}
pub fn norm(&mut self, norm: Norm) -> &mut Self {
self.norm = norm;
self
}
pub fn colorbar(&mut self, show: bool) -> &mut Self {
self.colorbar = show;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
pub(crate) fn resolved_levels(&self) -> Vec<f64> {
if let Some(ref levels) = self.levels {
levels.clone()
} else {
crate::recipes::resolve_tri_levels(
self.z.as_slice(),
&self.triangles,
None,
self.level_count,
)
}
}
pub(crate) fn value_limits(&self) -> (f64, f64) {
let levels = self.resolved_levels();
if levels.len() >= 2 {
(
levels.iter().copied().fold(f64::INFINITY, f64::min),
levels.iter().copied().fold(f64::NEG_INFINITY, f64::max),
)
} else {
crate::recipes::tripcolor_face_limits(self.z.as_slice(), &self.triangles)
.unwrap_or((0.0, 1.0))
}
}
}
#[derive(Debug, Clone)]
pub struct AxLinePlot {
pub(crate) xy1: (f64, f64),
pub(crate) xy2: (f64, f64),
pub(crate) color: Option<Color>,
pub(crate) width: f64,
pub(crate) linestyle: LineStyle,
pub(crate) label: Option<String>,
pub(crate) color_index: usize,
}
impl_artist_props!(AxLinePlot, LegendKind::Line(LineStyle::Solid));
impl AxLinePlot {
pub fn color(&mut self, color: Color) -> &mut Self {
self.color = Some(color);
self
}
pub fn width(&mut self, width: f64) -> &mut Self {
self.width = width.max(0.1);
self
}
pub fn linestyle(&mut self, style: LineStyle) -> &mut Self {
self.linestyle = style;
self
}
pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
self.label = Some(label.into());
self
}
}