mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
docx charts: automatic marker colour, data label positions, horizontal axis titles
Word draws markers without their own fill in the automatic palette colour of the series index, not the explicit line colour. Line and scatter data labels sit to the right of the point unless c:dLblPos says t, b, l, r or ctr. A vertical-axis title with rot="0" stays horizontal, wrapped into a strip of at most a quarter of the chart width, instead of being forced to -90 degrees. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
396402940c
commit
8761999c9b
5 files changed
+198
-19
No files matched your search
@@ -7,6 +7,33 @@ pub(super) struct DataLabel {
|
||||
pub(super) y: f32,
|
||||
pub(super) text: String,
|
||||
pub(super) inside: bool,
|
||||
/// Where a label that is not `inside` sits relative to (`x`, `y`).
|
||||
pub(super) pos: LabelPos,
|
||||
/// Distance from (`x`, `y`) to the label for `pos` other than `Center`, points (half the marker).
|
||||
pub(super) off: f32,
|
||||
}
|
||||
|
||||
/// Where a data label sits beside its point (`c:dLblPos`: `t`, `b`, `l`, `r`, `ctr`).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(super) enum LabelPos {
|
||||
Above,
|
||||
Below,
|
||||
Left,
|
||||
Right,
|
||||
Center,
|
||||
}
|
||||
|
||||
impl LabelPos {
|
||||
pub(super) fn parse(v: &str) -> Option<LabelPos> {
|
||||
Some(match v.trim() {
|
||||
"t" => LabelPos::Above,
|
||||
"b" => LabelPos::Below,
|
||||
"l" => LabelPos::Left,
|
||||
"r" => LabelPos::Right,
|
||||
"ctr" => LabelPos::Center,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
@@ -137,6 +164,10 @@ pub(super) struct Series {
|
||||
pub(super) labels: Option<bool>,
|
||||
/// The labels' own number format (`c:dLbls/c:numFmt` not linked to the source), if any.
|
||||
pub(super) label_fmt: Option<String>,
|
||||
/// The data labels' `c:dLblPos`, when the file gives a known one.
|
||||
pub(super) label_pos: Option<LabelPos>,
|
||||
/// The marker's own fill (`c:marker/c:spPr`); without it markers take the automatic colour.
|
||||
pub(super) marker_color: Option<Rgb>,
|
||||
/// The values as the file has them, for data labels (`values` become shares in a 100%
|
||||
/// stacked chart); empty when the series shows no labels.
|
||||
pub(super) raw: Vec<Option<f64>>,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::format::{date_text, excel_stamp, format_value, is_date_code};
|
||||
use super::model::{Axis, Grouping, Kind, Pos, Series, Symbol};
|
||||
use super::model::{Axis, Grouping, Kind, LabelPos, Pos, Series, Symbol};
|
||||
use super::plot::label;
|
||||
use super::{Chart, El, FONT, MAX_CELLS, MAX_POINTS, MAX_RADAR_SERIES, MAX_SERIES, MAX_SPOKES, Rgb, fill_colour, int, measure, slot};
|
||||
|
||||
@@ -102,6 +102,13 @@ impl Chart {
|
||||
s.and_then(|s| s.color).unwrap_or_else(|| self.accent(s.and_then(|s| s.idx).unwrap_or(j)))
|
||||
}
|
||||
|
||||
/// Colour of series `j`'s markers: the marker's own fill, else the automatic colour of the
|
||||
/// series index (not the series' explicit line colour, as Word draws them).
|
||||
pub(super) fn marker_colour(&self, j: usize) -> Rgb {
|
||||
let s = self.series.get(j);
|
||||
s.and_then(|s| s.marker_color).unwrap_or_else(|| self.accent(s.and_then(|s| s.idx).unwrap_or(j)))
|
||||
}
|
||||
|
||||
/// Colour of point `i` of series `j`: its `c:dPt` override, else the varied accent (pie) or
|
||||
/// the series colour.
|
||||
pub(super) fn point_colour(&self, j: usize, i: usize) -> Rgb {
|
||||
@@ -219,6 +226,7 @@ impl Series {
|
||||
let sp = ser.child("c:spPr");
|
||||
let ln = sp.and_then(|p| p.child("a:ln"));
|
||||
let marker = ser.child("c:marker").and_then(|m| m.child("c:spPr")).and_then(|p| fill_colour(p, theme));
|
||||
let marker_color = marker;
|
||||
let color = sp.and_then(|p| fill_colour(p, theme)).or_else(|| ln.and_then(|l| fill_colour(l, theme))).or(marker);
|
||||
let no_fill = sp.and_then(|p| p.child("a:noFill")).is_some();
|
||||
let line_w = ln.and_then(|l| l.attr("w")).and_then(|v| measure(v, 12_700.0)).map(|w| w.clamp(0.0, 20.0));
|
||||
@@ -251,6 +259,7 @@ impl Series {
|
||||
}
|
||||
let labels = show_values(ser.child("c:dLbls"));
|
||||
let label_fmt = label_format(ser.child("c:dLbls"));
|
||||
let label_pos = ser.child("c:dLbls").and_then(|d| d.child("c:dLblPos")).and_then(|p| p.attr("val")).and_then(LabelPos::parse);
|
||||
Series {
|
||||
name,
|
||||
idx,
|
||||
@@ -269,6 +278,8 @@ impl Series {
|
||||
x_fmt,
|
||||
labels,
|
||||
label_fmt,
|
||||
label_pos,
|
||||
marker_color,
|
||||
raw: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::canvas::{Rect, clip_text, fill, rect_segs, runs, seg, stroke, text, text_angle, text_at, text_w, widest, wrap};
|
||||
use super::format::format_value;
|
||||
use super::model::{DataLabel, Grouping, Kind, Series, Symbol};
|
||||
use super::model::{DataLabel, Grouping, Kind, LabelPos, Series, Symbol};
|
||||
use super::parse::category_text;
|
||||
use super::polar::{draw_data_labels, label_of};
|
||||
use super::{AXIS, Chart, El, GAP, GRID, GraphicItem, LINE, LINE_W, MAX_LABEL, MAX_TICKS, PathSeg, Rgb, ShapeKind, TextAlign, fill_colour, measure};
|
||||
@@ -164,7 +164,7 @@ fn axis_margins(c: &Chart, ax: &Axes, area: Rect) -> Margins {
|
||||
let scatter = c.kind == Kind::Scatter;
|
||||
let (cat_on, val_on) = (!c.cat.deleted, !c.val.deleted);
|
||||
let (cat_labels, val_labels) = (cat_on && c.cat.labels, val_on && c.val.labels);
|
||||
let title = title_left(c, cat_on, val_on, lh);
|
||||
let title = title_left(c, cat_on, val_on, area.w);
|
||||
let left = if c.horizontal {
|
||||
if cat_labels { widest(&c.cats, fs).min(area.w * 0.4) + GAP } else { 0.0 }
|
||||
} else if val_labels {
|
||||
@@ -195,9 +195,28 @@ fn axis_margins(c: &Chart, ax: &Axes, area: Rect) -> Margins {
|
||||
|
||||
/// Room for the vertical axis title on the plot's left: one line when the axis that sits there
|
||||
/// (the value axis for columns, the category axis for bars) has a title.
|
||||
fn title_left(c: &Chart, cat_on: bool, val_on: bool, lh: f32) -> f32 {
|
||||
/// A horizontal title (`rot="0"`) gets a strip as wide as its wrapped lines, at most a quarter of
|
||||
/// `area_w`, so the plot keeps its room.
|
||||
fn title_left(c: &Chart, cat_on: bool, val_on: bool, area_w: f32) -> f32 {
|
||||
let side = if c.horizontal { cat_on && c.cat.title.is_some() } else { val_on && c.val.title.is_some() };
|
||||
if side { lh } else { 0.0 }
|
||||
if !side {
|
||||
return 0.0;
|
||||
}
|
||||
match flat_title_lines(c, area_w) {
|
||||
Some(lines) => widest(&lines, c.text_size).min((area_w * 0.25).max(0.0)) + GAP,
|
||||
None => c.text_size * LINE,
|
||||
}
|
||||
}
|
||||
|
||||
/// The wrapped lines of the left axis's title when the file keeps it horizontal (`rot="0"`).
|
||||
fn flat_title_lines(c: &Chart, area_w: f32) -> Option<Vec<String>> {
|
||||
let left = if c.horizontal { &c.cat } else { &c.val };
|
||||
let r = left.title_rot?.rem_euclid(360.0);
|
||||
if !(r < 1.0 || r > 359.0) {
|
||||
return None;
|
||||
}
|
||||
let width = if area_w.is_finite() { (area_w * 0.18).max(1.0) } else { 1.0 };
|
||||
left.title.as_ref().map(|t| wrap(t, width, c.text_size))
|
||||
}
|
||||
|
||||
/// Room for the horizontal axis title under the plot: one line when the axis that sits there
|
||||
@@ -236,7 +255,7 @@ pub(super) fn cartesian(out: &mut Vec<GraphicItem>, c: &Chart, area: Rect) {
|
||||
draw_axes(out, c, &g);
|
||||
draw_data_labels(out, &labels, c.text_size, g.horiz && c.kind == Kind::Bar);
|
||||
if !c.val.deleted && c.val.labels {
|
||||
let room = title_left(c, !c.cat.deleted, true, c.text_size * LINE);
|
||||
let room = title_left(c, !c.cat.deleted, true, area.w);
|
||||
draw_value_labels(out, c, &g, &ax.vticks, Rect { x: area.x + room, w: m.left - room, ..area });
|
||||
}
|
||||
if !c.cat.deleted && c.cat.labels {
|
||||
@@ -254,9 +273,19 @@ fn draw_axis_titles(out: &mut Vec<GraphicItem>, c: &Chart, g: &Frame, area: Rect
|
||||
// bars) gets its title rotated there; the other axis's title sits under the plot.
|
||||
let (left, bottom) = if c.horizontal { (&c.cat, &c.val) } else { (&c.val, &c.cat) };
|
||||
// Word turns the vertical axis's title counter-clockwise (reading upwards) unless the file
|
||||
// turns it the other way; a title with any other rotation would not fit the strip.
|
||||
// turns it the other way or keeps it horizontal (`rot="0"`, wrapped in its own strip); any
|
||||
// other rotation would not fit the strip and is read as counter-clockwise.
|
||||
let up = if left.title_rot.is_some_and(|r| (r.rem_euclid(360.0) - 90.0).abs() < 1.0) { 90.0 } else { -90.0 };
|
||||
if !left.deleted
|
||||
&& let Some(lines) = flat_title_lines(c, area.w)
|
||||
{
|
||||
let strip = (g.plot.x - area.x - GAP).max(1.0);
|
||||
let top = g.plot.y + g.plot.h / 2.0 - lines.len() as f32 * lh / 2.0;
|
||||
for (k, l) in lines.iter().enumerate() {
|
||||
let l = clip_text(l, strip, c.text_size);
|
||||
text_angle(out, Rect { x: area.x, y: top + k as f32 * lh, w: strip, h: lh }, &l, c.text_size, TextAlign::Center, false, 0.0);
|
||||
}
|
||||
} else if !left.deleted
|
||||
&& let Some(t) = &left.title
|
||||
{
|
||||
let t = clip_text(t, g.plot.h, c.text_size);
|
||||
@@ -450,7 +479,7 @@ fn bars(out: &mut Vec<GraphicItem>, c: &Chart, g: &Frame, labels: &mut Vec<DataL
|
||||
(true, false) => (if v >= 0.0 { p1 } else { p0 }, mid),
|
||||
(false, false) => (mid, if v >= 0.0 { p0 } else { p1 }),
|
||||
};
|
||||
labels.push(DataLabel { x, y, text, inside: stacked });
|
||||
labels.push(DataLabel { x, y, text, inside: stacked, pos: LabelPos::Above, off: 0.0 });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -480,20 +509,32 @@ fn lines(out: &mut Vec<GraphicItem>, c: &Chart, g: &Frame, labels: &mut Vec<Data
|
||||
.collect();
|
||||
for (i, p) in pts.iter().enumerate() {
|
||||
if let (Some((x, y)), Some(text)) = (p, label_of(s, i)) {
|
||||
labels.push(DataLabel { x: *x, y: *y, text, inside: false });
|
||||
labels.push(DataLabel {
|
||||
x: *x,
|
||||
y: *y,
|
||||
text,
|
||||
inside: false,
|
||||
pos: s.label_pos.unwrap_or(LabelPos::Right),
|
||||
off: marker_half(s, s.markers == Some(true)) + 2.0,
|
||||
});
|
||||
}
|
||||
}
|
||||
if s.line {
|
||||
stroke(out, curve(pts.iter().copied(), s.smooth), c.series_colour(j), s.line_w.unwrap_or(LINE_W));
|
||||
}
|
||||
if s.markers == Some(true) {
|
||||
markers(out, pts.iter().flatten().copied(), c.series_colour(j), s);
|
||||
markers(out, pts.iter().flatten().copied(), c.marker_colour(j), s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A series' point markers in the series' symbol and size (`colour` fills or strokes them), for
|
||||
/// lines and scatter charts.
|
||||
/// Half a series' marker size, points; zero when it draws no markers.
|
||||
fn marker_half(s: &Series, drawn: bool) -> f32 {
|
||||
if drawn { s.marker_size.filter(|v| v.is_finite()).unwrap_or(6.0).clamp(2.0, 72.0) / 2.0 } else { 0.0 }
|
||||
}
|
||||
|
||||
fn markers(out: &mut Vec<GraphicItem>, pts: impl Iterator<Item = (f32, f32)>, colour: Rgb, s: &Series) {
|
||||
const MARKER: f32 = 6.0;
|
||||
let size = s.marker_size.filter(|v| v.is_finite()).unwrap_or(MARKER).clamp(2.0, 72.0);
|
||||
@@ -588,7 +629,7 @@ fn areas(out: &mut Vec<GraphicItem>, c: &Chart, g: &Frame, labels: &mut Vec<Data
|
||||
fill(out, segs, c.series_colour(j));
|
||||
for (i, (t, b)) in top.iter().zip(&base).enumerate() {
|
||||
if let Some(text) = label_of(s, i) {
|
||||
labels.push(DataLabel { x: g.cat_x(i), y: g.val((t + b) / 2.0), text, inside: true });
|
||||
labels.push(DataLabel { x: g.cat_x(i), y: g.val((t + b) / 2.0), text, inside: true, pos: LabelPos::Above, off: 0.0 });
|
||||
}
|
||||
}
|
||||
if c.stacked() {
|
||||
@@ -599,7 +640,6 @@ fn areas(out: &mut Vec<GraphicItem>, c: &Chart, g: &Frame, labels: &mut Vec<Data
|
||||
|
||||
/// Scatter series as lines, and as small markers when the series asks for them.
|
||||
fn draw_scatter(out: &mut Vec<GraphicItem>, c: &Chart, g: &Frame, labels: &mut Vec<DataLabel>) {
|
||||
const MARKER: f32 = 6.0;
|
||||
for (j, s) in c.series.iter().enumerate() {
|
||||
let pts: Vec<Option<(f32, f32)>> =
|
||||
s.xs.iter()
|
||||
@@ -611,14 +651,21 @@ fn draw_scatter(out: &mut Vec<GraphicItem>, c: &Chart, g: &Frame, labels: &mut V
|
||||
.collect();
|
||||
for (i, p) in pts.iter().enumerate() {
|
||||
if let (Some((x, y)), Some(text)) = (p, label_of(s, i)) {
|
||||
labels.push(DataLabel { x: *x, y: *y - MARKER / 2.0, text, inside: false });
|
||||
labels.push(DataLabel {
|
||||
x: *x,
|
||||
y: *y,
|
||||
text,
|
||||
inside: false,
|
||||
pos: s.label_pos.unwrap_or(LabelPos::Right),
|
||||
off: marker_half(s, s.markers != Some(false)) + 2.0,
|
||||
});
|
||||
}
|
||||
}
|
||||
if s.line {
|
||||
stroke(out, curve(pts.iter().copied(), s.smooth), c.series_colour(j), s.line_w.unwrap_or(LINE_W));
|
||||
}
|
||||
if s.markers != Some(false) {
|
||||
markers(out, pts.iter().flatten().copied(), c.series_colour(j), s);
|
||||
markers(out, pts.iter().flatten().copied(), c.marker_colour(j), s);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::canvas::{Rect, clip_text, fill, stroke, text, text_w, widest};
|
||||
use super::format::format_value;
|
||||
use super::model::{DataLabel, Kind, Series};
|
||||
use super::model::{DataLabel, Kind, LabelPos, Series};
|
||||
use super::plot::{Axes, has_values};
|
||||
use super::{Chart, FRAC_PI_2, GAP, GRID, GraphicItem, LINE, LINE_W, PathSeg, Rgb, ShapeKind, TAU, TEXT, TextAlign};
|
||||
|
||||
@@ -38,7 +38,7 @@ pub(super) fn pie(out: &mut Vec<GraphicItem>, c: &Chart, area: Rect) {
|
||||
// Halfway across the ring (pie: two thirds out from the centre).
|
||||
let at = if inner > 0.0 { (r + inner) / 2.0 } else { r * 0.65 };
|
||||
let (x, y) = polar(cx, cy, at, (a + b) / 2.0);
|
||||
labels.push(DataLabel { x: x as f32, y: y as f32, text, inside: true });
|
||||
labels.push(DataLabel { x: x as f32, y: y as f32, text, inside: true, pos: LabelPos::Above, off: 0.0 });
|
||||
}
|
||||
a = b;
|
||||
}
|
||||
@@ -99,7 +99,7 @@ pub(super) fn radar(out: &mut Vec<GraphicItem>, c: &Chart, area: Rect) {
|
||||
out.push(GraphicItem::Shape { rect, kind: ShapeKind::Ellipse, fill: Some(col), stroke: None, stroke_width: 0.0 });
|
||||
}
|
||||
if let Some(text) = label_of(s, i) {
|
||||
labels.push(DataLabel { x: p.0 as f32, y: p.1 as f32, text, inside: false });
|
||||
labels.push(DataLabel { x: p.0 as f32, y: p.1 as f32, text, inside: false, pos: LabelPos::Above, off: 0.0 });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -165,6 +165,15 @@ pub(super) fn draw_data_labels(out: &mut Vec<GraphicItem>, labels: &[DataLabel],
|
||||
let w = text_w(&l.text, fs) + 4.0;
|
||||
let (rect, align) = if l.inside {
|
||||
(Rect { x: l.x - w / 2.0, y: l.y - lh / 2.0, w, h: lh }, TextAlign::Center)
|
||||
} else if l.off > 0.0 || l.pos != LabelPos::Above {
|
||||
let o = l.off;
|
||||
match l.pos {
|
||||
LabelPos::Right => (Rect { x: l.x + o, y: l.y - lh / 2.0, w, h: lh }, TextAlign::Left),
|
||||
LabelPos::Left => (Rect { x: l.x - o - w, y: l.y - lh / 2.0, w, h: lh }, TextAlign::Right),
|
||||
LabelPos::Below => (Rect { x: l.x - w / 2.0, y: l.y + o, w, h: lh }, TextAlign::Center),
|
||||
LabelPos::Center => (Rect { x: l.x - w / 2.0, y: l.y - lh / 2.0, w, h: lh }, TextAlign::Center),
|
||||
LabelPos::Above => (Rect { x: l.x - w / 2.0, y: l.y - o - lh, w, h: lh }, TextAlign::Center),
|
||||
}
|
||||
} else if beside_right {
|
||||
(Rect { x: l.x + GAP / 2.0, y: l.y - lh / 2.0, w, h: lh }, TextAlign::Left)
|
||||
} else {
|
||||
|
||||
@@ -743,9 +743,90 @@ fn value_axis_title_turns_counter_clockwise_like_word() {
|
||||
assert_eq!(angle(&line_with("", &title(r#"<a:bodyPr rot="-5400000" vert="horz"/>"#))), Some(-90.0));
|
||||
assert_eq!(angle(&line_with("", &title("<a:bodyPr/>"))), Some(-90.0));
|
||||
assert_eq!(angle(&line_with("", &title(r#"<a:bodyPr rot="5400000" vert="horz"/>"#))), Some(90.0));
|
||||
// A horizontal title has no room beside the plot: it stays turned up.
|
||||
assert_eq!(angle(&line_with("", &title(r#"<a:bodyPr rot="0" vert="horz"/>"#))), Some(-90.0));
|
||||
// A horizontal title (`rot="0"`) stays horizontal: the plot makes room for it (see below).
|
||||
assert_eq!(angle(&line_with("", &title(r#"<a:bodyPr rot="0" vert="horz"/>"#))), Some(0.0));
|
||||
// Hostile values stay finite.
|
||||
let odd = angle(&line_with("", &title(r#"<a:bodyPr rot="99999999999999999999"/>"#)));
|
||||
assert!(odd.is_some_and(f32::is_finite));
|
||||
}
|
||||
|
||||
fn first_ellipse(items: &[GraphicItem]) -> Option<([f32; 4], Rgb)> {
|
||||
items.iter().find_map(|it| match it {
|
||||
GraphicItem::Shape { kind: ShapeKind::Ellipse, rect, fill: Some(c), .. } if rect[2] > 3.0 && rect[2] < 20.0 => Some((*rect, *c)),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn markers_use_the_automatic_series_colour_not_the_line_colour() {
|
||||
// The series has an explicit red line/fill but no marker fill: Word draws the markers in the
|
||||
// palette colour of the series index (accent 1 here).
|
||||
let items = line_with(&marker_xml("circle", 9), "");
|
||||
assert_eq!(first_ellipse(&items).map(|e| e.1), Some(theme()[4]));
|
||||
// A marker's own fill wins.
|
||||
let own =
|
||||
r#"<c:marker><c:symbol val="circle"/><c:size val="9"/><c:spPr><a:solidFill><a:srgbClr val="00AA00"/></a:solidFill></c:spPr></c:marker>"#;
|
||||
assert_eq!(first_ellipse(&line_with(own, "")).map(|e| e.1), Some(Rgb(0, 0xAA, 0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn line_data_labels_default_to_the_right_and_honour_dlblpos() {
|
||||
let dl = |pos: &str| format!(r#"{}<c:dLbls>{pos}<c:showVal val="1"/></c:dLbls>"#, marker_xml("circle", 9));
|
||||
let text_rect = |items: &[GraphicItem]| {
|
||||
items.iter().find_map(|it| if let GraphicItem::Text { text, rect, .. } = it { (text == "3.2").then_some(*rect) } else { None })
|
||||
};
|
||||
let at = |pos: &str| {
|
||||
let items = line_with(&dl(pos), "");
|
||||
let (m, _) = first_ellipse(&items).unwrap();
|
||||
let t = text_rect(&items).unwrap();
|
||||
// Centre of the marker and of the label box.
|
||||
((t[0] + t[2] / 2.0) - (m[0] + m[2] / 2.0), (t[1] + t[3] / 2.0) - (m[1] + m[3] / 2.0), t, m)
|
||||
};
|
||||
let (dx, dy, t, m) = at("");
|
||||
assert!(t[0] >= m[0] + m[2] && dy.abs() < 1.0, "default is right of the marker: {dx} {dy}");
|
||||
let (dx, dy, t, m) = at(r#"<c:dLblPos val="r"/>"#);
|
||||
assert!(t[0] >= m[0] + m[2] && dy.abs() < 1.0, "{dx} {dy}");
|
||||
let (dx, dy, _, _) = at(r#"<c:dLblPos val="t"/>"#);
|
||||
assert!(dx.abs() < 1.0 && dy < -4.0, "{dx} {dy}");
|
||||
let (dx, dy, _, _) = at(r#"<c:dLblPos val="b"/>"#);
|
||||
assert!(dx.abs() < 1.0 && dy > 4.0, "{dx} {dy}");
|
||||
let (dx, dy, t, m) = at(r#"<c:dLblPos val="l"/>"#);
|
||||
assert!(t[0] + t[2] <= m[0] && dy.abs() < 1.0, "{dx} {dy}");
|
||||
let (dx, dy, _, _) = at(r#"<c:dLblPos val="ctr"/>"#);
|
||||
assert!(dx.abs() < 1.0 && dy.abs() < 1.0, "{dx} {dy}");
|
||||
// Unknown positions fall back to the default.
|
||||
let (_, _, t, m) = at(r#"<c:dLblPos val="wibble"/>"#);
|
||||
assert!(t[0] >= m[0] + m[2]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn horizontal_value_axis_title_gets_its_own_strip() {
|
||||
let title = |rot: &str, name: &str| {
|
||||
format!(r#"<c:title><c:tx><c:rich><a:bodyPr{rot}/><a:p><a:r><a:t>{name}</a:t></a:r></a:p></c:rich></c:tx></c:title>"#)
|
||||
};
|
||||
let plot_left = |items: &[GraphicItem]| {
|
||||
// The first gridline's start is the plot's left edge.
|
||||
items.iter().find_map(|it| match it {
|
||||
GraphicItem::Path { segs, stroke: Some(_), fill: None, .. } if segs.len() == 2 => match segs.first() {
|
||||
Some(PathSeg::Move(x, _)) => Some(*x),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
})
|
||||
};
|
||||
let rect_of = |items: &[GraphicItem], name: &str| {
|
||||
items.iter().find_map(|it| if let GraphicItem::Text { text, rect, .. } = it { text.starts_with(name).then_some(*rect) } else { None })
|
||||
};
|
||||
let turned = line_with("", &title(r#" rot="-5400000" vert="horz""#, "Revenue"));
|
||||
let flat = line_with("", &title(r#" rot="0" vert="horz""#, "Revenue"));
|
||||
let (a, b) = (plot_left(&turned).unwrap(), plot_left(&flat).unwrap());
|
||||
assert!(b > a + 10.0, "a horizontal title takes more room than a turned one: {a} {b}");
|
||||
// The title box lies left of the plot and inside the chart.
|
||||
let r = rect_of(&flat, "Revenue").unwrap();
|
||||
assert!(r[0] >= 0.0 && r[0] + r[2] <= b + 0.5, "{r:?} {b}");
|
||||
// A hostile title never takes more than a quarter of the width and stays finite.
|
||||
let long = "Wibble ".repeat(500);
|
||||
let items = line_with("", &title(r#" rot="0""#, &long));
|
||||
let p = plot_left(&items).unwrap();
|
||||
assert!(p.is_finite() && p <= 300.0 * 0.3, "{p}");
|
||||
}
|
||||
Reference in new issue
Block a user