-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathcharts.py
More file actions
230 lines (166 loc) · 5.3 KB
/
charts.py
File metadata and controls
230 lines (166 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import enum
from typing import Any, List, Tuple, Optional, Union
class ChartType(str, enum.Enum):
"""
Chart types
"""
LINE = "line"
SCATTER = "scatter"
BAR = "bar"
PIE = "pie"
BOX_AND_WHISKER = "box_and_whisker"
SUPERCHART = "superchart"
UNKNOWN = "unknown"
class ScaleType(str, enum.Enum):
"""
Ax scale types
"""
LINEAR = "linear"
DATETIME = "datetime"
CATEGORICAL = "categorical"
LOG = "log"
SYMLOG = "symlog"
LOGIT = "logit"
FUNCTION = "function"
FUNCTIONLOG = "functionlog"
ASINH = "asinh"
UNKNOWN = "unknown"
class Chart:
"""
Extracted data from a chart. It's useful for building an interactive charts or custom visualizations.
"""
type: ChartType
title: str
elements: List[Any]
def __init__(self, **kwargs):
self._raw_data = kwargs
self.type = ChartType(kwargs["type"] or ChartType.UNKNOWN)
self.title = kwargs["title"]
self.elements = kwargs["elements"]
def to_dict(self) -> dict:
return self._raw_data
class Chart2D(Chart):
x_label: Optional[str]
y_label: Optional[str]
x_unit: Optional[str]
y_unit: Optional[str]
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.x_label = kwargs["x_label"]
self.y_label = kwargs["y_label"]
self.x_unit = kwargs["x_unit"]
self.y_unit = kwargs["y_unit"]
class PointData:
label: str
points: List[Tuple[Union[str, float], Union[str, float]]]
def __init__(self, **kwargs):
self.label = kwargs["label"]
self.points = [(x, y) for x, y in kwargs["points"]]
class PointChart(Chart2D):
x_ticks: List[Union[str, float]]
x_tick_labels: List[str]
x_scale: ScaleType
y_ticks: List[Union[str, float]]
y_tick_labels: List[str]
y_scale: ScaleType
elements: List[PointData]
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.x_label = kwargs["x_label"]
try:
self.x_scale = ScaleType(kwargs.get("x_scale"))
except ValueError:
self.x_scale = ScaleType.UNKNOWN
self.x_ticks = kwargs["x_ticks"]
self.x_tick_labels = kwargs["x_tick_labels"]
self.y_label = kwargs["y_label"]
try:
self.y_scale = ScaleType(kwargs.get("y_scale"))
except ValueError:
self.y_scale = ScaleType.UNKNOWN
self.y_ticks = kwargs["y_ticks"]
self.y_tick_labels = kwargs["y_tick_labels"]
self.elements = [PointData(**d) for d in kwargs["elements"]]
class LineChart(PointChart):
type = ChartType.LINE
class ScatterChart(PointChart):
type = ChartType.SCATTER
class BarData:
label: str
group: str
value: str
def __init__(self, **kwargs):
self.label = kwargs["label"]
self.value = kwargs["value"]
self.group = kwargs["group"]
class BarChart(Chart2D):
type = ChartType.BAR
elements: List[BarData]
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.elements = [BarData(**d) for d in kwargs["elements"]]
class PieData:
label: str
angle: float
radius: float
def __init__(self, **kwargs):
self.label = kwargs["label"]
self.angle = kwargs["angle"]
self.radius = kwargs["radius"]
class PieChart(Chart):
type = ChartType.PIE
elements: List[PieData]
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.elements = [PieData(**d) for d in kwargs["elements"]]
class BoxAndWhiskerData:
label: str
min: float
first_quartile: float
median: float
third_quartile: float
max: float
outliers: List[float]
def __init__(self, **kwargs):
self.label = kwargs["label"]
self.min = kwargs["min"]
self.first_quartile = kwargs["first_quartile"]
self.median = kwargs["median"]
self.third_quartile = kwargs["third_quartile"]
self.max = kwargs["max"]
self.outliers = kwargs.get("outliers") or []
class BoxAndWhiskerChart(Chart2D):
type = ChartType.BOX_AND_WHISKER
elements: List[BoxAndWhiskerData]
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.elements = [BoxAndWhiskerData(**d) for d in kwargs["elements"]]
class SuperChart(Chart):
type = ChartType.SUPERCHART
elements: List[
Union[LineChart, ScatterChart, BarChart, PieChart, BoxAndWhiskerChart]
]
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.elements = [_deserialize_chart(g) for g in kwargs["elements"]]
ChartTypes = Union[
LineChart, ScatterChart, BarChart, PieChart, BoxAndWhiskerChart, SuperChart
]
def _deserialize_chart(data: Optional[dict]) -> Optional[ChartTypes]:
if not data:
return None
if data["type"] == ChartType.LINE:
chart = LineChart(**data)
elif data["type"] == ChartType.SCATTER:
chart = ScatterChart(**data)
elif data["type"] == ChartType.BAR:
chart = BarChart(**data)
elif data["type"] == ChartType.PIE:
chart = PieChart(**data)
elif data["type"] == ChartType.BOX_AND_WHISKER:
chart = BoxAndWhiskerChart(**data)
elif data["type"] == ChartType.SUPERCHART:
chart = SuperChart(**data)
else:
chart = Chart(**data)
return chart