This repository was archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathdash-scattergl-random.py
151 lines (133 loc) · 4.26 KB
/
dash-scattergl-random.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
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import numpy as np
import pandas as pd
app = dash.Dash()
app.css.config.serve_locally = True
app.scripts.config.serve_locally = True
df = pd.DataFrame({
'Column {}'.format(i): np.random.rand(50) + i*10
for i in range(6)})
app.layout = html.Div([
html.Div(
dcc.Graph(
id='g1',
selectedData=None
), className="four columns"
),
html.Div(
dcc.Graph(
id='g2',
selectedData=None
), className="four columns"
),
html.Div(
dcc.Graph(
id='g3',
selectedData=None
), className="four columns"
)
], className="row")
def highlight(x, y):
def callback(*selectedDatas):
if not any(selectedDatas):
selectedpoints = []
else:
selectedpoints = df.index
for i, selected_data in enumerate(selectedDatas):
if selected_data is not None:
selected_index = [
p['customdata'] for p in selected_data['points']
]
if len(selected_index) > 0:
selectedpoints = np.intersect1d(
selectedpoints, selected_index)
figure = {
'data': [
dict({
'x': df[x],
'y': df[y],
'text': df.index,
'selectedpoints': selectedpoints,
'customdata': df.index,
'type': 'scatter',
'mode': 'markers+text',
'textposition': 'top',
'marker': {
'color': '#0074D9',
'size': 12
},
'selected': {
'marker': {
'opacity': 1
},
'mode': 'markers+text'
},
'unselected': {
'marker': {
'opacity': 0.3,
},
'mode': 'markers'
}
}),
],
'layout': {
'margin': {'l': 20, 'r': 0, 'b': 20, 't': 5},
'dragmode': 'select',
'hovermode': 'closest',
'showlegend': False
}
}
# Display a rectangle to highlight the previously selected region
shape = {
'type': 'rect',
'line': {
'width': 1,
'dash': 'dot',
'color': 'darkgrey'
}
}
if selectedDatas[0] and selectedDatas[0]['range']:
figure['layout']['shapes'] = [dict({
'x0': selectedDatas[0]['range']['x'][0],
'x1': selectedDatas[0]['range']['x'][1],
'y0': selectedDatas[0]['range']['y'][0],
'y1': selectedDatas[0]['range']['y'][1]
}, **shape)]
else:
figure['layout']['shapes'] = [dict({
'type': 'rect',
'x0': np.min(df[x]),
'x1': np.max(df[x]),
'y0': np.min(df[y]),
'y1': np.max(df[y])
}, **shape)]
return figure
return callback
app.css.append_css({
"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"})
# app.callback is a decorator which means that it takes a function
# as its argument.
# highlight is a function "generator": it's a function that returns function
app.callback(
Output('g1', 'figure'),
[Input('g1', 'selectedData'),
Input('g2', 'selectedData'),
Input('g3', 'selectedData')]
)(highlight('Column 0', 'Column 1'))
app.callback(
Output('g2', 'figure'),
[Input('g2', 'selectedData'),
Input('g1', 'selectedData'),
Input('g3', 'selectedData')]
)(highlight('Column 2', 'Column 3'))
app.callback(
Output('g3', 'figure'),
[Input('g3', 'selectedData'),
Input('g1', 'selectedData'),
Input('g2', 'selectedData')]
)(highlight('Column 4', 'Column 5'))
if __name__ == '__main__':
app.run_server(debug=True)