diff --git a/src/graph.rs b/src/graph.rs index 85359d8..e55f303 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -11,12 +11,13 @@ pub struct Graph { pub scale_y_start: f64, pub scale_y_size: f64, pub draw_patch: bool, + pub auto_adjust_y: bool, pub lines: Vec } pub struct Line { - points: Vec<(f64,f64)>, - color: (f64, f64, f64) + pub points: Vec<(f64,f64)>, + pub color: (f64, f64, f64) } impl Line { @@ -35,6 +36,7 @@ impl Graph { scale_y_start: f64, scale_y_size: f64, draw_patch: bool, + auto_adjust_y: bool, lines: Vec) -> Rc> { let graph = Rc::new(RefCell::new(Graph { @@ -44,6 +46,7 @@ impl Graph { scale_y_start, scale_y_size, draw_patch, + auto_adjust_y, lines })); @@ -66,8 +69,11 @@ impl Graph { let width = area.get_allocated_width() as f64; let height = area.get_allocated_height() as f64; - graph.borrow_mut().adjust_scale_automatic_y(); - graph.borrow_mut().adjust_scale_automatic_x(); + graph.borrow_mut().trim_lines(); + if graph.borrow().auto_adjust_y { + graph.borrow_mut().adjust_scale_automatic_y(); + } + // graph.borrow_mut().adjust_scale_automatic_x(); Graph::draw_boxes(ctx, width, height, 40.0, 20.0, 5.0, 0.3); Graph::draw_boxes(ctx, width, height, 40.0, 20.0, 50.0, 0.1); @@ -147,11 +153,11 @@ impl Graph { for line in self.lines.iter() { if let None = mx { - mx = Some(line.points[0].0); + mx = Some(line.points[0].1); } if let None = mi { - mi = Some(line.points[0].0); + mi = Some(line.points[0].1); } for (_,y) in line.points.iter().skip(1) { @@ -160,13 +166,72 @@ impl Graph { } } - let spread = (mx.unwrap() - mi.unwrap()).abs(); + let mx = match mx { + Some(val) => val, + None => 0.0 + }; + + let mi = match mi { + Some(val) => val, + None => 0.0 + }; + + let spread = (mx - mi).abs(); - self.scale_y_start = mi.unwrap() - spread * 0.1; + self.scale_y_start = mi - spread * 0.1; self.scale_y_size = spread * 1.2; } pub fn adjust_scale_automatic_x(&mut self) { + let mut mx:Option = None; + let mut mi:Option = None; + + for line in self.lines.iter() { + if let None = mx { + mx = Some(line.points[0].0); + } + if let None = mi { + mi = Some(line.points[0].0); + } + + for (x,_) in line.points.iter().skip(1) { + mx = Some(f64::max(mx.unwrap(), *x)); + mi = Some(f64::min(mi.unwrap(), *x)); + } + } + + let spread = (mx.unwrap() - mi.unwrap()).abs(); + + if spread < self.scale_x_size { + self.scale_x_start = mi.unwrap(); + } else { + self.scale_x_start = mx.unwrap() - self.scale_x_size; + } + } + + pub fn trim_lines(&mut self) { + let mut j = 0; + while j < self.lines.len() { + let mut i = 0; + while i < self.lines[j].points.len() { + println!("{:?}", self.lines[j].points); + match self.lines[j].points.get(i + 2) { + Some(_) => { + if self.lines[j].points[i+1].0 < self.scale_x_start { + self.lines[j].points.remove(i); + } + }, + None => { + if self.lines[j].points[self.lines[j].points.len() - 1].0 < self.scale_x_start { + self.lines.remove(j); + break; + } + } + } + i += 1; + } + j += 1; + } } } diff --git a/src/lib.rs b/src/lib.rs index b718632..2d1edc0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,10 +47,11 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { 0.0, 100.0, 0.0, 100.0, true, + true, vec![ Line::new(1.0,1.0,0.0,vec![(10.0,10.0),(20.0,20.0),(30.0,25.0), (40.0, 50.0),(50.0,25.0)]), - Line::new(1.0,0.0,0.0,vec![(50.0,10.0),(70.0,60.0)]), - Line::new(0.0,1.0,0.0,vec![(50.0,50.0)]) + // Line::new(1.0,0.0,0.0,vec![(50.0,10.0),(70.0,60.0)]), + // Line::new(0.0,1.0,0.0,vec![(50.0,50.0)]) ] ); @@ -92,7 +93,9 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { // nimna_stambh let nimna_stambh = builder.get_object::("nimna_stambh").expect("Resource file missing!"); + let tmp_graph = Rc::clone(&graph); nimna_stambh.connect_clicked(move |btn| { + tmp_graph.borrow_mut().auto_adjust_y = !btn.get_active(); stambh_1.set_sensitive(btn.get_active()); stambh_2.set_sensitive(btn.get_active()); }); @@ -294,13 +297,14 @@ pub fn build_ui(app: >k::Application, config: Arc::>) { // let (sender, receiver) = glib::MainContext::channel(glib::PRIORITY_DEFAULT); - // glib::timeout_add(100, move || { + // glib::timeout_add(300, move || { // sender.send(()).unwrap(); // glib::Continue(true) // }); // let tmp_graph = Rc::clone(&graph); // receiver.attach(None, move |_| { + // // println!("{:?}", tmp_graph.borrow_mut().lines[0].points); // tmp_graph.borrow_mut().scale_x_start += 1.0; // tmp_graph.borrow().area.queue_draw(); // glib::Continue(true) diff --git a/tests/plottest.rs b/tests/plottest.rs index c3b0b5f..b68374a 100644 --- a/tests/plottest.rs +++ b/tests/plottest.rs @@ -1,8 +1,34 @@ #[test] fn start() { - let a = [0,1,2,3,4,5,6,7,8,9]; - for x in a.iter().skip(1) { - println!("{}", x); - } + let a = vec![0,1,2,3,4,5,6,7,8,9]; + + let mut skiped = false; + let b: Vec = a.into_iter().filter(|x| { + if *x <= 4 { + return false; + } + + if !skiped { + skiped = true; + return false; + } + + return true; + }).collect(); + + println!("{:?}", b); + + // for (i, x) in a.iter().enumerate() { + // if *x <= 4 { + // continue; + // } + + // if !skiped { + // skiped = true; + // continue; + // } + + // a.remove(i); + // } } \ No newline at end of file