imaNNeo / fl_chart

FL Chart is a highly customizable Flutter chart library that supports Line Chart, Bar Chart, Pie Chart, Scatter Chart, and Radar Chart.
https://flchart.dev
MIT License
6.77k stars 1.74k forks source link

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

Open javaone199 opened 5 months ago

javaone199 commented 5 months ago

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.

tungthanh1497 commented 5 months ago

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]);
     }
   }