Skip to content

Commit

Permalink
Fix section with zero problems in PieChart
Browse files Browse the repository at this point in the history
  • Loading branch information
aimawari committed Oct 21, 2024
1 parent 2f3e6ee commit e707e2d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
3 changes: 1 addition & 2 deletions lib/src/chart/pie_chart/pie_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ class PieChartData extends BaseChartData with EquatableMixin {
PieTouchData? pieTouchData,
FlBorderData? borderData,
bool? titleSunbeamLayout,
}) : sections = sections?.where((element) => element.value != 0).toList() ??
const [],
}) : sections = sections ?? const [],
centerSpaceRadius = centerSpaceRadius ?? double.infinity,
centerSpaceColor = centerSpaceColor ?? Colors.transparent,
sectionsSpace = sectionsSpace ?? 2,
Expand Down
10 changes: 10 additions & 0 deletions lib/src/chart/pie_chart/pie_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class PieChartPainter extends BaseChartPainter<PieChartData> {
List<PieChartSectionData> sections,
double sumValue,
) {
if (sumValue == 0) {
return List<double>.filled(sections.length, 0);
}

return sections.map((section) {
return 360 * (section.value / sumValue);
}).toList();
Expand Down Expand Up @@ -100,6 +104,9 @@ class PieChartPainter extends BaseChartPainter<PieChartData> {

for (var i = 0; i < data.sections.length; i++) {
final section = data.sections[i];
if (section.value == 0) {
continue;
}
final sectionDegree = sectionsAngle[i];

if (sectionDegree == 360) {
Expand Down Expand Up @@ -358,6 +365,9 @@ class PieChartPainter extends BaseChartPainter<PieChartData> {

for (var i = 0; i < data.sections.length; i++) {
final section = data.sections[i];
if (section.value == 0) {
continue;
}
final startAngle = tempAngle;
final sweepAngle = 360 * (section.value / data.sumValue);
final sectionCenterAngle = startAngle + (sweepAngle / 2);
Expand Down
15 changes: 14 additions & 1 deletion lib/src/chart/pie_chart/pie_chart_renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,20 @@ class RenderPieChart extends RenderBaseChart<PieTouchResponse>
paintHolder,
);
canvas.restore();
defaultPaint(context, offset);
badgeWidgetPaint(context, offset);
}

void badgeWidgetPaint(PaintingContext context, Offset offset) {
RenderObject? child = firstChild;
var counter = 0;
while (child != null) {
final childParentData = child.parentData! as MultiChildLayoutParentData;
if (data.sections[counter].value > 0) {
context.paintChild(child, childParentData.offset + offset);
}
child = childParentData.nextSibling;
counter++;
}
}

@override
Expand Down

0 comments on commit e707e2d

Please sign in to comment.