Skip to content

Commit

Permalink
Fixed PieChart draw problem, imaNNeo#582.
Browse files Browse the repository at this point in the history
  • Loading branch information
imaNNeo committed Mar 23, 2021
1 parent 4d67c99 commit 7c7ebe8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
5 changes: 4 additions & 1 deletion lib/src/chart/pie_chart/pie_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 10 additions & 4 deletions lib/src/chart/pie_chart/pie_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class PieChartPainter extends BaseChartPainter<PieChartData> {
PaintHolder<PieChartData> 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;

Expand All @@ -99,8 +99,13 @@ class PieChartPainter extends BaseChartPainter<PieChartData> {
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);
Expand All @@ -112,13 +117,14 @@ class PieChartPainter extends BaseChartPainter<PieChartData> {
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;
Expand Down
6 changes: 5 additions & 1 deletion lib/src/chart/pie_chart/pie_chart_renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
}

Expand Down

0 comments on commit 7c7ebe8

Please sign in to comment.