oino_ui/widgets/
sparkline.rs1use serde::{Deserialize, Serialize};
4
5use crate::node::{Common, IntoNode, Node};
6
7#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
9pub enum SparkDirection {
10 #[default]
12 LeftToRight,
13 RightToLeft,
15}
16
17#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
19pub enum BarSet {
20 #[default]
22 Nine,
23 Three,
25}
26
27#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
29#[serde(rename_all = "camelCase")]
30pub struct Sparkline {
31 #[serde(flatten)]
33 pub common: Common,
34 pub data: Vec<Option<u64>>,
36 #[serde(default, skip_serializing_if = "Option::is_none")]
38 pub max: Option<u64>,
39 #[serde(default, skip_serializing_if = "Option::is_none")]
41 pub direction: Option<SparkDirection>,
42 #[serde(default, skip_serializing_if = "Option::is_none")]
44 pub bar_set: Option<BarSet>,
45 #[serde(default, skip_serializing_if = "Option::is_none")]
47 pub absent_value_symbol: Option<String>,
48}
49
50impl Sparkline {
51 #[must_use]
53 pub fn new(data: impl IntoIterator<Item = Option<u64>>) -> Self {
54 Self {
55 data: data.into_iter().collect(),
56 ..Self::default()
57 }
58 }
59
60 #[must_use]
62 pub fn max(mut self, max: u64) -> Self {
63 self.max = Some(max);
64 self
65 }
66
67 #[must_use]
69 pub fn direction(mut self, direction: SparkDirection) -> Self {
70 self.direction = Some(direction);
71 self
72 }
73
74 #[must_use]
76 pub fn bar_set(mut self, bar_set: BarSet) -> Self {
77 self.bar_set = Some(bar_set);
78 self
79 }
80
81 #[must_use]
83 pub fn absent_value_symbol(mut self, symbol: impl Into<String>) -> Self {
84 self.absent_value_symbol = Some(symbol.into());
85 self
86 }
87}
88
89impl IntoNode for Sparkline {}
90impl From<Sparkline> for Node {
91 fn from(s: Sparkline) -> Self {
92 Node::Sparkline(s)
93 }
94}