diff --git a/docs/source/reference/events.md b/docs/source/reference/events.md index 83a785a..63ee6e9 100644 --- a/docs/source/reference/events.md +++ b/docs/source/reference/events.md @@ -22,6 +22,11 @@ Fires when the range (visibleLogicalRange) changes. ``` +```{py:method} click -> (chart: Chart, time: NUM, price: NUM) +Fires when the mouse is clicked, returning the time and price of the clicked location. + +``` + ```` Tutorial: [Topbar & Events](../tutorials/events.md) diff --git a/lightweight_charts/util.py b/lightweight_charts/util.py index 1a441e8..4f2293a 100644 --- a/lightweight_charts/util.py +++ b/lightweight_charts/util.py @@ -159,4 +159,15 @@ class Events: wrapper=lambda o, c, *arg: o(c, *[float(a) for a in arg]) ) - + self.click = JSEmitter(chart, f'subscribe_click{salt}', + lambda o: chart.run_script(f''' + let clickHandler{salt} = (param) => {{ + if (!param.point) return; + const time = {chart.id}.chart.timeScale().coordinateToTime(param.point.x) + const price = {chart.id}.series.coordinateToPrice(param.point.y); + window.callbackFunction(`subscribe_click{salt}_~_${{time}};;;${{price}}`) + }} + {chart.id}.chart.subscribeClick(clickHandler{salt}) + '''), + wrapper=lambda func, c, *args: func(c, *[float(a) for a in args]) + )