import vizro.plotly.express as px
from vizro import Vizro
import vizro.models as vm
import pandas as pd
# Read and process data
df = pd.read_csv('merged_stock_data_iso_format.csv')
df['date'] = pd.to_datetime(df['date'], format='mixed', dayfirst=True)
df = df.sort_values('date')
# Define the date range selector buttons
fin_buttons = [
{'count': 48, 'label': '48HR', 'step': "hour", 'stepmode': "todate"},
{'count': 7, 'label': "1WTD", 'step': "day", 'stepmode': "todate"},
{'count': 1, 'label': "1M", 'step': "month", 'stepmode': "backward"},
{'count': 3, 'label': "3M", 'step': "month", 'stepmode': "backward"},
{'count': 6, 'label': "6M", 'step': "month", 'stepmode': "backward"},
{'count': 1, 'label': "YTD", 'step': "year", 'stepmode': "todate"},
{"step": "all"}
]
# Create the figure and apply layout changes
fig = px.line(
data_frame=df,
x='date',
y='adjusted_close',
color='dataset_name',
title='Stock Prices Over Time'
)
fig.update_xaxes(
title_text = 'Date',
autotickangles = [45, 60, 90],
dtick="M1",
tickformat="%b\n%Y"
)
fig.update_yaxes(
title_text = 'Adjusted Closing Price (MWK)'
)
fig.update_layout(
showlegend = False
)
fig.update_layout(
xaxis=dict(
rangeselector=dict(buttons=fin_buttons),
type="date"
)
)
# Define the Graph component
graph = vm.Graph(
id="stock_graph",
figure=fig
)
# Define the page with filters
page = vm.Page(
title="Stock prices trend lines over time",
id="stock_prices_trend_page",
components=[graph],
controls=[
vm.Filter(
column="dataset_name",
selector=vm.Dropdown(title='Stock name:'),
targets=["stock_graph"]
),
vm.Filter(
column="date",
selector=vm.DatePicker(title='Select date:'),
targets=["stock_graph"]
),
],
)
# Build and run the dashboard
dashboard = vm.Dashboard(pages=[page])
Vizro().build(dashboard).run()