plotly_types/
lib.rs

1use serde::Serialize;
2
3#[derive(Serialize)]
4pub struct ColorScaleElem<'s>(f64, &'s str);
5
6impl<'s> ColorScaleElem<'s> {
7    pub fn new(level: f64, color: &'s str) -> Self {
8        Self(level, color)
9    }
10}
11
12#[derive(Serialize)]
13pub enum ColorScaleName {
14    Greys,
15    YlGnBu,
16    Greens,
17    YlOrRd,
18    Bluered,
19    RdBu,
20    Reds,
21    Blues,
22    Picnic,
23    Rainbow,
24    Portland,
25    Jet,
26    Hot,
27    Blackbody,
28    Earth,
29    Electric,
30    Viridis,
31    Cividis,
32}
33
34#[derive(Serialize)]
35#[serde(untagged)]
36pub enum ColorScale<'a> {
37    Name(ColorScaleName),
38    Array(&'a [ColorScaleElem<'a>]),
39}
40
41pub type Any = serde_json::Value;
42pub type InfoArray = Vec<serde_json::Value>;
43pub type Flaglist<'a> = &'a str;
44pub type Angle = f64;
45
46struct IsEmpty<T> {
47    pub data: T,
48    pub is_empty: bool,
49}
50
51impl<T: serde::ser::Serialize> serde::ser::Serialize for IsEmpty<T> {
52    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
53    where
54        S: serde::Serializer,
55    {
56        self.data.serialize(serializer)
57    }
58}
59
60impl<T: Default> Default for IsEmpty<T> {
61    fn default() -> Self {
62        Self {
63            data: T::default(),
64            is_empty: true,
65        }
66    }
67}
68
69impl<T> IsEmpty<T> {
70    pub fn is_empty(&self) -> bool {
71        self.is_empty
72    }
73}
74
75include!(concat!(env!("OUT_DIR"), "/mod.rs"));
76mod error;
77pub use error::Error;
78
79pub struct Plot<'a, W, D> {
80    w: W,
81    graph_div: D,
82    layout: &'a layout::Layout<'a>,
83    config: &'a config::Config<'a>,
84    ntraces: usize,
85}
86
87impl<'a, W, D> Plot<'a, W, D>
88where
89    W: std::io::Write,
90    D: AsRef<str>,
91{
92    pub fn new(
93        mut w: W,
94        graph_div: D,
95        layout: &'a layout::Layout,
96        config: &'a config::Config,
97    ) -> Result<Self, Error> {
98        write!(w, "var data = [")?;
99        Ok(Self {
100            w,
101            graph_div,
102            layout,
103            config,
104            ntraces: 0,
105        })
106    }
107
108    pub fn add_trace<T>(&mut self, trace: T) -> Result<(), Error>
109    where
110        T: serde::Serialize,
111    {
112        if self.ntraces > 0 {
113            write!(self.w, ", ")?;
114        }
115
116        serde_json::to_writer(&mut self.w, &trace)?;
117        self.ntraces += 1;
118
119        Ok(())
120    }
121
122    pub fn finish(mut self) -> Result<(), Error> {
123        writeln!(self.w, "];")?;
124
125        write!(self.w, "var layout = ")?;
126        serde_json::to_writer(&mut self.w, self.layout)?;
127        writeln!(self.w, ";")?;
128
129        write!(self.w, "var config = ")?;
130        serde_json::to_writer(&mut self.w, self.config)?;
131        writeln!(self.w, ";")?;
132
133        writeln!(
134            self.w,
135            "Plotly.newPlot(\"{}\", data, layout, config);",
136            self.graph_div.as_ref()
137        )?;
138
139        Ok(())
140    }
141}