/
plotitem.py
448 lines (363 loc) · 14.3 KB
/
plotitem.py
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# Copyright 2014 TWO SIGMA OPEN SOURCE, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from enum import Enum
import math
import pandas as pd
from beakerx.utils import *
from dateutil.parser import parse
import numpy as np
import datetime as dt
import uuid
class ShapeType(Enum):
SQUARE = 1
CIRCLE = 2
TRIANGLE = 3
DIAMOND = 4
DCROSS = 5
DOWNTRIANGLE = 5
CROSS = 6
DEFAULT = 7
LEVEL = 8
VLEVEL = 9
LINECROSS = 10
class StrokeType(Enum):
NONE = 1
SOLID = 2
DASH = 3
DOT = 4
DASHDOT = 5
LONGDASH = 5
class PlotOrientationType(Enum):
VERTICAL = 1
HORIZONTAL = 2
class LabelPositionType(Enum):
VALUE_OUTSIDE = 1
VALUE_INSIDE = 2
CENTER = 3
BASE_OUTSIDE = 4
BASE_INSIDE = 5
class GradientColor:
def __init__(self, *args):
self.color = args[0]
GradientColor.BROWN_RED_YELLOW = GradientColor([Color(120, 0, 4),
Color(241, 88, 6),
Color(255, 206, 31)])
GradientColor.GREEN_YELLOW_WHITE = GradientColor([Color(0, 170, 0),
Color(102, 204, 0),
Color(238, 238, 0),
Color(238, 187, 68),
Color(238, 187, 153),
Color(255, 255, 255)])
GradientColor.WHITE_BLUE = GradientColor([Color(255, 255, 217),
Color(237, 248, 177),
Color(199, 233, 180),
Color(127, 205, 187),
Color(65, 182, 196),
Color(29, 145, 192),
Color(34, 94, 168),
Color(37, 52, 148),
Color(8, 29, 88)])
class Graphics(BaseObject):
def __init__(self, **kwargs):
super(Graphics, self).__init__(**kwargs)
self.type = self.__class__.__name__
self.visible = getValue(kwargs, 'visible', True)
self.yAxis = getValue(kwargs, 'yAxis')
self.clickTag = getValue(kwargs, 'tag', "")
self.hasClickAction = self.clickTag != ""
self.uid = str(uuid.uuid4())
self.onClickListeners = lambda *args: None
self.onKeyListeners = {}
self.keyTags = {}
self.keys = []
def onClick(self, on_click):
if isinstance(on_click, str):
self.clickTag = on_click
else:
self.onClickListeners = on_click
self.hasClickAction = True
return self
def onKey(self, key, on_key):
if isinstance(on_key, str):
self.keyTags[key] = on_key
else:
self.onKeyListeners[key] = on_key
if key not in self.keys:
self.keys.append(key)
return self
def fireClick(self, details):
self.onClickListeners(details)
def fireKey(self, details, key):
self.onKeyListeners.get(key, lambda *args: None)(details)
class ConstantLine(Graphics):
def __init__(self, **kwargs):
super(ConstantLine, self).__init__(**kwargs)
self.x = getValue(kwargs, 'x')
self.y = getValue(kwargs, 'y')
self.color = getColor(getValue(kwargs, 'color'))
self.width = getValue(kwargs, 'width', 1.5)
self.style = getValue(kwargs, 'style')
self.showLabel = getValue(kwargs, 'showLabel')
class ConstantBand(Graphics):
def __init__(self, **kwargs):
super(ConstantBand, self).__init__(**kwargs)
self.x = getValue(kwargs, 'x')
self.y = getValue(kwargs, 'y')
self.color = getColor(
getValue(kwargs, 'color', Color(0, 127, 255, 127)))
def is_date(string):
try:
parse(string)
return True
except Exception:
return False
class XYGraphics(Graphics):
def __init__(self, *args, **kwargs):
super(XYGraphics, self).__init__(**kwargs)
if len(args) > 0 and isinstance(args[0], pd.Series):
defX = args[0].index.tolist()
defY = args[0].tolist()
else:
defY = getValue(kwargs, 'y')
if defY is not None:
if isinstance(defY, pd.Series):
defY = defY.tolist()
defX = list(range(0, len(defY)))
else:
defX = []
local_x = getValue(kwargs, 'x', defX)
if local_x is not None:
if isinstance(local_x, pd.Series):
local_x = local_x.tolist()
self.x = [None] * len(local_x)
for idx in range(len(local_x)):
x = local_x[idx]
if isinstance(x, float) and math.isnan(x):
self.x[idx] = "NaN"
elif isinstance(x, dt.date) or isinstance(x, dt.time):
self.x[idx] = date_time_2_millis(x.isoformat())
elif is_date(x):
self.x[idx] = date_time_2_millis(x)
elif isinstance(x, np.datetime64):
self.x[idx] = date_time_2_millis(x.__str__())
else:
self.x[idx] = x
self.y = defY
if self.y is not None:
for idx in range(len(self.y)):
y = self.y[idx]
if isinstance(y, float) and math.isnan(y):
self.y[idx] = "NaN"
self.display_name = getValue(kwargs, 'displayName')
self.lod_filter = getValue(kwargs, 'lodFilter')
self.tooltips = getValue(kwargs, 'tooltips')
class Line(XYGraphics):
def __init__(self, *args, **kwargs):
super(Line, self).__init__(*args, **kwargs)
self.width = getValue(kwargs, 'width', 1.5)
self.style = getValue(kwargs, 'style')
self.interpolation = getValue(kwargs, 'interpolation')
self.color = getColor(getValue(kwargs, 'color'))
class BasedXYGraphics(XYGraphics):
def __init__(self, *args, **kwargs):
super(BasedXYGraphics, self).__init__(*args, **kwargs)
base = getValue(kwargs, 'base')
if isinstance(base, list):
self.bases = base
else:
self.base = getValue(kwargs, 'base', 0)
class Bars(BasedXYGraphics):
def __init__(self, *args, **kwargs):
super(Bars, self).__init__(*args, **kwargs)
width = getValue(kwargs, 'width')
if isinstance(width, list):
self.widths = width
else:
self.width = width
color = getColor(getValue(kwargs, 'color'))
if isinstance(color, list):
self.colors = color
else:
self.color = color
outlineColor = getColor(getValue(kwargs, 'outlineColor'))
if isinstance(outlineColor, list):
self.outline_colors = outlineColor
else:
self.outline_color = outlineColor
class Points(XYGraphics):
def __init__(self, *args, **kwargs):
super(Points, self).__init__(*args, **kwargs)
shape = getColor(getValue(kwargs, 'shape'))
if isinstance(shape, list):
self.shapes = shape
else:
self.shape = getValue(kwargs, 'shape', ShapeType.DEFAULT)
size = getColor(getValue(kwargs, 'size'))
if isinstance(size, list):
self.sizes = size
else:
self.size = getValue(kwargs, 'size', 6)
fill = getColor(getValue(kwargs, 'fill'))
if isinstance(fill, list):
self.fills = fill
else:
self.fill = fill
color = getColor(getValue(kwargs, 'color'))
if isinstance(color, list):
self.colors = color
else:
self.color = color
outlineColor = getColor(getValue(kwargs, 'outlineColor'))
if isinstance(outlineColor, list):
self.outline_colors = outlineColor
else:
self.outline_color = outlineColor
class Stems(BasedXYGraphics):
def __init__(self, *args, **kwargs):
super(Stems, self).__init__(*args, **kwargs)
self.width = getValue(kwargs, 'width', 1.5)
color = getColor(getValue(kwargs, 'color'))
if isinstance(color, list):
self.colors = color
else:
self.color = color
style = getValue(kwargs, 'style')
if isinstance(style, list):
self.styles = style
else:
self.style = getValue(kwargs, 'style', StrokeType.SOLID)
class Area(BasedXYGraphics):
def __init__(self, *args, **kwargs):
super(Area, self).__init__(*args, **kwargs)
self.color = getColor(getValue(kwargs, 'color'))
self.interpolation = getValue(kwargs, 'interpolation')
class Text(BaseObject):
def __init__(self, **kwargs):
super(Text, self).__init__(**kwargs)
self.x = getValue(kwargs, 'x', 0)
self.y = getValue(kwargs, 'y', 0)
self.color = getColor(getValue(kwargs, 'color'))
self.size = getValue(kwargs, 'size', 13)
self.text = getValue(kwargs, 'text', '')
self.show_pointer = getValue(kwargs, 'show_pointer', True)
self.pointer_angle = getValue(kwargs, 'pointerAngle',
(-0.25) * math.pi)
class YAxis(BaseObject):
def __init__(self, **kwargs):
super(YAxis, self).__init__(**kwargs)
self.label = getValue(kwargs, 'label', '')
self.auto_range = getValue(kwargs, 'autoRange', True)
self.auto_range_includes_zero = getValue(kwargs,
'autoRangeIncludesZero', False)
self.lower_margin = getValue(kwargs, 'lowerMargin', 0.05)
self.upper_margin = getValue(kwargs, 'upperMargin', 0.05)
self.lower_bound = getValue(kwargs, 'lowerBound', 0.0)
self.upper_bound = getValue(kwargs, 'upperBound', 0.0)
self.use_log = getValue(kwargs, 'logY', False)
self.log_base = getValue(kwargs, 'logBase', 10.0)
self.type = 'YAxis'
def setBound(self, min, max):
self.auto_range = False
self.lower_bound = min
self.upper_bound = max
return self.transform()
class XYStacker(BaseObject):
def __init__(self, **kwargs):
super(XYStacker, self).__init__(**kwargs)
def stack(self, graphicsList):
if graphicsList is None or len(graphicsList) == 1:
return graphicsList
else:
maxel = graphicsList[0]
for i in range(1, len(graphicsList)):
if len(graphicsList[i].y) > len(maxel.y):
maxel = graphicsList[i]
padYs(graphicsList[0], maxel)
stackedList = [graphicsList[0]]
for gIndex in range(1, len(graphicsList)):
current = graphicsList[gIndex]
padYs(current, maxel)
previous = graphicsList[gIndex - 1]
currentYs = current.y
previousYs = previous.y
for yIndex in range(len(currentYs)):
currentYs[yIndex] = currentYs[yIndex] + previousYs[yIndex]
current.bases = previousYs
stackedList.append(current)
return stackedList
class Crosshair(BasedXYGraphics):
def __init__(self, *args, **kwargs):
super(Crosshair, self).__init__(*args, **kwargs)
self.width = getValue(kwargs, 'width')
self.style = getValue(kwargs, 'style')
self.color = getColor(getValue(kwargs, 'color'))
class CategoryGraphics(Graphics):
def __init__(self, **kwargs):
super(CategoryGraphics, self).__init__(**kwargs)
self.center_series = getValue(kwargs, 'centerSeries', False)
self.use_tool_tip = getValue(kwargs, 'useToolTip', True)
self.showItemLabel = getValue(kwargs, 'showItemLabel', False)
self.outline = getValue(kwargs, 'outline', False)
self.labelPosition = getValue(kwargs, 'labelPosition', "CENTER")
self.fills = getValue(kwargs, 'fill')
self.itemLabels = getValue(kwargs, 'itemLabel')
self.seriesNames = getValue(kwargs, 'seriesNames')
self.style = getValue(kwargs, 'style')
self.size = getValue(kwargs, 'size')
outline = getValue(kwargs, 'outlineColor')
if isinstance(outline, list):
self.outline_colors = outline
else:
self.outline_color = outline
drawOutline = getValue(kwargs, 'drawOutline')
if isinstance(drawOutline, list):
self.outlines = drawOutline
else:
self.outline = drawOutline
base = getValue(kwargs, 'base', 0.0)
if isinstance(base, list):
self.bases = base
else:
self.base = base
width = getValue(kwargs, 'width')
if isinstance(width, list):
self.widths = width
else:
self.width = width
style = getValue(kwargs, 'style')
if isinstance(style, list):
self.styles = style
else:
self.style = style
self.value = getValue(kwargs, 'value', [])
color = getColor(getValue(kwargs, 'color'))
if isinstance(color, list):
self.colors = color
else:
self.color = color
class CategoryBars(CategoryGraphics):
def __init__(self, **kwargs):
super(CategoryBars, self).__init__(**kwargs)
class CategoryStems(CategoryGraphics):
def __init__(self, **kwargs):
super(CategoryStems, self).__init__(**kwargs)
class CategoryPoints(CategoryGraphics):
def __init__(self, **kwargs):
super(CategoryPoints, self).__init__(**kwargs)
class CategoryLines(CategoryGraphics):
def __init__(self, **kwargs):
super(CategoryLines, self).__init__(**kwargs)
class CategoryArea(CategoryGraphics):
def __init__(self, **kwargs):
super(CategoryArea, self).__init__(**kwargs)