liftof_tui/
widgets.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
use std::collections::VecDeque;

use ratatui::symbols;
use ratatui::symbols::line::*;
use ratatui::text::Span;
use ratatui::style::{
    Modifier,
    Color,
    Style,
};
use ratatui::widgets::{
    Axis,
    Block,
    BorderType,
    BarChart,
    GraphType,
    Dataset,
    Chart,
    LineGauge,
    Borders,
};

//extern crate ndhistogram;
use ndhistogram::{
    //ndhistogram,
    Histogram,
    Hist1D,
};
use ndhistogram::axis::{
    Uniform,
};

pub const LG_LINE_HORIZONTAL : &str = "░";
pub const LG_LINE: Set = Set {
        vertical         : THICK_VERTICAL,
        //horizontal       : THICK_HORIZONTAL,
        horizontal       : LG_LINE_HORIZONTAL,
        top_right        : THICK_TOP_RIGHT,
        top_left         : THICK_TOP_LEFT,
        bottom_right     : THICK_BOTTOM_RIGHT,
        bottom_left      : THICK_BOTTOM_LEFT,
        vertical_left    : THICK_VERTICAL_LEFT,
        vertical_right   : THICK_VERTICAL_RIGHT,
        horizontal_down  : THICK_HORIZONTAL_DOWN,
        horizontal_up    : THICK_HORIZONTAL_UP,
        cross            : THICK_CROSS,
};

//extern crate num_traits;
//use num_traits::Num;

use crate::colors::{
  ColorTheme,
};


//#[derive(Debug, Clone)]
//struct HistoDisplay {
//  pub nbin     : usize,
//  pub bin_low  : f32,
//  pub bin_high : f32,
//  pub histo    : Hist1D<Uniform<f32>>,
//}
//
//impl HistoDisplay {
//  pub fn new(nbin : usize, bin_low : f32, bin_high : f32) -> Self {
//    let bins = Uniform::new(nbin, bin_low, bin_high);  
//    Self {
//      nbin     : nbin,
//      bin_low  : bin_low,
//      bin_high : bin_high,
//      histo    : ndhistogram!(bins), 
//    }
//  }
//}


/// Adapt the bins of the histogram for the 
/// bar chart which will get rendered.
/// Always show a minimum number of bins, 
/// but if the max y-bin is "too far to the left"
/// then shorten the range for a better visualization
///
/// # Arguments
///
/// * labels       : bin labels for rendering
/// * clean_from   : leave bins below this 
///                  untouched
pub fn clean_data<'a>(histo      : &'a Hist1D<Uniform<f32>>, 
                      labels     : &'a Vec<String>, 
                      clean_from : usize) -> Vec<(&'a str,u64)> {
  let mut max_pop_bin = 0;
  let mut vec_index   = 0;
  let mut bins = Vec::<(u64, u64)>::new();
  for bin in histo.iter() {
    let bin_value = *bin.value as u64;
    bins.push((bin.index as u64, bin_value));
    // always show the first x bins, but if 
    // the bins with index > clean_from are not 
    // populated, discard them
    if bin_value > 0 && bin.index > clean_from {
      max_pop_bin = vec_index;
    }
    vec_index += 1;
  }
  bins.retain(|&(x,_)| x <= max_pop_bin);
  let mut clean_data = Vec::<(&str, u64)>::new();
  for n in bins.iter() {
    clean_data.push((&labels[n.0 as usize], n.1));
  }
  clean_data
}

/// Create the labels for a certain histogram
/// for rendering
pub fn create_labels(histo : &Hist1D<Uniform<f32>>) -> Vec<String> {
  let mut labels = Vec::<String>::new();
  for bin in histo.iter() {
    match bin.bin.start() {
      None => {
        labels.push(String::from("x"));
      },
      Some(value) => {
        labels.push(format!("{}", value as u64));
      }
    }
  }
  labels
}

// FIXME - merge this with clean data
/// Prepare data for histogram widget
///
/// # Arguments:
///
/// * remove_uf   : Remove underflow bin
pub fn prep_data<'a>(histo      : &'a Hist1D<Uniform<f32>>, 
                     labels     : &'a Vec<String>,
                     spacing    : usize,
                     remove_uf  : bool) -> Vec<(&'a str,u64)> {
  let mut data = Vec::<(&str, u64)>::new();
  for (k,bin) in histo.iter().enumerate() {
    if k == 0 && remove_uf {
      continue;
    }
    if k == 1 && remove_uf {
      data.push((&labels[k], *bin.value as u64));
      continue;
    }
    // k+1 to account for underflow bin
    if k % spacing != 0 {
      data.push(("-", *bin.value as u64));
    } else {
      data.push((&labels[k], *bin.value as u64));
    }
  }
  data
}

