question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Chart width is jumping on series change from area to candles

See original GitHub issue

Lightweight Charts Version: ^3.7.0

Actual behavior:

Width jumping on series change, see the next video:

https://user-images.githubusercontent.com/32956521/146533627-7bdf6313-e766-4b77-8b0f-b9407c8b3632.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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
iksentcommented, Mar 16, 2022

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

1reaction
timocovcommented, 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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Charts candle width changes when adding another series
The point width for these candles is set using _stockSeries["PointWidth"] = "0.6"; I then add a new series to the same chart area...
Read more >
Changing thickness of Bars and Candles without Zooming
Manipulating Bar and Candle Width This video demonstrates the process. Yes. Open your preference window for the chart (double-clicking in horizontal scale ...
Read more >
My chart isn't locking/following the candles. How do I fix the ...
It's under settings, then time axis. There will be a option to add (or remove) a gap on the right side.
Read more >
Candlestick Math - A New Way Of Using Candlesticks - YouTube
When two single candle lines are combined, the meaning on the chart can become much more significant. You'll get an even sharper view...
Read more >
Changing bar / candle width independently of spacing
When changing candle spacing on the chart, MC automatically adjusts the bar or candle width so as to display them in such a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found