|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import math |
4 | | -from typing import TYPE_CHECKING |
| 4 | +from typing import TYPE_CHECKING, Any |
5 | 5 |
|
6 | 6 | import matplotlib.ticker as mticker |
| 7 | +from matplotlib.backend_bases import Event |
| 8 | +from matplotlib.text import Annotation |
| 9 | + |
| 10 | +from ert.gui.tools.plot.plottery.plot_context import PlotType |
7 | 11 |
|
8 | 12 | if TYPE_CHECKING: |
9 | 13 | from datetime import date |
@@ -33,7 +37,7 @@ def __init__( |
33 | 37 | self, |
34 | 38 | low: float = 1e-3, |
35 | 39 | high: float = 1e4, |
36 | | - precision: float = 0, |
| 40 | + precision: int = 0, |
37 | 41 | ) -> None: |
38 | 42 | self.low = low |
39 | 43 | self.high = high |
@@ -61,7 +65,7 @@ class PlotTools: |
61 | 65 | def showGrid(axes: Axes, plot_context: PlotContext) -> None: |
62 | 66 | config = plot_context.plotConfig() |
63 | 67 | if config.isGridEnabled(): |
64 | | - if plot_context.plot_type == "BAR": |
| 68 | + if plot_context.plot_type == PlotType.BAR: |
65 | 69 | axes.grid(axis="y", color="black", alpha=0.1) |
66 | 70 | else: |
67 | 71 | axes.grid(visible=True, color="black", alpha=0.4) |
@@ -163,3 +167,56 @@ def __setupLabels( |
163 | 167 |
|
164 | 168 | if config.yLabel() is None: |
165 | 169 | config.setYLabel(default_y_label) |
| 170 | + |
| 171 | + @staticmethod |
| 172 | + def labels_on_hover( |
| 173 | + axes: Axes, |
| 174 | + plot_context: PlotContext, |
| 175 | + figure: Figure, |
| 176 | + data: tuple[list[Any], list[str]], |
| 177 | + ) -> None: |
| 178 | + annot = axes.annotate( |
| 179 | + "", |
| 180 | + xy=(0, 0), |
| 181 | + xytext=(-40, 10), |
| 182 | + textcoords="offset points", |
| 183 | + bbox={"boxstyle": "round,pad=0.5", "fc": "pink", "alpha": 0.7}, |
| 184 | + arrowprops={"arrowstyle": "->", "connectionstyle": "arc3,rad=0"}, |
| 185 | + ) |
| 186 | + annot.set_visible(False) |
| 187 | + |
| 188 | + if plot_context.plot_type == PlotType.BAR: |
| 189 | + figure.canvas.mpl_connect( |
| 190 | + "motion_notify_event", |
| 191 | + lambda event: PlotTools.__on_hover_bar_group( |
| 192 | + event, axes, annot, figure, data |
| 193 | + ), |
| 194 | + ) |
| 195 | + else: |
| 196 | + raise NotImplementedError( |
| 197 | + f"Hover labels not implemented for: {plot_context.plot_type}" |
| 198 | + ) |
| 199 | + |
| 200 | + @staticmethod |
| 201 | + def __on_hover_bar_group( |
| 202 | + event: Event, |
| 203 | + axes: Axes, |
| 204 | + annot: Annotation, |
| 205 | + figure: Figure, |
| 206 | + data: tuple[list[Any], list[str]], |
| 207 | + ) -> None: |
| 208 | + if event.inaxes == axes: # type: ignore |
| 209 | + for bar_container_idx, bars in enumerate(data[0]): |
| 210 | + for bar_idx, bar in enumerate(bars): |
| 211 | + if bar.contains(event)[0]: |
| 212 | + value = bar.get_height() |
| 213 | + |
| 214 | + annot.xy = (bar.get_x() + bar.get_width() / 2, value) |
| 215 | + idx = bar_idx * len(data[0]) + bar_container_idx |
| 216 | + annot.set_text(f"{data[1][idx]}\nValue: {value:.3g}") |
| 217 | + annot.set_visible(True) |
| 218 | + figure.canvas.draw_idle() |
| 219 | + return |
| 220 | + |
| 221 | + annot.set_visible(False) |
| 222 | + figure.canvas.draw_idle() |
0 commit comments