Skip to content

Commit cae965d

Browse files
committed
improve DonutChart draw
1 parent 160cc2a commit cae965d

2 files changed

Lines changed: 13 additions & 17 deletions

File tree

src/chart/types/donut_chart.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::gui::styles::style_constants::{FONT_SIZE_FOOTER, FONT_SIZE_SUBTITLE,
33
use crate::networking::types::data_representation::DataRepr;
44
use iced::alignment::Vertical;
55
use iced::widget::canvas::path::Arc;
6-
use iced::widget::canvas::{Frame, Text};
6+
use iced::widget::canvas::{Frame, Stroke, Text};
77
use iced::widget::text::Alignment;
88
use iced::widget::{Canvas, canvas};
99
use iced::{Length, Radians, Renderer, mouse};
@@ -91,28 +91,34 @@ impl<Message, Theme: Catalog> canvas::Program<Message, Theme> for DonutChart {
9191
) -> Vec<canvas::Geometry> {
9292
let mut frame = Frame::new(renderer, bounds.size());
9393
let center = frame.center();
94-
let radius = (frame.width().min(frame.height()) / 2.0) * 0.9;
94+
95+
let thickness = 6.0;
96+
let radius = (frame.width().min(frame.height()) / 2.0) * 0.9 - thickness / 2.0;
9597

9698
let style = <Theme as Catalog>::style(theme, &<Theme as Catalog>::default());
9799
let colors = [style.incoming, style.outgoing, style.dropped];
98100

99101
for ((start_angle, end_angle), color) in self.angles().into_iter().zip(colors) {
100102
let path = canvas::Path::new(|builder| {
103+
// build path using just an arc with thickness, because iced's paths are limited:
104+
// - no single close path can be constructed as the one we want (drawing an arc starts a new sub-path)
105+
// - counter-clockwise arcs are not supported
101106
builder.arc(Arc {
102107
center,
103108
radius,
104109
start_angle,
105110
end_angle,
106111
});
107-
builder.line_to(center);
108-
builder.close();
109112
});
110113

111-
frame.fill(&path, color);
114+
let stroke = Stroke {
115+
style: canvas::stroke::Style::Solid(color),
116+
width: thickness,
117+
..Default::default()
118+
};
119+
frame.stroke(&path, stroke);
112120
}
113121

114-
let inner_circle = canvas::Path::circle(center, radius - 6.0);
115-
frame.fill(&inner_circle, style.background);
116122
frame.fill_text(Text {
117123
content: self.title().clone(),
118124
position: center,

src/gui/styles/donut.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,7 @@ impl DonutType {
1212
fn active(&self, style: &StyleType) -> Style {
1313
let colors = style.get_palette();
1414
let ext = style.get_extension();
15-
let primary = colors.primary;
16-
let buttons = ext.buttons_color;
17-
let background = Color {
18-
r: primary.r + (buttons.r - primary.r) * ext.alpha_round_containers,
19-
g: primary.g + (buttons.g - primary.g) * ext.alpha_round_containers,
20-
b: primary.b + (buttons.b - primary.b) * ext.alpha_round_containers,
21-
a: 1.0,
22-
};
2315
Style {
24-
background,
2516
incoming: colors.secondary,
2617
outgoing: colors.outgoing,
2718
text_color: colors.text_body,
@@ -43,7 +34,6 @@ impl Catalog for StyleType {
4334
}
4435

4536
pub struct Style {
46-
pub(crate) background: Color,
4737
pub(crate) text_color: Color,
4838
pub(crate) incoming: Color,
4939
pub(crate) outgoing: Color,

0 commit comments

Comments
 (0)