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

Chart width is jumping on series change from area to candles #943

Closed
iksent opened this issue Dec 17, 2021 · 6 comments · Fixed by #959
Closed

Chart width is jumping on series change from area to candles #943

iksent opened this issue Dec 17, 2021 · 6 comments · Fixed by #959
Assignees
Labels
bug Unexpected problem or unintended behavior.
Milestone

Comments

@iksent
Copy link

iksent commented Dec 17, 2021

Lightweight Charts Version: ^3.7.0

Actual behavior:

Width jumping on series change, see the next video:

2021-12-17.15.50.29.mov

Expected behavior:

No width jumping.

Steps/code to reproduce:

import React, { useCallback, useEffect, useReducer, useRef } from 'react'
import { useOnResize } from 'Core/utils/useOnResize'
import {
  ChartOptions,
  createChart,
  DeepPartial,
  IChartApi,
  ISeriesApi,
  SeriesType,
} from 'lightweight-charts'
import { useChartStyles } from './styles'
import { ChartCandlestickShape, ChartCommonShape, ChartType } from './types'

type Props = {
  className?: string
  data: Array<ChartCommonShape> | Array<ChartCandlestickShape>
  options?: DeepPartial<ChartOptions>
  type: ChartType
}

function ChartLocal(props: Props): JSX.Element {
  const { className = '', data, options, type } = props
  const [, forceUpdate] = useReducer((p: number) => p + 1, 0)
  const chartStyles = useChartStyles()
  const prevChartStyles = useRef(chartStyles)

  const chartRef = useRef<HTMLDivElement | null>(null)
  const chart = useRef<IChartApi>()
  const series = useRef<ISeriesApi<SeriesType>>()

  const handleResize = useCallback(() => {
    if (chart.current && chartRef.current) {
      chart.current?.resize(
        chartRef.current.clientWidth,
        chartRef.current.clientHeight,
        true,
      )
      chart.current?.timeScale().fitContent()
    }
  }, [])

  useOnResize(handleResize, 50)

  useEffect(() => {
    if (chartRef.current !== null) {
      if (chart.current === undefined) {
        chart.current = createChart(chartRef.current, {
          height: chartRef.current.clientHeight,
          width: chartRef.current.clientWidth,
          ...chartStyles.basic,
          ...options,
        })
      }

      if (series.current) {
        chart.current?.removeSeries(series.current)
      }

      if (type === 'Candlestick') {
        series.current = chart.current?.addCandlestickSeries(chartStyles.candle)
      } else {
        series.current = chart.current?.addAreaSeries(chartStyles.area)
      }

      if (data) {
        series.current?.setData(data)
      }

      chart.current?.timeScale().resetTimeScale()
      chart.current?.timeScale().fitContent()
      forceUpdate() // Update for tooltip
    }
  }, [chartRef.current, data, chartStyles])

  return (
    <div ref={chartRef} className={className}/>
  )
}

export default ChartLocal
@timocov
Copy link
Contributor

timocov commented Dec 17, 2021

@iksent can you provide complete example on https://codesandbox.io please?

@iksent
Copy link
Author

iksent commented Dec 17, 2021

Ouch. Do you already have an example with switching between candlesticks and area/line types?

@timocov
Copy link
Contributor

timocov commented Dec 17, 2021

No, I don't think so.

@iksent
Copy link
Author

iksent commented Dec 20, 2021

https://jsfiddle.net/iksent/rxpcd0eL/

  • I only ran into this problem with certain data.
  • Options and configs, in my opinion, do not affect this.
  • Area => Canldes = OK
  • Candles => Area = Double jump

@timocov timocov added the needs investigation Needs further investigation. label Dec 29, 2021
@timocov timocov added this to the 3.8 milestone Dec 29, 2021
@timocov
Copy link
Contributor

timocov commented Dec 29, 2021

@iksent thanks! btw, if you add a new series first and then remove the old one it seems that it works fine. but this is quite strange for sure

@timocov timocov added bug Unexpected problem or unintended behavior. and removed needs investigation Needs further investigation. labels Dec 31, 2021
@timocov timocov self-assigned this Dec 31, 2021
timocov added a commit that referenced this issue Dec 31, 2021
@iksent
Copy link
Author

iksent commented Mar 16, 2022

I can confirm this issue is fixed, thanks a lot, @timocov!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected problem or unintended behavior.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants