Skip to content

Commit d2f43fb

Browse files
committed
[FIX] feedback
Signed-off-by: Jonas Klock <jonas.klock@exxeta.com>
1 parent cb3a03d commit d2f43fb

6 files changed

Lines changed: 54 additions & 53 deletions

File tree

example/lib/presentation/samples/pie/pie_chart_sample5.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class _PieChartSample5State extends State<PieChartSample5> {
4848
return List.generate(4, (i) {
4949
final isTouched = i == touchedIndex;
5050
final radius = 56.0;
51-
final sectionOffset = isTouched ? 12.0 : 0.0;
51+
final radialOffset = isTouched ? 12.0 : 0.0;
5252
final fontSize = isTouched ? 18.0 : 14.0;
5353
const shadows = [Shadow(color: Colors.black, blurRadius: 2)];
5454

@@ -58,7 +58,7 @@ class _PieChartSample5State extends State<PieChartSample5> {
5858
value: 40,
5959
title: '40%',
6060
radius: radius,
61-
sectionOffset: sectionOffset,
61+
radialOffset: radialOffset,
6262
titleStyle: TextStyle(
6363
fontSize: fontSize,
6464
fontWeight: FontWeight.bold,
@@ -78,7 +78,7 @@ class _PieChartSample5State extends State<PieChartSample5> {
7878
value: 30,
7979
title: '30%',
8080
radius: radius,
81-
sectionOffset: sectionOffset,
81+
radialOffset: radialOffset,
8282
titleStyle: TextStyle(
8383
fontSize: fontSize,
8484
fontWeight: FontWeight.bold,
@@ -91,7 +91,7 @@ class _PieChartSample5State extends State<PieChartSample5> {
9191
value: 20,
9292
title: '20%',
9393
radius: radius,
94-
sectionOffset: sectionOffset,
94+
radialOffset: radialOffset,
9595
titleStyle: TextStyle(
9696
fontSize: fontSize,
9797
fontWeight: FontWeight.bold,
@@ -111,7 +111,7 @@ class _PieChartSample5State extends State<PieChartSample5> {
111111
value: 10,
112112
title: '10%',
113113
radius: radius,
114-
sectionOffset: sectionOffset,
114+
radialOffset: radialOffset,
115115
titleStyle: TextStyle(
116116
fontSize: fontSize,
117117
fontWeight: FontWeight.bold,

lib/src/chart/pie_chart/pie_chart_data.dart

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class PieChartSectionData with EquatableMixin {
157157
Color? color,
158158
this.gradient,
159159
double? radius,
160-
double? sectionOffset,
160+
double? radialOffset,
161161
bool? showTitle,
162162
this.titleStyle,
163163
String? title,
@@ -170,7 +170,7 @@ class PieChartSectionData with EquatableMixin {
170170
}) : value = value ?? 10,
171171
color = color ?? Colors.cyan,
172172
radius = (radius ?? 40).clamp(0, double.infinity).toDouble(),
173-
sectionOffset = sectionOffset ?? 0,
173+
radialOffset = radialOffset ?? 0,
174174
showTitle = showTitle ?? true,
175175
title = title ?? (value == null ? '' : value.toString()),
176176
borderSide = borderSide ?? const BorderSide(width: 0),
@@ -199,7 +199,10 @@ class PieChartSectionData with EquatableMixin {
199199

200200
/// Additional radial translation applied to the whole section (in logical pixels).
201201
/// Positive values move the section outward along its center angle.
202-
final double sectionOffset;
202+
///
203+
/// Note: This parameter is ignored when there is only a single section
204+
/// occupying 360 degrees, as there is no meaningful direction to offset.
205+
final double radialOffset;
203206

204207
/// Defines show or hide the title of section.
205208
final bool showTitle;
@@ -250,7 +253,7 @@ class PieChartSectionData with EquatableMixin {
250253
Color? color,
251254
Gradient? gradient,
252255
double? radius,
253-
double? sectionOffset,
256+
double? radialOffset,
254257
bool? showTitle,
255258
TextStyle? titleStyle,
256259
String? title,
@@ -266,7 +269,7 @@ class PieChartSectionData with EquatableMixin {
266269
color: color ?? this.color,
267270
gradient: gradient ?? this.gradient,
268271
radius: radius ?? this.radius,
269-
sectionOffset: sectionOffset ?? this.sectionOffset,
272+
radialOffset: radialOffset ?? this.radialOffset,
270273
showTitle: showTitle ?? this.showTitle,
271274
titleStyle: titleStyle ?? this.titleStyle,
272275
title: title ?? this.title,
@@ -291,7 +294,7 @@ class PieChartSectionData with EquatableMixin {
291294
color: Color.lerp(a.color, b.color, t),
292295
gradient: Gradient.lerp(a.gradient, b.gradient, t),
293296
radius: lerpDouble(a.radius, b.radius, t),
294-
sectionOffset: lerpDouble(a.sectionOffset, b.sectionOffset, t),
297+
radialOffset: lerpDouble(a.radialOffset, b.radialOffset, t),
295298
showTitle: b.showTitle,
296299
titleStyle: TextStyle.lerp(a.titleStyle, b.titleStyle, t),
297300
title: b.title,
@@ -322,7 +325,7 @@ class PieChartSectionData with EquatableMixin {
322325
color,
323326
gradient,
324327
radius,
325-
sectionOffset,
328+
radialOffset,
326329
showTitle,
327330
titleStyle,
328331
title,

lib/src/chart/pie_chart/pie_chart_painter.dart

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,10 @@ class PieChartPainter extends BaseChartPainter<PieChartData> {
109109
}
110110
final sectionDegree = sectionsAngle[i];
111111

112-
// Compute offset for "exploded" sections
113112
final sectionCenterAngle = tempAngle + (sectionDegree / 2);
114-
final sectionOffset = section.sectionOffset;
115-
final offsetDx =
116-
math.cos(Utils().radians(sectionCenterAngle)) * sectionOffset;
117-
final offsetDy =
118-
math.sin(Utils().radians(sectionCenterAngle)) * sectionOffset;
119-
final sectionCenter = center.translate(offsetDx, offsetDy);
113+
final radialOffset =
114+
_computeRadialOffset(section.radialOffset, sectionCenterAngle);
115+
final sectionCenter = center + radialOffset;
120116

121117
if (sectionDegree == 360) {
122118
final fullCirclePath = generateSegmentPath(
@@ -732,14 +728,12 @@ class PieChartPainter extends BaseChartPainter<PieChartData> {
732728
}
733729
}
734730

735-
final sectionOffset = section.sectionOffset;
736-
final offsetDx =
737-
math.cos(Utils().radians(sectionCenterAngle)) * sectionOffset;
738-
final offsetDy =
739-
math.sin(Utils().radians(sectionCenterAngle)) * sectionOffset;
731+
final radialOffset =
732+
_computeRadialOffset(section.radialOffset, sectionCenterAngle);
740733

741734
Offset sectionCenter(double percentageOffset) =>
742-
center.translate(offsetDx, offsetDy) +
735+
center +
736+
radialOffset +
743737
Offset(
744738
math.cos(Utils().radians(sectionCenterAngle)) *
745739
(centerRadius + (section.radius * percentageOffset)),
@@ -836,20 +830,16 @@ class PieChartPainter extends BaseChartPainter<PieChartData> {
836830
break;
837831
}
838832

839-
// Account for outward offset when hit-testing
840833
final sectionCenterAngle = tempAngle + (sectionAngle / 2);
841-
final sectionOffset = section.sectionOffset;
842-
final offsetDx =
843-
math.cos(Utils().radians(sectionCenterAngle)) * sectionOffset;
844-
final offsetDy =
845-
math.sin(Utils().radians(sectionCenterAngle)) * sectionOffset;
834+
final radialOffset =
835+
_computeRadialOffset(section.radialOffset, sectionCenterAngle);
846836

847837
final sectionPath = generateSectionPath(
848838
section,
849839
data.sectionsSpace,
850840
tempAngle,
851841
sectionAngle,
852-
center.translate(offsetDx, offsetDy),
842+
center + radialOffset,
853843
centerRadius,
854844
);
855845

@@ -893,14 +883,12 @@ class PieChartPainter extends BaseChartPainter<PieChartData> {
893883
final sectionCenterAngle = startAngle + (sweepAngle / 2);
894884
final centerRadius = calculateCenterRadius(viewSize, holder);
895885

896-
final sectionOffset = section.sectionOffset;
897-
final offsetDx =
898-
math.cos(Utils().radians(sectionCenterAngle)) * sectionOffset;
899-
final offsetDy =
900-
math.sin(Utils().radians(sectionCenterAngle)) * sectionOffset;
886+
final radialOffset =
887+
_computeRadialOffset(section.radialOffset, sectionCenterAngle);
901888

902889
Offset sectionCenter(double percentageOffset) =>
903-
center.translate(offsetDx, offsetDy) +
890+
center +
891+
radialOffset +
904892
Offset(
905893
math.cos(Utils().radians(sectionCenterAngle)) *
906894
(centerRadius + (section.radius * percentageOffset)),
@@ -918,4 +906,15 @@ class PieChartPainter extends BaseChartPainter<PieChartData> {
918906

919907
return badgeWidgetsOffsets;
920908
}
909+
910+
/// Computes the radial offset translation for a section along its center angle.
911+
Offset _computeRadialOffset(
912+
double radialOffset,
913+
double sectionCenterAngle,
914+
) {
915+
return Offset(
916+
math.cos(Utils().radians(sectionCenterAngle)) * radialOffset,
917+
math.sin(Utils().radians(sectionCenterAngle)) * radialOffset,
918+
);
919+
}
921920
}

repo_files/documentations/pie_chart.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ When you change the chart's state, it animates to the new state internally (usin
3737
|color| colors the section| Colors.red|
3838
|gradient| You can use any [Gradient](https://api.flutter.dev/flutter/dart-ui/Gradient-class.html) here. such as [LinearGradient](https://api.flutter.dev/flutter/painting/LinearGradient-class.html) or [RadialGradient](https://api.flutter.dev/flutter/painting/RadialGradient-class.html) (you have to provide either `color` or `gradient`)|null|
3939
|radius| the width radius of each section|40|
40-
|sectionOffset| radial translation (in logical pixels) applied to the whole section, moving it outward along its center angle. Useful for "exploded" pie charts|0|
40+
|radialOffset| radial translation (in logical pixels) applied to the whole section, moving it outward along its center angle. Useful for "exploded" pie charts. Ignored when there is only a single section (360 degrees)|0|
4141
|showTitle| determines whether to show or hide the titles on each section|true|
4242
|titleStyle| TextStyle of the titles| TextStyle(color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold)|
4343
|title| title of the section| value|

test/chart/pie_chart/pie_chart_data_test.dart

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,43 +278,42 @@ void main() {
278278
group('PieChartSectionData', () {
279279
test('equality', () {
280280
final a =
281-
PieChartSectionData(value: 10, color: Colors.red, sectionOffset: 8);
281+
PieChartSectionData(value: 10, color: Colors.red, radialOffset: 8);
282282
final b =
283-
PieChartSectionData(value: 10, color: Colors.red, sectionOffset: 8);
283+
PieChartSectionData(value: 10, color: Colors.red, radialOffset: 8);
284284
expect(a == b, true);
285285

286286
expect(
287-
a ==
288-
PieChartSectionData(value: 10, color: Colors.red, sectionOffset: 0),
287+
a == PieChartSectionData(value: 10, color: Colors.red, radialOffset: 0),
289288
false,
290289
);
291290
});
292291

293292
test('copyWith', () {
294293
final original = PieChartSectionData(value: 10, color: Colors.red);
295-
expect(original.sectionOffset, 0);
294+
expect(original.radialOffset, 0);
296295

297-
final withOffset = original.copyWith(sectionOffset: 12);
298-
expect(withOffset.sectionOffset, 12);
296+
final withOffset = original.copyWith(radialOffset: 12);
297+
expect(withOffset.radialOffset, 12);
299298
expect(withOffset.value, 10);
300299

301300
expect(original.copyWith() == original, true);
302301
});
303302

304303
test('lerp', () {
305304
final a =
306-
PieChartSectionData(value: 10, color: Colors.red, sectionOffset: 0);
305+
PieChartSectionData(value: 10, color: Colors.red, radialOffset: 0);
307306
final b =
308-
PieChartSectionData(value: 10, color: Colors.red, sectionOffset: 20);
307+
PieChartSectionData(value: 10, color: Colors.red, radialOffset: 20);
309308

310309
final atZero = PieChartSectionData.lerp(a, b, 0);
311-
expect(atZero.sectionOffset, 0);
310+
expect(atZero.radialOffset, 0);
312311

313312
final atOne = PieChartSectionData.lerp(a, b, 1);
314-
expect(atOne.sectionOffset, 20);
313+
expect(atOne.radialOffset, 20);
315314

316315
final mid = PieChartSectionData.lerp(a, b, 0.5);
317-
expect(mid.sectionOffset, 10);
316+
expect(mid.radialOffset, 10);
318317
});
319318
});
320319
}

test/chart/pie_chart/pie_chart_painter_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,15 @@ void main() {
370370
expect(results[3]['paint_style'] as PaintingStyle, PaintingStyle.fill);
371371
});
372372

373-
test('test 3 - sectionOffset translates section along center angle', () {
373+
test('test 3 - radialOffset translates section along center angle', () {
374374
const viewSize = Size(200, 200);
375375
const centerRadius = 10.0;
376376
const offset = 20.0;
377377

378378
final section0 = PieChartSectionData(
379379
color: MockData.color1,
380380
value: 1,
381-
sectionOffset: offset,
381+
radialOffset: offset,
382382
);
383383
final section1 = PieChartSectionData(
384384
color: MockData.color2,

0 commit comments

Comments
 (0)