From 773eb0f1bf4884a0d915d5706169364bb092ff6d Mon Sep 17 00:00:00 2001 From: Samir Hosny Mohamed Date: Sun, 26 Nov 2023 04:09:52 +0200 Subject: [PATCH 1/5] edit sample 1 to illustrate the issue --- .../presentation/samples/pie/pie_chart_sample1.dart | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/example/lib/presentation/samples/pie/pie_chart_sample1.dart b/example/lib/presentation/samples/pie/pie_chart_sample1.dart index 957735aaa..37634b5da 100644 --- a/example/lib/presentation/samples/pie/pie_chart_sample1.dart +++ b/example/lib/presentation/samples/pie/pie_chart_sample1.dart @@ -89,7 +89,7 @@ class PieChartSample1State extends State { borderData: FlBorderData( show: false, ), - sectionsSpace: 1, + sectionsSpace: 30, centerSpaceRadius: 0, sections: showingSections(), ), @@ -117,7 +117,7 @@ class PieChartSample1State extends State { color: color0, value: 25, title: '', - radius: 80, + radius: 80 * 2, titlePositionPercentageOffset: 0.55, borderSide: isTouched ? const BorderSide( @@ -130,7 +130,7 @@ class PieChartSample1State extends State { color: color1, value: 25, title: '', - radius: 65, + radius: 80 * 2, titlePositionPercentageOffset: 0.55, borderSide: isTouched ? const BorderSide( @@ -143,7 +143,7 @@ class PieChartSample1State extends State { color: color2, value: 25, title: '', - radius: 60, + radius: 80 * 2, titlePositionPercentageOffset: 0.6, borderSide: isTouched ? const BorderSide( @@ -156,7 +156,7 @@ class PieChartSample1State extends State { color: color3, value: 25, title: '', - radius: 70, + radius: 80 * 2, titlePositionPercentageOffset: 0.55, borderSide: isTouched ? const BorderSide( From e2f9f9f749e5b1d3b4e71f56fafaeaa380e784c1 Mon Sep 17 00:00:00 2001 From: Samir Hosny Mohamed Date: Sun, 26 Nov 2023 04:11:35 +0200 Subject: [PATCH 2/5] use Path.contains to calculate if the touched position is inside the section --- .../chart/pie_chart/pie_chart_painter.dart | 35 +++++++------------ 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/lib/src/chart/pie_chart/pie_chart_painter.dart b/lib/src/chart/pie_chart/pie_chart_painter.dart index 9517c72ec..075ed7261 100644 --- a/lib/src/chart/pie_chart/pie_chart_painter.dart +++ b/lib/src/chart/pie_chart/pie_chart_painter.dart @@ -433,6 +433,7 @@ class PieChartPainter extends BaseChartPainter { ) { final data = holder.data; final sectionsAngle = calculateSectionsAngle(data.sections, data.sumValue); + final centerRadius = calculateCenterRadius(viewSize, holder); final center = Offset(viewSize.width / 2, viewSize.height / 2); @@ -448,33 +449,21 @@ class PieChartPainter extends BaseChartPainter { PieChartSectionData? foundSectionData; var foundSectionDataPosition = -1; - /// Find the nearest section base on the touch spot - final relativeTouchAngle = (touchAngle - data.startDegreeOffset) % 360; - var tempAngle = 0.0; + var tempAngle = data.startDegreeOffset; for (var i = 0; i < data.sections.length; i++) { final section = data.sections[i]; - var sectionAngle = sectionsAngle[i]; - - tempAngle %= 360; - if (data.sections.length == 1) { - sectionAngle = 360; - } else { - sectionAngle %= 360; - } + final sectionAngle = sectionsAngle[i]; - /// degree criteria - final space = data.sectionsSpace / 2; - final fromDegree = tempAngle + space; - final toDegree = sectionAngle + tempAngle - space; - final isInDegree = - relativeTouchAngle >= fromDegree && relativeTouchAngle <= toDegree; - - /// radius criteria - final centerRadius = calculateCenterRadius(viewSize, holder); - final sectionRadius = centerRadius + section.radius; - final isInRadius = touchR > centerRadius && touchR <= sectionRadius; + final sectionPath = generateSectionPath( + section, + data.sectionsSpace, + tempAngle, + sectionAngle, + center, + centerRadius, + ); - if (isInDegree && isInRadius) { + if (sectionPath.contains(localPosition)) { foundSectionData = section; foundSectionDataPosition = i; break; From 8de70c16517544fc479e1747ee80f0cc795c97f0 Mon Sep 17 00:00:00 2001 From: Samir Hosny Mohamed Date: Sun, 26 Nov 2023 04:27:40 +0200 Subject: [PATCH 3/5] handle the case of one section has angle 360 --- lib/src/chart/pie_chart/pie_chart_painter.dart | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/src/chart/pie_chart/pie_chart_painter.dart b/lib/src/chart/pie_chart/pie_chart_painter.dart index 075ed7261..697240b5f 100644 --- a/lib/src/chart/pie_chart/pie_chart_painter.dart +++ b/lib/src/chart/pie_chart/pie_chart_painter.dart @@ -454,6 +454,17 @@ class PieChartPainter extends BaseChartPainter { final section = data.sections[i]; final sectionAngle = sectionsAngle[i]; + if (sectionAngle == 360) { + final distance = math.sqrt(math.pow(localPosition.dx - center.dx, 2) + + math.pow(localPosition.dy - center.dy, 2)); + if (distance >= centerRadius && + distance <= section.radius + centerRadius) { + foundSectionData = section; + foundSectionDataPosition = i; + } + break; + } + final sectionPath = generateSectionPath( section, data.sectionsSpace, From 40f0228ed31e0fdd3352ee09f17b3b0c7d4756ef Mon Sep 17 00:00:00 2001 From: Samir Hosny Mohamed Date: Sun, 26 Nov 2023 04:48:45 +0200 Subject: [PATCH 4/5] return the sample 1 to normal --- .../presentation/samples/pie/pie_chart_sample1.dart | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/example/lib/presentation/samples/pie/pie_chart_sample1.dart b/example/lib/presentation/samples/pie/pie_chart_sample1.dart index 37634b5da..957735aaa 100644 --- a/example/lib/presentation/samples/pie/pie_chart_sample1.dart +++ b/example/lib/presentation/samples/pie/pie_chart_sample1.dart @@ -89,7 +89,7 @@ class PieChartSample1State extends State { borderData: FlBorderData( show: false, ), - sectionsSpace: 30, + sectionsSpace: 1, centerSpaceRadius: 0, sections: showingSections(), ), @@ -117,7 +117,7 @@ class PieChartSample1State extends State { color: color0, value: 25, title: '', - radius: 80 * 2, + radius: 80, titlePositionPercentageOffset: 0.55, borderSide: isTouched ? const BorderSide( @@ -130,7 +130,7 @@ class PieChartSample1State extends State { color: color1, value: 25, title: '', - radius: 80 * 2, + radius: 65, titlePositionPercentageOffset: 0.55, borderSide: isTouched ? const BorderSide( @@ -143,7 +143,7 @@ class PieChartSample1State extends State { color: color2, value: 25, title: '', - radius: 80 * 2, + radius: 60, titlePositionPercentageOffset: 0.6, borderSide: isTouched ? const BorderSide( @@ -156,7 +156,7 @@ class PieChartSample1State extends State { color: color3, value: 25, title: '', - radius: 80 * 2, + radius: 70, titlePositionPercentageOffset: 0.55, borderSide: isTouched ? const BorderSide( From d1e7e66e55194a9a6d69d6935a957b72f50bf03b Mon Sep 17 00:00:00 2001 From: Samir Hosny Mohamed Date: Tue, 28 Nov 2023 02:03:44 +0200 Subject: [PATCH 5/5] nit --- lib/src/chart/pie_chart/pie_chart_painter.dart | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/src/chart/pie_chart/pie_chart_painter.dart b/lib/src/chart/pie_chart/pie_chart_painter.dart index 697240b5f..70331cb1d 100644 --- a/lib/src/chart/pie_chart/pie_chart_painter.dart +++ b/lib/src/chart/pie_chart/pie_chart_painter.dart @@ -455,8 +455,10 @@ class PieChartPainter extends BaseChartPainter { final sectionAngle = sectionsAngle[i]; if (sectionAngle == 360) { - final distance = math.sqrt(math.pow(localPosition.dx - center.dx, 2) + - math.pow(localPosition.dy - center.dy, 2)); + final distance = math.sqrt( + math.pow(localPosition.dx - center.dx, 2) + + math.pow(localPosition.dy - center.dy, 2), + ); if (distance >= centerRadius && distance <= section.radius + centerRadius) { foundSectionData = section;