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

[BUG] Precision is not updated in line legends

Open csanchez-aureum opened this issue 1 year ago • 1 comments

Expected Behavior

When I set chart.precision(5) I expect a 5 digits number be shown in the line legend

Current Behaviour

Even when I set the precision I always see a 2 digits number instead.

I've made a temporal fix by changing: this.legendItemFormat(s.value,t.precision) by this.legendItemFormat(s.value,this.handler.precision)

But I'm not sure if this is the "right" fix and this fix also depends on this PR to me merged https://github.com/louisnw01/lightweight-charts-python/pull/500

Reproducible Example

import pandas as pd
from lightweight_charts import Chart

def calculate_sma(df, period: int = 50):
    return pd.DataFrame({
        'time': df['date'],
        f'SMA {period}': df['close'].rolling(window=period).mean()
    }).dropna()

if __name__ == '__main__':
    chart = Chart()
    line = chart.create_line(name='SMA 50')

    df = pd.read_csv('ohlcv.csv')
    sma_df = calculate_sma(df, period=50)

    chart.precision(5)
    chart.legend(visible=True)

    chart.set(df)
    line.set(sma_df)
    
    chart.show(block=True)

Environment

- OS: Windows and Mac
- Library: 2.1

csanchez-aureum avatar Dec 30 '24 13:12 csanchez-aureum

I should have called line.precision(5) and the code works.

Maybe it will be a good idea to use the chart's precision when the line is created by for example calling: self.precision(chart.num_decimals)

csanchez-aureum avatar Jan 07 '25 14:01 csanchez-aureum