Skip to content

Commit

Permalink
feat: support series label for radar chart
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Dec 27, 2023
1 parent 7d1199a commit b3577cf
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ readme = "./README.md"
[dependencies]
charts-rs-derive = { path = "./charts-rs-derive", version = "0.1.21" }
fontdue = "0.8.0"
once_cell = "1.18.0"
once_cell = "1.19.0"
png = { version = "0.17.10", optional = true }
regex = "1.10.2"
resvg = { version = "0.36.0", default-features = false, features = [ "text", "system-fonts" ], optional = true }
resvg = { version = "0.37.0", default-features = false, features = [ "text", "system-fonts" ], optional = true }
serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.108"
snafu = "0.7.5"
Expand Down
9 changes: 9 additions & 0 deletions asset/radar_chart/three_points.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 31 additions & 1 deletion src/charts/radar_chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ impl RadarChart {
});
}

let mut label_positions = vec![];
for (index, series) in self.series_list.iter().enumerate() {
let color = get_color(&self.series_colors, series.index.unwrap_or(index));
let mut points = vec![];
Expand All @@ -273,10 +274,16 @@ impl RadarChart {
} else {
*value / item.max * r
};

if ir > r {
ir = r;
}
let p = get_pie_point(cx, cy, ir, angle * i as f32);
if series.label_show {
let label =
format_series_value(value.to_owned(), &self.series_label_formatter);
label_positions.push((p.clone(), label));
}
points.push(p);
}
}
Expand All @@ -289,6 +296,28 @@ impl RadarChart {
..Default::default()
});
}
for item in label_positions.iter() {
let mut dx = None;
let text = item.1.clone();
let point = item.0;
if let Ok(value) =
measure_text_width_family(&self.font_family, self.series_label_font_size, &text)
{
dx = Some(-value.width() / 2.0);
}
c.text(Text {
text: text.clone(),
dy: Some(-8.0),
dx,
font_family: Some(self.font_family.clone()),
font_color: Some(self.series_label_font_color),
font_size: Some(self.series_label_font_size),
font_weight: self.series_label_font_weight.clone(),
x: Some(point.x),
y: Some(point.y),
..Default::default()
});
}

c.svg()
}
Expand Down Expand Up @@ -417,7 +446,7 @@ mod tests {

#[test]
fn radar_three_points() {
let radar_chart = RadarChart::new(
let mut radar_chart = RadarChart::new(
vec![
Series::new(
"Allocated Budget".to_string(),
Expand All @@ -434,6 +463,7 @@ mod tests {
("Information Technology", 30000.0).into(),
],
);
radar_chart.series_list[0].label_show = true;

assert_eq!(
include_str!("../../asset/radar_chart/three_points.svg"),
Expand Down
28 changes: 14 additions & 14 deletions src/charts/table_chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,21 +616,21 @@ mod tests {
],
]);
table_chart.title_text = "NASDAQ".to_string();
table_chart.text_aligns = vec![
Align::Left,
Align::Center,
table_chart.text_aligns = vec![Align::Left, Align::Center];
table_chart.cell_styles = vec![
TableCellStyle {
indexes: vec![1, 2],
font_weight: Some("bold".to_string()),
background_color: Some("#3bb357".into()),
font_color: Some(("#fff").into()),
},
TableCellStyle {
indexes: vec![2, 1],
background_color: Some("#3bb357".into()),
font_color: Some(("#fff").into()),
..Default::default()
},
];
table_chart.cell_styles = vec![TableCellStyle {
indexes: vec![1, 2],
font_weight: Some("bold".to_string()),
background_color: Some("#3bb357".into()),
font_color: Some(("#fff").into()),
}, TableCellStyle {
indexes: vec![2, 1],
background_color: Some("#3bb357".into()),
font_color: Some(("#fff").into()),
..Default::default()
}];
assert_eq!(
include_str!("../../asset/table_chart/multi_lines.svg"),
table_chart.svg().unwrap()
Expand Down

0 comments on commit b3577cf

Please sign in to comment.