from typing import Optional, List
from decimal import Decimal
from validator_collection import validators, checkers
from highcharts_core import constants, errors
from highcharts_core.decorators import class_sensitive
from highcharts_core.options.series.data.base import DataBase
from highcharts_core.options.series.data.collections import DataPointCollection
from highcharts_core.options.plot_options.drag_drop import DragDropOptions
from highcharts_core.utility_classes.data_labels import DataLabel
[docs]class ConnectionBase(DataBase):
"""Data point that represents a connection ``from`` one data point, ``to`` another."""
def __init__(self, **kwargs):
self._from_ = None
self._to = None
self.from_ = kwargs.get('from_', None)
self.to = kwargs.get('to', None)
super().__init__(**kwargs)
@property
def from_(self) -> Optional[str]:
"""The node that the link runs from. Defaults to :obj:`None <python:None>`.
:rtype: :class:`str <python:str>` or :obj:`None <python:None>`
"""
return self._from_
@from_.setter
def from_(self, value):
self._from_ = validators.string(value, allow_empty = True)
@property
def to(self) -> Optional[str]:
"""The node that the link runs to. Defaults to :obj:`None <python:None>`.
:rtype: :class:`str <python:str>` or :obj:`None <python:None>`
"""
return self._to
@to.setter
def to(self, value):
self._to = validators.string(value, allow_empty = True)
[docs] @classmethod
def from_list(cls, value):
if not value:
return []
elif checkers.is_string(value):
try:
value = validators.json(value)
except (ValueError, TypeError):
pass
elif not checkers.is_iterable(value):
value = [value]
collection = []
for item in value:
if checkers.is_type(item, cls):
as_obj = item
elif checkers.is_dict(item):
as_obj = cls.from_dict(item)
elif item is None or isinstance(item, constants.EnforcedNullType):
as_obj = cls()
else:
raise errors.HighchartsValueError(f'each data point supplied must either '
f'be a Connection Data Point or be '
f'coercable to one. Could not coerce: '
f'{item}')
collection.append(as_obj)
return collection
@classmethod
def _get_kwargs_from_dict(cls, as_dict):
"""Convenience method which returns the keyword arguments used to initialize the
class from a Highcharts Javascript-compatible :class:`dict <python:dict>` object.
:param as_dict: The HighCharts JS compatible :class:`dict <python:dict>`
representation of the object.
:type as_dict: :class:`dict <python:dict>`
:returns: The keyword arguments that would be used to initialize an instance.
:rtype: :class:`dict <python:dict>`
"""
kwargs = {
'accessibility': as_dict.get('accessibility', None),
'class_name': as_dict.get('className', None),
'color': as_dict.get('color', None),
'color_index': as_dict.get('colorIndex', None),
'custom': as_dict.get('custom', None),
'description': as_dict.get('description', None),
'events': as_dict.get('events', None),
'id': as_dict.get('id', None),
'label_rank': as_dict.get('labelrank',
None) or as_dict.get('labelRank',
None),
'name': as_dict.get('name', None),
'selected': as_dict.get('selected', None),
'from_': as_dict.get('from', None),
'to': as_dict.get('to', None),
}
return kwargs
def _to_untrimmed_dict(self, in_cls = None) -> dict:
untrimmed = {
'from': self.from_,
'to': self.to,
'accessibility': self.accessibility,
'className': self.class_name,
'color': self.color,
'colorIndex': self.color_index,
'custom': self.custom,
'description': self.description,
'events': self.events,
'id': self.id,
'labelrank': self.label_rank,
'name': self.name,
'selected': self.selected,
}
return untrimmed
[docs]class ConnectionData(ConnectionBase):
"""Data point that represents a connection ``from`` one data point, ``to`` another."""
def __init__(self, **kwargs):
self._data_labels = None
self._drag_drop = None
self.data_labels = kwargs.get('data_labels', None)
self.drag_drop = kwargs.get('drag_drop', None)
super().__init__(**kwargs)
@property
def data_labels(self) -> Optional[DataLabel]:
"""Individual data label for the data point.
:rtype: :class:`DataLabel` or :obj:`None <python:None>`
"""
return self._data_labels
@data_labels.setter
@class_sensitive(DataLabel)
def data_labels(self, value):
self._data_labels = value
@property
def drag_drop(self) -> Optional[DragDropOptions]:
"""The draggable-points module allows points to be moved around or modified in the
chart.
In addition to the options mentioned under the dragDrop API structure, the module
fires three (JavaScript) events:
* ``point.dragStart``
* ``point.drag``
* ``point.drop``
:rtype: :class:`DragDropOptions` or :obj:`None <python:None>`
"""
return self._drag_drop
@drag_drop.setter
@class_sensitive(DragDropOptions)
def drag_drop(self, value):
self._drag_drop = value
[docs] @classmethod
def from_list(cls, value):
"""Generator method which produces a collection of :class:`ConnectionData`
instances derived from ``value``. Generally consumed by the setter methods in
series-type specific data classes.
:rtype: :class:`list <python:list>` of :obj:`ConnectionData` instances
"""
if not value:
return []
elif checkers.is_string(value):
try:
value = validators.json(value)
except (ValueError, TypeError):
pass
elif not checkers.is_iterable(value):
value = [value]
collection = []
for item in value:
if checkers.is_type(item, 'ConnectionData'):
as_obj = item
elif checkers.is_dict(item):
as_obj = cls.from_dict(item)
elif item is None or isinstance(item, constants.EnforcedNullType):
as_obj = cls()
elif len(item) == 2:
as_obj = cls(from_ = item[0],
to = item[1])
else:
raise errors.HighchartsValueError(f'each data point supplied must either '
f'be a Connection Data Point or be '
f'coercable to one. Could not coerce: '
f'{item}')
collection.append(as_obj)
return collection
@classmethod
def _get_supported_dimensions(cls) -> List[int]:
"""Returns a list of the supported dimensions for the data point.
:rtype: :class:`list <python:list>` of :class:`int <python:int>`
"""
return [1, 2]
[docs] @classmethod
def from_ndarray(cls, value):
"""Creates a collection of data points from a `NumPy <https://numpy.org>`__
:class:`ndarray <numpy:ndarray>` instance.
:returns: A collection of data point values.
:rtype: :class:`DataPointCollection <highcharts_core.options.series.data.collections.DataPointCollection>`
"""
return ConnectionDataCollection.from_ndarray(value)
@classmethod
def _get_props_from_array(cls, length = None) -> List[str]:
"""Returns a list of the property names that can be set using the
:meth:`.from_array() <highcharts_core.options.series.data.base.DataBase.from_array>`
method.
:param length: The length of the array, which may determine the properties to
parse. Defaults to :obj:`None <python:None>`, which returns the full list of
properties.
:type length: :class:`int <python:int>` or :obj:`None <python:None>`
:rtype: :class:`list <python:list>` of :class:`str <python:str>`
"""
prop_list = {
None: ['from_', 'to'],
2: ['from_', 'to']
}
return cls._get_props_from_array_helper(prop_list, length)
@classmethod
def _get_kwargs_from_dict(cls, as_dict):
"""Convenience method which returns the keyword arguments used to initialize the
class from a Highcharts Javascript-compatible :class:`dict <python:dict>` object.
:param as_dict: The HighCharts JS compatible :class:`dict <python:dict>`
representation of the object.
:type as_dict: :class:`dict <python:dict>`
:returns: The keyword arguments that would be used to initialize an instance.
:rtype: :class:`dict <python:dict>`
"""
kwargs = {
'accessibility': as_dict.get('accessibility', None),
'class_name': as_dict.get('className', None),
'color': as_dict.get('color', None),
'color_index': as_dict.get('colorIndex', None),
'custom': as_dict.get('custom', None),
'description': as_dict.get('description', None),
'events': as_dict.get('events', None),
'id': as_dict.get('id', None),
'label_rank': as_dict.get('labelrank',
None) or as_dict.get('labelRank',
None),
'name': as_dict.get('name', None),
'selected': as_dict.get('selected', None),
'data_labels': as_dict.get('dataLabels', None),
'drag_drop': as_dict.get('dragDrop', None),
'from_': as_dict.get('from', None),
'to': as_dict.get('to', None),
}
return kwargs
def _to_untrimmed_dict(self, in_cls = None) -> dict:
untrimmed = {
'dataLabels': self.data_labels,
'dragDrop': self.drag_drop,
}
parent_as_dict = super()._to_untrimmed_dict(in_cls = in_cls) or {}
for key in parent_as_dict:
untrimmed[key] = parent_as_dict[key]
return untrimmed
[docs]class ConnectionDataCollection(DataPointCollection):
@classmethod
def _get_data_point_class(cls):
"""The Python class to use as the underlying data point within the Collection.
:rtype: class object
"""
return ConnectionData
[docs]class WeightedConnectionData(ConnectionData):
"""Variant of :class:`ConnectionData` that also applies a ``weight`` to the
connection."""
def __init__(self, **kwargs):
self._weight = None
self.weight = kwargs.get('weight', None)
super().__init__(**kwargs)
@property
def weight(self) -> Optional[int | float | Decimal]:
"""The weight of the link. Defaults to :obj:`None <python:None>`.
:rtype: numeric or :obj:`None <python:None>`
"""
return self._weight
@weight.setter
def weight(self, value):
self._weight = validators.numeric(value, allow_empty = True)
[docs] @classmethod
def from_list(cls, value):
"""Generator method which produces a collection of :class:`ConnectionData`
instances derived from ``value``. Generally consumed by the setter methods in
series-type specific data classes.
:rtype: :class:`list <python:list>` of :obj:`ConnectionData` instances
"""
if not value:
return []
elif checkers.is_string(value):
try:
value = validators.json(value)
except (ValueError, TypeError):
pass
elif not checkers.is_iterable(value):
value = [value]
collection = []
for item in value:
if checkers.is_type(item, 'ConnectionData'):
as_obj = item
elif checkers.is_dict(item):
as_obj = cls.from_dict(item)
elif item is None or isinstance(item, constants.EnforcedNullType):
as_obj = cls()
else:
raise errors.HighchartsValueError(f'each data point supplied must either '
f'be a Connection Data Point or be '
f'coercable to one. Could not coerce: '
f'{item}')
collection.append(as_obj)
return collection
[docs] @classmethod
def from_ndarray(cls, value):
"""Creates a collection of data points from a `NumPy <https://numpy.org>`__
:class:`ndarray <numpy:ndarray>` instance.
:returns: A collection of data point values.
:rtype: :class:`DataPointCollection <highcharts_core.options.series.data.collections.DataPointCollection>`
"""
return WeightedConnectionDataCollection.from_ndarray(value)
@classmethod
def _get_supported_dimensions(cls) -> List[int]:
"""Returns a list of the supported dimensions for the data point.
:rtype: :class:`list <python:list>` of :class:`int <python:int>`
"""
return [1]
@classmethod
def _get_props_from_array(cls, length = None) -> List[str]:
"""Returns a list of the property names that can be set using the
:meth:`.from_array() <highcharts_core.options.series.data.base.DataBase.from_array>`
method.
:param length: The length of the array, which may determine the properties to
parse. Defaults to :obj:`None <python:None>`, which returns the full list of
properties.
:type length: :class:`int <python:int>` or :obj:`None <python:None>`
:rtype: :class:`list <python:list>` of :class:`str <python:str>`
"""
prop_list = {}
return cls._get_props_from_array_helper(prop_list, length)
@classmethod
def _get_kwargs_from_dict(cls, as_dict):
"""Convenience method which returns the keyword arguments used to initialize the
class from a Highcharts Javascript-compatible :class:`dict <python:dict>` object.
:param as_dict: The HighCharts JS compatible :class:`dict <python:dict>`
representation of the object.
:type as_dict: :class:`dict <python:dict>`
:returns: The keyword arguments that would be used to initialize an instance.
:rtype: :class:`dict <python:dict>`
"""
kwargs = {
'accessibility': as_dict.get('accessibility', None),
'class_name': as_dict.get('className', None),
'color': as_dict.get('color', None),
'color_index': as_dict.get('colorIndex', None),
'custom': as_dict.get('custom', None),
'description': as_dict.get('description', None),
'events': as_dict.get('events', None),
'id': as_dict.get('id', None),
'label_rank': as_dict.get('labelrank',
None) or as_dict.get('labelRank',
None),
'name': as_dict.get('name', None),
'selected': as_dict.get('selected', None),
'data_labels': as_dict.get('dataLabels', None),
'drag_drop': as_dict.get('dragDrop', None),
'from_': as_dict.get('from', None),
'to': as_dict.get('to', None),
'weight': as_dict.get('weight', None),
}
return kwargs
def _to_untrimmed_dict(self, in_cls = None) -> dict:
untrimmed = {
'weight': self.weight,
'dataLabels': self.data_labels,
'dragDrop': self.drag_drop,
'from': self.from_,
'to': self.to,
'accessibility': self.accessibility,
'className': self.class_name,
'color': self.color,
'colorIndex': self.color_index,
'custom': self.custom,
'description': self.description,
'events': self.events,
'id': self.id,
'labelrank': self.label_rank,
'name': self.name,
'selected': self.selected
}
return untrimmed
[docs]class WeightedConnectionDataCollection(DataPointCollection):
@classmethod
def _get_data_point_class(cls):
"""The Python class to use as the underlying data point within the Collection.
:rtype: class object
"""
return WeightedConnectionData
[docs]class OutgoingWeightedConnectionData(WeightedConnectionData):
"""Variant of :class:`WeightedConnectionData` that supports the ``outoging`` flag."""
def __init__(self, **kwargs):
self._outgoing = None
self.outgoing = kwargs.get('outgoing', None)
super().__init__(**kwargs)
@property
def outgoing(self) -> Optional[bool]:
"""If ``True``, indicates that the data point links out of the system. Defaults
to :obj:`None <python:None>`, which behaves as ``False``.
:rtype: :class:`bool <python:bool>` or :obj:`None <python:None>`
"""
return self._outgoing
@outgoing.setter
def outgoing(self, value):
if value is None:
self._outgoing = None
else:
self._outgoing = bool(value)
[docs] @classmethod
def from_list(cls, value):
"""Generator method which produces a collection of
:class:`OutgoingWeightedConnectionData` instances derived from ``value``.
Generally consumed by the setter methods in series-type specific data classes.
:rtype: :class:`list <python:list>` of :obj:`OutgoingWeightedConnectionData`
instances
"""
if not value:
return []
elif checkers.is_string(value):
try:
value = validators.json(value)
except (ValueError, TypeError):
pass
elif not checkers.is_iterable(value):
value = [value]
collection = []
for item in value:
if checkers.is_type(item, 'OutgoingWeightedConnectionData'):
as_obj = item
elif checkers.is_dict(item):
as_obj = cls.from_dict(item)
elif item is None or isinstance(item, constants.EnforcedNullType):
as_obj = cls()
else:
raise errors.HighchartsValueError(f'each data point supplied must either '
f'be a Connection Data Point or be '
f'coercable to one. Could not coerce: '
f'{item}')
collection.append(as_obj)
return collection
[docs] @classmethod
def from_ndarray(cls, value):
"""Creates a collection of data points from a `NumPy <https://numpy.org>`__
:class:`ndarray <numpy:ndarray>` instance.
:returns: A collection of data point values.
:rtype: :class:`DataPointCollection <highcharts_core.options.series.data.collections.DataPointCollection>`
"""
return OutgoingWeightedConnectionDataCollection.from_ndarray(value)
@classmethod
def _get_kwargs_from_dict(cls, as_dict):
"""Convenience method which returns the keyword arguments used to initialize the
class from a Highcharts Javascript-compatible :class:`dict <python:dict>` object.
:param as_dict: The HighCharts JS compatible :class:`dict <python:dict>`
representation of the object.
:type as_dict: :class:`dict <python:dict>`
:returns: The keyword arguments that would be used to initialize an instance.
:rtype: :class:`dict <python:dict>`
"""
kwargs = {
'outgoing': as_dict.get('outgoing', None),
'weight': as_dict.get('weight', None),
'data_labels': as_dict.get('dataLabels', None),
'drag_drop': as_dict.get('dragDrop', None),
'from_': as_dict.get('from', None),
'to': as_dict.get('to', None),
'accessibility': as_dict.get('accessibility', None),
'class_name': as_dict.get('className', None),
'color': as_dict.get('color', None),
'color_index': as_dict.get('colorIndex', None),
'custom': as_dict.get('custom', None),
'description': as_dict.get('description', None),
'events': as_dict.get('events', None),
'id': as_dict.get('id', None),
'label_rank': as_dict.get('labelrank',
None) or as_dict.get('labelRank',
None),
'name': as_dict.get('name', None),
'selected': as_dict.get('selected', None)
}
return kwargs
def _to_untrimmed_dict(self, in_cls = None) -> dict:
untrimmed = {
'outgoing': self.outgoing
}
parent_as_dict = super()._to_untrimmed_dict(in_cls = in_cls) or {}
for key in parent_as_dict:
untrimmed[key] = parent_as_dict[key]
return untrimmed
[docs]class OutgoingWeightedConnectionDataCollection(DataPointCollection):
@classmethod
def _get_data_point_class(cls):
"""The Python class to use as the underlying data point within the Collection.
:rtype: class object
"""
return OutgoingWeightedConnectionData