From 45f1ca2209a98dd5b643072c3248e8fbb675b7d0 Mon Sep 17 00:00:00 2001 From: David Austin Date: Mon, 9 Dec 2024 18:30:36 -0500 Subject: [PATCH] framed axes and x/ylabel --- prefig/core/grid_axes.py | 88 ++++++++++++++++++++++++--- prefig/resources/schema/pf_schema.rnc | 24 +++++++- prefig/resources/schema/pf_schema.rng | 66 ++++++++++++++++++++ 3 files changed, 169 insertions(+), 9 deletions(-) diff --git a/prefig/core/grid_axes.py b/prefig/core/grid_axes.py index 57b01ed..a4e96e9 100644 --- a/prefig/core/grid_axes.py +++ b/prefig/core/grid_axes.py @@ -192,6 +192,7 @@ def axes(element, diagram, parent, outline_status): top_labels = False y_axis_location = 0 y_axis_offsets = (0,0) + h_zero_include = False if bbox[1] * bbox[3] >= 0: if bbox[3] <= 0: top_labels = True @@ -202,11 +203,24 @@ def axes(element, diagram, parent, outline_status): if abs(bbox[1]) > 1e-10: y_axis_location = bbox[1] y_axis_offsets = (5,0) + + h_frame = element.get('h-frame', None) + if h_frame == 'bottom': + y_axis_location = bbox[1] + y_axis_offsets = (0,0) + h_zero_include = True + if h_frame == 'top': + y_axis_location = bbox[3] + y_axis_offsets = (0,0) + h_zero_include = True + top_labels = True + y_axis_offsets = np.array(y_axis_offsets) right_labels = False x_axis_location = 0 x_axis_offsets = (0,0) + v_zero_include = False if bbox[0] * bbox[2] >= 0: if bbox[2] <= 0: right_labels = True @@ -217,8 +231,59 @@ def axes(element, diagram, parent, outline_status): if abs(bbox[0]) > 1e-10: x_axis_location = bbox[0] x_axis_offsets = (10,0) + + v_frame = element.get('v-frame', None) + if v_frame == 'left': + x_axis_location = bbox[0] + x_axis_offsets = (0,0) + v_zero_include = True + if v_frame == 'right': + x_axis_location = bbox[2] + x_axis_offsets = (0,0) + v_zero_include = True + right_labels = True + x_axis_offsets = np.array(x_axis_offsets) + try: + arrows = int(element.get('arrows', '0')) + except: + log.error(f"Error in parsing arrows={element.get('arrows')}") + arrows = 0 + + # process xlabel and ylabel + for child in element: + if child.tag == "xlabel": + child.tag = "label" + child.set("user-coords", "no") + anchor = diagram.transform((bbox[2], y_axis_location)) + child.set("anchor", util.pt2str(anchor, spacer=",")) + if child.get("alignment", None) is None: + child.set("alignment", "east") + if child.get("offset", None) is None: + if arrows > 0: + child.set("offset", "(2,0)") + else: + child.set("offset", "(1,0)") + label.label(child, diagram, parent) + continue + if child.tag == "ylabel": + child.tag = "label" + child.set("user-coords", "no") + anchor = diagram.transform((x_axis_location, bbox[3])) + child.set("anchor", util.pt2str(anchor, spacer=",")) + if child.get("alignment", None) is None: + child.set("alignment", "north") + if child.get("offset", None) is None: + if arrows > 0: + child.set("offset", "(0,2)") + else: + child.set("offset", "(0,1)") + label.label(child, diagram, parent) + continue + log.info(f"{child.tag} element is not allowed inside a