From 7c7ebe8dbb8013f851f89a60928a07ac565b67a3 Mon Sep 17 00:00:00 2001 From: imaNNeoFighT Date: Tue, 23 Mar 2021 21:09:45 +0430 Subject: [PATCH] Fixed PieChart draw problem, #582. --- CHANGELOG.md | 2 ++ lib/src/chart/pie_chart/pie_chart_data.dart | 5 ++++- lib/src/chart/pie_chart/pie_chart_painter.dart | 14 ++++++++++---- lib/src/chart/pie_chart/pie_chart_renderer.dart | 6 +++++- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b39da2820..57db9f8d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ ## newVersion * [IMPROVEMENT] Added `children` property in the [LineTooltipItem](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/line_chart.md#linetooltipitem), [BarTooltipItem](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/bar_chart.md#bartooltipitem) and [ScatterTooltipItem](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/scatter_chart.md#scattertooltipitem) which accepts a list of [TextSpan](https://api.flutter.dev/flutter/painting/TextSpan-class.html). It allows you to have more customized texts inside the tooltip. See [BarChartSample1](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/bar_chart.md#sample-1-source-code) and [ScatterSample2](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/scatter_chart.md#sample-2-source-code). * [IMPROVEMENT] Added `getTouchLineStart` and `getTouchLineEnd` in [LineTouchData](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/line_chart.md#linetouchdata-read-about-touch-handling) to give more customizability over showing the touch lines. see [SampleLineChart9](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/line_chart.md#sample-8-source-code). +* [BUGFIX] Fixed some bugs on drawing PieChart (for example when we have only one section), #582, +* [BREAKING] You cannot set `0` value on [PieChartSectionData](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/pie_chart.md#piechartsectiondata).value anymore, instead remove it from list. * [BREAKING] Removed `fullHeightTouchLine` property from [LineTouchData](https://github.com/imaNNeoFighT/fl_chart/blob/master/repo_files/documentations/line_chart.md#linetouchdata-read-about-touch-handling). Now you can have a full line with following snippet: ```dart LineTouchData( diff --git a/lib/src/chart/pie_chart/pie_chart_data.dart b/lib/src/chart/pie_chart/pie_chart_data.dart index 59f1057ac..5efbe570c 100644 --- a/lib/src/chart/pie_chart/pie_chart_data.dart +++ b/lib/src/chart/pie_chart/pie_chart_data.dart @@ -54,7 +54,10 @@ class PieChartData extends BaseChartData with EquatableMixin { double? startDegreeOffset, PieTouchData? pieTouchData, FlBorderData? borderData, - }) : assert(!_sectionsContainsZero(sections), "section's value can't be zero", ), + }) : assert( + !_sectionsContainsZero(sections), + "section's value can't be zero", + ), sections = sections ?? const [], centerSpaceRadius = centerSpaceRadius ?? double.infinity, centerSpaceColor = centerSpaceColor ?? Colors.transparent, diff --git a/lib/src/chart/pie_chart/pie_chart_painter.dart b/lib/src/chart/pie_chart/pie_chart_painter.dart index 35b63f8eb..ec93db30c 100644 --- a/lib/src/chart/pie_chart/pie_chart_painter.dart +++ b/lib/src/chart/pie_chart/pie_chart_painter.dart @@ -73,7 +73,7 @@ class PieChartPainter extends BaseChartPainter { PaintHolder holder, ) { final data = holder.data; - final shouldDrawSeparators = data.sectionsSpace != 0 && data.sections.length != 1; + final shouldDrawSeparators = data.sectionsSpace != 0 && data.sections.length > 1; final viewSize = canvasWrapper.size; @@ -99,8 +99,13 @@ class PieChartPainter extends BaseChartPainter { radius: centerRadius, ); - _sectionPaint.color = section.color; - _sectionPaint.strokeWidth = section.radius; + if (sectionDegree == 360) { + _sectionPaint.color = section.color; + _sectionPaint.strokeWidth = section.radius; + _sectionPaint.style = PaintingStyle.stroke; + canvasWrapper.drawCircle(center, centerRadius + section.radius / 2, _sectionPaint); + return; + } final startRadians = radians(tempAngle); final sweepRadians = radians(sectionDegree); @@ -112,13 +117,14 @@ class PieChartPainter extends BaseChartPainter { final startLine = Line(startLineFrom, startLineTo); final endLineDirection = Offset(math.cos(endRadians), math.sin(endRadians)); - final endLineFrom = center + endLineDirection * data.centerSpaceRadius; + final endLineFrom = center + endLineDirection * centerRadius; final endLineTo = endLineFrom + endLineDirection * section.radius; final endLine = Line(endLineFrom, endLineTo); final sectionPath = _generateSectionPath( startLine, endLine, startRadians, endRadians, sectionRadiusRect, centerRadiusRect); + _sectionPaint.color = section.color; _sectionPaint.style = PaintingStyle.fill; canvasWrapper.drawPath(sectionPath, _sectionPaint); tempAngle += sectionDegree; diff --git a/lib/src/chart/pie_chart/pie_chart_renderer.dart b/lib/src/chart/pie_chart/pie_chart_renderer.dart index a4975f5c5..9955af671 100644 --- a/lib/src/chart/pie_chart/pie_chart_renderer.dart +++ b/lib/src/chart/pie_chart/pie_chart_renderer.dart @@ -110,11 +110,15 @@ class RenderPieChart extends RenderBox var counter = 0; var badgeOffsets = _painter.getBadgeOffsets(size, paintHolder); while (child != null) { + if (counter >= badgeOffsets.length) { + break; + } child.layout(childConstraints, parentUsesSize: true); final childParentData = child.parentData! as MultiChildLayoutParentData; final sizeOffset = Offset(child.size.width / 2, child.size.height / 2); - childParentData.offset = badgeOffsets[counter++]! - sizeOffset; + childParentData.offset = badgeOffsets[counter]! - sizeOffset; child = childParentData.nextSibling; + counter++; } }