Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PaneRendererCustomData has x=NaN in bars outside of visible range #1612

Closed
stefda opened this issue Jun 3, 2024 · 3 comments
Closed

PaneRendererCustomData has x=NaN in bars outside of visible range #1612

stefda opened this issue Jun 3, 2024 · 3 comments

Comments

@stefda
Copy link

stefda commented Jun 3, 2024

Lightweight Charts™ Version: 4.1.4

Steps/code to reproduce:

  1. Spin up a Custom Series projecs (called MyCustomSeries) using the provided npm create lwc-plugin@latest utility
  2. console.log(data) in the created MyCustomSeries.update method
  3. Inspect the logged data, namely the bars property and their x attribute - it is NaN for any bars that fall outside of the currently visible range

Actual behavior:

The bars.x property in the PaneRendererCustomData provided through the MyCustomSeries.update method is NaN in bars outside of the visible range.

Expected behavior:

The bars.x property in the PaneRendererCustomData provided through the MyCustomSeries.update method is set with the correct x value, including for bars outside the visible range. The x will be negative for values before the start of the visible range and greater than the media width of the visible pane for any bars beyond the visible range.

Screenshots:

image

Use case

I am creating a renderer to draw custom series that have a price value at each bar (and that value may be drawn as a point or as a k-line), and some some of the points are connectes with a line (think open position point and realized PNL point). The start/end of a line may fall outside of the visible range when scrolling through the chart, but as the bar.x value is NaN outside the visible range, I have no way of determining the correct placement of those points in such scenario.

I would like to know if the issue described above is indeed a bug or a feature. If it's the latter then it might be useful to discuss this case in the CustomBarItemData docs.

@stefda
Copy link
Author

stefda commented Jun 3, 2024

I have a feeling it might be related to the issue described here. This was a red herring, ignore.

@SlicedSilver
Copy link
Contributor

Providing NaN values is the expected behaviour. It is slightly quicker to generate the rendering data, and also makes it easier to write the renderer code such that it is only drawing what is visible. However if you need to know what the offscreen values would be then it is possible to calculate it.

The spacing between each x coordinate is consistent, and is defined by the barSpacing of the timescale. The PaneRendererCustomData which is provided to the custom series' update method contains this barSpacing value. So where you are currently reading the bars array, a sibling property provides the spacing.

It would be quite simple to calculate what the offscreen x values would be by using this information. For example, if you have a barSpacing of 6 of an list of x values like this: [NaN, NaN, -2, 4, 10, 16, NaN] then you subtract or add the barSpacing consecutively to get: [-14, -8, -2, 4, 10, 16, 22]

I would recommend that you calculate such values required for the renderer within the 'View' but you can do this within the renderer as well (no forced rules on this).

@stefda
Copy link
Author

stefda commented Jun 4, 2024

Thanks, got it. What you're suggesting is exactly what I ended up doing. Good to get a validation!

@stefda stefda closed this as completed Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants