Skip to content

Commit

Permalink
feat: support round rect of legend
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Aug 12, 2023
1 parent e1740b5 commit eba82a6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 45 deletions.
12 changes: 4 additions & 8 deletions asset/bar_chart/basic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/charts/bar_chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ mod tests {
bottom: 10.0,
..Default::default()
});
bar_chart.legend_category = LegendCategory::RoundRect;
bar_chart.y_axis_configs[0].axis_formatter = Some("{c} ml".to_string());
bar_chart.series_list[0].label_show = true;
assert_eq!(
Expand Down
94 changes: 57 additions & 37 deletions src/charts/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,7 @@ pub(crate) fn measure_legends(
pub enum LegendCategory {
#[default]
Normal,
RoundRect,
Rect,
}

Expand All @@ -1225,43 +1226,62 @@ impl Legend {
pub fn svg(&self) -> String {
let stroke_width = 2.0;
let mut data: Vec<String> = vec![];
if self.category == LegendCategory::Rect {
let height = 10.0_f32;
data.push(
Rect {
color: self.stroke_color,
fill: self.stroke_color,
left: self.left,
top: self.top + (LEGEND_HEIGHT - height) / 2.0,
width: LEGEND_WIDTH,
height,
..Default::default()
}
.svg(),
);
} else {
data.push(
Line {
stroke_width,
color: self.stroke_color,
left: self.left,
top: self.top + LEGEND_HEIGHT / 2.0,
right: self.left + LEGEND_WIDTH,
bottom: self.top + LEGEND_HEIGHT / 2.0,
}
.svg(),
);
data.push(
Circle {
stroke_width,
stroke_color: self.stroke_color,
fill: self.fill,
cx: self.left + LEGEND_WIDTH / 2.0,
cy: self.top + LEGEND_HEIGHT / 2.0,
r: 5.5,
}
.svg(),
);
match self.category {
LegendCategory::Rect => {
let height = 10.0_f32;
data.push(
Rect {
color: self.stroke_color,
fill: self.stroke_color,
left: self.left,
top: self.top + (LEGEND_HEIGHT - height) / 2.0,
width: LEGEND_WIDTH,
height,
..Default::default()
}
.svg(),
);
}
LegendCategory::RoundRect => {
let height = 10.0_f32;
data.push(
Rect {
color: self.stroke_color,
fill: self.stroke_color,
left: self.left,
top: self.top + (LEGEND_HEIGHT - height) / 2.0,
width: LEGEND_WIDTH,
height,
rx: Some(2.0),
ry: Some(2.0),
}
.svg(),
);
}
_ => {
data.push(
Line {
stroke_width,
color: self.stroke_color,
left: self.left,
top: self.top + LEGEND_HEIGHT / 2.0,
right: self.left + LEGEND_WIDTH,
bottom: self.top + LEGEND_HEIGHT / 2.0,
}
.svg(),
);
data.push(
Circle {
stroke_width,
stroke_color: self.stroke_color,
fill: self.fill,
cx: self.left + LEGEND_WIDTH / 2.0,
cy: self.top + LEGEND_HEIGHT / 2.0,
r: 5.5,
}
.svg(),
);
}
}
data.push(
Text {
Expand Down
1 change: 1 addition & 0 deletions src/charts/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub(crate) fn get_legend_category_from_value(
if let Some(value) = value.as_str() {
let value = match value.to_lowercase().as_str() {
"rect" => LegendCategory::Rect,
"round_rect" => LegendCategory::RoundRect,
_ => LegendCategory::Normal,
};
return Some(value);
Expand Down

0 comments on commit eba82a6

Please sign in to comment.