Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed line chart shows negative values #1594

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lib/src/chart/line_chart/line_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,7 @@ class LineChartPainter extends AxisChartPainter<LineChartData> {
..transparentIfWidthIsZero();

barPath = barPath.toDashedPath(barData.dashArray);
barPath = adjustPath(barPath, rectAroundTheLine);
canvasWrapper.drawPath(barPath, _barPaint);
}

Expand Down Expand Up @@ -1291,6 +1292,42 @@ class LineChartPainter extends AxisChartPainter<LineChartData> {
return null;
}
}

Path adjustPath(Path barPath, Rect rectAroundTheLine) {
final newPath = Path();
final pathMetrics = barPath.computeMetrics();

for (final pathMetric in pathMetrics) {
for (var i = 0.0; i < pathMetric.length; i++) {
final tangent = pathMetric.getTangentForOffset(i);
if (tangent != null) {
var point = tangent.position;

if (!rectAroundTheLine.contains(point)) {
if (point.dx < rectAroundTheLine.left) {
point = Offset(rectAroundTheLine.left, point.dy);
} else if (point.dx > rectAroundTheLine.right) {
point = Offset(rectAroundTheLine.right, point.dy);
}

if (point.dy < rectAroundTheLine.top) {
point = Offset(point.dx, rectAroundTheLine.top);
} else if (point.dy > rectAroundTheLine.bottom) {
point = Offset(point.dx, rectAroundTheLine.bottom);
}
}

if (i == 0.0) {
newPath.moveTo(point.dx, point.dy);
} else {
newPath.lineTo(point.dx, point.dy);
}
}
}
}

return newPath;
}
}

@visibleForTesting
Expand Down
Loading