From 865a578e08370a8d85f1e673c740ddfb2876b246 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Thu, 4 Jan 2024 05:26:58 -0800 Subject: [PATCH] Add the ability to set the color of a line --- line.py | 9 +++++++++ line_chart.py | 12 +++++++----- line_chart_example.py | 3 ++- multi_line_chart_example.py | 6 +++++- 4 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 line.py diff --git a/line.py b/line.py new file mode 100644 index 0000000..47cebc1 --- /dev/null +++ b/line.py @@ -0,0 +1,9 @@ +from pandas import Series +from typing import List + +class Line(): + + def __init__(self, values: List[Series], name: str = '', color: str = 'yellow'): + self.values = values + self.name = name + self.color = color \ No newline at end of file diff --git a/line_chart.py b/line_chart.py index 944171e..69940ae 100644 --- a/line_chart.py +++ b/line_chart.py @@ -1,22 +1,24 @@ from chart import Chart +from line import Line from pandas import Series from plotly.graph_objects import Scatter from typing import List class LineChart(Chart): - def __init__(self, x: Series, values: List[Series], title: str = ''): + def __init__(self, x: Series, lines: List[Line], title: str = ''): self.x = x - self.values = values + self.lines = lines self.title = title def traces(self) -> List[Scatter]: traces = [] - for value in self.values: + for line in self.lines: traces.append(Scatter( x = self.x, - y = value, + y = line.values, mode = 'lines', - line=dict(color = 'yellow') # TODO: Make this configurable. + name = line.name, + line = dict(color = line.color) )) return traces \ No newline at end of file diff --git a/line_chart_example.py b/line_chart_example.py index 009b2cb..e52947b 100644 --- a/line_chart_example.py +++ b/line_chart_example.py @@ -1,6 +1,7 @@ from database.ohlc import ohlc from datetime import datetime, timedelta +from line import Line from line_chart import LineChart from plot import plot @@ -10,7 +11,7 @@ data = ohlc('SPX.XO', '1d', start_date = start_date, end_date = end_date) line_chart = LineChart( x = data['Timestamp'], - values = [data['Close']], + lines = [Line(data['Close'], name = 'Close')], title = 'SPX (Close)' ) plot(line_chart) \ No newline at end of file diff --git a/multi_line_chart_example.py b/multi_line_chart_example.py index a1f2e41..53055b6 100644 --- a/multi_line_chart_example.py +++ b/multi_line_chart_example.py @@ -1,6 +1,7 @@ from database.ohlc import ohlc from datetime import datetime, timedelta +from line import Line from line_chart import LineChart from plot import plot @@ -10,7 +11,10 @@ data = ohlc('SPX.XO', '1d', start_date = start_date, end_date = end_date) line_chart = LineChart( x = data['Timestamp'], - values = [data['High'], data['Low']], + lines = [ + Line(data['High'], name = 'High', color = 'limegreen'), + Line(data['Low'], name = 'Low', color = 'red') + ], title = 'SPX (High, Low)' ) plot(line_chart) \ No newline at end of file