Skip to content

Commit dc02c95

Browse files
aimawariimaNNeo
authored andcommitted
Fix section with zero problems in PieChart
1 parent 1794397 commit dc02c95

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

lib/src/chart/pie_chart/pie_chart_data.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ class PieChartData extends BaseChartData with EquatableMixin {
3131
PieTouchData? pieTouchData,
3232
FlBorderData? borderData,
3333
bool? titleSunbeamLayout,
34-
}) : sections = sections?.where((element) => element.value != 0).toList() ??
35-
const [],
34+
}) : sections = sections ?? const [],
3635
centerSpaceRadius = centerSpaceRadius ?? double.infinity,
3736
centerSpaceColor = centerSpaceColor ?? Colors.transparent,
3837
sectionsSpace = sectionsSpace ?? 2,

lib/src/chart/pie_chart/pie_chart_painter.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class PieChartPainter extends BaseChartPainter<PieChartData> {
6060
List<PieChartSectionData> sections,
6161
double sumValue,
6262
) {
63+
if (sumValue == 0) {
64+
return List<double>.filled(sections.length, 0);
65+
}
66+
6367
return sections.map((section) {
6468
return 360 * (section.value / sumValue);
6569
}).toList();
@@ -100,6 +104,9 @@ class PieChartPainter extends BaseChartPainter<PieChartData> {
100104

101105
for (var i = 0; i < data.sections.length; i++) {
102106
final section = data.sections[i];
107+
if (section.value == 0) {
108+
continue;
109+
}
103110
final sectionDegree = sectionsAngle[i];
104111

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

359366
for (var i = 0; i < data.sections.length; i++) {
360367
final section = data.sections[i];
368+
if (section.value == 0) {
369+
continue;
370+
}
361371
final startAngle = tempAngle;
362372
final sweepAngle = 360 * (section.value / data.sumValue);
363373
final sectionCenterAngle = startAngle + (sweepAngle / 2);

lib/src/chart/pie_chart/pie_chart_renderer.dart

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,20 @@ class RenderPieChart extends RenderBaseChart<PieTouchResponse>
143143
paintHolder,
144144
);
145145
canvas.restore();
146-
defaultPaint(context, offset);
146+
badgeWidgetPaint(context, offset);
147+
}
148+
149+
void badgeWidgetPaint(PaintingContext context, Offset offset) {
150+
RenderObject? child = firstChild;
151+
var counter = 0;
152+
while (child != null) {
153+
final childParentData = child.parentData! as MultiChildLayoutParentData;
154+
if (data.sections[counter].value > 0) {
155+
context.paintChild(child, childParentData.offset + offset);
156+
}
157+
child = childParentData.nextSibling;
158+
counter++;
159+
}
147160
}
148161

149162
@override

0 commit comments

Comments
 (0)