Skip to content

Commit f287315

Browse files
committed
Added .from_array to ArcDiagramData.
1 parent a03b168 commit f287315

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

highcharts_core/options/series/data/arcdiagram.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from typing import Optional
22
from decimal import Decimal
3+
from collections import UserDict
34

4-
from validator_collection import validators
5+
from validator_collection import validators, checkers
56

6-
from highcharts_core import utility_functions
7+
from highcharts_core import utility_functions, constants
78
from highcharts_core.options.series.data.base import DataBase
89
from highcharts_core.utility_classes.gradients import Gradient
910
from highcharts_core.utility_classes.patterns import Pattern
@@ -84,6 +85,47 @@ def weight(self) -> Optional[int | float | Decimal]:
8485
def weight(self, value):
8586
self._weight = validators.numeric(value, allow_empty = True)
8687

88+
@classmethod
89+
def from_array(cls, value):
90+
if not value:
91+
return []
92+
elif checkers.is_string(value):
93+
try:
94+
value = validators.json(value)
95+
except (ValueError, TypeError):
96+
pass
97+
elif not checkers.is_iterable(value):
98+
value = [value]
99+
100+
collection = []
101+
for item in value:
102+
if checkers.is_type(item, 'SinglePointData'):
103+
as_obj = item
104+
elif checkers.is_dict(item):
105+
as_obj = cls.from_dict(item)
106+
elif item is None or isinstance(item, constants.EnforcedNullType):
107+
as_obj = cls(y = None)
108+
elif checkers.is_numeric(item):
109+
as_obj = cls(y = item)
110+
elif checkers.is_iterable(item, forbid_literals = (str, bytes, dict, UserDict)):
111+
if len(item) == 3:
112+
as_obj = cls(from_ = item[0],
113+
to = item[1],
114+
weight = item[2])
115+
else:
116+
raise errors.HighchartsValueError(f'each data point supplied must either '
117+
f'be an Arc Diagram Data Point or be '
118+
f'coercable to one. Could not coerce: '
119+
f'{item}')
120+
else:
121+
raise errors.HighchartsValueError(f'each data point supplied must either '
122+
f'be an Arc Diagram Data Point or be '
123+
f'coercable to one. Could not coerce: '
124+
f'{item}')
125+
collection.append(as_obj)
126+
127+
return collection
128+
87129
@classmethod
88130
def _get_kwargs_from_dict(cls, as_dict):
89131
"""Convenience method which returns the keyword arguments used to initialize the

0 commit comments

Comments
 (0)