fluttercandies / flutter-interactive-chart

A candlestick chart that supports pinch-to-zoom and panning.
https://pub.dev/packages/interactive_chart
MIT License
209 stars 66 forks source link

Setting a default zoom #6

Closed calculabs closed 2 years ago

calculabs commented 2 years ago

Would it be possible for you implement the ability to set a default zoom? By this, we mean zoomed all the way in or zoomed all the way out, when the chart is created.

Currently, it's zooming in a bit by default and there are candlesticks outside of the viewport width.

h65wang commented 2 years ago

This is addressed in version 0.3.2 with a new parameter initialVisibleCandleCount.

kevincherryholme commented 2 years ago

@h65wang Hey! Thank you for the quick response time with initialVisibleCandleCount.

I believe I found an issue regarding it however.

Whenever I scroll past the original candle data, past the initialVisibleCandleCount I get this error: in chart_painter.dart line 152

Failed assertion: line 52 pos 10: '<optimized out>': Offset argument contained a NaN value.

It seems once I pan to the right, passing the index of initialVisibleCandleCount the entire graph crashes.

Thank you!

h65wang commented 2 years ago

@kevincherryholme This is not a bug regarding the new parameter. It might be something wrong with your data, or other modifications you made in your fork.

kevincherryholme commented 2 years ago

@h65wang Hey! I think you're right that it does involve the data, but I think the exception can be avoided via code.

If you provide a list of CandleData with the exact same data, for example

[
CandleData(timestamp: DateTime.now().millisecondsSinceEpoch, open: 48, close: 51, volume: 4),
CandleData(timestamp: DateTime.now().millisecondsSinceEpoch, open: 48, close: 51, volume: 4),
CandleData(timestamp: DateTime.now().millisecondsSinceEpoch, open: 48, close: 51, volume: 4),
CandleData(timestamp: DateTime.now().millisecondsSinceEpoch, open: 48, close: 51, volume: 4),
CandleData(timestamp: DateTime.now().millisecondsSinceEpoch, open: 48, close: 51, volume: 4),
...
]

The fitVolume() method of painter_params.dart throws a NaN exception. Specifically, the volGridSize equals to Infinity because we divide by 0. See below my results from debugging:

// This is the method:
double fitVolume(double y) {
    try {
      final gap = 12; // the gap between price bars and volume bars
      final baseAmount = 2; // display at least "something" for the lowest volume
      final volGridSize = (volumeHeight - baseAmount - gap) / (maxVol - minVol);
      final vol = (y - minVol) * volGridSize;
      return volumeHeight - vol + priceHeight - baseAmount;
    } catch (e) {
      print(e);
      return 0;
    }
  }

Screenshot: image image

I believe this happens because (maxVol - minVol) is (4 - 4) which results to 0. Dividing by zero throws the exception.

I am not sure if this means the data should be changed, or if we should change the fitVolume() method. What do you think @h65wang ?

I hope this helps clarify things! Cheers for the awesome package!

h65wang commented 2 years ago

This has been fixed in v0.3.4.

akshg05 commented 4 months ago

Hello The initial zoom level doesn't work when I update the state of a widget which updates the candles for the same chart component. For example, a monthly and yearly view. If I move from yearly view to monthly view where candles in monthly few are much less then yearly view it throws error. However switching from monthly view to yearly works fine.