Skip to content

Commit

Permalink
fix: fix pie chart label overlap, #19
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Aug 15, 2024
1 parent 2bcc5e5 commit 085dae7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
6 changes: 3 additions & 3 deletions asset/pie_chart/not_rose.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions asset/pie_chart/not_rose_radius.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 23 additions & 2 deletions src/charts/pie_chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use super::util::*;
use super::Canvas;
use crate::charts::measure_text_width_family;
use charts_rs_derive::Chart;
use core::f32;
use std::sync::Arc;
use std::u8;

#[derive(Clone, Debug, Default, Chart)]
pub struct PieChart {
Expand Down Expand Up @@ -186,6 +188,8 @@ impl PieChart {
}
let rose_type = self.rose_type.unwrap_or_default();

let mut prev_quadrant = u8::MAX;
let mut prev_end_y = f32::MAX;
for (index, series) in self.series_list.iter().enumerate() {
let value = values[index];
let mut cr = value / max * (r - self.inner_radius) + self.inner_radius;
Expand Down Expand Up @@ -219,6 +223,23 @@ impl PieChart {
let mut points = vec![];
points.push(get_pie_point(cx, cy, cr, angle));
let mut end = get_pie_point(cx, cy, r + label_offset, angle);

let quadrant = get_quadrant(cx, cy, &end);
// quadrant change
if quadrant != prev_quadrant {
prev_end_y = f32::MAX;
prev_quadrant = quadrant;
}
// label overlap
if (end.y - prev_end_y).abs() < self.series_label_font_size {
if quadrant == 1 || quadrant == 4 {
end.y = prev_end_y + self.series_label_font_size;
} else {
end.y = prev_end_y - self.series_label_font_size;
}
}
prev_end_y = end.y;

points.push(end);

let is_left = angle > 180.0;
Expand Down Expand Up @@ -339,7 +360,7 @@ mod tests {
pie_chart.title_text = "Pie Chart".to_string();
pie_chart.sub_title_text = "Fake Data".to_string();
assert_eq!(
include_str!("../../asset/pie_chart/not_rose.svg"),
include_str!("../../asset/pie_chart/not_rose.svg").trim(),
pie_chart.svg().unwrap()
);
}
Expand All @@ -362,7 +383,7 @@ mod tests {
pie_chart.title_text = "Pie Chart".to_string();
pie_chart.sub_title_text = "Fake Data".to_string();
assert_eq!(
include_str!("../../asset/pie_chart/not_rose_radius.svg"),
include_str!("../../asset/pie_chart/not_rose_radius.svg").trim(),
pie_chart.svg().unwrap()
);
}
Expand Down
16 changes: 16 additions & 0 deletions src/charts/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,22 @@ pub fn convert_to_points(values: &[(f32, f32)]) -> Vec<Point> {
values.iter().map(|item| item.to_owned().into()).collect()
}

pub fn get_quadrant(cx: f32, cy: f32, point: &Point) -> u8 {
if point.x > cx {
if point.y > cy {
4
} else {
1
}
} else {
if point.y > cy {
3
} else {
2
}
}
}

#[derive(Clone, Debug, Default)]
pub(crate) struct LabelOption {
pub series_name: String,
Expand Down

0 comments on commit 085dae7

Please sign in to comment.