Skip to content

Commit

Permalink
Implement lerp method for FlDotPainter
Browse files Browse the repository at this point in the history
  • Loading branch information
imaNNeo committed Dec 3, 2023
1 parent 711824c commit 59e58a8
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 14 deletions.
64 changes: 64 additions & 0 deletions lib/src/chart/line_chart/line_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,8 @@ abstract class FlDotPainter with EquatableMixin {

/// Used to show default UIs, for example [defaultScatterTooltipItem]
Color get mainColor;

FlDotPainter lerp(FlDotPainter a, FlDotPainter b, double t);
}

/// This class is an implementation of a [FlDotPainter] that draws
Expand Down Expand Up @@ -876,6 +878,27 @@ class FlDotCirclePainter extends FlDotPainter {
strokeColor,
strokeWidth,
];

FlDotCirclePainter _lerp(
FlDotCirclePainter a,
FlDotCirclePainter b,
double t,
) {
return FlDotCirclePainter(
color: Color.lerp(a.color, b.color, t)!,
radius: lerpDouble(a.radius, b.radius, t),
strokeColor: Color.lerp(a.strokeColor, b.strokeColor, t)!,
strokeWidth: lerpDouble(a.strokeWidth, b.strokeWidth, t)!,
);
}

@override
FlDotPainter lerp(FlDotPainter a, FlDotPainter b, double t) {
if (a is! FlDotCirclePainter || b is! FlDotCirclePainter) {
return b;
}
return _lerp(a, b, t);
}
}

/// This class is an implementation of a [FlDotPainter] that draws
Expand Down Expand Up @@ -948,6 +971,27 @@ class FlDotSquarePainter extends FlDotPainter {
strokeColor,
strokeWidth,
];

FlDotSquarePainter _lerp(
FlDotSquarePainter a,
FlDotSquarePainter b,
double t,
) {
return FlDotSquarePainter(
color: Color.lerp(a.color, b.color, t)!,
size: lerpDouble(a.size, b.size, t)!,
strokeColor: Color.lerp(a.strokeColor, b.strokeColor, t)!,
strokeWidth: lerpDouble(a.strokeWidth, b.strokeWidth, t)!,
);
}

@override
FlDotPainter lerp(FlDotPainter a, FlDotPainter b, double t) {
if (a is! FlDotSquarePainter || b is! FlDotSquarePainter) {
return b;
}
return _lerp(a, b, t);
}
}

/// This class is an implementation of a [FlDotPainter] that draws
Expand Down Expand Up @@ -998,6 +1042,26 @@ class FlDotCrossPainter extends FlDotPainter {
@override
Color get mainColor => color;

FlDotCrossPainter _lerp(
FlDotCrossPainter a,
FlDotCrossPainter b,
double t,
) {
return FlDotCrossPainter(
color: Color.lerp(a.color, b.color, t)!,
size: lerpDouble(a.size, b.size, t)!,
width: lerpDouble(a.width, b.width, t)!,
);
}

@override
FlDotPainter lerp(FlDotPainter a, FlDotPainter b, double t) {
if (a is! FlDotCrossPainter || b is! FlDotCrossPainter) {
return b;
}
return _lerp(a, b, t);
}

/// Used for equality check, see [EquatableMixin].
@override
List<Object?> get props => [
Expand Down
2 changes: 1 addition & 1 deletion lib/src/chart/scatter_chart/scatter_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ class ScatterSpot extends FlSpot with EquatableMixin {
lerpDouble(a.x, b.x, t)!,
lerpDouble(a.y, b.y, t)!,
show: b.show,
dotPainter: t == 0 ? a.dotPainter : b.dotPainter,
dotPainter: a.dotPainter.lerp(a.dotPainter, b.dotPainter, t),
);
}

Expand Down
69 changes: 56 additions & 13 deletions test/chart/scatter_chart/scatter_chart_painter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,27 @@ void main() {
holder,
);

verify(mockCanvasWrapper.drawDot(dotPainter1, spot1, const Offset(10, 90))).called(1);
verify(mockCanvasWrapper.drawDot(dotPainter3, spot3, const Offset(80, 80))).called(1);
verify(mockCanvasWrapper.drawDot(dotPainter4, spot4, const Offset(70, 50))).called(1);
verify(
mockCanvasWrapper.drawDot(
dotPainter1,
spot1,
const Offset(10, 90),
),
).called(1);
verify(
mockCanvasWrapper.drawDot(
dotPainter3,
spot3,
const Offset(80, 80),
),
).called(1);
verify(
mockCanvasWrapper.drawDot(
dotPainter4,
spot4,
const Offset(70, 50),
),
).called(1);

verifyNever(mockCanvasWrapper.drawText(any, any));
verify(mockCanvasWrapper.clipRect(any)).called(1);
Expand Down Expand Up @@ -210,16 +228,41 @@ void main() {
holder,
);

verify(mockCanvasWrapper.drawDot(any, data.scatterSpots[0], const Offset(10, 90)))
.called(1);
verify(mockCanvasWrapper.drawDot(any, data.scatterSpots[1], const Offset(20, 80)))
.called(1);
verify(mockCanvasWrapper.drawDot(any, data.scatterSpots[3], const Offset(80, 20)))
.called(1);
verify(mockCanvasWrapper.drawDot(any, data.scatterSpots[4], const Offset(70, 50)))
.called(1);
verify(mockCanvasWrapper.drawDot(any, data.scatterSpots[5], const Offset(40, 40)))
.called(1);
verify(
mockCanvasWrapper.drawDot(
any,
data.scatterSpots[0],
const Offset(10, 90),
),
).called(1);
verify(
mockCanvasWrapper.drawDot(
any,
data.scatterSpots[1],
const Offset(20, 80),
),
).called(1);
verify(
mockCanvasWrapper.drawDot(
any,
data.scatterSpots[3],
const Offset(80, 20),
),
).called(1);
verify(
mockCanvasWrapper.drawDot(
any,
data.scatterSpots[4],
const Offset(70, 50),
),
).called(1);
verify(
mockCanvasWrapper.drawDot(
any,
data.scatterSpots[5],
const Offset(40, 40),
),
).called(1);

verify(mockCanvasWrapper.drawText(any, any)).called(4);

Expand Down

0 comments on commit 59e58a8

Please sign in to comment.