tools.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import plotly.graph_objects as go
  2. import math
  3. import pandas as pd
  4. def get_options(options_list):
  5. dict_list = []
  6. for i in options_list:
  7. dict_list.append({'label': i, 'value': i})
  8. return dict_list
  9. def generate_figure(cols, df, title):
  10. trace = []
  11. # STEP 2
  12. # Draw and append traces for each stock
  13. for c in cols if cols is not None else []:
  14. s = df[c].dropna()
  15. trace.append(go.Scatter(x=s.index,
  16. y=s,
  17. mode='lines',
  18. opacity=0.7,
  19. name=c,
  20. textposition='bottom center',
  21. showlegend=True))
  22. # STEP 3
  23. traces = [trace]
  24. data = [val for sublist in traces for val in sublist]
  25. # Define Figure
  26. # STEP 4
  27. figure = {'data': data,
  28. 'layout': go.Layout(
  29. colorway=["#5E0DAC", '#FF4F00', '#375CB1', '#FF7400', '#FFF400', '#FF0056'],
  30. template='plotly_dark',
  31. paper_bgcolor='rgba(0, 0, 0, 0)',
  32. plot_bgcolor='rgba(0, 0, 0, 0)',
  33. margin={'b': 15},
  34. hovermode='x',
  35. autosize=True,
  36. title={'text': title,
  37. 'font': {'color': 'white'}, 'x': 0.5},
  38. xaxis={'range': [df.index.min() if df is not None else 0,
  39. df.index.max() if df is not None else 0]},
  40. ),
  41. }
  42. return figure