fl_chart icon indicating copy to clipboard operation
fl_chart copied to clipboard

PieChart: touched section index is incorrect if some values are zero

Open javaone199 opened this issue 2 years ago • 1 comments

PieChart: touched section index is incorrect if some values are zero. For example,

values: 0, 0, 100, 200, 300.

when touching the 3rd value 100, touched index is 0 that should be 2.

javaone199 avatar Apr 11 '24 17:04 javaone199

I had the same problem and temporarily fixed it with the following code

For example we are using PieChart to display List<Int> values First, create another variable List<Int> valueFilterZero with values greater than 0 only

final List<Int> value;
late final List<Int> valueFilterZero;
PieChartStatistic({super.key, required this.value}) {
     valueFilterZero = value.where((item) => item > 0).toList();
}

Then when handling touchCallback, instead of using index with value, use it with valueFilterZero

touchCallback: (FlTouchEvent event, touchResponse) {
     if (event is FlTapUpEvent && touchResponse != null) {
       int indexSelectedItem =
           touchResponse.touchedSection?.touchedSectionIndex ?? 0;
       // doSmt(value[realIndexSelectedItem]);
       doSmt(valueFilterZero[realIndexSelectedItem]);
     }
   }

tungthanh1497 avatar May 03 '24 07:05 tungthanh1497