tools.py 1.5 KB

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