From 0078f1db273b0c78ad04c9c61a36e702d1cda986 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Wed, 9 Aug 2023 11:03:08 -0400 Subject: [PATCH] Improve the API of the pie chart --- dashboard/src/drawing/pie_chart.rs | 36 ++++++++++++++---------------- dashboard/src/main.rs | 6 ++--- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/dashboard/src/drawing/pie_chart.rs b/dashboard/src/drawing/pie_chart.rs index 4d394d2..4b0e7bb 100644 --- a/dashboard/src/drawing/pie_chart.rs +++ b/dashboard/src/drawing/pie_chart.rs @@ -1,4 +1,4 @@ -use cairo::{Context, Format, ImageSurface}; +use cairo::Context; use std::f64::consts::PI; #[derive(Clone, Debug)] @@ -18,8 +18,9 @@ pub struct Wedge { pub struct PieChart { rotation: f64, wedges: Vec, - width: i32, - height: i32, + center_x: f64, + center_y: f64, + radius: f64, } impl PieChart { @@ -27,8 +28,9 @@ impl PieChart { Self { rotation: 0., wedges: vec![], - width: 0, - height: 0, + center_x: 0., + center_y: 0., + radius: 0., } } @@ -43,22 +45,18 @@ impl PieChart { self } - pub fn width(mut self, width: i32) -> Self { - self.width = width; + pub fn center(mut self, center_x: f64, center_y: f64) -> Self { + self.center_x = center_x; + self.center_y = center_y; self } - pub fn height(mut self, height: i32) -> Self { - self.height = height; + pub fn radius(mut self, radius: f64) -> Self { + self.radius = radius; self } pub fn draw(self, context: &Context) { - println!("drawing: {} {}", self.width, self.height); - let radius = self.width.min(self.height) as f64 / 2.; - let center_x = (self.width / 2) as f64; - let center_y = (self.height / 2) as f64; - context.set_source_rgba(0., 0., 0., 0.); let _ = context.paint(); @@ -80,12 +78,12 @@ impl PieChart { end_angle + self.rotation, self.rotation ); - context.move_to(center_x, center_y); + context.move_to(self.center_x, self.center_y); context.set_source_rgb(color.r, color.g, color.b); context.arc( - center_x, - center_y, - radius, + self.center_x, + self.center_y, + self.radius, start_angle + self.rotation, end_angle + self.rotation, ); @@ -94,7 +92,7 @@ impl PieChart { ); context.set_source_rgb(1., 1., 1.); - context.arc(center_x, center_y, radius, 0., 2. * PI); + context.arc(self.center_x, self.center_y, self.radius, 0., 2. * PI); let _ = context.stroke(); } } diff --git a/dashboard/src/main.rs b/dashboard/src/main.rs index ecf1ec7..88fc830 100644 --- a/dashboard/src/main.rs +++ b/dashboard/src/main.rs @@ -259,8 +259,8 @@ impl TransitClock { let sunset = info.sunset - NaiveTime::from_hms_opt(0, 0, 0).unwrap(); PieChart::new() - .width(width) - .height(height) + .center(center_x, center_y) + .radius(radius) .rotation(-PI / 2.) .wedges( vec![Wedge { @@ -280,7 +280,7 @@ impl TransitClock { context.set_source_rgb(0., 0., 0.); context.translate(center_x, center_y); context.rotate(tick as f64 * (PI / 12.)); - context.move_to(radius, 0.); + context.move_to(radius - 5., 0.); context.line_to(radius - 10., 0.); let _ = context.stroke(); context.identity_matrix();