-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
69 lines (62 loc) · 1.81 KB
/
Copy pathapp.py
File metadata and controls
69 lines (62 loc) · 1.81 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
import dash
from dash import dcc, html
from dash.dependencies import Input, Output
import numpy as np
# Initialize the Dash app
app = dash.Dash(__name__)
# Sample data
categories = ['A', 'B', 'C', 'D']
values1 = np.random.randint(10, 100, size=4)
values2 = np.random.randint(10, 100, size=4)
# App layout
app.layout = html.Div([
dcc.Graph(id='bar-chart'),
dcc.Dropdown(
id='chart-type-dropdown',
options=[
{'label': 'Grouped', 'value': 'group'},
{'label': 'Stacked', 'value': 'stack'}
],
value='group', # Default value
clearable=False,
style={'width': '50%'}
),
dcc.Store(id='bar-data', data={'categories': categories, 'values1': list(values1), 'values2': list(values2)})
])
# Client-side callback (JavaScript function)
app.clientside_callback(
"""
function(chartType, data) {
var categories = data.categories;
var values1 = data.values1;
var values2 = data.values2;
var barmode = chartType === 'stack' ? 'stack' : 'group';
return {
'data': [
{
'x': categories,
'y': values1,
'type': 'bar',
'name': 'Series 1'
},
{
'x': categories,
'y': values2,
'type': 'bar',
'name': 'Series 2'
}
],
'layout': {
'title': `Bar Chart (${chartType === 'stack' ? 'Stacked' : 'Grouped'})`,
'barmode': barmode
}
};
}
""",
Output('bar-chart', 'figure'),
[Input('chart-type-dropdown', 'value')],
[Input('bar-data', 'data')]
)
# Run the app
if __name__ == '__main__':
app.run_server(debug=False)