diff --git a/crates/model/src/chart.rs b/crates/model/src/chart.rs index 2ea6e5a7..b620be88 100644 --- a/crates/model/src/chart.rs +++ b/crates/model/src/chart.rs @@ -282,6 +282,9 @@ pub struct Chart { pub y_format: Option, /// The value axis is deleted (`c:delete`): no labels, though gridlines may stay. pub y_hidden: bool, + /// The category axis runs backwards (`c:orientation val="maxMin"`): right to left, or on a + /// horizontal bar chart top to bottom (the default puts the first category at the bottom). + pub cat_reversed: bool, /// A combo chart's secondary value axis: bounds, major unit and number format. pub y2_min: Option, pub y2_max: Option, @@ -345,6 +348,7 @@ impl Default for Chart { y_major: None, y_format: None, y_hidden: false, + cat_reversed: false, y2_min: None, y2_max: None, y2_major: None, diff --git a/crates/pptx/src/read/chart.rs b/crates/pptx/src/read/chart.rs index 28b8d0c4..c7e0e132 100644 --- a/crates/pptx/src/read/chart.rs +++ b/crates/pptx/src/read/chart.rs @@ -327,6 +327,7 @@ fn chart_within(root: &El, mut budget: usize) -> Option { c.y_hidden = deleted(v); } if let Some(a) = cat_ax { + c.cat_reversed = a.child("scaling").and_then(|s| s.child_val("orientation")).is_some_and(|o| o == "maxMin"); c.axis_titles.0 = a.child("title").map(|t| rich_text(t).unwrap_or_else(|| "Axis Title".into())); } // Text sizes as the file sets them (PowerPoint draws chart text at these sizes, not scaled to @@ -511,6 +512,13 @@ mod tests { let xml = r#"1"#; let c = read_chart_xml(xml.as_bytes()).unwrap(); assert_eq!(c.text_color, Some(deckcraft_model::ColorRef::rgb(deckcraft_color::Rgba::rgb(255, 255, 255)))); + assert!(!c.cat_reversed); + let rev = read_chart_xml( + xml.replace("", "") + .as_bytes(), + ) + .unwrap(); + assert!(rev.cat_reversed, "maxMin reverses the category axis"); // No c:gapWidth / c:overlap: the schema's 150 % and 0 %. assert_eq!((c.gap_width, c.overlap), (1.5, 0.0)); } diff --git a/crates/pptx/src/read/mod.rs b/crates/pptx/src/read/mod.rs index 8f093e8d..4516acca 100644 --- a/crates/pptx/src/read/mod.rs +++ b/crates/pptx/src/read/mod.rs @@ -1,6 +1,6 @@ //! PPTX import: package → [`Presentation`]. -mod chart; +pub(crate) mod chart; mod chartex; pub(crate) mod dml; mod shapes; diff --git a/crates/pptx/src/write/chart.rs b/crates/pptx/src/write/chart.rs index 9696d907..694157f2 100644 --- a/crates/pptx/src/write/chart.rs +++ b/crates/pptx/src/write/chart.rs @@ -332,7 +332,7 @@ fn axes(w: &mut W, c: &Chart, g: Group) { w.open0("c:catAx"); w.val("c:axId", AX_CAT); w.open0("c:scaling"); - w.val("c:orientation", "minMax"); + w.val("c:orientation", if c.cat_reversed { "maxMin" } else { "minMax" }); w.close("c:scaling"); w.val("c:delete", 0); w.val("c:axPos", cat_pos); @@ -811,3 +811,19 @@ mod region_map_tests { assert_eq!(back.series.first().and_then(|s| s.values.get(1).copied().flatten()), Some(20.0)); } } + +#[cfg(test)] +mod tests { + use deckcraft_model::chart::{Chart, ChartType}; + + #[test] + fn reversed_category_axis_round_trips() { + let mut c = Chart::sample(ChartType::Bar); + c.cat_reversed = true; + let xml = super::chart_xml(&c); + assert!(String::from_utf8_lossy(&xml).contains(r#""#)); + assert!(crate::read::chart::read_chart_xml(&xml).unwrap().cat_reversed); + c.cat_reversed = false; + assert!(!crate::read::chart::read_chart_xml(&super::chart_xml(&c)).unwrap().cat_reversed); + } +} diff --git a/crates/render/src/chart.rs b/crates/render/src/chart.rs index 217deca8..fe8df06a 100644 --- a/crates/render/src/chart.rs +++ b/crates/render/src/chart.rs @@ -445,10 +445,15 @@ fn draw_inner(ctx: &mut RenderContext, rctx: &Ctx, c: &Chart, m: Affine, w: f64, let (back, fwd) = forecast_pad(c); let nspan = ncat as f64 + back + fwd; let band = if horiz { plot.height() / nspan } else { plot.width() / nspan }; + // Where category `i`'s band starts along its axis: left to right, or bottom to top on a + // horizontal bar chart, unless the axis is reversed. The forecast's empty slots stay on + // their own side of the data. + let backwards = horiz != c.cat_reversed; + let slot = |i: usize| if backwards { ncat.saturating_sub(i + 1) as f64 + fwd } else { i as f64 + back }; // Category labels: wrapped onto two lines when they don't fit their band (long date ranges). let wraps = !horiz && c.categories.iter().any(|n| n.chars().count() as f64 * base * 0.5 > band); for (i, name) in c.categories.iter().enumerate() { - let mid = if horiz { plot.y0 + band * (i as f64 + back + 0.5) } else { plot.x0 + band * (i as f64 + back + 0.5) }; + let mid = if horiz { plot.y0 + band * (slot(i) + 0.5) } else { plot.x0 + band * (slot(i) + 0.5) }; if horiz { label( ctx, @@ -507,7 +512,7 @@ fn draw_inner(ctx: &mut RenderContext, rctx: &Ctx, c: &Chart, m: Affine, w: f64, for (bi, (si, s)) in bars.iter().enumerate() { let sec = c.on_secondary(s); let v = s.values.get(i).copied().flatten().unwrap_or(0.0) / total; - let start = band * (i as f64 + back) + (band - group) / 2.0 + if stacked { 0.0 } else { step * bi as f64 }; + let start = band * slot(i) + (band - group) / 2.0 + if stacked { 0.0 } else { step * bi as f64 }; let (a, b) = if waterfall { c.waterfall_bars().get(i).copied().unwrap_or_default() } else if stacked { @@ -612,9 +617,9 @@ fn draw_inner(ctx: &mut RenderContext, rctx: &Ctx, c: &Chart, m: Affine, w: f64, let xs: Vec = s.x_values.iter().flatten().copied().collect(); let (xl, xh) = (xs.iter().copied().fold(f64::MAX, f64::min), xs.iter().copied().fold(f64::MIN, f64::max)); let xv = s.x_values.get(i).copied().flatten().unwrap_or(i as f64); - if xh > xl { plot.x0 + (xv - xl) / (xh - xl) * plot.width() } else { plot.x0 + band * (i as f64 + back + 0.5) } + if xh > xl { plot.x0 + (xv - xl) / (xh - xl) * plot.width() } else { plot.x0 + band * (slot(i) + 0.5) } } - _ => plot.x0 + band * (i as f64 + back + 0.5), + _ => plot.x0 + band * (slot(i) + 0.5), }; pts.push(Point::new(x, to_axis(sec, v))); } diff --git a/crates/render/src/tests.rs b/crates/render/src/tests.rs index 1cdc69b1..8c3554e6 100644 --- a/crates/render/src/tests.rs +++ b/crates/render/src/tests.rs @@ -324,6 +324,26 @@ fn column_points_take_their_own_fill_and_smooth_lines_curve() { assert!(mid.y > mid.x, "the first half bows towards the peak instead of following the chord: {mid:?}"); } +#[test] +fn horizontal_bars_list_the_first_category_at_the_bottom_unless_reversed() { + let draw = |reversed: bool| { + let mut c = Chart::sample(ChartType::Bar); + c.title = None; + c.legend = None; + c.series.truncate(1); + c.categories = vec!["A".into(), "B".into(), "C".into()]; + c.series[0].values = vec![Some(10.0), Some(10.0), Some(10.0)]; + c.series[0].point_fills = vec![(0, deckcraft_model::style::Fill::solid(ColorRef::rgb(deckcraft_color::Rgba::rgb(255, 0, 0))))]; + c.cat_reversed = reversed; + let sh = Shape { id: ShapeId(623), xfrm: Some(Xfrm::new(0.0, 0.0, 960.0, 540.0)), kind: ShapeKind::Chart(Box::new(c)), ..Default::default() }; + let img = render_slide(&deck_with(vec![sh]), 0, &RenderOpts { scale: 1.0, ..Default::default() }); + let rows: Vec = (0..540).filter(|y| (0..960).any(|x| img.pixel(x, *y)[..3] == [255, 0, 0])).collect(); + (rows[0] + rows[rows.len() - 1]) / 2 + }; + assert!(draw(false) > 360, "first category at the bottom: {}", draw(false)); + assert!(draw(true) < 180, "reversed: first category at the top: {}", draw(true)); +} + #[test] fn placeholder_prompts_only_in_edit_view() { let p = Presentation::default();