Source code for energytrackr.plot.builtin_plot_objects.legend_policy
"""LegendPolicy - sets click_policy on all legend entries."""from__future__importannotationsfromdataclassesimportdataclass,fieldfromtypingimportAny,Never,castfrombokeh.core.enumsimportLegendClickPolicy,LegendClickPolicyTypefrombokeh.modelsimportLegendfrombokeh.plottingimportfigurefromenergytrackr.plot.core.contextimportContextfromenergytrackr.plot.core.interfacesimportConfigurable,PlotObjfromenergytrackr.utils.exceptionsimportInvalidLegendClickPolicyError
[docs]@dataclass(frozen=True)classLegendPolicyConfig:"""Configuration for LegendPolicy. Attributes: policy (LegendClickPolicyType): The click policy for the legend. Options are "hide", "mute", or "disable". """policy:LegendClickPolicyType=field(default="hide")
[docs]classLegendPolicy(PlotObj,Configurable[LegendPolicyConfig]):"""A PlotObj that sets click_policy on all legend entries."""def__init__(self,**params:dict[str,Any])->None:"""Initialize the LegendPolicy with a click policy. Args: **params: Configuration parameters for the LegendPolicy. Raises: InvalidLegendClickPolicyError: If the provided policy is not valid. """super().__init__(LegendPolicyConfig,**params)if(policy:=self.config.policy)isNone:policy=cast(LegendClickPolicyType,LegendClickPolicy._default)if(policy_str:=str(policy))notinLegendClickPolicy:valid=list(LegendClickPolicy)raiseInvalidLegendClickPolicyError(policy_str,valid)
[docs]defadd(self,ctx:Context,fig:figure)->None:# noqa: ARG002"""Adds legend policy and styling to the figure's legends in the given context. This method sets the `click_policy` and `label_text_font` attributes for each `Legend` object found in the figure associated with the provided context. Args: ctx (Context): The plotting context containing the figure (`fig`) to modify. fig (figure): The Bokeh figure to which the legend policy will be applied. """legends:list[Legend]|list[Never]=fig.legendifisinstance(fig.legend,(list,tuple))else[fig.legend]forlegendinlegends:ifisinstance(legend,Legend):legend.click_policy=self.config.policylegend.label_text_font="Roboto"legend.location="top_left"