Skip to content

Commit fff23bd

Browse files
committed
Add sync_crosshairs_only parameter to create_subchart (#293)
1 parent 745c14b commit fff23bd

2 files changed

Lines changed: 26 additions & 11 deletions

File tree

lightweight_charts/abstract.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,14 @@ def create_table(
9898
heading_background_colors, return_clicked_cells, func)
9999

100100
def create_subchart(self, position: FLOAT = 'left', width: float = 0.5, height: float = 0.5,
101-
sync_id: str = None, scale_candles_only: bool = False, toolbox: bool = False
101+
sync_id: str = None, scale_candles_only: bool = False,
102+
sync_crosshairs_only: bool = False, toolbox: bool = False
102103
) -> 'AbstractChart':
103104
subchart = AbstractChart(self, width, height, scale_candles_only, toolbox, position=position)
104105
if not sync_id:
105106
return subchart
106107
self.run_script(f'''
107-
syncCharts({subchart.id}, {sync_id})
108+
syncCharts({subchart.id}, {sync_id}, {jbool(sync_crosshairs_only)})
108109
{subchart.id}.chart.timeScale().setVisibleLogicalRange(
109110
{sync_id}.chart.timeScale().getVisibleLogicalRange()
110111
)
@@ -1021,7 +1022,9 @@ def screenshot(self) -> bytes:
10211022

10221023
def create_subchart(self, position: FLOAT = 'left', width: float = 0.5, height: float = 0.5,
10231024
sync: Union[str, bool] = None, scale_candles_only: bool = False,
1025+
sync_crosshairs_only: bool = False,
10241026
toolbox: bool = False) -> 'AbstractChart':
10251027
if sync is True:
10261028
sync = self.id
1027-
return self.win.create_subchart(position, width, height, sync, scale_candles_only, toolbox)
1029+
return self.win.create_subchart(position, width, height, sync,
1030+
scale_candles_only, sync_crosshairs_only, toolbox)

lightweight_charts/js/funcs.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ if (!window.Chart) {
310310
return num.toString().padStart(8, ' ');
311311
}
312312

313-
legendHandler(param) {
313+
legendHandler(param, usingPoint= false) {
314314
let options = this.chart.series.options()
315315

316316
if (!param.time) {
@@ -319,8 +319,6 @@ if (!window.Chart) {
319319
return
320320
}
321321

322-
let usingPoint = !param.point && param.time
323-
324322
let data, logical
325323

326324
if (usingPoint) {
@@ -353,7 +351,14 @@ if (!window.Chart) {
353351
str += '| ' + percentStr
354352
}
355353
}
356-
let volumeData = param.seriesData.get(this.chart.volumeSeries)
354+
355+
let volumeData;
356+
if (usingPoint) {
357+
volumeData = this.chart.volumeSeries.dataByIndex(logical)
358+
}
359+
else {
360+
volumeData = param.seriesData.get(this.chart.volumeSeries)
361+
}
357362
if (volumeData) {
358363
str += this.ohlcEnabled ? `<br>V ${this.shorthandFormat(volumeData.value)}` : ''
359364
}
@@ -390,24 +395,31 @@ if (!window.Chart) {
390395
window.Legend = Legend
391396
}
392397

393-
function syncCharts(childChart, parentChart) {
398+
function syncCharts(childChart, parentChart, crosshairOnly= false) {
394399

395400
function crosshairHandler(chart, point) {
396401
if (!point) {
397402
chart.chart.clearCrosshairPosition()
398403
return
399404
}
400405
chart.chart.setCrosshairPosition(point.value || point.close, point.time, chart.series);
401-
chart.legend.legendHandler(point)
406+
chart.legend.legendHandler(point, true)
402407
}
403408

404409
function getPoint(series, param) {
405410
if (!param.time) return null;
406411
return param.seriesData.get(series) || null;
407412
}
408413

409-
let setChildRange = (timeRange) => childChart.chart.timeScale().setVisibleLogicalRange(timeRange)
410-
let setParentRange = (timeRange) => parentChart.chart.timeScale().setVisibleLogicalRange(timeRange)
414+
let setChildRange, setParentRange;
415+
if (crosshairOnly) {
416+
setChildRange = (timeRange) => { }
417+
setParentRange = (timeRange) => { }
418+
}
419+
else {
420+
setChildRange = (timeRange) => childChart.chart.timeScale().setVisibleLogicalRange(timeRange)
421+
setParentRange = (timeRange) => parentChart.chart.timeScale().setVisibleLogicalRange(timeRange)
422+
}
411423

412424
let setParentCrosshair = (param) => {
413425
crosshairHandler(parentChart, getPoint(childChart.series, param))

0 commit comments

Comments
 (0)