Remove gaps from charts due to weekends and holidays

This commit is contained in:
moshferatu 2024-01-02 05:36:08 -08:00
parent 81ae787aa5
commit 0993aa8c3d
2 changed files with 25 additions and 3 deletions

View File

@ -1,7 +1,18 @@
from pandas import date_range, Series
from plotly.basedatatypes import BaseTraceType
from typing import List
class Chart:
x: Series = None
def traces(self) -> List[BaseTraceType]:
pass
pass
def range_breaks(self) -> List[dict]:
# Assuming x is either a date or timestamp.
start_date = self.x.min()
end_date = self.x.max()
all_dates = date_range(start = start_date, end = end_date)
missing_dates = all_dates.difference(self.x)
return [dict(values = missing_dates)]

15
plot.py
View File

@ -13,8 +13,19 @@ def figure_with_subplots(subplots: List[List[Chart]]) -> Figure:
for j, chart in enumerate(row, start = 1):
for trace in chart.traces():
figure.add_trace(trace, row = i, col = j)
figure.update_xaxes(showgrid = False, showticklabels = True, rangeslider = dict(visible = False), row = i, col = j)
figure.update_yaxes(showgrid = False, side = 'right', row = i, col = j)
figure.update_xaxes(
showgrid = False,
showticklabels = True,
rangebreaks = chart.range_breaks(),
rangeslider = dict(visible = False),
row = i, col = j
)
figure.update_yaxes(
showgrid = False,
side = 'right',
row = i, col = j
)
# TODO: Make this configurable.
figure.update_layout(