pub fn histogram<'a>(hist_data : Vec<(&'a str, u64)>,
                     title     : String,
                     bar_width : u16,
                     bar_gap   : u16,
                     theme     : &ColorTheme) -> BarChart<'a> {
  //let bins = Uniform::new(nbin, bin_low, bin_high);
  //let mut histo = ndhistogram!(bins);
  //for k in data {
  //  histo.fill(&k);
  //}
  let chart  = BarChart::default()
    .block(Block::default().title(title).borders(Borders::ALL))
    .data(hist_data.as_slice())
    .bar_width(bar_width)
    .bar_gap(bar_gap)
    //.bar_style(Style::default().fg(Color::Blue))
    .bar_style(theme.style())
    .value_style(
      theme.style()
      //Style::default()
      //.bg(Color::Blue)
      .add_modifier(Modifier::BOLD),
    )
    .style(theme.background());
  chart
}

pub fn timeseries<'a>(data        : &'a mut VecDeque<(f64,f64)>,
                      ds_name     : String,
                      xlabel      : String,
                      theme       : &'a ColorTheme) -> Chart<'a> {
  let x_only : Vec::<f64> = data.iter().map(|z| z.0).collect();
  // get timing axis
  let t_min : u64;
  let mut t_max : u64;
  if x_only.len() == 0 {
    t_min = 0;
    t_max = 0;
  } else {   
    t_min = x_only[0] as u64;
    t_max = x_only[x_only.len() -1] as u64;
  }
  t_max += (0.05*t_max as f64).round() as u64;
  let t_spacing = (t_max - t_min)/10;
  let mut t_labels = Vec::<Span>::new();
  for k in 0..10 {
    let _label = format!("{}", (t_min + t_spacing * k as u64));
    t_labels.push(Span::from(_label));
  }

  let y_only : Vec::<f64> = data.iter().map(|z| z.1).collect();
  let mut y_min = f64::MAX;
  let mut y_max = f64::MIN;
  if y_only.len() == 0 {
    y_max = 0.0;
    y_min = 0.0;
  }
  for y in y_only {
    if y < y_min {
      y_min = y;
    }
    if y > y_max {
      y_max = y;
    }
  }
  y_max += f64::abs(y_max)*0.05;
  y_min -= f64::abs(y_min)*0.05;
  let y_spacing = f64::abs(y_max - y_min)/5.0;
  let mut y_labels = Vec::<Span>::new() ;
  let mut precision = 0u8;
  if f64::abs(y_max - y_min) <= 10.0 {
    precision = 1;
  }
  if f64::abs(y_max - y_min) <= 1.0 {
    precision = 2;
  }
  for k in 0..5 {
    match precision {
      0 => {
        let _label = format!("{}", (y_min + y_spacing * k as f64).round() as i64);
        y_labels.push(Span::from(_label));
      },
      1 => {
        let _label = format!("{:.1}", (y_min + y_spacing * k as f64));
        y_labels.push(Span::from(_label));
      },
      2 => {
        let _label = format!("{:.2}", (y_min + y_spacing * k as f64));
        y_labels.push(Span::from(_label));
      },
      _ => (),
    }
  }

  let dataset = vec![Dataset::default()
      .name(ds_name)
      .marker(symbols::Marker::Braille)
      .graph_type(GraphType::Line)
      .style(theme.style())
      .data(data.make_contiguous())];
  let chart = Chart::new(dataset)
    .block(
      Block::default()
        .borders(Borders::ALL)
        .style(theme.style())
        .title(xlabel )
        .border_type(BorderType::Rounded),
    )
    .x_axis(Axis::default()
      .title(Span::styled("MET [s]", Style::default().fg(Color::White)))
      .style(theme.style())
      .bounds([t_min as f64, t_max as f64])
      //.labels(t_labels.clone().iter().cloned().map(Span::from).collect()))
      .labels(t_labels.clone())
    )
    .y_axis(Axis::default()
      //.title(Span::styled("T [\u{00B0}C]", Style::default().fg(Color::White)))
      .style(theme.style())
      .bounds([y_min as f64, y_max as f64])
      //.labels(y_labels.clone().iter().cloned().map(Span::from).collect()))
      .labels(y_labels.clone())
    )
    .style(theme.style());
    chart
}


/// A simple line gauge, that is bacically a progress bar
pub fn gauge(title : String,
             label : String,
             ratio : f64,
             theme : &ColorTheme) -> LineGauge {
    let gauge = LineGauge::default()
      .block(
        Block::default()
        .borders(Borders::ALL)
        .style(theme.style())
        .title(title)
        .border_type(BorderType::Rounded)
      )
      .filled_style(
        Style::default()
          .fg(theme.hc)
          .bg(theme.bg1)
          .add_modifier(Modifier::BOLD)
      )
      //.use_unicode(true)
      .label(label)
      //.line_set(symbols::line::THICK)  // THICK
      .line_set(LG_LINE)
      //.percent(self.disk_usage as u16);
      .ratio(ratio);
    gauge
}