use crate::Error;
use crate::LinePattern;
use crate::{CategoryValues, Color, NumericValues};
#[derive(Debug, Clone, PartialEq)]
pub struct GuideStyle {
color: Color,
width: f32,
pattern: LinePattern,
}
impl GuideStyle {
pub fn new(color: Color, width: f32, pattern: LinePattern) -> Result<Self, Error> {
if !width.is_finite() || width <= 0.0 {
return Err(Error::Data(format!(
"guide width must be finite and strictly positive; got {width}"
)));
}
Ok(Self {
color,
width,
pattern,
})
}
pub const fn get_color(&self) -> Color {
self.color
}
pub const fn get_width(&self) -> f32 {
self.width
}
pub const fn get_pattern(&self) -> LinePattern {
self.pattern
}
pub fn color(self, color: Color) -> Result<Self, Error> {
Self::new(color, self.width, self.pattern)
}
pub fn width(self, width: f32) -> Result<Self, Error> {
Self::new(self.color, width, self.pattern)
}
pub fn pattern(self, pattern: LinePattern) -> Result<Self, Error> {
Self::new(self.color, self.width, pattern)
}
pub(crate) fn line_style(&self) -> crate::render::backend::LineStyle {
crate::render::backend::LineStyle {
color: self.color,
width: self.width,
pattern: self.pattern.stroke_pattern(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum GuideSpec {
Horizontal {
y: f64,
style: GuideStyle,
},
Vertical {
x: f64,
style: GuideStyle,
},
Segment {
start: (f64, f64),
end: (f64, f64),
style: GuideStyle,
},
}
impl GuideSpec {
#[must_use]
pub const fn horizontal(y: f64, style: GuideStyle) -> Self {
Self::Horizontal { y, style }
}
#[must_use]
pub const fn vertical(x: f64, style: GuideStyle) -> Self {
Self::Vertical { x, style }
}
#[must_use]
pub const fn segment(
x_start: f64,
y_start: f64,
x_end: f64,
y_end: f64,
style: GuideStyle,
) -> Self {
Self::Segment {
start: (x_start, y_start),
end: (x_end, y_end),
style,
}
}
}
#[derive(Debug, Clone)]
pub struct PointLayer {
pub(crate) label_layout: Option<crate::TextLayout>,
x: NumericValues,
y: NumericValues,
labels: Option<CategoryValues>,
legend_label: crate::Text,
color: Option<Color>,
radius: Option<u32>,
alpha: Option<u8>,
}
#[derive(Debug, Clone)]
pub struct BarPointLayer {
category: CategoryValues,
series: Option<CategoryValues>,
value: NumericValues,
color: Color,
radius: u32,
}
impl BarPointLayer {
pub fn new(
category: CategoryValues,
series: Option<CategoryValues>,
value: NumericValues,
color: Color,
radius: u32,
) -> Result<Self, Error> {
validate_point_radius("bar-point-layer", radius)?;
Ok(Self {
category,
series,
value,
color,
radius,
})
}
#[must_use]
pub const fn category_values(&self) -> &CategoryValues {
&self.category
}
#[must_use]
pub const fn series_values(&self) -> Option<&CategoryValues> {
self.series.as_ref()
}
#[must_use]
pub const fn values(&self) -> &NumericValues {
&self.value
}
#[must_use]
pub const fn color(&self) -> Color {
self.color
}
#[must_use]
pub const fn radius(&self) -> u32 {
self.radius
}
pub(crate) fn point_style(&self) -> crate::render::backend::PointStyle {
crate::render::backend::PointStyle {
color: self.color,
radius: self.radius,
outline: None,
shape: crate::PointShape::Circle,
}
}
}
impl PointLayer {
#[must_use]
pub fn label_text_layout(mut self, layout: crate::TextLayout) -> Self {
self.label_layout = Some(layout);
self
}
#[must_use]
pub fn new(x: NumericValues, y: NumericValues) -> Self {
Self {
x,
label_layout: None,
y,
labels: None,
legend_label: crate::Text::UNSET,
color: None,
radius: None,
alpha: None,
}
}
#[must_use]
pub fn color(mut self, color: Color) -> Self {
self.color = Some(color);
self
}
pub fn radius(mut self, radius: u32) -> Result<Self, Error> {
validate_point_radius("point-layer", radius)?;
self.radius = Some(radius);
Ok(self)
}
#[must_use]
pub const fn alpha(mut self, alpha: u8) -> Self {
self.alpha = Some(alpha);
self
}
pub const fn get_color(&self) -> Option<Color> {
self.color
}
pub const fn get_alpha(&self) -> Option<u8> {
self.alpha
}
pub const fn get_radius(&self) -> Option<u32> {
self.radius
}
#[must_use]
pub fn labels(mut self, labels: CategoryValues) -> Self {
self.labels = Some(labels);
self
}
#[must_use]
pub fn legend_label(mut self, label: impl Into<crate::Text>) -> Self {
self.legend_label
.replace_from(label.into())
.expect("public Text always has a source");
self
}
#[must_use]
pub fn clear_legend_label_style(mut self) -> Self {
self.legend_label.edit_clear_style();
self
}
#[must_use]
pub fn clear_legend_label_layout(mut self) -> Self {
self.legend_label.edit_clear_layout();
self
}
pub(crate) const fn x_values(&self) -> &NumericValues {
&self.x
}
pub(crate) const fn y_values(&self) -> &NumericValues {
&self.y
}
pub(crate) const fn label_values(&self) -> Option<&CategoryValues> {
self.labels.as_ref()
}
pub(crate) const fn legend_content(&self) -> Option<&crate::Text> {
self.legend_label.provided()
}
pub(crate) fn point_style(
&self,
automatic_color: (u8, u8, u8),
defaults: &crate::theme::mark::SupplementalPointTheme,
) -> crate::render::backend::PointStyle {
let color = self.color.unwrap_or_else(|| {
crate::theme::color_from_rgb(automatic_color).alpha(defaults.get_fill_alpha())
});
crate::render::backend::PointStyle {
color: self.alpha.map_or(color, |alpha| color.alpha(alpha)),
radius: self.radius.unwrap_or(defaults.get_radius()),
outline: None,
shape: crate::PointShape::Circle,
}
}
pub(crate) fn label_appearance(
&self,
role: &crate::theme::TextTheme,
) -> Result<crate::theme::TextTheme, Error> {
let paint = self.color.unwrap_or(role.get_color());
role.try_copy()?
.color(self.alpha.map_or(paint, |alpha| paint.alpha(alpha)))
}
}
fn validate_point_radius(role: &str, radius: u32) -> Result<(), Error> {
if radius == 0 {
return Err(Error::Data(format!(
"{role} radius must be greater than zero"
)));
}
Ok(())
}