Skip to content

unable to delete drawings in subchart #525

@Xuan-Zheng-Xie

Description

@Xuan-Zheng-Xie

The drawings work when there is only one chart, but they cannot be deleted when multiple charts are present.

import pandas as pd
from lightweight_charts import Chart

def get_bar_data(symbol, timeframe):
if symbol not in ('AAPL', 'GOOGL', 'TSLA'):
print(f'No data for "{symbol}"')
return pd.DataFrame()
return pd.read_csv(f'bar_data/{symbol}_{timeframe}.csv')

def on_search(chart, searched_string):
new_data = get_bar_data(searched_string, chart.topbar['timeframe'].value)
if new_data.empty:
return
chart.topbar['symbol'].set(searched_string)
chart.set(new_data)

chart.toolbox.load_drawings(searched_string)

def on_timeframe_selection(chart):
new_data = get_bar_data(chart.topbar['symbol'].value, chart.topbar['timeframe'].value)
if new_data.empty:
return
chart.set(new_data, keep_drawings=True)

if name == 'main':
chart = Chart(toolbox=True, inner_width=1, inner_height=0.5, position='top')
chart2 = chart.create_subchart(toolbox=True, width=1, height=0.5, position='bottom')

chart.legend(True)

chart.events.search += on_search
chart.topbar.textbox('symbol', 'TSLA')
chart.topbar.switcher(
    'timeframe',
    ('1min', '5min', '30min'),
    default='5min',
    func=on_timeframe_selection
)

df = get_bar_data('TSLA', '5min')

chart.set(df)
chart2.set(df)
chart.toolbox.import_drawings('drawings.json')
chart.toolbox.load_drawings(chart.topbar['symbol'].value)  
chart.toolbox.save_drawings_under(chart.topbar['symbol'])  
chart.show(block=True)
chart.toolbox.export_drawings('drawings.json') 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions