-
-
Notifications
You must be signed in to change notification settings - Fork 682
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
draft for donut charts implementation
- Loading branch information
Showing
5 changed files
with
275 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
use crate::gui::styles::donut::Catalog; | ||
use crate::gui::styles::style_constants::FONT_SIZE_BODY; | ||
use iced::alignment::{Horizontal, Vertical}; | ||
use iced::widget::canvas::path::Arc; | ||
use iced::widget::canvas::{Frame, Text}; | ||
use iced::widget::text::Shaping; | ||
use iced::widget::{canvas, Canvas}; | ||
use iced::{mouse, Font, Point, Radians, Renderer}; | ||
use std::f32::consts; | ||
|
||
pub struct DonutChart { | ||
percentage: f32, | ||
label: String, | ||
font: Font, | ||
} | ||
|
||
impl DonutChart { | ||
pub fn new(percentage: f32, label: impl Into<String>, font: Font) -> Self { | ||
let label = label.into(); | ||
Self { | ||
percentage, | ||
label, | ||
font, | ||
} | ||
} | ||
} | ||
|
||
impl<Message, Theme: Catalog> canvas::Program<Message, Theme> for DonutChart { | ||
type State = (); | ||
|
||
fn draw( | ||
&self, | ||
_: &Self::State, | ||
renderer: &Renderer, | ||
theme: &Theme, | ||
bounds: iced::Rectangle, | ||
_: mouse::Cursor, | ||
) -> Vec<canvas::Geometry> { | ||
let mut frame = Frame::new(renderer, bounds.size()); | ||
let center = frame.center(); | ||
let radius = frame.width().min(frame.height()) / 2.0; | ||
|
||
let start_angle1 = Radians(-consts::FRAC_PI_2); | ||
let end_angle1 = Radians(consts::TAU) * self.percentage / 100.0 + start_angle1; | ||
|
||
let style = <Theme as Catalog>::style(theme, &<Theme as Catalog>::default()); | ||
|
||
// let circle_at_angle = |angle: Radians| { | ||
// canvas::Path::circle( | ||
// Point::new( | ||
// center.x + fixed_radius * angle.0.cos(), | ||
// center.y + fixed_radius * angle.0.sin(), | ||
// ), | ||
// inner_ball_radius, | ||
// ) | ||
// }; | ||
|
||
let circle = canvas::Path::circle(center, radius); | ||
|
||
let incoming = canvas::Path::new(|builder| { | ||
builder.arc(Arc { | ||
center, | ||
radius, | ||
start_angle: start_angle1, | ||
end_angle: end_angle1, | ||
}); | ||
builder.line_to(center); | ||
builder.close(); | ||
}); | ||
|
||
let outgoing = canvas::Path::new(|builder| { | ||
builder.arc(Arc { | ||
center, | ||
radius, | ||
start_angle: end_angle1, | ||
end_angle: start_angle1 + Radians(consts::PI * 2.0), | ||
}); | ||
builder.line_to(center); | ||
builder.close(); | ||
}); | ||
|
||
let inner_circle = canvas::Path::circle(center, radius - 6.0); | ||
|
||
frame.fill_text(Text { | ||
content: if self.label.is_empty() { | ||
format!("{:.2}%", self.percentage) | ||
} else { | ||
self.label.clone() | ||
}, | ||
position: center, | ||
vertical_alignment: Vertical::Center, | ||
horizontal_alignment: Horizontal::Center, | ||
color: style.text_color, | ||
size: FONT_SIZE_BODY.into(), | ||
font: self.font, | ||
..Default::default() | ||
}); | ||
frame.fill(&circle, style.background); | ||
frame.fill(&incoming, style.incoming); | ||
frame.fill(&outgoing, style.outgoing); | ||
frame.fill(&inner_circle, style.rail); | ||
|
||
vec![frame.into_geometry()] | ||
} | ||
} | ||
|
||
/// Creates a radial progress bar widget. | ||
/// | ||
/// This function returns a `Canvas` widget that displays a radial progress bar | ||
/// with a specified percentage and content. If the content is empty, the | ||
/// percentage will be displayed by default. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// use atoms::widgets::radial_progress_bar; | ||
/// | ||
/// let progress = radial_progress_bar(0.75, "75% Complete"); | ||
/// let default_progress = radial_progress_bar(0.75, ""); | ||
/// ``` | ||
pub fn donut_chart<Message, Theme: Catalog>( | ||
percentage: f32, | ||
label: impl Into<String>, | ||
font: Font, | ||
) -> Canvas<DonutChart, Message, Theme, Renderer> { | ||
let radius = 95.0; | ||
iced::widget::canvas(DonutChart::new(percentage, label, font)) | ||
.width(radius) | ||
.height(radius) | ||
} | ||
|
||
/// The status of a [`ProgressBar`]. | ||
#[derive(Debug, Clone, Copy)] | ||
pub enum Status { | ||
/// The progress bar is idle. | ||
Idle, | ||
/// The progress bar is currently progressing. | ||
Progressing, | ||
/// The progress bar has finished. | ||
Finished, | ||
/// The progress bar has failed. | ||
Failed, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod chart_type; | ||
pub mod donut_chart; | ||
pub mod traffic_chart; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use crate::chart::types::donut_chart::Status; | ||
use crate::gui::styles::button::ButtonType; | ||
use crate::gui::styles::types::style_type::StyleType; | ||
use iced::Color; | ||
|
||
#[derive(Default)] | ||
pub enum DonutType { | ||
#[default] | ||
Standard, | ||
} | ||
|
||
impl DonutType { | ||
fn active(&self, style: &StyleType) -> Style { | ||
let colors = style.get_palette(); | ||
let ext = style.get_extension(); | ||
Style { | ||
background: Color::TRANSPARENT, | ||
incoming: colors.secondary, | ||
outgoing: colors.outgoing, | ||
rail: colors.primary, | ||
text_color: colors.text_body, | ||
} | ||
} | ||
} | ||
|
||
impl Catalog for StyleType { | ||
type Class<'a> = DonutType; | ||
|
||
fn default<'a>() -> Self::Class<'a> { | ||
Self::Class::default() | ||
} | ||
|
||
fn style(&self, class: &Self::Class<'_>) -> Style { | ||
class.active(self) | ||
} | ||
} | ||
|
||
pub struct Style { | ||
pub(crate) background: Color, | ||
pub(crate) incoming: Color, | ||
pub(crate) outgoing: Color, | ||
pub(crate) rail: Color, | ||
pub(crate) text_color: Color, | ||
} | ||
|
||
/// The theme catalog of a [`ProgressBar`]. | ||
pub trait Catalog: Sized { | ||
/// The item class of the [`Catalog`]. | ||
type Class<'a>; | ||
|
||
/// The default class produced by the [`Catalog`]. | ||
fn default<'a>() -> Self::Class<'a>; | ||
|
||
/// The [`Style`] of a class with the given status. | ||
fn style(&self, class: &Self::Class<'_>) -> Style; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters