lichtung_plot/text/
position.rs1use super::layout::Finite;
4use crate::{coordinates::DataAreaCorner, Result};
5pub(crate) mod resolve;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct DataPosition {
10 pub(crate) x: DataCoordinate,
11 pub(crate) y: DataCoordinate,
12}
13impl DataPosition {
14 pub const fn new(x: DataCoordinate, y: DataCoordinate) -> Self {
16 Self { x, y }
17 }
18
19 #[must_use]
21 pub const fn get_x(&self) -> &DataCoordinate {
22 &self.x
23 }
24
25 #[must_use]
27 pub const fn get_y(&self) -> &DataCoordinate {
28 &self.y
29 }
30}
31
32#[derive(Debug, Clone, PartialEq, Eq)]
34pub struct DataCoordinate(pub(crate) DataValue);
35#[derive(Debug, Clone, PartialEq, Eq)]
36pub(crate) enum DataValue {
37 Numeric(Finite),
38 Category(String),
39 Series(String, String),
40}
41impl DataCoordinate {
42 pub fn numeric(value: f64) -> Result<Self> {
44 Ok(Self(DataValue::Numeric(Finite::new(value)?)))
45 }
46 pub fn category(raw: impl Into<String>) -> Self {
48 Self(DataValue::Category(raw.into()))
49 }
50 pub fn category_series(category: impl Into<String>, series: impl Into<String>) -> Self {
52 Self(DataValue::Series(category.into(), series.into()))
53 }
54
55 #[must_use]
57 pub const fn get_numeric_value(&self) -> Option<f64> {
58 match &self.0 {
59 DataValue::Numeric(value) => Some(value.get()),
60 DataValue::Category(_) | DataValue::Series(..) => None,
61 }
62 }
63
64 #[must_use]
66 pub fn get_category_value(&self) -> Option<&str> {
67 match &self.0 {
68 DataValue::Category(value) => Some(value),
69 DataValue::Numeric(_) | DataValue::Series(..) => None,
70 }
71 }
72
73 #[must_use]
75 pub fn get_category_series_value(&self) -> Option<(&str, &str)> {
76 match &self.0 {
77 DataValue::Series(category, series) => Some((category, series)),
78 DataValue::Numeric(_) | DataValue::Category(_) => None,
79 }
80 }
81}
82#[derive(Debug, Clone, PartialEq, Eq)]
84pub struct TextCoordinate(pub(crate) Coordinate);
85#[derive(Debug, Clone, PartialEq, Eq)]
86pub(crate) enum Coordinate {
87 Data(DataCoordinate),
88 Panel(Finite),
89 Figure(Finite),
90}
91impl TextCoordinate {
92 pub const fn data(value: DataCoordinate) -> Self {
94 Self(Coordinate::Data(value))
95 }
96 pub fn data_area_fraction(value: f64) -> Result<Self> {
98 Ok(Self(Coordinate::Panel(Finite::new(value)?)))
99 }
100 pub fn figure_fraction(value: f64) -> Result<Self> {
102 Ok(Self(Coordinate::Figure(Finite::new(value)?)))
103 }
104}
105#[derive(Debug, Clone, PartialEq, Eq)]
107pub struct TextPosition(pub(crate) Position);
108#[derive(Debug, Clone, PartialEq, Eq)]
109pub(crate) enum Position {
110 Coordinates(TextCoordinate, TextCoordinate),
111 Corner(DataAreaCorner),
112}
113impl TextPosition {
114 pub const fn new(x: TextCoordinate, y: TextCoordinate) -> Self {
116 Self(Position::Coordinates(x, y))
117 }
118 pub const fn corner(value: DataAreaCorner) -> Self {
120 Self(Position::Corner(value))
121 }
122 pub const fn as_corner(&self) -> Option<DataAreaCorner> {
124 match self.0 {
125 Position::Corner(value) => Some(value),
126 Position::Coordinates(..) => None,
127 }
128 }
129}