tradingview-apilightweight-charts

Adjust Default Zoom in lightweight charts


Does anyone know if there is a way to adjust the default zoom used on trading view lightweight charts when double-clicking the x-axis? To my knowledge, double-clicking evokes resetTimeScale but there doesn't seem to be any way to set which value is used as default.


Solution

  • The default zoom is determined by the barSpacing property of the time scale options.

    You can calculate what the barSpacing is for the current state of a chart by using the getVisibleLogicalRange method on the time scale API and the width of the time scale.

    For example:

    const visibleLogicalRange = chart.timeScale().getVisibleLogicalRange();
    const currentBarSpacing = chart.timeScale().width() / (visibleLogicalRange.to - visibleLogicalRange.from);
    
    // change the current barSpacing
    chart.timeScale().applyOptions({
      barSpacing: 12, // default is 6
    })