fl_chart icon indicating copy to clipboard operation
fl_chart copied to clipboard

Tooltip on Barchart: index out of range

Open SuricateTrading opened this issue 3 years ago • 4 comments

[Short description] I get an index out of range when switching between different BarCharts with different length of BarChartGroupData when tooltip is always showing.

I have lots of BarCharts with multiple BarChartGroupData each. I switch between different BarCharts with a slider. The number of BarChartGroupData for each Barchart differs! The tooltip is always showing by setting "showingTooltipIndicators: [0]".

When I switch between a Barchart with 10 BarChartGroupData to a Barchart with 11 BarChartGroupData everything works fine! However, when I switch from 11 to 10, the exception occurs.

Important: I am only able to reproduce this bug when the tooltip is always showing with "showingTooltipIndicators: [0]".

I use most recent version of flutter and fl_chart

EDIT: here is code to reproduce:

class MyWidget2 extends StatelessWidget {
  final int max;
  const MyWidget2(this.max, {Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 100,
      child: BarChart(
        BarChartData(
          barGroups: [
            for(int i=0; i<max; i++)
              BarChartGroupData(
                x: i,
                showingTooltipIndicators: [0],
                barRods: [
                  BarChartRodData(
                    y: 1,
                  ),
                ],
              ),
          ],
        ),
      ),
    );
  }
}

If you switch between different max values, for example MyWidget2(11) to MyWidget2(10), an Index out of range exception occurs.

SuricateTrading avatar Dec 06 '21 14:12 SuricateTrading

The problem is caused by the animation when changing. Ideally, the tooltips should be deactivated in the package during animation. Alternatively, the chart can be used in a stateful widget and the permanent tooltips can be activated via a PostFrameCallback.

The simplest workaround is to disable the animation.

BarCart(
    ...
    swapAnimationDuration: Duration.zero,
)

jannikrh avatar Jan 12 '22 11:01 jannikrh

Hi guys. Can you please check it with the latest version?

imaNNeo avatar Feb 14 '22 19:02 imaNNeo

If it still happens, please give me a reproducible code.

imaNNeo avatar Feb 14 '22 19:02 imaNNeo

If it still happens, please give me a reproducible code.

I updated my pubspec but get the same error with the code I have attached in the initial post.

SuricateTrading avatar Feb 15 '22 13:02 SuricateTrading

Specifying unique key on BarChart() fixes this

BarChart(
    key: UniqueKey(),
...
),

sgshivamgarg8 avatar Nov 14 '22 12:11 sgshivamgarg8