-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage.py
More file actions
349 lines (318 loc) · 9.65 KB
/
Copy pathusage.py
File metadata and controls
349 lines (318 loc) · 9.65 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
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
"""
Dash MUI Charts - LineChart Usage Example
This example demonstrates the basic usage of the LineChart component,
including multiple series, axis configuration, grid, click callbacks,
biaxial charts (dual Y-axes), and Pro features like zoom and pan.
"""
import os
from dash import Dash, html, callback, Input, Output
import json
# Load .env file if python-dotenv is available
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
pass
# Import the LineChart component
from dash_mui_charts import LineChart
# Load MUI X Pro license key from environment
MUI_LICENSE_KEY = os.environ.get('MUI_PRO_API_KEY', '')
app = Dash(__name__)
# =============================================================================
# Sample Data: US Unemployment Rate and GDP Per Capita (2000-2023)
# Similar to the MUI X Charts Pro example
# =============================================================================
years = list(range(2000, 2024))
# Unemployment rate data (%)
unemployment_rate = [
4.0, 4.7, 5.8, 6.0, 5.5, 5.1, 4.6, 4.6, 5.8, 9.3,
9.6, 8.9, 8.1, 7.4, 6.2, 5.3, 4.9, 4.4, 3.9, 3.7,
8.1, 5.4, 3.6, 3.7
]
# GDP per capita data ($)
gdp_per_capita = [
36330, 37134, 37998, 39496, 41713, 44115, 46299, 48050, 48570, 47099,
48468, 50066, 51784, 53107, 55049, 56863, 58021, 60110, 63064, 65280,
63544, 70249, 76343, 80035
]
app.layout = html.Div([
html.H1("Dash MUI Charts - LineChart Demo"),
# ==========================================================================
# Biaxial Chart: Unemployment Rate vs GDP Per Capita
# Inspired by MUI X Charts Pro LineOverview example
# ==========================================================================
html.H2("Biaxial Chart: US Unemployment Rate vs GDP Per Capita"),
html.P(
"This chart demonstrates dual Y-axes (biaxial) with different scales, "
"similar to the MUI X Charts Pro example.",
style={'color': '#666', 'marginBottom': '10px'}
),
LineChart(
id='biaxial-linechart',
licenseKey=MUI_LICENSE_KEY,
height=350,
series=[
{
'id': 'unemployment',
'data': unemployment_rate,
'label': 'Unemployment rate',
'color': '#af3838',
'showMark': False,
'yAxisId': 'unemployment-axis',
},
{
'id': 'gdp',
'data': gdp_per_capita,
'label': 'GDP per capita',
'color': '#4caf50',
'showMark': False,
'yAxisId': 'gdp-axis',
'connectNulls': True,
},
],
xAxis=[{
'data': years,
'scaleType': 'point',
}],
yAxis=[
{
'id': 'unemployment-axis',
'label': 'Unemployment Rate (%)',
'position': 'left',
'width': 60,
},
{
'id': 'gdp-axis',
'label': 'GDP per capita ($)',
'position': 'right',
'width': 80,
},
],
grid={'horizontal': True},
margin={'left': 70, 'right': 90, 'top': 20, 'bottom': 30},
),
html.P(
"Source: Sample data based on FRED",
style={'color': '#999', 'fontSize': '12px', 'marginTop': '5px'}
),
html.Hr(),
# ==========================================================================
# Pro Feature: Zoom and Pan
# ==========================================================================
html.H2("Pro Feature: Zoom and Pan"),
html.P(
"Use the slider below the chart to select a range, or drag the handles to zoom. "
"The slider provides intuitive control for exploring data over time.",
style={'color': '#666', 'marginBottom': '10px'}
),
LineChart(
id='zoom-linechart',
licenseKey=MUI_LICENSE_KEY,
height=400,
series=[
{
'id': 'unemployment',
'data': unemployment_rate,
'label': 'Unemployment Rate (%)',
'color': '#1976d2',
'showMark': False,
},
],
xAxis=[{
'id': 'x-axis',
'data': years,
'scaleType': 'point',
'label': 'Year',
'zoom': {
'minSpan': 5,
'maxSpan': 100,
'panning': True,
},
}],
yAxis=[{
'label': 'Unemployment Rate (%)',
}],
grid={'horizontal': True},
# Show the zoom slider below the chart
showSlider=True,
# Initial zoom state (start zoomed in to first half)
initialZoom=[{'axisId': 'x-axis', 'start': 0, 'end': 50}],
),
html.H3("Zoom State:"),
html.Pre(
id='zoom-output',
style={
'background': '#f5f5f5',
'padding': '15px',
'borderRadius': '5px',
'whiteSpace': 'pre-wrap',
}
),
html.Hr(),
# Basic LineChart with multiple series
html.H2("Basic Line Chart with Grid"),
LineChart(
id='basic-linechart',
licenseKey=MUI_LICENSE_KEY,
height=400,
series=[
{
'data': [2, 5.5, 2, 8.5, 1.5, 5],
'label': 'Series A',
'showMark': True,
},
{
'data': [4, 3.5, 6, 2.5, 4.5, 3],
'label': 'Series B',
},
],
xAxis=[{
'data': [1, 2, 3, 4, 5, 6],
'scaleType': 'point',
}],
grid={'horizontal': True, 'vertical': True},
),
html.Hr(),
# Area chart example
html.H2("Area Chart"),
LineChart(
id='area-linechart',
licenseKey=MUI_LICENSE_KEY,
height=300,
series=[
{
'data': [2, 5.5, 2, 8.5, 1.5, 5],
'label': 'Revenue',
'area': True,
'color': '#1976d2',
},
],
xAxis=[{
'data': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
'scaleType': 'band',
}],
),
html.Hr(),
# Stacked Area Chart
html.H2("Stacked Area Chart"),
LineChart(
id='stacked-area-linechart',
licenseKey=MUI_LICENSE_KEY,
height=300,
series=[
{
'data': [4, 3, 5, 4, 6, 3, 5],
'label': 'Product A',
'area': True,
'stack': 'total',
},
{
'data': [3, 4, 3, 5, 4, 6, 4],
'label': 'Product B',
'area': True,
'stack': 'total',
},
{
'data': [2, 2, 3, 2, 3, 2, 3],
'label': 'Product C',
'area': True,
'stack': 'total',
},
],
xAxis=[{
'data': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
'scaleType': 'point',
}],
colors=['#4e79a7', '#f28e2c', '#e15759'],
),
html.Hr(),
# Chart with custom colors and curves
html.H2("Custom Colors and Curves"),
LineChart(
id='custom-linechart',
licenseKey=MUI_LICENSE_KEY,
height=300,
series=[
{
'data': [1, 4, 2, 5, 7, 2, 4],
'label': 'Linear',
'curve': 'linear',
},
{
'data': [2, 3, 5, 4, 6, 3, 5],
'label': 'Monotone',
'curve': 'monotoneX',
},
{
'data': [3, 2, 4, 6, 5, 4, 3],
'label': 'Step',
'curve': 'step',
},
],
xAxis=[{
'data': [0, 1, 2, 3, 4, 5, 6],
}],
colors=['#ff6384', '#36a2eb', '#ffce56'],
),
html.Hr(),
# Interactive chart with click callback
html.H2("Interactive Chart with Click Events"),
LineChart(
id='interactive-linechart',
licenseKey=MUI_LICENSE_KEY,
height=350,
series=[
{
'data': [2400, 1398, 9800, 3908, 4800, 3800, 4300],
'label': 'Sales',
'showMark': True,
},
{
'data': [4000, 3000, 2000, 2780, 1890, 2390, 3490],
'label': 'Expenses',
'showMark': True,
},
],
xAxis=[{
'data': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
'scaleType': 'point',
'label': 'Day of Week',
}],
yAxis=[{
'label': 'Amount ($)',
}],
grid={'horizontal': True},
),
html.H3("Click Data:"),
html.Pre(
id='click-output',
style={
'background': '#f5f5f5',
'padding': '15px',
'borderRadius': '5px',
'whiteSpace': 'pre-wrap',
}
),
], style={'maxWidth': '900px', 'margin': '0 auto', 'padding': '20px'})
@callback(
Output('click-output', 'children'),
Input('interactive-linechart', 'clickData'),
prevent_initial_call=True
)
def display_click(click_data):
"""Display click event data."""
if click_data:
return json.dumps(click_data, indent=2)
return "Click on the chart to see event data"
@callback(
Output('zoom-output', 'children'),
Input('zoom-linechart', 'zoomData'),
prevent_initial_call=True
)
def display_zoom(zoom_data):
"""Display zoom state data."""
if zoom_data:
return json.dumps(zoom_data, indent=2)
return "Zoom the chart to see state changes"
if __name__ == '__main__':
app.run(debug=True, port=7666)