Add titles to charts

This commit is contained in:
moshferatu 2024-01-03 06:32:29 -08:00
parent 6740993535
commit aa06e17dcf
7 changed files with 28 additions and 7 deletions

View File

@ -5,12 +5,21 @@ from typing import List
class CandlestickChart(Chart):
def __init__(self, x: Series, opens: Series, highs: Series, lows: Series, closes: Series):
def __init__(
self,
x: Series,
opens: Series,
highs: Series,
lows: Series,
closes: Series,
title: str = ''
):
self.x = x
self.opens = opens
self.highs = highs
self.lows = lows
self.closes = closes
self.title = title
def traces(self) -> List[Candlestick]:
return [Candlestick(

View File

@ -13,6 +13,7 @@ candlestick_chart = CandlestickChart(
opens = data['Open'],
highs = data['High'],
lows = data['Low'],
closes = data['Close']
closes = data['Close'],
title = 'SPX'
)
plot(candlestick_chart)

View File

@ -5,6 +5,7 @@ from typing import List
class Chart:
x: Series = None
title: str = None
def traces(self) -> List[BaseTraceType]:
pass

View File

@ -5,9 +5,10 @@ from typing import List
class LineChart(Chart):
def __init__(self, x: Series, values: List[Series]):
def __init__(self, x: Series, values: List[Series], title: str = ''):
self.x = x
self.values = values
self.title = title
def traces(self) -> List[Scatter]:
traces = []

View File

@ -10,6 +10,7 @@ data = ohlc('SPX.XO', '1d', start_date = start_date, end_date = end_date)
line_chart = LineChart(
x = data['Timestamp'],
values = [data['Close']]
values = [data['Close']],
title = 'SPX (Close)'
)
plot(line_chart)

View File

@ -10,6 +10,7 @@ data = ohlc('SPX.XO', '1d', start_date = start_date, end_date = end_date)
line_chart = LineChart(
x = data['Timestamp'],
values = [data['High'], data['Low']]
values = [data['High'], data['Low']],
title = 'SPX (High, Low)'
)
plot(line_chart)

View File

@ -3,11 +3,18 @@ from plotly.graph_objects import Figure
from plotly.subplots import make_subplots
from typing import List
def subplot_titles(subplots: List[List[Chart]]) -> List[str]:
subplot_titles = []
for row in subplots:
for chart in row:
subplot_titles.append(chart.title)
return subplot_titles
def figure_with_subplots(subplots: List[List[Chart]]) -> Figure:
num_rows = len(subplots)
num_columns = len(subplots[0])
figure = make_subplots(rows = num_rows, cols = num_columns)
figure = make_subplots(rows = num_rows, cols = num_columns, subplot_titles = subplot_titles(subplots))
for i, row in enumerate(subplots, start = 1):
for j, chart in enumerate(row, start = 1):