Skip to content

Commit

Permalink
Fix extra lines padding and background. Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
k0psutin committed Aug 27, 2024
1 parent 27d8667 commit 0c505be
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 40 deletions.
4 changes: 2 additions & 2 deletions lib/src/chart/base/axis_chart/axis_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,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,
Expand Down Expand Up @@ -1213,7 +1213,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(
Expand Down
39 changes: 20 additions & 19 deletions lib/src/chart/base/axis_chart/axis_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -483,31 +483,32 @@ abstract class AxisChartPainter<D extends AxisChartData>
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
Expand Down
31 changes: 12 additions & 19 deletions test/chart/line_chart/line_chart_painter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 0c505be

Please sign in to comment.