mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
docx: category gridlines follow line and area points when the axis crosses mid
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
40d27e4ede
commit
0edccaf1bc
2 files changed
+52
-7
No files matched your search
@@ -220,7 +220,7 @@ pub(super) fn cartesian(out: &mut Vec<GraphicItem>, c: &Chart, area: Rect) {
|
||||
draw_grid(out, &g, &ax.vticks);
|
||||
}
|
||||
if c.cat.grid {
|
||||
draw_cat_grid(out, &g, &ax.xticks, c.kind == Kind::Scatter);
|
||||
draw_cat_grid(out, &g, &ax.xticks, c.kind);
|
||||
}
|
||||
let mut labels = Vec::new();
|
||||
match c.kind {
|
||||
@@ -274,11 +274,12 @@ fn draw_grid(out: &mut Vec<GraphicItem>, g: &Frame, ticks: &[(f64, String)]) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Gridlines along the category axis: across the plot at each category's centre, at the x ticks
|
||||
/// of a scatter chart.
|
||||
fn draw_cat_grid(out: &mut Vec<GraphicItem>, g: &Frame, xticks: &[(f64, String)], scatter: bool) {
|
||||
/// Gridlines along the category axis: across the plot at each category's centre (where line and
|
||||
/// area points sit, on the tick marks when the axis crosses "midCat"), at the x ticks of a
|
||||
/// scatter chart.
|
||||
fn draw_cat_grid(out: &mut Vec<GraphicItem>, g: &Frame, xticks: &[(f64, String)], kind: Kind) {
|
||||
let plot = g.plot;
|
||||
if scatter {
|
||||
if kind == Kind::Scatter {
|
||||
for (t, _) in xticks {
|
||||
let p = g.scat_x(*t);
|
||||
stroke(out, seg(p, plot.y, p, plot.bottom()), GRID, 0.75);
|
||||
@@ -286,8 +287,14 @@ fn draw_cat_grid(out: &mut Vec<GraphicItem>, g: &Frame, xticks: &[(f64, String)]
|
||||
return;
|
||||
}
|
||||
for i in 0..g.n {
|
||||
let p = g.band_start(i) + g.band() / 2.0;
|
||||
let segs = if g.horiz { seg(plot.x, p, plot.right(), p) } else { seg(p, plot.y, p, plot.bottom()) };
|
||||
let segs = if g.horiz {
|
||||
let p = g.band_start(i) + g.band() / 2.0;
|
||||
seg(plot.x, p, plot.right(), p)
|
||||
} else {
|
||||
// Bars stay in their bands; only line and area points move onto the ticks.
|
||||
let p = if matches!(kind, Kind::Line | Kind::Area) { g.cat_x(i) } else { g.band_start(i) + g.band() / 2.0 };
|
||||
seg(p, plot.y, p, plot.bottom())
|
||||
};
|
||||
stroke(out, segs, GRID, 0.75);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -618,3 +618,41 @@ fn bar_category_labels_start_after_the_axis_title() {
|
||||
assert!(labels.iter().all(|r| r[0] >= strip_end - 1e-3), "labels {labels:?} overlap the title strip ending at {strip_end}");
|
||||
assert!(labels.iter().all(|r| r[2] <= 0.4 * 300.0), "{labels:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn category_gridlines_follow_the_points_when_the_axis_crosses_mid() {
|
||||
let line = |cross: &str| {
|
||||
let plot = format!(
|
||||
r#"<c:lineChart><c:grouping val="standard"/>{}<c:axId val="1"/><c:axId val="2"/></c:lineChart><c:catAx><c:axId val="1"/><c:majorGridlines/></c:catAx><c:valAx><c:axId val="2"/><c:crossBetween val="{cross}"/></c:valAx>"#,
|
||||
ser(0, "L", Some("FF0000"), &["Q1", "Q2", "Q3"], &["1", "5", "3"]),
|
||||
);
|
||||
let items = chart_items(&space(&plot), &theme(), 300.0, 200.0);
|
||||
let grid: Vec<f32> = items
|
||||
.iter()
|
||||
.filter_map(|it| match it {
|
||||
GraphicItem::Path { segs, stroke: Some(c), .. } if *c == GRID => match segs.first() {
|
||||
Some(PathSeg::Move(x, _)) => Some(*x),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
let pts: Vec<f32> = items
|
||||
.iter()
|
||||
.filter_map(|it| match it {
|
||||
GraphicItem::Path { segs, stroke: Some(c), .. } if *c == Rgb(255, 0, 0) => Some(
|
||||
segs.iter().filter_map(|s| if let PathSeg::Move(x, _) | PathSeg::Line(x, _) = s { Some(*x) } else { None }).collect::<Vec<f32>>(),
|
||||
),
|
||||
_ => None,
|
||||
})
|
||||
.next()
|
||||
.unwrap_or_default();
|
||||
(grid, pts)
|
||||
};
|
||||
for cross in ["midCat", "between"] {
|
||||
let (grid, pts) = line(cross);
|
||||
assert_eq!(grid.len(), 3, "{cross}");
|
||||
assert_eq!(pts.len(), 3, "{cross}");
|
||||
assert!(grid.iter().zip(&pts).all(|(g, p)| (g - p).abs() < 1e-3), "{cross}: grid {grid:?} points {pts:?}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user