From e1d4f6595aba08014e3f0532fdd97d0a9c03cd23 Mon Sep 17 00:00:00 2001 From: Jani Koponen Date: Sun, 21 Jul 2024 18:54:31 +0300 Subject: [PATCH] Fix extra lines padding and background. Fix tests --- .../base/axis_chart/axis_chart_data.dart | 4 +- .../base/axis_chart/axis_chart_painter.dart | 39 ++++++++++--------- .../line_chart/line_chart_painter_test.dart | 31 ++++++--------- 3 files changed, 34 insertions(+), 40 deletions(-) diff --git a/lib/src/chart/base/axis_chart/axis_chart_data.dart b/lib/src/chart/base/axis_chart/axis_chart_data.dart index 88b6fee0d..37aedf906 100644 --- a/lib/src/chart/base/axis_chart/axis_chart_data.dart +++ b/lib/src/chart/base/axis_chart/axis_chart_data.dart @@ -1136,7 +1136,7 @@ class HorizontalLineLabel extends FlLineLabel with EquatableMixin { /// [show] determines showing label or not. /// [direction] determines if the direction of the text should be horizontal or vertical. HorizontalLineLabel({ - super.padding = const EdgeInsets.all(6), + super.padding = const EdgeInsets.all(2), super.horizontalOffset = 0.0, super.verticalOffset = 0.0, super.style, @@ -1195,7 +1195,7 @@ class VerticalLineLabel extends FlLineLabel with EquatableMixin { /// [show] determines showing label or not. /// [direction] determines if the direction of the text should be horizontal or vertical. VerticalLineLabel({ - super.padding = const EdgeInsets.all(6), + super.padding = const EdgeInsets.all(2), super.horizontalOffset = 0.0, super.verticalOffset = 0.0, super.style = const TextStyle( diff --git a/lib/src/chart/base/axis_chart/axis_chart_painter.dart b/lib/src/chart/base/axis_chart/axis_chart_painter.dart index 56b2601a3..eca497c15 100644 --- a/lib/src/chart/base/axis_chart/axis_chart_painter.dart +++ b/lib/src/chart/base/axis_chart/axis_chart_painter.dart @@ -483,31 +483,32 @@ abstract class AxisChartPainter currenfOffset.dy - verticalOffset, ); - final backgroundRect = Rect.fromCenter( - center: Offset( - offset.dx, - offset.dy, - ), - width: tp.width, - height: tp.height, + final backgroundRect = Rect.fromLTWH( + offset.dx, + offset.dy, + tp.width, + tp.height, ).applyPadding(padding); - canvasWrapper.drawRect( - backgroundRect, - _labelBackgroundPaint..color = backgroundColor, - ); + canvasWrapper.save(); if (direction == LabelDirection.vertical) { - canvasWrapper.drawVerticalText( - tp, - offset, - ); - } else { - canvasWrapper.drawText( + canvasWrapper + ..translate(currenfOffset.dx, currenfOffset.dy) + ..rotate(Utils().radians(90)) + ..translate(-currenfOffset.dx, -currenfOffset.dy); + } + + canvasWrapper + ..drawRect( + backgroundRect, + _labelBackgroundPaint..color = backgroundColor, + ) + ..drawText( tp, offset, - ); - } + ) + ..restore(); } /// With this function we can convert our [FlSpot] x diff --git a/test/chart/line_chart/line_chart_painter_test.dart b/test/chart/line_chart/line_chart_painter_test.dart index b510bb49d..1b200c5dc 100644 --- a/test/chart/line_chart/line_chart_painter_test.dart +++ b/test/chart/line_chart/line_chart_painter_test.dart @@ -2369,19 +2369,12 @@ void main() { ); final result1 = verify(mockCanvasWrapper.drawText(any, captureAny)) - ..called(2); - final result2 = - verify(mockCanvasWrapper.drawVerticalText(any, captureAny)) - ..called(2); + ..called(4); final offset1 = result1.captured[0] as Offset; final offset2 = result1.captured[1] as Offset; - final offset3 = result2.captured[0] as Offset; - final offset4 = result2.captured[1] as Offset; expect(offset1, const Offset(0, 56)); - expect(offset2, const Offset(30, 86)); - expect(offset3, const Offset(14, -16)); - expect(offset4, const Offset(74, 44)); + expect(offset2, const Offset(14, -16)); }); }); @@ -2673,7 +2666,7 @@ void main() { expect(rect != null, true, reason: 'Expected that Rect is not null'); - const expected = Rect.fromLTRB(-27, 8.5, 27, 34.5); + const expected = Rect.fromLTRB(-2, 19.5, 44, 37.5); expect( HelperMethods.equalsRects( @@ -2745,7 +2738,7 @@ void main() { final rect = results[0]['rect'] as Rect; - const expected = Rect.fromLTRB(-27, 28.5, 27, 54.5); + const expected = Rect.fromLTRB(-2, 39.5, 44, 57.5); expect( HelperMethods.equalsRects( @@ -2817,7 +2810,7 @@ void main() { final rect = results[0]['rect'] as Rect; - const expected = Rect.fromLTRB(-17, 18.5, 37, 44.5); + const expected = Rect.fromLTRB(8, 29.5, 54, 47.5); expect( HelperMethods.equalsRects( @@ -2889,7 +2882,7 @@ void main() { final rect = results[0]['rect'] as Rect; - const expected = Rect.fromLTRB(-37, 18.5, 17, 44.5); + const expected = Rect.fromLTRB(-12, 29.5, 34, 47.5); expect( HelperMethods.equalsRects( rect, @@ -2960,7 +2953,7 @@ void main() { final rect = results[0]['rect'] as Rect; - const expected = Rect.fromLTRB(27.5, 83, 81.5, 109); + const expected = Rect.fromLTRB(52.5, 94, 98.5, 112); expect( HelperMethods.equalsRects( rect, @@ -3031,7 +3024,7 @@ void main() { final rect = results[0]['rect'] as Rect; - const expected = Rect.fromLTRB(27.5, 63, 81.5, 89); + const expected = Rect.fromLTRB(52.5, 74, 98.5, 92); expect( HelperMethods.equalsRects( rect, @@ -3102,7 +3095,7 @@ void main() { final rect = results[0]['rect'] as Rect; - const expected = Rect.fromLTRB(37.5, 73, 91.5, 99); + const expected = Rect.fromLTRB(62.5, 84, 108.5, 102); expect( HelperMethods.equalsRects( rect, @@ -3173,7 +3166,7 @@ void main() { final rect = results[0]['rect'] as Rect; - const expected = Rect.fromLTRB(17.5, 73, 71.5, 99); + const expected = Rect.fromLTRB(42.5, 84, 88.5, 102); expect( HelperMethods.equalsRects( rect, @@ -3244,7 +3237,7 @@ void main() { final rect = results[0]['rect'] as Rect; - const expected = Rect.fromLTRB(17.5, 63, 91.5, 109); + const expected = Rect.fromLTRB(38.5, 70, 112.5, 116); expect( HelperMethods.equalsRects( rect, @@ -3315,7 +3308,7 @@ void main() { final rect = results[0]['rect'] as Rect; - const expected = Rect.fromLTRB(-37, 8.5, 37, 54.5); + const expected = Rect.fromLTRB(-16, 15.5, 58, 61.5); expect( HelperMethods.equalsRects( rect,