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

Upgrade to [email protected] #16

Open
mozeryansky opened this issue Oct 23, 2023 · 2 comments · Fixed by #17
Open

Upgrade to [email protected] #16

mozeryansky opened this issue Oct 23, 2023 · 2 comments · Fixed by #17
Assignees

Comments

@mozeryansky
Copy link

Here's the release notes: https://tradingview.github.io/lightweight-charts/docs/release-notes#410

@trash-and-fire
Copy link
Owner

Yes, sure. You can use the new version and some parts of the API right now. I'm looking for the best approach to implement <CustomSeries> and <SeriesPrimitive> into the svelte-wrapper repository and will do the same here asap.

@mozeryansky
Copy link
Author

You set the peerDependencies to ">=4.0.0", so I was able to use 4.1.0. It's been working fine for me and I meant to post back here when I got it all setup. For 4.1.0 they also made it easier to sync the cursor and I just got that all working too.

Based off their example from here: https://tradingview.github.io/lightweight-charts/tutorials/how_to/set-crosshair-position

This is how I did the same with the wrapper library:

  const ref1 = useRef<IChartApi>(null)
  const ref2 = useRef<IChartApi>(null)

  const seriesRef1 = useRef<ISeriesApi<any>>(null)
  const seriesRef2 = useRef<ISeriesApi<any>>(null)

  function syncCrosshair(
    otherRef: RefObject<IChartApi>,
    otherSeries: RefObject<ISeriesApi<any>>
  ) {
    return (param: MouseEventParams<HorzScaleItem>) => {
      const otherData = otherSeries.current?.data() || []
      const point = otherData.find(point => point.time == param.time)
      if (!point) return
      otherRef.current?.setCrosshairPosition(
        point.value,
        point.time,
        otherSeries.current!
      )
    }
  }

  function syncVisibleRange(ref: RefObject<IChartApi>) {
    return (timeRange: LogicalRange | null) => {
      if (!timeRange) return
      ref.current?.timeScale().setVisibleLogicalRange(timeRange)
    }
  }
      <div className="gap-2 overflow-auto rounded-lg border">
        <Chart
          container={{ style: { height: '400px' } }}
          autoSize
          ref={ref1}
          onCrosshairMove={syncCrosshair(ref2, seriesRef2)}
          timeScale={{ visible: false }}
          crosshair={{ mode: 0 }}
        >
          <CandlestickSeries data={candleData} ref={seriesRef1} />
          <TimeScale onVisibleLogicalRangeChange={syncVisibleRange(ref2)} />
        </Chart>

        <Chart
          container={{ style: { height: '200px' } }}
          autoSize
          ref={ref2}
          onCrosshairMove={syncCrosshair(ref1, seriesRef1)}
          rightPriceScale={{ minimumWidth: 66 }}
        >
          <HistogramSeries
            data={volume}
            priceScaleId="overlay"
            color="#26a69a"
            priceFormat={{ type: 'volume' }}
            ref={seriesRef2}
          />
          <TimeScale onVisibleLogicalRangeChange={syncVisibleRange(ref1)} />
        </Chart>
      </div>

It'd be cool if the wrapper could handle the syncing code, but it's fairly minimal code anyway.

@trash-and-fire trash-and-fire self-assigned this Oct 24, 2023
@trash-and-fire trash-and-fire linked a pull request Oct 24, 2023 that will close this issue
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

Successfully merging a pull request may close this issue.

2 participants