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 Sep 23, 2024
1 parent 15c66cf commit 26aa0df
Show file tree
Hide file tree
Showing 3 changed files with 26 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
11 changes: 11 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 Expand Up @@ -453,6 +463,7 @@ class PieChartPainter extends BaseChartPainter<PieChartData> {
var tempAngle = 0.0;
for (var i = 0; i < data.sections.length; i++) {
final section = data.sections[i];

var sectionAngle = sectionsAngle[i];

tempAngle %= 360;
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 @@ -144,7 +144,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 26aa0df

Please sign in to comment.