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;
|
use std::f64::consts::PI;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
@ -18,8 +18,9 @@ pub struct Wedge {
|
||||||
pub struct PieChart {
|
pub struct PieChart {
|
||||||
rotation: f64,
|
rotation: f64,
|
||||||
wedges: Vec<Wedge>,
|
wedges: Vec<Wedge>,
|
||||||
width: i32,
|
center_x: f64,
|
||||||
height: i32,
|
center_y: f64,
|
||||||
|
radius: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PieChart {
|
impl PieChart {
|
||||||
|
@ -27,8 +28,9 @@ impl PieChart {
|
||||||
Self {
|
Self {
|
||||||
rotation: 0.,
|
rotation: 0.,
|
||||||
wedges: vec![],
|
wedges: vec![],
|
||||||
width: 0,
|
center_x: 0.,
|
||||||
height: 0,
|
center_y: 0.,
|
||||||
|
radius: 0.,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,22 +45,18 @@ impl PieChart {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn width(mut self, width: i32) -> Self {
|
pub fn center(mut self, center_x: f64, center_y: f64) -> Self {
|
||||||
self.width = width;
|
self.center_x = center_x;
|
||||||
|
self.center_y = center_y;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn height(mut self, height: i32) -> Self {
|
pub fn radius(mut self, radius: f64) -> Self {
|
||||||
self.height = height;
|
self.radius = radius;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(self, context: &Context) {
|
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.);
|
context.set_source_rgba(0., 0., 0., 0.);
|
||||||
let _ = context.paint();
|
let _ = context.paint();
|
||||||
|
|
||||||
|
@ -80,12 +78,12 @@ impl PieChart {
|
||||||
end_angle + self.rotation,
|
end_angle + self.rotation,
|
||||||
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.set_source_rgb(color.r, color.g, color.b);
|
||||||
context.arc(
|
context.arc(
|
||||||
center_x,
|
self.center_x,
|
||||||
center_y,
|
self.center_y,
|
||||||
radius,
|
self.radius,
|
||||||
start_angle + self.rotation,
|
start_angle + self.rotation,
|
||||||
end_angle + self.rotation,
|
end_angle + self.rotation,
|
||||||
);
|
);
|
||||||
|
@ -94,7 +92,7 @@ impl PieChart {
|
||||||
);
|
);
|
||||||
|
|
||||||
context.set_source_rgb(1., 1., 1.);
|
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();
|
let _ = context.stroke();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -259,8 +259,8 @@ impl TransitClock {
|
||||||
let sunset = info.sunset - NaiveTime::from_hms_opt(0, 0, 0).unwrap();
|
let sunset = info.sunset - NaiveTime::from_hms_opt(0, 0, 0).unwrap();
|
||||||
|
|
||||||
PieChart::new()
|
PieChart::new()
|
||||||
.width(width)
|
.center(center_x, center_y)
|
||||||
.height(height)
|
.radius(radius)
|
||||||
.rotation(-PI / 2.)
|
.rotation(-PI / 2.)
|
||||||
.wedges(
|
.wedges(
|
||||||
vec![Wedge {
|
vec![Wedge {
|
||||||
|
@ -280,7 +280,7 @@ impl TransitClock {
|
||||||
context.set_source_rgb(0., 0., 0.);
|
context.set_source_rgb(0., 0., 0.);
|
||||||
context.translate(center_x, center_y);
|
context.translate(center_x, center_y);
|
||||||
context.rotate(tick as f64 * (PI / 12.));
|
context.rotate(tick as f64 * (PI / 12.));
|
||||||
context.move_to(radius, 0.);
|
context.move_to(radius - 5., 0.);
|
||||||
context.line_to(radius - 10., 0.);
|
context.line_to(radius - 10., 0.);
|
||||||
let _ = context.stroke();
|
let _ = context.stroke();
|
||||||
context.identity_matrix();
|
context.identity_matrix();
|
||||||
|
|
Loading…
Reference in New Issue