lightweight-charts icon indicating copy to clipboard operation
lightweight-charts copied to clipboard

[Bug] `timeScale` width is 0 after `visiblity` change

Open nichita-pasecinic opened this issue 1 year ago • 5 comments

Lightweight Charts™ Version: 4.0.1

  1. Create a chart instance with timeScale.visible: false.
  2. Using chart.applyOptions({ timescale: { visible: true } }) to make timescale visible
  3. Then read width of the timescale - in the same call stack (it will be 0)

Repro on CodeSandbox

if calling the .width() method in a timeout it works, is this expected ?

chart.applyOptions({
    timeScale: { visible: true },
  });

setTimeout(() => {
    const width = chart.timeScale().width();
    console.log("width: ", width);
}, 0);

nichita-pasecinic avatar Aug 17 '23 10:08 nichita-pasecinic

Yes, I believe that this is expected behaviour (only check value after a timeout like requestAnimationFrame). For the chart to know the width of the timeScale it needs to render it. So it can't be checked immediately.

However, it should be possible for the chart to know what the width of the timescale would be if it was visible (even if it is not) because the timescale width is the same as the pane width (chart width minus the price scale widths).

SlicedSilver avatar Aug 17 '23 13:08 SlicedSilver

@SlicedSilver thanks for explanations.

I agree with:

However, it should be possible for the chart to know what the width of the timescale would be if it was visible (even if it is not) because the timescale width is the same as the pane width (chart width minus the price scale widths).

nichita-pasecinic avatar Aug 17 '23 13:08 nichita-pasecinic

I'm also running into an issue around this where I'm trying to render markers over a series and when I call setMarkers on the series the markers don't render. This is because when this._internal__series._internal_setMarkers(convertedMarkers) is called within in setMarkers there is a call to _private__recalculateMarkers which retrieves the timescale and then does a check timeScale._internal_isEmpty(). This check checks the width this._private__width === 0 and is causing the markers to not render.

ghost avatar Sep 01 '23 17:09 ghost

@skdillon Would you say that your issue is the same as this one?

  • https://github.com/tradingview/lightweight-charts/issues/1271
  • https://github.com/tradingview/lightweight-charts/pull/1281

SlicedSilver avatar Sep 01 '23 18:09 SlicedSilver

Yup, bumping our patch version resolved the issue. Thanks for pointing that out @SlicedSilver

ghost avatar Sep 01 '23 19:09 ghost