diff --git a/ladybug_display/visualization.py b/ladybug_display/visualization.py index b13babe..c5762d3 100644 --- a/ladybug_display/visualization.py +++ b/ladybug_display/visualization.py @@ -1455,11 +1455,11 @@ def __init__(self, values, legend_parameters=None, data_type=None, unit=None): assert isinstance(data_type, DataTypeBase), \ 'data_type should be a ladybug DataType. Got {}'.format(type(data_type)) if self.legend_parameters.is_title_default: - unit = unit if unit else data_type.units[0] - data_type.is_unit_acceptable(unit) - self.legend_parameters.title = unit if \ + self._unit = unit if unit else data_type.units[0] + data_type.is_unit_acceptable(self._unit) + self.legend_parameters.title = self._unit if \ self.legend_parameters.vertical \ - else '{} ({})'.format(data_type.name, unit) + else '{} ({})'.format(data_type.name, self._unit) if data_type.unit_descr is not None and \ self.legend_parameters.ordinal_dictionary is None and not \ isinstance(self.legend_parameters, LegendParametersCategorized): @@ -1556,6 +1556,59 @@ def graphic_container(self, min_point, max_point): self.values, min_point, max_point, self._legend_parameters, self._data_type, self._unit) + def convert_to_unit(self, unit): + """Convert the VisualizationData to the input unit. + + Note that the VisualizationData must have a data_type and unit assigned + to it in order for this method to run successfully and not raise an exception. + + Args: + unit: Text indicating the units to which the value should be + converted (eg. 'kWh/m2'). See ladybug.datatype.UNITS for + a dictionary containing all acceptable units for each data type. + """ + assert self._data_type is not None and self._unit is not None, \ + 'VisualizationData must have a data_type and unit assigned in ' \ + 'order to perform unit conversions.' + new_values = self._data_type.to_unit(self.values, unit, self._unit) + self._change_units(new_values, unit) + + def convert_to_ip(self): + """Convert the Data Collection to IP units. + + Note that this mutates the data collection object, which can have unintended + consequences depending on how the data collection is used. Use to_ip to + get a new instance of a collection without mutating this one. + """ + assert self._data_type is not None and self._unit is not None, \ + 'VisualizationData must have a data_type and unit assigned in ' \ + 'order to perform unit conversions.' + new_values, new_unit = self._data_type.to_ip(self.values, self._unit) + self._change_units(new_values, new_unit) + + def convert_to_si(self): + """Convert the Data Collection to SI units. + + Note that this mutates the data collection object, which can have unintended + consequences depending on how the data collection is used. Use to_si to + get a new instance of a collection without mutating this one. + """ + assert self._data_type is not None and self._unit is not None, \ + 'VisualizationData must have a data_type and unit assigned in ' \ + 'order to perform unit conversions.' + new_values, new_unit = self._data_type.to_si(self.values, self._unit) + self._change_units(new_values, new_unit) + + def _change_units(self, new_values, new_unit): + self._unit = new_unit + if self._legend.is_min_default: + self._legend_parameters.min = None + if self._legend.is_max_default: + self._legend_parameters.max = None + self._legend = Legend(new_values, self._legend_parameters) + self.legend_parameters.title = new_unit if self.legend_parameters.vertical \ + else '{} ({})'.format(self._data_type.name, new_unit) + def to_dict(self): """Get visualization data as a dictionary.""" base = {