forked from plotly/plotly.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdash_app_with_tooltip.py
88 lines (80 loc) · 2.09 KB
/
dash_app_with_tooltip.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
"""
This Dash application is intended to be run with a custom version of plotly.js that includes tooltip functionality.
The custom plotly.js file should be placed in the assets folder of the Dash project.
"""
import dash
from dash import dcc, html
import plotly.graph_objs as go
import datetime
import numpy as np
# Initialize the Dash app
app = dash.Dash(__name__)
# Generate date and time values
def generate_date_time(start, count):
dates = []
current = datetime.datetime.fromisoformat(start)
for _ in range(count):
dates.append(current)
current += datetime.timedelta(hours=7)
return dates
# Generate random y-values
def generate_random_y_values(count):
return np.random.random(count) * 20
# Generate data points
# Define the base trace dictionary
base_trace = {
'name': None,
'x': None,
'y': None,
'type': 'scatter',
'mode': 'markers',
'marker': {'size': 10},
'tooltiptemplate': "%{fullData.name}<br>%{x|%H:%M:%S}<br>y: %{y:.2e}",
'tooltip': {
'align': 'left',
'arrowcolor': 'blue',
'arrowhead': 2,
'arrowsize': 1.2,
'arrowwidth': 1,
'font': {
'color': 'red',
'family': 'Arial',
'size': 16
},
'showarrow': True,
'xanchor': 'left'
}
}
# Create 3 traces using dictionary comprehension
traces = [
{
**base_trace,
'name': f'Trace {i+1}',
'x': generate_date_time(f'2025-04-0{i+1}T12:00:00', 5),
'y': generate_random_y_values(5)
}
for i in range(3)
]
# Layout definition
layout = go.Layout(
title='Custom Tooltip Example',
hovermode='closest'
)
# App layout
app.layout = html.Div([
html.H1("Plotly Tooltip Button Test"),
dcc.Graph(
id='plot',
figure={
'data': [*traces],
'layout': layout
},
config={
'editable': True,
'modeBarButtonsToAdd': ['tooltip', 'hoverclosest', 'hovercompare', 'togglespikelines'],
'displaylogo': False,
}
)
])
if __name__ == '__main__':
app.run_server(debug=True)