|
| 1 | +"""plot code for metric class.""" |
| 2 | + |
| 3 | +# This only gets called if there's a plot() call |
| 4 | +# This speeds up load time for all the non plot() users |
| 5 | +# |
| 6 | +# Only loads matplotlib etc during __init__ |
| 7 | +# |
| 8 | + |
| 9 | +class MetricPlot: |
| 10 | + r""" |
| 11 | + A Class for `MetricPlot` object. |
| 12 | +
|
| 13 | + Internal use only at present |
| 14 | +
|
| 15 | + """ |
| 16 | + |
| 17 | + def __init__(self, *args, **kwargs): |
| 18 | + """Functions as a Constructor for the Metric object.""" |
| 19 | + try: |
| 20 | + import matplotlib.pyplot as plt |
| 21 | + from pandas.plotting import register_matplotlib_converters |
| 22 | + |
| 23 | + register_matplotlib_converters() |
| 24 | + except ImportError as exce: # noqa F841 |
| 25 | + raise ImportError("matplotlib was not found") |
| 26 | + |
| 27 | + # One graph with potentially N lines - if plot() is called twice |
| 28 | + self._plt = plt |
| 29 | + self._fig, self._axis = self._plt.subplots(*args, **kwargs) |
| 30 | + |
| 31 | + def plot_date(self, metric): |
| 32 | + """Plot a very simple line graph for the metric time-series.""" |
| 33 | + |
| 34 | + # If we made it here, then we know matplotlib is installed and available |
| 35 | + self._axis.plot_date(metric.metric_values.ds, metric.metric_values.y, |
| 36 | + linestyle="solid", |
| 37 | + label=str(metric.metric_name), |
| 38 | + ) |
| 39 | + self._fig.autofmt_xdate() |
| 40 | + # These are provided for documentation reasons only - it's presumptuous for this code to call them |
| 41 | + # self._axis.set_xlabel('Date/Time') |
| 42 | + # self._axis.set_ylabel('Metric') |
| 43 | + # self._axis.set_title('Prometheus') |
| 44 | + if len(self._axis.lines) > 1: |
| 45 | + # We show a legend (or update the legend) if there's more than line on the plot |
| 46 | + self._axis.legend() |
| 47 | + |
| 48 | + def show(self, block=None): |
| 49 | + """convience show() call.""" |
| 50 | + self._plt.show(block=block) |
| 51 | + |
| 52 | + @property |
| 53 | + def plt(self): |
| 54 | + """ pyplot value for present plotting """ |
| 55 | + return self._plt |
| 56 | + |
| 57 | + @property |
| 58 | + def axis(self): |
| 59 | + """ Axis value for present plotting """ |
| 60 | + return self._axis |
| 61 | + |
| 62 | + @property |
| 63 | + def fig(self): |
| 64 | + """ Figure value for present plotting """ |
| 65 | + return self._fig |
0 commit comments