Skip to content

Commit e2f9f9f

Browse files
Samir Hosny MohamedimaNNeo
authored andcommitted
use Path.contains to calculate if the touched position is inside the section
1 parent 773eb0f commit e2f9f9f

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

lib/src/chart/pie_chart/pie_chart_painter.dart

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ class PieChartPainter extends BaseChartPainter<PieChartData> {
433433
) {
434434
final data = holder.data;
435435
final sectionsAngle = calculateSectionsAngle(data.sections, data.sumValue);
436+
final centerRadius = calculateCenterRadius(viewSize, holder);
436437

437438
final center = Offset(viewSize.width / 2, viewSize.height / 2);
438439

@@ -448,33 +449,21 @@ class PieChartPainter extends BaseChartPainter<PieChartData> {
448449
PieChartSectionData? foundSectionData;
449450
var foundSectionDataPosition = -1;
450451

451-
/// Find the nearest section base on the touch spot
452-
final relativeTouchAngle = (touchAngle - data.startDegreeOffset) % 360;
453-
var tempAngle = 0.0;
452+
var tempAngle = data.startDegreeOffset;
454453
for (var i = 0; i < data.sections.length; i++) {
455454
final section = data.sections[i];
456-
var sectionAngle = sectionsAngle[i];
457-
458-
tempAngle %= 360;
459-
if (data.sections.length == 1) {
460-
sectionAngle = 360;
461-
} else {
462-
sectionAngle %= 360;
463-
}
455+
final sectionAngle = sectionsAngle[i];
464456

465-
/// degree criteria
466-
final space = data.sectionsSpace / 2;
467-
final fromDegree = tempAngle + space;
468-
final toDegree = sectionAngle + tempAngle - space;
469-
final isInDegree =
470-
relativeTouchAngle >= fromDegree && relativeTouchAngle <= toDegree;
471-
472-
/// radius criteria
473-
final centerRadius = calculateCenterRadius(viewSize, holder);
474-
final sectionRadius = centerRadius + section.radius;
475-
final isInRadius = touchR > centerRadius && touchR <= sectionRadius;
457+
final sectionPath = generateSectionPath(
458+
section,
459+
data.sectionsSpace,
460+
tempAngle,
461+
sectionAngle,
462+
center,
463+
centerRadius,
464+
);
476465

477-
if (isInDegree && isInRadius) {
466+
if (sectionPath.contains(localPosition)) {
478467
foundSectionData = section;
479468
foundSectionDataPosition = i;
480469
break;

0 commit comments

Comments
 (0)