Skip to content

Commit

Permalink
refactor: adjust multi chart
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Aug 16, 2023
1 parent 0636ef7 commit f63e172
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ charts-rs-derive = { path = "./charts-rs-derive", version = "0.1.7"}
resvg = { version = "0.35.0", default-features = false, features = [ "text", "system-fonts" ], optional = true }
png = { version = "0.17.9", optional = true }
serde = { version = "1.0.183", features = ["derive"] }
serde_json = "1.0.104"
serde_json = "1.0.105"
regex = "1.9.3"

[features]
Expand Down
2 changes: 1 addition & 1 deletion asset/charts-override.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 41 additions & 7 deletions src/charts/multi_chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,18 @@ impl MultiChart {
let mut arr = vec![];
let mut y = 0.0;
let mut x = 0.0;
let mut should_add_gap = false;
for item in self.charts.iter_mut() {
if should_add_gap {
y += self.gap;
}
let result = match item {
ChildChart::Bar(c, postion) => {
c.y = y;
// 指定定位的不增加gap
if let Some((x, y)) = postion {
c.y = y.to_owned();
c.x = x.to_owned();
} else if y > 0.0{
// 非首个图,而且未设置定位
y += self.gap;
c.y = y;
}

ChildChartResult {
Expand All @@ -62,9 +63,14 @@ impl MultiChart {
}
ChildChart::Candlestick(c, postion) => {
c.y = y;
// 指定定位的不增加gap
if let Some((x, y)) = postion {
c.y = y.to_owned();
c.x = x.to_owned();
} else if y > 0.0{
// 非首个图,而且未设置定位
y += self.gap;
c.y = y;
}

ChildChartResult {
Expand All @@ -75,9 +81,14 @@ impl MultiChart {
}
ChildChart::HorizontalBar(c, postion) => {
c.y = y;
// 指定定位的不增加gap
if let Some((x, y)) = postion {
c.y = y.to_owned();
c.x = x.to_owned();
} else if y > 0.0{
// 非首个图,而且未设置定位
y += self.gap;
c.y = y;
}

ChildChartResult {
Expand All @@ -88,9 +99,14 @@ impl MultiChart {
}
ChildChart::Line(c, postion) => {
c.y = y;
// 指定定位的不增加gap
if let Some((x, y)) = postion {
c.y = y.to_owned();
c.x = x.to_owned();
} else if y > 0.0{
// 非首个图,而且未设置定位
y += self.gap;
c.y = y;
}

ChildChartResult {
Expand All @@ -101,9 +117,14 @@ impl MultiChart {
}
ChildChart::Pie(c, postion) => {
c.y = y;
// 指定定位的不增加gap
if let Some((x, y)) = postion {
c.y = y.to_owned();
c.x = x.to_owned();
} else if y > 0.0{
// 非首个图,而且未设置定位
y += self.gap;
c.y = y;
}

ChildChartResult {
Expand All @@ -114,9 +135,14 @@ impl MultiChart {
}
ChildChart::Radar(c, postion) => {
c.y = y;
// 指定定位的不增加gap
if let Some((x, y)) = postion {
c.y = y.to_owned();
c.x = x.to_owned();
} else if y > 0.0{
// 非首个图,而且未设置定位
y += self.gap;
c.y = y;
}

ChildChartResult {
Expand All @@ -127,9 +153,14 @@ impl MultiChart {
}
ChildChart::Scatter(c, postion) => {
c.y = y;
// 指定定位的不增加gap
if let Some((x, y)) = postion {
c.y = y.to_owned();
c.x = x.to_owned();
} else if y > 0.0{
// 非首个图,而且未设置定位
y += self.gap;
c.y = y;
}

ChildChartResult {
Expand All @@ -140,9 +171,14 @@ impl MultiChart {
}
ChildChart::Table(c, postion) => {
c.y = y;
// 指定定位的不增加gap
if let Some((x, y)) = postion {
c.y = y.to_owned();
c.x = x.to_owned();
} else if y > 0.0{
// 非首个图,而且未设置定位
y += self.gap;
c.y = y;
}
// svg中会重新计算c.height
let svg = c.svg()?;
Expand All @@ -155,9 +191,6 @@ impl MultiChart {
};
if result.bottom > y {
y = result.bottom;
should_add_gap = true;
} else {
should_add_gap = false;
}
if result.right > x {
x = result.right;
Expand Down Expand Up @@ -423,6 +456,7 @@ mod tests {
);
charts.add(ChildChart::Bar(bar_chart, None));


let mut pie_chart = PieChart::new(vec![
("rose 1", vec![40.0]).into(),
("rose 2", vec![38.0]).into(),
Expand Down

0 comments on commit f63e172

Please sign in to comment.