feat: Add option for graph drawing direction (#1113)

* feat: Add option for vertical graph

* doc: Add feature to changelog
This commit is contained in:
Leni Aniva 2024-08-25 10:15:43 -04:00 committed by GitHub
parent dc4def5327
commit 452cab7677
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 39 additions and 6 deletions

View file

@ -20,6 +20,7 @@ All notable changes to eww will be listed here, starting at changes since versio
- Add support for `:hover` css selectors for tray items (By: zeapoz) - Add support for `:hover` css selectors for tray items (By: zeapoz)
- Add scss support for the `:style` widget property (By: ovalkonia) - Add scss support for the `:style` widget property (By: ovalkonia)
- Add `min` and `max` function calls to simplexpr (By: ovalkonia) - Add `min` and `max` function calls to simplexpr (By: ovalkonia)
- Add `flip-x`, `flip-y`, `vertical` options to the graph widget to determine its direction
## [0.6.0] (21.04.2024) ## [0.6.0] (21.04.2024)

View file

@ -38,6 +38,13 @@ pub struct GraphPriv {
#[property(get, set, nick = "Time Range", blurb = "The Time Range", minimum = 0u64, maximum = u64::MAX, default = 10u64)] #[property(get, set, nick = "Time Range", blurb = "The Time Range", minimum = 0u64, maximum = u64::MAX, default = 10u64)]
time_range: RefCell<u64>, time_range: RefCell<u64>,
#[property(get, set, nick = "Flip X", blurb = "Flip the x axis", default = true)]
flip_x: RefCell<bool>,
#[property(get, set, nick = "Flip Y", blurb = "Flip the y axis", default = true)]
flip_y: RefCell<bool>,
#[property(get, set, nick = "Vertical", blurb = "Exchange the x and y axes", default = false)]
vertical: RefCell<bool>,
history: RefCell<VecDeque<(std::time::Instant, f64)>>, history: RefCell<VecDeque<(std::time::Instant, f64)>>,
extra_point: RefCell<Option<(std::time::Instant, f64)>>, extra_point: RefCell<Option<(std::time::Instant, f64)>>,
last_updated_at: RefCell<std::time::Instant>, last_updated_at: RefCell<std::time::Instant>,
@ -53,6 +60,9 @@ impl Default for GraphPriv {
max: RefCell::new(100.0), max: RefCell::new(100.0),
dynamic: RefCell::new(true), dynamic: RefCell::new(true),
time_range: RefCell::new(10), time_range: RefCell::new(10),
flip_x: RefCell::new(true),
flip_y: RefCell::new(true),
vertical: RefCell::new(false),
history: RefCell::new(VecDeque::new()), history: RefCell::new(VecDeque::new()),
extra_point: RefCell::new(None), extra_point: RefCell::new(None),
last_updated_at: RefCell::new(std::time::Instant::now()), last_updated_at: RefCell::new(std::time::Instant::now()),
@ -77,6 +87,16 @@ impl GraphPriv {
} }
history.push_back(v); history.push_back(v);
} }
/**
* Receives normalized (0-1) coordinates `x` and `y` and convert them to the
* point on the widget.
*/
fn value_to_point(&self, width: f64, height: f64, x: f64, y: f64) -> (f64, f64) {
let x = if *self.flip_x.borrow() { 1.0 - x } else { x };
let y = if *self.flip_y.borrow() { 1.0 - y } else { y };
let (x, y) = if *self.vertical.borrow() { (y, x) } else { (x, y) };
(width * x, height * y)
}
} }
impl ObjectImpl for GraphPriv { impl ObjectImpl for GraphPriv {
@ -110,6 +130,15 @@ impl ObjectImpl for GraphPriv {
"line-style" => { "line-style" => {
self.line_style.replace(value.get().unwrap()); self.line_style.replace(value.get().unwrap());
} }
"flip-x" => {
self.flip_x.replace(value.get().unwrap());
}
"flip-y" => {
self.flip_y.replace(value.get().unwrap());
}
"vertical" => {
self.vertical.replace(value.get().unwrap());
}
x => panic!("Tried to set inexistant property of Graph: {}", x,), x => panic!("Tried to set inexistant property of Graph: {}", x,),
} }
} }
@ -214,18 +243,15 @@ impl WidgetImpl for GraphPriv {
.iter() .iter()
.map(|(instant, value)| { .map(|(instant, value)| {
let t = last_updated_at.duration_since(*instant).as_millis() as f64; let t = last_updated_at.duration_since(*instant).as_millis() as f64;
let x = width * (1.0 - (t / time_range)); self.value_to_point(width, height, t / time_range, (value - min) / value_range)
let y = height * (1.0 - ((value - min) / value_range));
(x, y)
}) })
.collect::<VecDeque<(f64, f64)>>(); .collect::<VecDeque<(f64, f64)>>();
// Aad an extra point outside of the graph to extend the line to the left // Aad an extra point outside of the graph to extend the line to the left
if let Some((instant, value)) = extra_point { if let Some((instant, value)) = extra_point {
let t = last_updated_at.duration_since(instant).as_millis() as f64; let t = last_updated_at.duration_since(instant).as_millis() as f64;
let x = -width * ((t - time_range) / time_range); let (x, y) = self.value_to_point(width, height, (t - time_range) / time_range, (value - min) / value_range);
let y = height * (1.0 - ((value - min) / value_range)); points.push_front(if *self.vertical.borrow() { (x, -y) } else { (-x, y) });
points.push_front((x, y));
} }
points points
}; };

View file

@ -1246,6 +1246,12 @@ fn build_graph(bargs: &mut BuilderArgs) -> Result<super::graph::Graph> {
// @prop line-style - changes the look of the edges in the graph. Values: "miter" (default), "round", // @prop line-style - changes the look of the edges in the graph. Values: "miter" (default), "round",
// "bevel" // "bevel"
prop(line_style: as_string) { w.set_property("line-style", line_style); }, prop(line_style: as_string) { w.set_property("line-style", line_style); },
// @prop flip-x - whether the x axis should go from high to low
prop(flip_x: as_bool) { w.set_property("flip-x", flip_x); },
// @prop flip-y - whether the y axis should go from high to low
prop(flip_y: as_bool) { w.set_property("flip-y", flip_y); },
// @prop vertical - if set to true, the x and y axes will be exchanged
prop(vertical: as_bool) { w.set_property("vertical", vertical); },
}); });
Ok(w) Ok(w)
} }