|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +from .wrappers import ProxyWrapper, _stale_wrapper |
| 4 | +from .containers import DataContainer |
| 5 | + |
| 6 | +from matplotlib.patches import Patch as _Patch, Rectangle as _Rectangle |
| 7 | + |
| 8 | + |
| 9 | +class PatchWrapper(ProxyWrapper): |
| 10 | + _wrapped_class = _Patch |
| 11 | + _privtized_methods = ( |
| 12 | + "get_edgecolor", |
| 13 | + "get_facecolor", |
| 14 | + "get_linewidth", |
| 15 | + "get_linestyle", |
| 16 | + "get_antialiased", |
| 17 | + "get_hatch", |
| 18 | + "get_fill", |
| 19 | + "get_capstyle", |
| 20 | + "get_joinstyle", |
| 21 | + "get_path", |
| 22 | + "set_edgecolor", |
| 23 | + "set_facecolor", |
| 24 | + "set_linewidth", |
| 25 | + "set_linestyle", |
| 26 | + "set_antialiased", |
| 27 | + "set_hatch", |
| 28 | + "set_fill", |
| 29 | + "set_capstyle", |
| 30 | + "set_joinstyle", |
| 31 | + "set_path", |
| 32 | + ) |
| 33 | + _xunits = () |
| 34 | + _yunits = () |
| 35 | + required_keys = { |
| 36 | + "edgecolor", |
| 37 | + "facecolor", |
| 38 | + "linewidth", |
| 39 | + "linestyle", |
| 40 | + "antialiased", |
| 41 | + "hatch", |
| 42 | + "fill", |
| 43 | + "capstyle", |
| 44 | + "joinstyle", |
| 45 | + } |
| 46 | + |
| 47 | + def __init__(self, data: DataContainer, nus=None, /, **kwargs): |
| 48 | + super().__init__(data, nus) |
| 49 | + self._wrapped_instance = self._wrapped_class([0, 0], 0, 0, **kwargs) |
| 50 | + |
| 51 | + @_stale_wrapper |
| 52 | + def draw(self, renderer): |
| 53 | + self._update_wrapped(self._query_and_transform(renderer, xunits=self._xunits, yunits=self._yunits)) |
| 54 | + return self._wrapped_instance.draw(renderer) |
| 55 | + |
| 56 | + def _update_wrapped(self, data): |
| 57 | + for k, v in data.items(): |
| 58 | + getattr(self._wrapped_instance, f"set_{k}")(v) |
| 59 | + |
| 60 | + |
| 61 | +class RectangleWrapper(PatchWrapper): |
| 62 | + _wrapped_class = _Rectangle |
| 63 | + _privtized_methods = PatchWrapper._privtized_methods + ( |
| 64 | + "get_x", |
| 65 | + "get_y", |
| 66 | + "get_width", |
| 67 | + "get_height", |
| 68 | + "get_angle", |
| 69 | + "get_rotation_point" "set_x", |
| 70 | + "set_y", |
| 71 | + "set_width", |
| 72 | + "set_height", |
| 73 | + "set_angle", |
| 74 | + "set_rotation_point", |
| 75 | + ) |
| 76 | + _xunits = ("x", "width") |
| 77 | + _yunits = ("y", "height") |
| 78 | + required_keys = PatchWrapper.required_keys | {"x", "y", "width", "height", "angle", "rotation_point"} |
| 79 | + |
| 80 | + def _update_wrapped(self, data): |
| 81 | + for k, v in data.items(): |
| 82 | + if k == "rotation_point": |
| 83 | + self._wrapped_instance.rotation_point = v |
| 84 | + continue |
| 85 | + # linestyle and hatch do not work as arrays, |
| 86 | + # but ArrayContainer requires arrays, so index into an array if needed |
| 87 | + elif k in ("linestyle", "hatch"): |
| 88 | + if isinstance(v, np.ndarray): |
| 89 | + v = v[0] |
| 90 | + getattr(self._wrapped_instance, f"set_{k}")(v) |
0 commit comments