Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gives wrong touchedSectionIndex when value is 0 in PieChart #697

Closed
2shrestha22 opened this issue Jun 16, 2021 · 3 comments
Closed

Gives wrong touchedSectionIndex when value is 0 in PieChart #697

2shrestha22 opened this issue Jun 16, 2021 · 3 comments
Labels
bug Something isn't working Pie Chart

Comments

@2shrestha22
Copy link

Describe the bug
If some value is 0 in PieChartSectionData then pieTouchResponse.touchedSection!.touchedSectionIndex gives wrong index.

To Reproduce
I used official example code and some of SectionData value is 0 in that condition it gives wrong index.

Screenshots
Here is a gif to show the touch response when some value 0 in pie chart.
Peek 2021-06-16 20-17

Versions

Flutter 2.2.0 • channel stable • https://github.com/flutter/flutter.git
Framework • revision b22742018b (5 weeks ago) • 2021-05-14 19:12:57 -0700
Engine • revision a9d88a4d18
Tools • Dart 2.13.0

fl_chart: ^0.36.1

@imaNNeo imaNNeo added bug Something isn't working Pie Chart labels Jun 17, 2021
@imaNNeo imaNNeo mentioned this issue Jun 17, 2021
@imaNNeo
Copy link
Owner

imaNNeo commented Jun 17, 2021

Hi. Thanks for reporting!

Fixed in 0.36.2

@imaNNeo imaNNeo closed this as completed Jun 17, 2021
@2shrestha22
Copy link
Author

2shrestha22 commented Dec 22, 2021

Sorry to reporting again. Even though now it provides correct index but today I found that some section are not clickable. So for workaround manually filtering values with 0 is needed.

List<PieChartSectionData>? sections() {
    return widget.chart.pieChartData
        // // if value is 0, then section will not build
        // // and gives wrong index on touch
        // // https://github.com/imaNNeoFighT/fl_chart/issues/697
        .where((element) => element.value != 0)
        .toList()
        .asMap()
        .entries
        .map(
      (entry) {
        final isTouched = entry.key == touchedIndex;
        final fontSize = isTouched ? 18.0 : 16.0;
        final radius = isTouched ? 135.0 : 130.0;

        return PieChartSectionData(
          color: AppColor.dashChartColors[entry.key],
          value: entry.value.value.toDouble(),
          title: '',
          radius: radius,
          badgeWidget: (isTouched && widget.tooltipBuilder != null)
              ? Container(
                  padding: const EdgeInsets.all(cTooltipPadding),
                  decoration: BoxDecoration(
                    color: cTooltipBgColor,
                    borderRadius: BorderRadius.circular(cTooltipRadius),
                    // boxShadow: [boxShadow],
                  ),
                  child: Builder(
                    builder: (context) {
                      final tooltipData =
                          widget.tooltipBuilder!.call(entry.value);
                      return RichText(
                        text: TextSpan(
                          text: tooltipData.top,
                          style: AppTextStyle.s16w400Grey2,
                          children: [
                            if (tooltipData.midA != null)
                              TextSpan(
                                text: "\n${tooltipData.midA}",
                                style: AppTextStyle.heading2,
                              ),
                            if (tooltipData.midB != null)
                              TextSpan(
                                text: " ${tooltipData.midB}",
                                style: AppTextStyle.button.copyWith(
                                  color: AppColor.primary1,
                                  height: 4,
                                ),
                              ),
                            if (tooltipData.bottom != null)
                              TextSpan(
                                text: "\n${tooltipData.bottom}",
                                style: AppTextStyle.legend.copyWith(
                                  color: AppColor.primary,
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                          ],
                        ),
                      );
                    },
                  ),
                )
              : null,
          borderSide:
              isTouched ? const BorderSide(color: AppColor.primary) : null,
          badgePositionPercentageOffset: 1,
          titleStyle: TextStyle(
            fontSize: fontSize,
            fontWeight: FontWeight.bold,
            color: const Color(0xffffffff),
          ),
        );
      },
    ).toList();
  }

@imaNNeo
Copy link
Owner

imaNNeo commented Jan 28, 2022

@2shrestha22 Please provide me a reproducible code (a main.dart file).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Pie Chart
Projects
None yet
Development

No branches or pull requests

2 participants