/
indicators.py
1385 lines (1092 loc) · 47.9 KB
/
indicators.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Indicators
==========
Indicators can be used to indicate status or progress
Check out the Panel gallery of indicators
https://panel.holoviz.org/reference/index.html#indicators for inspiration.
How to use indicators
---------------------
>>> pn.indicators.Number(
... name='Rate', value=72, format='{value}%',
... colors=[(80, 'green'), (100, 'red')]
... )
"""
from __future__ import annotations
import math
import os
import sys
from math import pi
from typing import (
TYPE_CHECKING, ClassVar, Dict, List, Mapping, Optional, Tuple, Type,
)
import numpy as np
import param
from bokeh.models import ColumnDataSource, FixedTicker, Tooltip
from bokeh.plotting import figure
from tqdm.asyncio import tqdm as _tqdm
from .._param import Align
from ..io.resources import CDN_DIST
from ..layout import Column, Panel, Row
from ..models import (
HTML, Progress as _BkProgress, TooltipIcon as _BkTooltipIcon,
TrendIndicator as _BkTrendIndicator,
)
from ..pane.markup import Str
from ..reactive import SyncableData
from ..util import PARAM_NAME_PATTERN, escape, updating
from ..util.warnings import deprecated
from ..viewable import Viewable
from .base import Widget
if TYPE_CHECKING:
from _thread import LockType
from bokeh.document import Document
from bokeh.model import Model
from pyviz_comms import Comm
RED = "#d9534f"
GREEN = "#5cb85c"
BLUE = "#428bca"
class Indicator(Widget):
"""
Indicator is a baseclass for widgets which indicate some state.
"""
sizing_mode = param.ObjectSelector(default='fixed', objects=[
'fixed', 'stretch_width', 'stretch_height', 'stretch_both',
'scale_width', 'scale_height', 'scale_both', None])
_linked_properties: ClassVar[Tuple[str,...]] = ()
_rename: ClassVar[Mapping[str, str | None]] = {'name': None}
__abstract = True
class BooleanIndicator(Indicator):
"""
BooleanIndicator is an abstract baseclass for indicators that
visually indicate a boolean value.
"""
value = param.Boolean(default=False, doc="""
Whether the indicator is active or not.""")
__abstract = True
class BooleanStatus(BooleanIndicator):
"""
The `BooleanStatus` is a boolean indicator providing a visual
representation of a boolean status as filled or non-filled circle.
If the value is set to `True` the indicator will be filled while
setting it to `False` will cause it to be non-filled.
Reference: https://panel.holoviz.org/reference/indicators/BooleanStatus.html
:Example:
>>> BooleanStatus(value=True, color='primary', width=100, height=100)
"""
color = param.ObjectSelector(default='dark', objects=[
'primary', 'secondary', 'success', 'info', 'danger', 'warning', 'light', 'dark'], doc="""
The color of the circle, one of 'primary', 'secondary', 'success', 'info', 'danger',
'warning', 'light', 'dark'""")
height = param.Integer(default=20, doc="""
height of the circle.""")
width = param.Integer(default=20, doc="""
Width of the circle.""")
value = param.Boolean(default=False, doc="""
Whether the indicator is active or not.""")
_source_transforms: ClassVar[Mapping[str, str | None]] = {'value': None, 'color': None}
_stylesheets: ClassVar[List[str]] = [f'{CDN_DIST}css/booleanstatus.css']
_widget_type: ClassVar[Type[Model]] = HTML
def _process_param_change(self, msg):
msg = super()._process_param_change(msg)
value = msg.pop('value', None)
color = msg.pop('color', None)
if value is None and not color:
return msg
msg['css_classes'] = ['dot-filled', self.color] if self.value else ['dot']
return msg
class LoadingSpinner(BooleanIndicator):
"""
The `LoadingSpinner` is a boolean indicator providing a visual
representation of the loading status.
If the value is set to `True` the spinner will rotate while
setting it to `False` will disable the rotating segment.
Reference: https://panel.holoviz.org/reference/indicators/LoadingSpinner.html
:Example:
>>> LoadingSpinner(value=True, color='primary', bgcolor='light', width=100, height=100)
"""
bgcolor = param.ObjectSelector(default='light', objects=['dark', 'light'])
color = param.ObjectSelector(default='dark', objects=[
'primary', 'secondary', 'success', 'info', 'danger', 'warning',
'light', 'dark'])
size = param.Integer(default=125, doc="""
Size of the spinner in pixels.""")
value = param.Boolean(default=False, doc="""
Whether the indicator is active or not.""")
_rename = {'name': 'text'}
_source_transforms: ClassVar[Mapping[str, str | None]] = {
'value': None, 'color': None, 'bgcolor': None, 'size': None
}
_stylesheets: ClassVar[List[str]] = [f'{CDN_DIST}css/loadingspinner.css']
_widget_type: ClassVar[Type[Model]] = HTML
def _process_param_change(self, msg):
msg = super()._process_param_change(msg)
if 'text' in msg:
text = msg.pop('text')
if not PARAM_NAME_PATTERN.match(text):
msg['text'] = escape(f'<span><b>{text}</b></span>')
value = msg.pop('value', None)
color = msg.pop('color', None)
bgcolor = msg.pop('bgcolor', None)
if msg.get('sizing_mode') == 'fixed':
msg['sizing_mode'] = None
if 'size' in msg or 'height' in msg or 'stylesheets' in msg:
if 'width' in msg and msg['width'] == msg.get('height'):
del msg['width']
size = int(min(msg.pop('height', self.height) or float('inf'), msg.pop('size', self.size)))
msg['stylesheets'] = ([f':host {{ --loading-spinner-size: {size}px; }}'] +
msg.get('stylesheets', []))
msg['min_width'] = msg['min_height'] = size
if value is None and not (color or bgcolor):
return msg
color_cls = f'{self.color}-{self.bgcolor}'
msg['css_classes'] = ['loader', 'spin', color_cls] if self.value else ['loader', self.bgcolor]
return msg
class ValueIndicator(Indicator):
"""
A ValueIndicator provides a visual representation for a numeric
value.
"""
value = param.Number(default=None, allow_None=True)
__abstract = True
class Progress(ValueIndicator):
"""
The `Progress` widget displays the progress towards some target
based on the current `value` and the `max` value.
If no `value` is set, the `Progress` widget is in indeterminate
mode and will animate depending on whether it is `active` or
not. A more beautiful indicator for this use case is the
`LoadingSpinner`.
Reference: https://panel.holoviz.org/reference/indicators/Progress.html
:Example:
>>> Progress(value=20, max=100, bar_color="primary")
"""
active = param.Boolean(default=True, doc="""
If no value is set the active property toggles animation of the
progress bar on and off.""")
bar_color = param.ObjectSelector(default='success', objects=[
'primary', 'secondary', 'success', 'info', 'danger', 'warning',
'light', 'dark'])
max = param.Integer(default=100, doc="The maximum value of the progress bar.")
sizing_mode = param.ObjectSelector(default=None, objects=[
'fixed', 'stretch_width', 'stretch_height', 'stretch_both',
'scale_width', 'scale_height', 'scale_both', None])
value = param.Integer(default=-1, bounds=(-1, None), doc="""
The current value of the progress bar. If set to -1 the progress
bar will be indeterminate and animate depending on the active
parameter.""")
width = param.Integer(default=300)
_stylesheets: ClassVar[List[str]] = [f'{CDN_DIST}css/progress.css']
_widget_type: ClassVar[Type[Model]] = _BkProgress
@param.depends('max', watch=True)
def _update_value_bounds(self):
self.param.value.bounds = (-1, self.max)
def __init__(self,**params):
super().__init__(**params)
self._update_value_bounds()
class Number(ValueIndicator):
"""
The `Number` indicator renders the `value` as text optionally
colored according to the `colors` thresholds.
Reference: https://panel.holoviz.org/reference/indicators/Number.html
:Example:
>>> Number(name='Rate', value=72, format='{value}%', colors=[(80, 'green'), (100, 'red')]
"""
default_color = param.String(default='black', doc="""
The color of the Number indicator if no colors are provided""")
colors = param.List(default=None, doc="""
Color thresholds for the Number indicator, specified as a tuple of the absolute thresholds
and the color to switch to.""")
format = param.String(default='{value}', doc="""
A formatter string which accepts a {value}.""")
font_size = param.String(default='54pt', doc="""
The size of number itself.""")
nan_format = param.String(default='-', doc="""
How to format nan values.""")
title_size = param.String(default='18pt', doc="""
The size of the title given by the name.""")
_rename: ClassVar[Mapping[str, str | None]] = {'name': 'name'}
_source_transforms: ClassVar[Mapping[str, str | None]] = {
'value': None, 'colors': None, 'default_color': None,
'font_size': None, 'format': None, 'nan_format': None,
'title_size': None
}
_widget_type: ClassVar[Type[Model]] = HTML
def __init__(self, **params):
if "sizing_mode" not in params:
params["sizing_mode"] = None
super().__init__(**params)
def _process_param_change(self, msg):
msg = super()._process_param_change(msg)
if not any(p in self._source_transforms or p == 'name' for p in msg):
return msg
font_size = msg.pop('font_size', self.font_size)
title_font_size = msg.pop('title_size', self.title_size)
name = msg.pop('name', self.name)
format = msg.pop('format', self.format)
value = msg.pop('value', self.value)
nan_format = msg.pop('nan_format', self.nan_format)
color = msg.pop('default_color', self.default_color)
colors = msg.pop('colors', self.colors)
for val, clr in (colors or [])[::-1]:
if value is not None and value <= val:
color = clr
if value is None:
value = float('nan')
value = format.format(value=value).replace('nan', nan_format)
text = f'<div style="font-size: {font_size}; color: {color}">{value}</div>'
if self.name:
title_font_size = msg.pop('title_size', self.title_size)
text = f'<div style="font-size: {title_font_size}; color: {color}">{name}</div>\n{text}'
msg['text'] = escape(text)
return msg
class String(ValueIndicator):
"""
The String indicator renders a string with a title.
"""
default_color = param.String(default='black', doc="""
The color of the Number indicator if no colors are provided""")
font_size = param.String(default='54pt', doc="""
The size of number itself.""")
title_size = param.String(default='18pt', doc="""
The size of the title given by the name.""")
value = param.String(default=None, allow_None=True, doc="""
The string to display""")
_rename: ClassVar[Mapping[str, str | None]] = {}
_source_transforms: ClassVar[Mapping[str, str | None]] = {
'value': None, 'default_color': None, 'font_size': None, 'title_size': None
}
_widget_type: ClassVar[Type[Model]] = HTML
def __init__(self, **params):
if "sizing_mode" not in params:
params["sizing_mode"] = None
super().__init__(**params)
def _process_param_change(self, msg):
msg = super()._process_param_change(msg)
if not any(p in self._source_transforms or p == 'name' for p in msg):
return msg
font_size = msg.pop('font_size', self.font_size)
title_font_size = msg.pop('title_size', self.title_size)
name = msg.pop('name', self.name)
value = msg.pop('value', self.value)
color = msg.pop('default_color', self.default_color)
text = f'<div style="font-size: {font_size}; color: {color}">{value}</div>'
if self.name:
title_font_size = msg.pop('title_size', self.title_size)
text = f'<div style="font-size: {title_font_size}; color: {color}">{name}</div>\n{text}'
msg['text'] = escape(text)
return msg
class Gauge(ValueIndicator):
"""
A `Gauge` represents a value in some range as a position on
speedometer or gauge. It is similar to a `Dial` but visually a lot
busier.
Reference: https://panel.holoviz.org/reference/indicators/Gauge.html
:Example:
>>> Gauge(name='Speed', value=79, bounds=(0, 200), colors=[(0.4, 'green'), (1, 'red')])
"""
annulus_width = param.Integer(default=10, doc="""
Width of the gauge annulus.""")
bounds = param.Range(default=(0, 100), doc="""
The upper and lower bound of the dial.""")
colors = param.List(default=None, doc="""
Color thresholds for the Gauge, specified as a list of tuples
of the fractional threshold and the color to switch to.""")
custom_opts = param.Dict(doc="""
Additional options to pass to the ECharts Gauge definition.""")
height = param.Integer(default=300, bounds=(0, None))
end_angle = param.Number(default=-45, doc="""
Angle at which the gauge ends.""")
format = param.String(default='{value}%', doc="""
Formatting string for the value indicator.""")
num_splits = param.Integer(default=10, doc="""
Number of splits along the gauge.""")
show_ticks = param.Boolean(default=True, doc="""
Whether to show ticks along the dials.""")
show_labels = param.Boolean(default=True, doc="""
Whether to show tick labels along the dials.""")
start_angle = param.Number(default=225, doc="""
Angle at which the gauge starts.""")
tooltip_format = param.String(default='{b} : {c}%', doc="""
Formatting string for the hover tooltip.""")
title_size = param.Integer(default=18, doc="""
Size of title font.""")
value = param.Number(default=25, doc="""
Value to indicate on the gauge a value within the declared bounds.""")
width = param.Integer(default=300, bounds=(0, None))
_rename: ClassVar[Mapping[str, str | None]] = {}
_source_transforms: ClassVar[Mapping[str, str | None]] = {
'annulus_width': None, 'bounds': None, 'colors': None,
'custom_opts': None, 'end_angle': None, 'format': None,
'num_splits': None, 'show_ticks': None, 'show_labels': None,
'start_angle': None, 'tooltip_format': None, 'title_size': None,
'value': None
}
@property
def _widget_type(self):
if 'panel.models.echarts' not in sys.modules:
from ..models.echarts import ECharts
else:
ECharts = sys.modules['panel.models.echarts'].ECharts
return ECharts
def __init__(self, **params):
super().__init__(**params)
self._update_value_bounds()
@param.depends('bounds', watch=True)
def _update_value_bounds(self):
self.param.value.bounds = self.bounds
def _process_param_change(self, msg):
msg = super()._process_param_change(msg)
vmin, vmax = msg.pop('bounds', self.bounds)
msg['data'] = {
'tooltip': {
'formatter': msg.pop('tooltip_format', self.tooltip_format)
},
'series': [{
'name': 'Gauge',
'type': 'gauge',
'axisTick': {'show': msg.pop('show_ticks', self.show_ticks)},
'axisLabel': {'show': msg.pop('show_labels', self.show_labels)},
'title': {'fontWeight': 'bold', 'fontSize': msg.pop('title_size', self.title_size)},
'splitLine': {'show': True},
'radius': '100%',
'detail': {'formatter': msg.pop('format', self.format)},
'min': vmin,
'max': vmax,
'startAngle': msg.pop('start_angle', self.start_angle),
'endAngle': msg.pop('end_angle', self.end_angle),
'splitNumber': msg.pop('num_splits', self.num_splits),
'data': [{'value': msg.pop('value', self.value), 'name': self.name}],
'axisLine': {
'lineStyle': {
'width': msg.pop('annulus_width', self.annulus_width),
}
}
}]
}
colors = msg.pop('colors', self.colors)
if colors:
msg['data']['series'][0]['axisLine']['lineStyle']['color'] = colors
custom_opts = msg.pop('custom_opts', self.custom_opts)
if custom_opts:
gauge = msg['data']['series'][0]
for k, v in custom_opts.items():
if k not in gauge or not isinstance(gauge[k], dict):
gauge[k] = v
else:
gauge[k].update(v)
return msg
class Dial(ValueIndicator):
"""
A `Dial` represents a value in some range as a position on an
annular dial. It is similar to a `Gauge` but more minimal
visually.
Reference: https://panel.holoviz.org/reference/indicators/Dial.html
:Example:
>>> Dial(name='Speed', value=79, format="{value} km/h", bounds=(0, 200), colors=[(0.4, 'green'), (1, 'red')])
"""
annulus_width = param.Number(default=0.2, doc="""
Width of the radial annulus as a fraction of the total.""")
bounds = param.Range(default=(0, 100), doc="""
The upper and lower bound of the dial.""")
colors = param.List(default=None, doc="""
Color thresholds for the Dial, specified as a list of tuples
of the fractional threshold and the color to switch to.""")
default_color = param.String(default='lightblue', doc="""
Color of the radial annulus if not color thresholds are supplied.""")
end_angle = param.Number(default=25, doc="""
Angle at which the dial ends.""")
format = param.String(default='{value}%', doc="""
Formatting string for the value indicator and lower/upper bounds.""")
height = param.Integer(default=250, bounds=(1, None))
label_color = param.String(default='black', doc="""
Color for all extraneous labels.""")
nan_format = param.String(default='-', doc="""
How to format nan values.""")
needle_color = param.String(default='black', doc="""
Color of the Dial needle.""")
needle_width = param.Number(default=0.1, doc="""
Radial width of the needle.""")
start_angle = param.Number(default=-205, doc="""
Angle at which the dial starts.""")
tick_size = param.String(default=None, doc="""
Font size of the Dial min/max labels.""")
title_size = param.String(default=None, doc="""
Font size of the Dial title.""")
unfilled_color = param.String(default='whitesmoke', doc="""
Color of the unfilled region of the Dial.""")
value_size = param.String(default=None, doc="""
Font size of the Dial value label.""")
value = param.Number(default=25, allow_None=True, doc="""
Value to indicate on the dial a value within the declared bounds.""")
width = param.Integer(default=250, bounds=(1, None))
_manual_params: ClassVar[List[str]] = [
'value', 'start_angle', 'end_angle', 'bounds',
'annulus_width', 'format', 'background', 'needle_width',
'tick_size', 'title_size', 'value_size', 'colors',
'default_color', 'unfilled_color', 'height',
'width', 'nan_format', 'needle_color', 'label_color'
]
_data_params: ClassVar[List[str]] = _manual_params
_rename: ClassVar[Mapping[str, str | None]] = {
'background': 'background_fill_color'
}
def __init__(self, **params):
super().__init__(**params)
self._update_value_bounds()
@param.depends('bounds', watch=True)
def _update_value_bounds(self):
self.param.value.bounds = self.bounds
def _get_data(self, properties):
vmin, vmax = self.bounds
value = self.value
if value is None:
value = float('nan')
fraction = (value-vmin)/(vmax-vmin)
start = (np.radians(360-self.start_angle) - pi % (2*pi)) + pi
end = (np.radians(360-self.end_angle) - pi % (2*pi)) + pi
distance = (abs(end-start) % (pi*2))
if end>start:
distance = (pi*2)-distance
radial_fraction = distance*fraction
angle = start if np.isnan(fraction) else (start-radial_fraction)
inner_radius = 1-self.annulus_width
color = self.default_color
for val, clr in (self.colors or [])[::-1]:
if fraction <= val:
color = clr
annulus_data = {
'starts': np.array([start, angle]),
'ends' : np.array([angle, end]),
'color': [color, self.unfilled_color],
'radius': np.array([inner_radius, inner_radius])
}
x0s, y0s, x1s, y1s, clrs = [], [], [], [], []
colors = self.colors or []
for (val, _), (_, clr) in zip(colors[:-1], colors[1:]):
tangle = start-(distance*val)
if (vmin + val * (vmax-vmin)) <= value:
continue
x0, y0 = np.cos(tangle), np.sin(tangle)
x1, y1 = x0*inner_radius, y0*inner_radius
x0s.append(x0)
y0s.append(y0)
x1s.append(x1)
y1s.append(y1)
clrs.append(clr)
threshold_data = {
'x0': x0s, 'y0': y0s, 'x1': x1s, 'y1': y1s, 'color': clrs
}
center_radius = 1-self.annulus_width/2.
x, y = np.cos(angle)*center_radius, np.sin(angle)*center_radius
needle_start = pi+angle-(self.needle_width/2.)
needle_end = pi+angle+(self.needle_width/2.)
needle_data = {
'x': np.array([x]),
'y': np.array([y]),
'start': np.array([needle_start]),
'end': np.array([needle_end]),
'radius': np.array([center_radius])
}
value = self.format.format(value=value).replace('nan', self.nan_format)
min_value = self.format.format(value=vmin)
max_value = self.format.format(value=vmax)
tminx, tminy = np.cos(start)*center_radius, np.sin(start)*center_radius
tmaxx, tmaxy = np.cos(end)*center_radius, np.sin(end)*center_radius
tmin_angle, tmax_angle = start+pi, end+pi % pi
scale = (self.height/400)
title_size = self.title_size if self.title_size else '%spt' % (scale*32)
value_size = self.value_size if self.value_size else '%spt' % (scale*48)
tick_size = self.tick_size if self.tick_size else '%spt' % (scale*18)
text_data= {
'x': np.array([0, 0, tminx, tmaxx]),
'y': np.array([-.2, -.5, tminy, tmaxy]),
'text': [self.name, value, min_value, max_value],
'rot': np.array([0, 0, tmin_angle, tmax_angle]),
'size': [title_size, value_size, tick_size, tick_size],
'color': [self.label_color, color, self.label_color, self.label_color]
}
return annulus_data, needle_data, threshold_data, text_data
def _get_model(self, doc, root=None, parent=None, comm=None):
properties = self._get_properties(doc)
model = figure(
x_range=(-1,1), y_range=(-1,1), tools=[],
outline_line_color=None, toolbar_location=None,
width=self.width, height=self.height, **properties
)
model.xaxis.visible = False
model.yaxis.visible = False
model.grid.visible = False
annulus, needle, threshold, text = self._get_data(properties)
# Draw annulus
annulus_source = ColumnDataSource(data=annulus, name='annulus_source')
model.annular_wedge(
x=0, y=0, inner_radius='radius', outer_radius=1, start_angle='starts',
end_angle='ends', line_color='gray', color='color', direction='clock',
source=annulus_source
)
# Draw needle
needle_source = ColumnDataSource(data=needle, name='needle_source')
model.wedge(
x='x', y='y', radius='radius', start_angle='start', end_angle='end',
fill_color=self.needle_color, line_color=self.needle_color,
source=needle_source, name='needle_renderer'
)
# Draw thresholds
threshold_source = ColumnDataSource(data=threshold, name='threshold_source')
model.segment(
x0='x0', x1='x1', y0='y0', y1='y1', line_color='color', source=threshold_source,
line_width=2
)
# Draw labels
text_source = ColumnDataSource(data=text, name='label_source')
model.text(
x='x', y='y', text='text', font_size='size', text_align='center',
text_color='color', source=text_source, text_baseline='top',
angle='rot'
)
# Apply theme
self._design.apply_bokeh_theme_to_model(model)
if root is None:
root = model
self._models[root.ref['id']] = (model, parent)
return model
def _manual_update(self, events, model, doc, root, parent, comm):
update_data = False
for event in events:
if event.name in ('width', 'height'):
model.update(**{event.name: event.new})
if event.name in self._data_params:
update_data = True
elif event.name == 'needle_color':
needle_r = model.select(name='needle_renderer')
needle_r.glyph.line_color = event.new
needle_r.glyph.fill_color = event.new
if not update_data:
return
properties = self._get_properties(doc)
annulus, needle, threshold, labels = self._get_data(properties)
model.select(name='annulus_source').data.update(annulus)
model.select(name='needle_source').data.update(needle)
model.select(name='threshold_source').data.update(threshold)
model.select(name='label_source').data.update(labels)
class LinearGauge(ValueIndicator):
"""
A LinearGauge represents a value in some range as a position on an
linear scale. It is similar to a Dial/Gauge but visually more
compact.
Reference: https://panel.holoviz.org/reference/indicators/LinearGauge.html
:Example:
>>> LinearGauge(value=30, default_color='red', bounds=(0, 100))
"""
bounds = param.Range(default=(0, 100), doc="""
The upper and lower bound of the gauge.""")
default_color = param.String(default='lightblue', doc="""
Color of the radial annulus if not color thresholds are supplied.""")
colors = param.Parameter(default=None, doc="""
Color thresholds for the gauge, specified as a list of tuples
of the fractional threshold and the color to switch to.""")
format = param.String(default='{value:.2f}%', doc="""
Formatting string for the value indicator and lower/upper bounds.""")
height = param.Integer(default=300, bounds=(1, None))
horizontal = param.Boolean(default=False, doc="""
Whether to display the linear gauge horizontally.""")
nan_format = param.String(default='-', doc="""
How to format nan values.""")
needle_color = param.String(default='black', doc="""
Color of the gauge needle.""")
show_boundaries = param.Boolean(default=False, doc="""
Whether to show the boundaries between colored regions.""")
unfilled_color = param.String(default='whitesmoke', doc="""
Color of the unfilled region of the LinearGauge.""")
title_size = param.String(default=None, doc="""
Font size of the gauge title.""")
tick_size = param.String(default=None, doc="""
Font size of the gauge tick labels.""")
value_size = param.String(default=None, doc="""
Font size of the gauge value label.""")
value = param.Number(default=25, allow_None=True, doc="""
Value to indicate on the dial a value within the declared bounds.""")
width = param.Integer(default=125, bounds=(1, None))
_manual_params = [
'value', 'bounds', 'format', 'title_size', 'value_size',
'horizontal', 'height', 'colors', 'tick_size',
'unfilled_color', 'width', 'nan_format', 'needle_color'
]
_data_params = [
'value', 'bounds', 'format', 'nan_format', 'needle_color',
'colors'
]
_rerender_params = ['horizontal']
_rename: ClassVar[Mapping[str, str | None]] = {
'background': 'background_fill_color',
'name': 'name',
'show_boundaries': None,
'default_color': None
}
_updates = False
def __init__(self, **params):
super().__init__(**params)
self._update_value_bounds()
@param.depends('bounds', watch=True)
def _update_value_bounds(self):
self.param.value.bounds = self.bounds
@property
def _color_intervals(self):
vmin, vmax = self.bounds
value = self.value
ncolors = len(self.colors) if self.colors else 1
interval = (vmax-vmin)
if math.isfinite(value):
fraction = value / interval
idx = round(fraction * (ncolors-1))
else:
fraction = 0
idx = 0
if not self.colors:
intervals = [
(fraction, self.default_color)
]
intervals.append((1, self.unfilled_color))
elif self.show_boundaries:
intervals = [
c if isinstance(c, tuple) else ((i+1)/(ncolors), c)
for i, c in enumerate(self.colors)
]
else:
intervals = [
self.colors[idx] if isinstance(self.colors[0], tuple)
else (fraction, self.colors[idx])
]
intervals.append((1, self.unfilled_color))
return intervals
def _get_data(self, properties):
vmin, vmax = self.bounds
value = self.value
interval = (vmax-vmin)
colors, values = [], [vmin]
above = False
prev = None
for (v, color) in self._color_intervals:
val = v*interval
if val == prev:
continue
elif val > value:
if not above:
colors.append(color)
values.append(value)
above = True
color = self.unfilled_color
colors.append(color)
values.append(val)
prev = val
value = self.format.format(value=value).replace('nan', self.nan_format)
return (
{'y0': values[:-1], 'y1': values[1:], 'color': colors},
{'y': [self.value], 'text': [value]}
)
def _get_model(self, doc, root=None, parent=None, comm=None):
params = self._get_properties(doc)
model = figure(
outline_line_color=None, toolbar_location=None, tools=[],
x_axis_location='above', y_axis_location='right', **params
)
model.grid.visible = False
model.xaxis.major_label_standoff = 2
model.yaxis.major_label_standoff = 2
model.xaxis.axis_label_standoff = 2
model.yaxis.axis_label_standoff = 2
self._update_name(model)
self._update_title_size(model)
self._update_tick_size(model)
self._update_figure(model)
self._update_axes(model)
self._update_renderers(model)
self._update_bounds(model)
self._design.apply_bokeh_theme_to_model(model)
root = root or model
self._models[root.ref['id']] = (model, parent)
return model
def _update_name(self, model):
model.xaxis.axis_label = self.name
model.yaxis.axis_label = self.name
def _update_title_size(self, model):
title_size = self.title_size or f'{self.width/6}px'
model.xaxis.axis_label_text_font_size = title_size
model.yaxis.axis_label_text_font_size = title_size
def _update_tick_size(self, model):
tick_size = self.tick_size or f'{self.width/9}px'
model.xaxis.major_label_text_font_size = tick_size
model.yaxis.major_label_text_font_size = tick_size
def _update_renderers(self, model):
model.renderers = []
properties = self._get_properties(model.document)
data, needle_data = self._get_data(properties)
bar_source = ColumnDataSource(data=data, name='bar_source')
needle_source = ColumnDataSource(data=needle_data, name='needle_source')
if self.horizontal:
model.hbar(
y=0.1, left='y0', right='y1', height=1, color='color',
source=bar_source
)
wedge_params = {'y': 0.5, 'x': 'y', 'angle': np.deg2rad(180)}
text_params = {
'y': -0.4, 'x': 0, 'text_align': 'left',
'text_baseline': 'top'
}
else:
model.vbar(
x=0.1, bottom='y0', top='y1', width=0.9, color='color',
source=bar_source
)
wedge_params = {'x': 0.5, 'y': 'y', 'angle': np.deg2rad(90)}
text_params = {
'x': -0.4, 'y': 0, 'text_align': 'left',
'text_baseline': 'bottom', 'angle': np.deg2rad(90)
}
model.scatter(
fill_color=self.needle_color, line_color=self.needle_color,
source=needle_source, name='needle_renderer', marker='triangle',
size=int(self.width/8), level='overlay', **wedge_params
)
value_size = self.value_size or f'{self.width/8}px'
model.text(
text='text', source=needle_source, text_font_size=value_size,
**text_params
)
def _update_bounds(self, model):
if self.horizontal:
x_range, y_range = tuple(self.bounds), (-0.8, 0.5)
else:
x_range, y_range = (-0.8, 0.5), tuple(self.bounds)
model.x_range.update(start=x_range[0], end=x_range[1])
model.y_range.update(start=y_range[0], end=y_range[1])
def _update_axes(self, model):
vmin, vmax = self.bounds
interval = (vmax-vmin)
if self.show_boundaries:
ticks = [vmin] + [v*interval for (v, _) in self._color_intervals]
else:
ticks = [vmin, vmax]
ticker = FixedTicker(ticks=ticks)
if self.horizontal:
model.xaxis.visible = True
model.xaxis.ticker = ticker
model.yaxis.visible = False
else:
model.xaxis.visible = False
model.yaxis.visible = True
model.yaxis.ticker = ticker
def _update_figure(self, model):
params = self._get_properties(model.document)
if self.horizontal:
params.update(width=self.height, height=self.width)
else:
params.update(width=self.width, height=self.height)
model.update(**params)
def _manual_update(self, events, model, doc, root, parent, comm):
update_data = False
for event in events:
if event.name in ('width', 'height'):
self._update_figure(model)
elif event.name == 'bounds':
self._update_bounds(model)
self._update_renderers(model)
elif event.name in self._data_params: