From 59e58a87764800d54c9f79bffb6f0d2f91e808b1 Mon Sep 17 00:00:00 2001 From: imaNNeoFighT Date: Sun, 3 Dec 2023 20:24:58 +0100 Subject: [PATCH] Implement lerp method for FlDotPainter --- lib/src/chart/line_chart/line_chart_data.dart | 64 +++++++++++++++++ .../scatter_chart/scatter_chart_data.dart | 2 +- .../scatter_chart_painter_test.dart | 69 +++++++++++++++---- 3 files changed, 121 insertions(+), 14 deletions(-) diff --git a/lib/src/chart/line_chart/line_chart_data.dart b/lib/src/chart/line_chart/line_chart_data.dart index e1ad5ab8c..fb1fb616c 100644 --- a/lib/src/chart/line_chart/line_chart_data.dart +++ b/lib/src/chart/line_chart/line_chart_data.dart @@ -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 @@ -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 @@ -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 @@ -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 get props => [ diff --git a/lib/src/chart/scatter_chart/scatter_chart_data.dart b/lib/src/chart/scatter_chart/scatter_chart_data.dart index ee19fd21f..b32f26d37 100644 --- a/lib/src/chart/scatter_chart/scatter_chart_data.dart +++ b/lib/src/chart/scatter_chart/scatter_chart_data.dart @@ -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), ); } diff --git a/test/chart/scatter_chart/scatter_chart_painter_test.dart b/test/chart/scatter_chart/scatter_chart_painter_test.dart index 351f6c03e..3664d910d 100644 --- a/test/chart/scatter_chart/scatter_chart_painter_test.dart +++ b/test/chart/scatter_chart/scatter_chart_painter_test.dart @@ -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); @@ -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);