Add sync_crosshairs_only parameter to create_subchart (#293)

This commit is contained in:
louisnw
2024-02-24 13:26:59 +00:00
parent 745c14bb7f
commit fff23bd477
2 changed files with 26 additions and 11 deletions

View File

@ -98,13 +98,14 @@ class Window:
heading_background_colors, return_clicked_cells, func) heading_background_colors, return_clicked_cells, func)
def create_subchart(self, position: FLOAT = 'left', width: float = 0.5, height: float = 0.5, def create_subchart(self, position: FLOAT = 'left', width: float = 0.5, height: float = 0.5,
sync_id: str = None, scale_candles_only: bool = False, toolbox: bool = False sync_id: str = None, scale_candles_only: bool = False,
sync_crosshairs_only: bool = False, toolbox: bool = False
) -> 'AbstractChart': ) -> 'AbstractChart':
subchart = AbstractChart(self, width, height, scale_candles_only, toolbox, position=position) subchart = AbstractChart(self, width, height, scale_candles_only, toolbox, position=position)
if not sync_id: if not sync_id:
return subchart return subchart
self.run_script(f''' self.run_script(f'''
syncCharts({subchart.id}, {sync_id}) syncCharts({subchart.id}, {sync_id}, {jbool(sync_crosshairs_only)})
{subchart.id}.chart.timeScale().setVisibleLogicalRange( {subchart.id}.chart.timeScale().setVisibleLogicalRange(
{sync_id}.chart.timeScale().getVisibleLogicalRange() {sync_id}.chart.timeScale().getVisibleLogicalRange()
) )
@ -1021,7 +1022,9 @@ class AbstractChart(Candlestick, Pane):
def create_subchart(self, position: FLOAT = 'left', width: float = 0.5, height: float = 0.5, def create_subchart(self, position: FLOAT = 'left', width: float = 0.5, height: float = 0.5,
sync: Union[str, bool] = None, scale_candles_only: bool = False, sync: Union[str, bool] = None, scale_candles_only: bool = False,
sync_crosshairs_only: bool = False,
toolbox: bool = False) -> 'AbstractChart': toolbox: bool = False) -> 'AbstractChart':
if sync is True: if sync is True:
sync = self.id sync = self.id
return self.win.create_subchart(position, width, height, sync, scale_candles_only, toolbox) return self.win.create_subchart(position, width, height, sync,
scale_candles_only, sync_crosshairs_only, toolbox)

View File

@ -310,7 +310,7 @@ if (!window.Chart) {
return num.toString().padStart(8, ' '); return num.toString().padStart(8, ' ');
} }
legendHandler(param) { legendHandler(param, usingPoint= false) {
let options = this.chart.series.options() let options = this.chart.series.options()
if (!param.time) { if (!param.time) {
@ -319,8 +319,6 @@ if (!window.Chart) {
return return
} }
let usingPoint = !param.point && param.time
let data, logical let data, logical
if (usingPoint) { if (usingPoint) {
@ -353,7 +351,14 @@ if (!window.Chart) {
str += '| ' + percentStr str += '| ' + percentStr
} }
} }
let volumeData = param.seriesData.get(this.chart.volumeSeries)
let volumeData;
if (usingPoint) {
volumeData = this.chart.volumeSeries.dataByIndex(logical)
}
else {
volumeData = param.seriesData.get(this.chart.volumeSeries)
}
if (volumeData) { if (volumeData) {
str += this.ohlcEnabled ? `<br>V ${this.shorthandFormat(volumeData.value)}` : '' str += this.ohlcEnabled ? `<br>V ${this.shorthandFormat(volumeData.value)}` : ''
} }
@ -390,7 +395,7 @@ if (!window.Chart) {
window.Legend = Legend window.Legend = Legend
} }
function syncCharts(childChart, parentChart) { function syncCharts(childChart, parentChart, crosshairOnly= false) {
function crosshairHandler(chart, point) { function crosshairHandler(chart, point) {
if (!point) { if (!point) {
@ -398,7 +403,7 @@ function syncCharts(childChart, parentChart) {
return return
} }
chart.chart.setCrosshairPosition(point.value || point.close, point.time, chart.series); chart.chart.setCrosshairPosition(point.value || point.close, point.time, chart.series);
chart.legend.legendHandler(point) chart.legend.legendHandler(point, true)
} }
function getPoint(series, param) { function getPoint(series, param) {
@ -406,8 +411,15 @@ function syncCharts(childChart, parentChart) {
return param.seriesData.get(series) || null; return param.seriesData.get(series) || null;
} }
let setChildRange = (timeRange) => childChart.chart.timeScale().setVisibleLogicalRange(timeRange) let setChildRange, setParentRange;
let setParentRange = (timeRange) => parentChart.chart.timeScale().setVisibleLogicalRange(timeRange) if (crosshairOnly) {
setChildRange = (timeRange) => { }
setParentRange = (timeRange) => { }
}
else {
setChildRange = (timeRange) => childChart.chart.timeScale().setVisibleLogicalRange(timeRange)
setParentRange = (timeRange) => parentChart.chart.timeScale().setVisibleLogicalRange(timeRange)
}
let setParentCrosshair = (param) => { let setParentCrosshair = (param) => {
crosshairHandler(parentChart, getPoint(childChart.series, param)) crosshairHandler(parentChart, getPoint(childChart.series, param))