Skip to content

Commit

Permalink
added preventCurveOverShooting on BarData (in the LineChartData) to fix
Browse files Browse the repository at this point in the history
#25, and updated line_chart.md
  • Loading branch information
imaNNeo committed Jul 7, 2019
1 parent 8035314 commit dba523e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
5 changes: 5 additions & 0 deletions lib/src/chart/line_chart/line_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ class LineChartBarData {
/// if it is 0.0, the lines draw with sharp corners.
final double curveSmoothness;

/// prevent overshooting when draw curve line on linear sequence spots
/// check this [issue](https://github.com/imaNNeoFighT/fl_chart/issues/25)
final bool preventCurveOverShooting;

final bool isStrokeCapRound;

/// to fill space below the bar line,
Expand All @@ -151,6 +155,7 @@ class LineChartBarData {
this.barWidth = 2.0,
this.isCurved = false,
this.curveSmoothness = 0.35,
this.preventCurveOverShooting = false,
this.isStrokeCapRound = false,
this.belowBarData = const BelowBarData(),
this.dotData = const FlDotData(),
Expand Down
57 changes: 39 additions & 18 deletions lib/src/chart/line_chart/line_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,40 +167,61 @@ class LineChartPainter extends AxisChartPainter {
int size = barData.spots.length;
path.reset();

double lX = 0.0, lY = 0.0;
var temp = const Offset(0.0, 0.0);

double x = getPixelX(barData.spots[0].x, viewSize);
double y = getPixelY(barData.spots[0].y, viewSize);
path.moveTo(x, y);
for (int i = 1; i < size; i++) {
/// CurrentSpot
FlSpot p = barData.spots[i];
double px = getPixelX(p.x, viewSize);
double py = getPixelY(p.y, viewSize);
final current = Offset(
getPixelX(barData.spots[i].x, viewSize),
getPixelY(barData.spots[i].y, viewSize),
);

/// previous spot
FlSpot p0 = barData.spots[i - 1];
double p0x = getPixelX(p0.x, viewSize);
double p0y = getPixelY(p0.y, viewSize);
final previous = Offset(
getPixelX(barData.spots[i - 1].x, viewSize),
getPixelY(barData.spots[i - 1].y, viewSize),
);

double x1 = p0x + lX;
double y1 = p0y + lY;
/// next point
final next = Offset(
getPixelX(barData.spots[i + 1 < size ? i + 1 : i].x, viewSize),
getPixelY(barData.spots[i + 1 < size ? i + 1 : i].y, viewSize),
);

/// next point
FlSpot p1 = barData.spots[i + 1 < size ? i + 1 : i];
double p1x = getPixelX(p1.x, viewSize);
double p1y = getPixelY(p1.y, viewSize);
final next2 = Offset(
getPixelX(barData.spots[i + 2 < size ? i + 2 : i].x, viewSize),
getPixelY(barData.spots[i + 2 < size ? i + 2 : i].y, viewSize),
);

final controlPoint1 = previous + temp;

/// if the isCurved is false, we set 0 for smoothness,
/// it means we should not have any smoothness then we face with
/// the sharped corners line
double smoothness = barData.isCurved ? barData.curveSmoothness : 0.0;
lX = ((p1x - p0x) / 2) * smoothness;
lY = ((p1y - p0y) / 2) * smoothness;
double x2 = px - lX;
double y2 = py - lY;
final smoothness = barData.isCurved ? barData.curveSmoothness : 0.0;
temp = ((next - previous) / 2) * smoothness;

path.cubicTo(x1, y1, x2, y2, px, py);
if (barData.preventCurveOverShooting) {
if ((next - current).dy <= 10 || (current - previous).dy <= 10) {
temp = Offset(temp.dx, 0);
}

if ((next - current).dx <= 10 || (current - previous).dx <= 10) {
temp = Offset(0, temp.dy);
}
}

final controlPoint2 = current - temp;

path.cubicTo(
controlPoint1.dx, controlPoint1.dy,
controlPoint2.dx, controlPoint2.dy,
current.dx, current.dy,
);
}

return path;
Expand Down
1 change: 1 addition & 0 deletions repo_files/documentations/line_chart.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ FlChart(
|barWidth| gets the stroke width of the line bar|2.0|
|isCurved| curves the corners of the line on the spot's positions| false|
|curveSmoothness| smoothness radius of the curve corners (works when isCurved is true) | 0.35|
|preventCurveOverShooting|prevent overshooting when draw curve line on linear sequence spots, check this [issue](https://github.com/imaNNeoFighT/fl_chart/issues/25)| false|
|isStrokeCapRound| determines whether start and end of the bar line is Qubic or Round | false|
|belowBarData| check the [BelowBarData](#BelowBarData) |BelowBarData()|
|dotData| check the [FlDotData](#FlDotData) | FlDotData()|
Expand Down

0 comments on commit dba523e

Please sign in to comment.