Improve the API of the pie chart
This commit is contained in:
parent
6fb7749642
commit
0078f1db27
|
@ -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<Wedge>,
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue