Skip to content

Commit

Permalink
Implement simple drawing error functionality (tested in line_chart_sa…
Browse files Browse the repository at this point in the history
…mple2.dart)
  • Loading branch information
imaNNeo committed Jan 8, 2025
1 parent 34630a2 commit dd5e703
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 18 deletions.
21 changes: 14 additions & 7 deletions example/lib/presentation/samples/line/line_chart_sample2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,37 +171,44 @@ class _LineChartSample2State extends State<LineChartSample2> {
FlSpot(
0,
3,
yError: FlErrorRange.symmetric(5),
yError: FlErrorRange.symmetric(1.0),
xError: FlErrorRange.symmetric(1.0),
),
FlSpot(
2.6,
2,
yError: FlErrorRange.symmetric(5),
yError: FlErrorRange.symmetric(1.0),
xError: FlErrorRange.symmetric(1.0),
),
FlSpot(
4.9,
5,
yError: FlErrorRange.symmetric(5),
yError: FlErrorRange.symmetric(1.0),
xError: FlErrorRange.symmetric(1.0),
),
FlSpot(
6.8,
3.1,
yError: FlErrorRange.symmetric(5),
yError: FlErrorRange.symmetric(1.0),
xError: FlErrorRange.symmetric(1.0),
),
FlSpot(
8,
4,
yError: FlErrorRange.symmetric(5),
yError: FlErrorRange.symmetric(1.0),
xError: FlErrorRange.symmetric(1.0),
),
FlSpot(
9.5,
3,
yError: FlErrorRange.symmetric(5),
yError: FlErrorRange.symmetric(1.0),
xError: FlErrorRange.symmetric(1.0),
),
FlSpot(
11,
4,
yError: FlErrorRange.symmetric(5),
yError: FlErrorRange.symmetric(1.0),
xError: FlErrorRange.symmetric(1.0),
),
],
isCurved: true,
Expand Down
81 changes: 72 additions & 9 deletions lib/src/chart/base/axis_chart/axis_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,7 @@ abstract class FlSpotErrorRangePainter with EquatableMixin {
Canvas canvas,
Offset offsetInCanvas,
FlSpot origin,
Rect errorRelativeRect,
);

Size getSize(FlSpot spot);
Expand All @@ -1735,18 +1736,80 @@ class FlSimpleErrorPainter extends FlSpotErrorRangePainter with EquatableMixin {
const FlSimpleErrorPainter();

@override
void draw(Canvas canvas, Offset offsetInCanvas, FlSpot origin) {
print('Drawing error indicator at $offsetInCanvas');
final lineFrom = Offset(offsetInCanvas.dx, offsetInCanvas.dy - 10);
final lineTo = Offset(offsetInCanvas.dx, offsetInCanvas.dy + 10);
void draw(
Canvas canvas,
Offset offsetInCanvas,
FlSpot origin,
Rect errorRelativeRect,
) {
final rect = errorRelativeRect.shift(offsetInCanvas);

final hasVerticalError = errorRelativeRect.width != 0;
if (hasVerticalError) {
_drawDirectErrorLine(canvas, rect.topCenter, rect.bottomCenter);
}

final hasHorizontalError = errorRelativeRect.height != 0;
if (hasHorizontalError) {
_drawDirectErrorLine(canvas, rect.centerLeft, rect.centerRight);
}
}

void _drawDirectErrorLine(Canvas canvas, Offset from, Offset to) {
canvas.drawLine(
lineFrom,
lineTo,
from,
to,
Paint()
..color = Colors.red
..style = PaintingStyle.stroke
..strokeWidth = 2,
..color = Colors.white
..strokeWidth = 1
..style = PaintingStyle.stroke,
);

// Draw edge lines
const edgeLength = 8.0;
if (from.dx == to.dx) {
// Line is vertical
canvas
// draw top edge
..drawLine(
Offset(from.dx - (edgeLength / 2), from.dy),
Offset(from.dx + (edgeLength / 2), from.dy),
Paint()
..color = Colors.white
..strokeWidth = 1
..style = PaintingStyle.stroke,
)
// draw bottom edge
..drawLine(
Offset(to.dx - (edgeLength / 2), to.dy),
Offset(to.dx + (edgeLength / 2), to.dy),
Paint()
..color = Colors.white
..strokeWidth = 1
..style = PaintingStyle.stroke,
);
} else {
// // Line is horizontal
canvas
// draw left edge
..drawLine(
Offset(from.dx, from.dy - (edgeLength / 2)),
Offset(from.dx, from.dy + (edgeLength / 2)),
Paint()
..color = Colors.white
..strokeWidth = 1
..style = PaintingStyle.stroke,
)
// draw right edge
..drawLine(
Offset(to.dx, to.dy - (edgeLength / 2)),
Offset(to.dx, to.dy + (edgeLength / 2)),
Paint()
..color = Colors.white
..strokeWidth = 1
..style = PaintingStyle.stroke,
);
}
}

@override
Expand Down
32 changes: 31 additions & 1 deletion lib/src/chart/line_chart/line_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,38 @@ class LineChartPainter extends AxisChartPainter<LineChartData> {
if (spot.isNotNull()) {
final x = getPixelX(spot.x, viewSize, holder);
final y = getPixelY(spot.y, viewSize, holder);
if (spot.xError == null && spot.yError == null) {

Check warning on line 394 in lib/src/chart/line_chart/line_chart_painter.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/line_chart/line_chart_painter.dart#L389-L394

Added lines #L389 - L394 were not covered by tests
continue;
}

var left = 0.0;
var right = 0.0;
if (spot.xError != null) {
left = getPixelX(spot.x - spot.xError!.lowerBy, viewSize, holder) - x;

Check warning on line 401 in lib/src/chart/line_chart/line_chart_painter.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/line_chart/line_chart_painter.dart#L400-L401

Added lines #L400 - L401 were not covered by tests
right =
getPixelX(spot.x + spot.xError!.upperBy, viewSize, holder) - x;

Check warning on line 403 in lib/src/chart/line_chart/line_chart_painter.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/line_chart/line_chart_painter.dart#L403

Added line #L403 was not covered by tests
}

var top = 0.0;
var bottom = 0.0;
if (spot.yError != null) {
top = getPixelY(spot.y + spot.yError!.lowerBy, viewSize, holder) - y;

Check warning on line 409 in lib/src/chart/line_chart/line_chart_painter.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/line_chart/line_chart_painter.dart#L408-L409

Added lines #L408 - L409 were not covered by tests
bottom =
getPixelY(spot.y - spot.yError!.upperBy, viewSize, holder) - y;

Check warning on line 411 in lib/src/chart/line_chart/line_chart_painter.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/line_chart/line_chart_painter.dart#L411

Added line #L411 was not covered by tests
}
final relativeErrorPixelsRect = Rect.fromLTRB(

Check warning on line 413 in lib/src/chart/line_chart/line_chart_painter.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/line_chart/line_chart_painter.dart#L413

Added line #L413 was not covered by tests
left,
top,
right,
bottom,
);
final painter = errorIndicatorData.errorPainter;
canvasWrapper.drawErrorIndicator(painter, spot, Offset(x, y));
canvasWrapper.drawErrorIndicator(

Check warning on line 420 in lib/src/chart/line_chart/line_chart_painter.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/line_chart/line_chart_painter.dart#L419-L420

Added lines #L419 - L420 were not covered by tests
painter,
spot,
Offset(x, y),

Check warning on line 423 in lib/src/chart/line_chart/line_chart_painter.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/chart/line_chart/line_chart_painter.dart#L423

Added line #L423 was not covered by tests
relativeErrorPixelsRect,
);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/src/utils/canvas_wrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ class CanvasWrapper {
FlSpotErrorRangePainter painter,
FlSpot origin,
Offset offset,
Rect errorRelativeRect,
) {
painter.draw(canvas, offset, origin);
painter.draw(canvas, offset, origin, errorRelativeRect);

Check warning on line 128 in lib/src/utils/canvas_wrapper.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/utils/canvas_wrapper.dart#L128

Added line #L128 was not covered by tests
}

/// Handles performing multiple draw actions rotated.
Expand Down

0 comments on commit dd5e703

Please sign in to comment.