1 | use crate::{ |
2 | element::{Drawable, PointCollection}, |
3 | style::{IntoFont, RGBColor, TextStyle, BLACK}, |
4 | }; |
5 | use plotters_backend::{BackendCoord, DrawingBackend, DrawingErrorKind}; |
6 | use std::{error::Error, f64::consts::PI, fmt::Display}; |
7 | |
8 | #[derive(Debug)] |
9 | enum PieError { |
10 | LengthMismatch, |
11 | } |
12 | impl Display for PieError { |
13 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
14 | match self { |
15 | &PieError::LengthMismatch => write!(f, "Length Mismatch" ), |
16 | } |
17 | } |
18 | } |
19 | |
20 | impl Error for PieError {} |
21 | |
22 | /// A Pie Graph |
23 | pub struct Pie<'a, Coord, Label: Display> { |
24 | center: &'a Coord, // cartesian coord |
25 | radius: &'a f64, |
26 | sizes: &'a [f64], |
27 | colors: &'a [RGBColor], |
28 | labels: &'a [Label], |
29 | total: f64, |
30 | start_radian: f64, |
31 | label_style: TextStyle<'a>, |
32 | label_offset: f64, |
33 | percentage_style: Option<TextStyle<'a>>, |
34 | } |
35 | |
36 | impl<'a, Label: Display> Pie<'a, (i32, i32), Label> { |
37 | /// Build a Pie object. |
38 | /// Assumes a start angle at 0.0, which is aligned to the horizontal axis. |
39 | pub fn new( |
40 | center: &'a (i32, i32), |
41 | radius: &'a f64, |
42 | sizes: &'a [f64], |
43 | colors: &'a [RGBColor], |
44 | labels: &'a [Label], |
45 | ) -> Self { |
46 | // fold iterator to pre-calculate total from given slice sizes |
47 | let total = sizes.iter().sum(); |
48 | |
49 | // default label style and offset as 5% of the radius |
50 | let radius_5pct = radius * 0.05; |
51 | |
52 | // strong assumption that the background is white for legibility. |
53 | let label_style = TextStyle::from(("sans-serif" , radius_5pct).into_font()).color(&BLACK); |
54 | Self { |
55 | center, |
56 | radius, |
57 | sizes, |
58 | colors, |
59 | labels, |
60 | total, |
61 | start_radian: 0.0, |
62 | label_style, |
63 | label_offset: radius_5pct, |
64 | percentage_style: None, |
65 | } |
66 | } |
67 | |
68 | /// Pass an angle in degrees to change the default. |
69 | /// Default is set to start at 0, which is aligned on the x axis. |
70 | /// ``` |
71 | /// use plotters::prelude::*; |
72 | /// let mut pie = Pie::new(&(50,50), &10.0, &[50.0, 25.25, 20.0, 5.5], &[RED, BLUE, GREEN, WHITE], &["Red" , "Blue" , "Green" , "White" ]); |
73 | /// pie.start_angle(-90.0); // retract to a right angle, so it starts aligned to a vertical Y axis. |
74 | /// ``` |
75 | pub fn start_angle(&mut self, start_angle: f64) { |
76 | // angle is more intuitive in degrees as an API, but we use it as radian offset internally. |
77 | self.start_radian = start_angle.to_radians(); |
78 | } |
79 | |
80 | /// |
81 | pub fn label_style<T: Into<TextStyle<'a>>>(&mut self, label_style: T) { |
82 | self.label_style = label_style.into(); |
83 | } |
84 | |
85 | /// Sets the offset to labels, to distanciate them further/closer from the center. |
86 | pub fn label_offset(&mut self, offset_to_radius: f64) { |
87 | self.label_offset = offset_to_radius |
88 | } |
89 | |
90 | /// enables drawing the wedge's percentage in the middle of the wedge, with the given style |
91 | pub fn percentages<T: Into<TextStyle<'a>>>(&mut self, label_style: T) { |
92 | self.percentage_style = Some(label_style.into()); |
93 | } |
94 | } |
95 | |
96 | impl<'a, DB: DrawingBackend, Label: Display> Drawable<DB> for Pie<'a, (i32, i32), Label> { |
97 | fn draw<I: Iterator<Item = BackendCoord>>( |
98 | &self, |
99 | _pos: I, |
100 | backend: &mut DB, |
101 | _parent_dim: (u32, u32), |
102 | ) -> Result<(), DrawingErrorKind<DB::ErrorType>> { |
103 | let mut offset_theta = self.start_radian; |
104 | |
105 | // const reused for every radian calculation |
106 | // the bigger the radius, the more fine-grained it should calculate |
107 | // to avoid being aliasing from being too noticeable. |
108 | // this all could be avoided if backend could draw a curve/bezier line as part of a polygon. |
109 | let radian_increment = PI / 180.0 / self.radius.sqrt() * 2.0; |
110 | let mut perc_labels = Vec::new(); |
111 | for (index, slice) in self.sizes.iter().enumerate() { |
112 | let slice_style = self |
113 | .colors |
114 | .get(index) |
115 | .ok_or_else(|| DrawingErrorKind::FontError(Box::new(PieError::LengthMismatch)))?; |
116 | let label = self |
117 | .labels |
118 | .get(index) |
119 | .ok_or_else(|| DrawingErrorKind::FontError(Box::new(PieError::LengthMismatch)))?; |
120 | // start building wedge line against the previous edge |
121 | let mut points = vec![*self.center]; |
122 | let ratio = slice / self.total; |
123 | let theta_final = ratio * 2.0 * PI + offset_theta; // end radian for the wedge |
124 | |
125 | // calculate middle for labels before mutating offset |
126 | let middle_theta = ratio * PI + offset_theta; |
127 | |
128 | // calculate every fraction of radian for the wedge, offsetting for every iteration, clockwise |
129 | // |
130 | // a custom Range such as `for theta in offset_theta..=theta_final` would be more elegant |
131 | // but f64 doesn't implement the Range trait, and it would requires the Step trait (increment by 1.0 or 0.0001?) |
132 | // which is unstable therefore cannot be implemented outside of std, even as a newtype for radians. |
133 | while offset_theta <= theta_final { |
134 | let coord = theta_to_ordinal_coord(*self.radius, offset_theta, self.center); |
135 | points.push(coord); |
136 | offset_theta += radian_increment; |
137 | } |
138 | // final point of the wedge may not fall exactly on a radian, so add it extra |
139 | let final_coord = theta_to_ordinal_coord(*self.radius, theta_final, self.center); |
140 | points.push(final_coord); |
141 | // next wedge calculation will start from previous wedges's last radian |
142 | offset_theta = theta_final; |
143 | |
144 | // draw wedge |
145 | // TODO: Currently the backend doesn't have API to draw an arc. We need add that in the |
146 | // future |
147 | backend.fill_polygon(points, slice_style)?; |
148 | |
149 | // label coords from the middle |
150 | let mut mid_coord = |
151 | theta_to_ordinal_coord(self.radius + self.label_offset, middle_theta, self.center); |
152 | |
153 | // ensure label's doesn't fall in the circle |
154 | let label_size = backend.estimate_text_size(&label.to_string(), &self.label_style)?; |
155 | // if on the left hand side of the pie, offset whole label to the left |
156 | if mid_coord.0 <= self.center.0 { |
157 | mid_coord.0 -= label_size.0 as i32; |
158 | } |
159 | // put label |
160 | backend.draw_text(&label.to_string(), &self.label_style, mid_coord)?; |
161 | if let Some(percentage_style) = &self.percentage_style { |
162 | let perc_label = format!("{:.1}%" , (ratio * 100.0)); |
163 | let label_size = backend.estimate_text_size(&perc_label, percentage_style)?; |
164 | let text_x_mid = (label_size.0 as f64 / 2.0).round() as i32; |
165 | let text_y_mid = (label_size.1 as f64 / 2.0).round() as i32; |
166 | let perc_coord = theta_to_ordinal_coord( |
167 | self.radius / 2.0, |
168 | middle_theta, |
169 | &(self.center.0 - text_x_mid, self.center.1 - text_y_mid), |
170 | ); |
171 | // perc_coord.0 -= middle_label_size.0.round() as i32; |
172 | perc_labels.push((perc_label, perc_coord)); |
173 | } |
174 | } |
175 | // while percentages are generated during the first main iterations, |
176 | // they have to go on top of the already drawn wedges, so require a new iteration. |
177 | for (label, coord) in perc_labels { |
178 | let style = self.percentage_style.as_ref().unwrap(); |
179 | backend.draw_text(&label, style, coord)?; |
180 | } |
181 | Ok(()) |
182 | } |
183 | } |
184 | |
185 | impl<'a, Label: Display> PointCollection<'a, (i32, i32)> for &'a Pie<'a, (i32, i32), Label> { |
186 | type Point = &'a (i32, i32); |
187 | type IntoIter = std::iter::Once<&'a (i32, i32)>; |
188 | fn point_iter(self) -> std::iter::Once<&'a (i32, i32)> { |
189 | std::iter::once(self.center) |
190 | } |
191 | } |
192 | |
193 | fn theta_to_ordinal_coord(radius: f64, theta: f64, ordinal_offset: &(i32, i32)) -> (i32, i32) { |
194 | // polar coordinates are (r, theta) |
195 | // convert to (x, y) coord, with center as offset |
196 | |
197 | let (sin, cos) = theta.sin_cos(); |
198 | ( |
199 | // casting f64 to discrete i32 pixels coordinates is inevitably going to lose precision |
200 | // if plotters can support float coordinates, this place would surely benefit, especially for small sizes. |
201 | // so far, the result isn't so bad though |
202 | (radius * cos + ordinal_offset.0 as f64).round() as i32, // x |
203 | (radius * sin + ordinal_offset.1 as f64).round() as i32, // y |
204 | ) |
205 | } |
206 | #[cfg (test)] |
207 | mod test { |
208 | use super::*; |
209 | // use crate::prelude::*; |
210 | |
211 | #[test] |
212 | fn polar_coord_to_cartestian_coord() { |
213 | let coord = theta_to_ordinal_coord(800.0, 1.5_f64.to_radians(), &(5, 5)); |
214 | // rounded tends to be more accurate. this gets truncated to (804, 25) without rounding. |
215 | assert_eq!(coord, (805, 26)); //coord calculated from theta |
216 | } |
217 | #[test] |
218 | fn pie_calculations() { |
219 | let mut center = (5, 5); |
220 | let mut radius = 800.0; |
221 | |
222 | let sizes = vec![50.0, 25.0]; |
223 | // length isn't validated in new() |
224 | let colors = vec![]; |
225 | let labels: Vec<&str> = vec![]; |
226 | let pie = Pie::new(¢er, &radius, &sizes, &colors, &labels); |
227 | assert_eq!(pie.total, 75.0); // total calculated from sizes |
228 | |
229 | // not ownership greedy |
230 | center.1 += 1; |
231 | radius += 1.0; |
232 | assert!(colors.get(0).is_none()); |
233 | assert!(labels.get(0).is_none()); |
234 | assert_eq!(radius, 801.0); |
235 | } |
236 | } |
237 | |