from typing import Optional, List, Dict
from decimal import Decimal
import datetime
from validator_collection import validators, checkers
try:
import numpy as np
HAS_NUMPY = True
except ImportError:
HAS_NUMPY = False
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
from highcharts_core.utility_classes.markers import Marker
[docs]class CartesianData(DataBase):
"""Data point that can be represented on a :term:`Cartesian chart <Cartesian Charts>`,
featuring an ``x`` and ``y`` value."""
def __init__(self, **kwargs):
self._data_labels = None
self._drag_drop = None
self._drilldown = None
self._marker = None
self._x = None
self._y = None
self.data_labels = kwargs.get('data_labels', None)
self.drag_drop = kwargs.get('drag_drop', None)
self.drilldown = kwargs.get('drilldown', None)
self.marker = kwargs.get('marker', None)
self.x = kwargs.get('x', None)
self.y = kwargs.get('y', 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
@property
def drilldown(self) -> Optional[str]:
"""The :meth:`id <SeriesBase.id>` of a series in the ``drilldown.series`` array
to use as a drilldown destination for this point. Defaults to
:obj:`None <python:None>`.
:rtype: :class:`str <python:str>` or :obj:`None <python:None>`
"""
return self._drilldown
@drilldown.setter
def drilldown(self, value):
self._drilldown = validators.string(value, allow_empty = True)
@property
def marker(self) -> Optional[Marker]:
"""Options for the point markers of line-like series.
:rtype: :class:`Marker` or :obj:`None <python:None>`
"""
return self._marker
@marker.setter
@class_sensitive(Marker)
def marker(self, value):
self._marker = value
@property
def x(self) -> Optional[str | datetime.date | datetime.datetime | int | float | Decimal]:
"""The point's location on the x-axis. Defaults to :obj:`None <python:None>`.
If :obj:`None <python:None>`, the point's position on the x-axis will be
automatically determined based on its position in the series'
:meth:`data <SeriesBase.data>` array. The first point will be given an ``x`` value
of ``0``, or the series' :meth:`point_start <SeriesBase.point_start>` value.
Each subsequent point will be incremented either by ``1`` or the value of
:meth:`point_interval <SeriesBase.point_interval>`.
:rtype: numeric or :class:`str <python:str>` or
:class:`date <python:datetime.date>` or
:class:`datetime <python:datetime.datetime>` or :obj:`None <python:None>`
"""
return self._x
@x.setter
def x(self, value):
if value is None:
self._x = None
else:
if checkers.is_datetime(value):
value = validators.datetime(value)
elif checkers.is_date(value):
value = validators.date(value)
elif HAS_NUMPY and hasattr(value, 'dtype') and value.dtype.char == 'M':
value = (value.astype(np.int64) / 10**6).astype(np.int64)
elif checkers.is_numeric(value):
value = validators.numeric(value)
else:
value = validators.string(value)
self._x = value
@property
def y(self) -> Optional[int | float | Decimal | type(None) | constants.EnforcedNullType]:
"""The position of the data point on the Y-axis. Defaults to
:obj:`None <python:None>`.
:rtype: numeric or :class:`EnforcedNullType` or :obj:`None <python:None>`
"""
return self._y
@y.setter
def y(self, value):
if value is None:
self._y = None
elif isinstance(value, constants.EnforcedNullType):
self._y = constants.EnforcedNull
else:
self._y = validators.numeric(value)
[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, 'CartesianData'):
as_obj = item
elif checkers.is_dict(item):
as_obj = cls.from_dict(item)
elif isinstance(item, constants.EnforcedNullType):
as_obj = cls(y = constants.EnforcedNull)
elif item is None or checkers.is_numeric(item):
as_obj = cls(y = item)
elif checkers.is_iterable(item):
if len(item) == 2:
as_obj = cls(x = item[0], y = item[1])
elif len(item) == 1:
as_obj = cls(y = item[0])
else:
raise errors.HighchartsValueError(f'data expects either a 1D or 2D '
f'collection. Collection received '
f'had {len(item)} dimensions.')
else:
raise errors.HighchartsValueError(f'each data point supplied must either '
f'be a Cartesian Data Point or be '
f'coercable to one. Could not coerce: '
f'{item}')
if checkers.is_string(as_obj.x) and not as_obj.name:
as_obj.name = as_obj.x
as_obj.x = None
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 CartesianDataCollection.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: ['x', 'y', 'name'],
1: ['y'],
2: ['x', 'y']
}
return cls._get_props_from_array_helper(prop_list, length)
[docs] def to_array(self, force_object = False) -> List | Dict:
"""Generate the array representation of the data point (the inversion
of
:meth:`.from_array() <highcharts_core.options.series.data.base.DataBase.from_array>`).
.. warning::
If the data point *cannot* be serialized to a JavaScript array,
this method will instead return the untrimmed :class:`dict <python:dict>`
representation of the data point as a fallback.
:param force_object: if ``True``, forces the return of the instance's
untrimmed :class:`dict <python:dict>` representation. Defaults to ``False``.
:type force_object: :class:`bool <python:bool>`
:returns: The array representation of the data point.
:rtype: :class:`list <python:list>` of values or :class:`dict <python:dict>`
"""
if self.requires_js_object or force_object:
return self._to_untrimmed_dict()
if self.y is not None:
y = self.y
else:
y = constants.EnforcedNull
if self.x is None and self.name is None:
x = self.x
elif self.name is None:
x = self.x
else:
x = self.name
if self.x is None and self.name is None:
return [y]
return [x, y]
@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),
'drilldown': as_dict.get('drilldown', None),
'marker': as_dict.get('marker', None),
'x': as_dict.get('x', None),
'y': as_dict.get('y', None),
}
return kwargs
def _to_untrimmed_dict(self, in_cls = None) -> dict:
untrimmed = {
'dataLabels': self.data_labels,
'dragDrop': self.drag_drop,
'drilldown': self.drilldown,
'marker': self.marker,
'x': self.x,
'y': self.y,
'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 CartesianDataCollection(DataPointCollection):
"""A collection of :class:`CartesianData` objects.
.. note::
When serializing to JS literals, if possible, the collection is serialized to a primitive
array to boost performance within Python *and* JavaScript. However, this may not always be
possible if data points have non-array-compliant properties configured (e.g. adjusting their
style, names, identifiers, etc.). If serializing to a primitive array is not possible, the
results are serialized as JS literal objects.
"""
@classmethod
def _get_data_point_class(cls):
"""The Python class to use as the underlying data point within the Collection.
:rtype: class object
"""
return CartesianData
[docs]class Cartesian3DData(CartesianData):
"""Variant of :class:`CartesianData` which supports three dimensions (an x, y, and
z-axis)."""
def __init__(self, **kwargs):
self._z = None
self.z = kwargs.get('z', None)
super().__init__(**kwargs)
@property
def z(self) -> Optional[int | float | Decimal | type(None) | constants.EnforcedNullType]:
"""The position of the data point on the z-axis. Defaults to
:obj:`None <python:None>`.
:rtype: numeric or :class:`EnforcedNullType` or :obj:`None <python:None>`
"""
return self._z
@z.setter
def z(self, value):
if value is None or isinstance(value, constants.EnforcedNullType):
self._z = None
else:
self._z = validators.numeric(value)
[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, 'Cartesian3DData'):
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(x = None,
y = None,
z = None)
elif checkers.is_iterable(item):
if len(item) == 3:
as_dict = {
'x': item[0],
'y': item[1],
'z': item[2]
}
elif len(item) == 2:
as_dict = {
'x': None,
'y': item[0],
'z': item[1]
}
else:
raise errors.HighchartsValueError(f'data expects either a 3D or 3D '
f'collection. Collection received '
f'had {len(item)} dimensions.')
as_obj = cls.from_dict(as_dict)
else:
raise errors.HighchartsValueError(f'each data point supplied must either '
f'be a Cartesian 3D Data Point or be '
f'coercable to one. Could not coerce: '
f'{item}')
if checkers.is_string(as_obj.x) and not as_obj.name:
as_obj.name = as_obj.x
as_obj.x = None
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, 3]
[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 Cartesian3DDataCollection.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: ['x', 'y', 'z', 'name'],
3: ['x', 'y', 'z'],
2: ['y', 'z']
}
return cls._get_props_from_array_helper(prop_list, length)
[docs] def to_array(self, force_object = False) -> List | Dict:
"""Generate the array representation of the data point (the inversion
of
:meth:`.from_array() <highcharts_core.options.series.data.base.DataBase.from_array>`).
.. warning::
If the data point *cannot* be serialized to a JavaScript array,
this method will instead return the untrimmed :class:`dict <python:dict>`
representation of the data point as a fallback.
:param force_object: if ``True``, forces the return of the instance's
untrimmed :class:`dict <python:dict>` representation. Defaults to ``False``.
:type force_object: :class:`bool <python:bool>`
:returns: The array representation of the data point.
:rtype: :class:`list <python:list>` of values or :class:`dict <python:dict>`
"""
if self.requires_js_object or force_object:
return self._to_untrimmed_dict()
if self.y is not None:
y = self.y
else:
y = constants.EnforcedNull
if self.z is not None:
z = self.z
else:
z = constants.EnforcedNull
if self.x is None and self.name is not None:
x = self.name
elif self.name is None and self.x is not None:
x = self.x
else:
x = constants.EnforcedNull
if self.x is None and self.name is None:
return [y, z]
return [x, y, z]
@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),
'drilldown': as_dict.get('drilldown', None),
'marker': as_dict.get('marker', None),
'x': as_dict.get('x', None),
'y': as_dict.get('y', None),
'z': as_dict.get('z', None),
}
return kwargs
def _to_untrimmed_dict(self, in_cls = None) -> dict:
untrimmed = {
'z': self.z,
'dataLabels': self.data_labels,
'dragDrop': self.drag_drop,
'drilldown': self.drilldown,
'marker': self.marker,
'x': self.x,
'y': self.y,
'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 Cartesian3DDataCollection(DataPointCollection):
"""A collection of :class:`Cartesian3DData` objects.
.. note::
When serializing to JS literals, if possible, the collection is serialized to a primitive
array to boost performance within Python *and* JavaScript. However, this may not always be
possible if data points have non-array-compliant properties configured (e.g. adjusting their
style, names, identifiers, etc.). If serializing to a primitive array is not possible, the
results are serialized as JS literal objects.
"""
@classmethod
def _get_data_point_class(cls):
"""The Python class to use as the underlying data point within the Collection.
:rtype: class object
"""
return Cartesian3DData
[docs]class CartesianValueData(CartesianData):
"""Variant of :class:`CartesianData` which supports three values (an ``x``, ``y``, and
``value``)."""
def __init__(self, **kwargs):
self._point_padding = None
self._value = None
self.point_padding = kwargs.get('point_padding', None)
self.value = kwargs.get('value', None)
super().__init__(**kwargs)
@property
def point_padding(self) -> Optional[int | float | Decimal]:
"""Point padding for the data point. Defaults to :obj:`None <python:None>`.
:rtype: numeric or :obj:`None <python:None>`
"""
return self._point_padding
@point_padding.setter
def point_padding(self, value):
self._point_padding = validators.numeric(value, allow_empty = True)
@property
def value(self) -> Optional[int | float | Decimal | type(None) | constants.EnforcedNullType]:
"""The ``value`` of the data point. Defaults to :obj:`None <python:None>`.
:rtype: numeric or :class:`EnforcedNullType` or :obj:`None <python:None>`
"""
return self._value
@value.setter
def value(self, value_):
if value_ is None or isinstance(value_, constants.EnforcedNullType):
self._value = None
else:
self._value = validators.numeric(value_)
[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, 'CartesianValueData'):
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(x = None,
y = None,
value = None)
elif checkers.is_iterable(item):
if len(item) == 3:
as_dict = {
'x': item[0],
'y': item[1],
'value': item[2]
}
elif len(item) == 2:
as_dict = {
'x': None,
'y': item[0],
'value': item[1]
}
else:
raise errors.HighchartsValueError(f'data expects either a 3D or 3D '
f'collection. Collection received '
f'had {len(item)} dimensions.')
as_obj = cls.from_dict(as_dict)
else:
raise errors.HighchartsValueError(f'each data point supplied must either '
f'be a Cartesian Value Data Point or be'
f' coercable to one. Could not coerce: '
f'{item}')
if checkers.is_string(as_obj.x) and not as_obj.name:
as_obj.name = as_obj.x
as_obj.x = None
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, 3]
[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 CartesianValueDataCollection.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: ['x', 'y', 'value', 'name'],
3: ['x', 'y', 'value'],
2: ['y', 'value'],
}
return cls._get_props_from_array_helper(prop_list, length)
[docs] def to_array(self, force_object = False) -> List | Dict:
"""Generate the array representation of the data point (the inversion
of
:meth:`.from_array() <highcharts_core.options.series.data.base.DataBase.from_array>`).
.. warning::
If the data point *cannot* be serialized to a JavaScript array,
this method will instead return the untrimmed :class:`dict <python:dict>`
representation of the data point as a fallback.
:param force_object: if ``True``, forces the return of the instance's
untrimmed :class:`dict <python:dict>` representation. Defaults to ``False``.
:type force_object: :class:`bool <python:bool>`
:returns: The array representation of the data point.
:rtype: :class:`list <python:list>` of values or :class:`dict <python:dict>`
"""
if self.requires_js_object or force_object:
return self._to_untrimmed_dict()
if self.y is not None:
y = self.y
else:
y = constants.EnforcedNull
if self.value is not None:
value = self.value
else:
value = constants.EnforcedNull
if self.x is None and self.name is not None:
x = self.name
elif self.x is not None:
x = self.x
else:
x = constants.EnforcedNull
if self.x is None and self.name is None:
return [y, value]
return [x, y, 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 = {
'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),
'drilldown': as_dict.get('drilldown', None),
'marker': as_dict.get('marker', None),
'x': as_dict.get('x', None),
'y': as_dict.get('y', None),
'point_padding': as_dict.get('pointPadding', None),
'value': as_dict.get('value', None),
}
return kwargs
def _to_untrimmed_dict(self, in_cls = None) -> dict:
untrimmed = {
'pointPadding': self.point_padding,
'value': self.value,
'dataLabels': self.data_labels,
'dragDrop': self.drag_drop,
'drilldown': self.drilldown,
'marker': self.marker,
'x': self.x,
'y': self.y,
'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 CartesianValueDataCollection(CartesianDataCollection):
"""A collection of :class:`Cartesian3DData` objects.
.. note::
When serializing to JS literals, if possible, the collection is serialized to a primitive
array to boost performance within Python *and* JavaScript. However, this may not always be
possible if data points have non-array-compliant properties configured (e.g. adjusting their
style, names, identifiers, etc.). If serializing to a primitive array is not possible, the
results are serialized as JS literal objects.
"""
@classmethod
def _get_data_point_class(cls):
"""The Python class to use as the underlying data point within the Collection.
:rtype: class object
"""
return CartesianValueData