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

Add curved step line edges #1636

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions lib/src/chart/line_chart/line_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ class LineChartBarData with EquatableMixin {
this.dashArray,
this.shadow = const Shadow(color: Colors.transparent),
this.isStepLineChart = false,
this.isStepLineEdgesCurved = false,
this.stepLineEdgesSmoothness = 2,
this.lineChartStepData = const LineChartStepData(),
}) : color =
color ?? ((color == null && gradient == null) ? Colors.cyan : null),
Expand Down Expand Up @@ -360,6 +362,12 @@ class LineChartBarData with EquatableMixin {
/// If sets true, it draws the chart in Step Line Chart style, using [LineChartBarData.lineChartStepData].
final bool isStepLineChart;

/// if sets true, it draws the chart in Step Line Chart style with curved edges.
final bool isStepLineEdgesCurved;

/// If [isStepLineEdgesCurved] is true, it determines the curve smoothness of the step line edges.
final double stepLineEdgesSmoothness;

/// Holds data for representing a Step Line Chart, and works only if [isStepChart] is true.
final LineChartStepData lineChartStepData;

Expand Down Expand Up @@ -392,6 +400,8 @@ class LineChartBarData with EquatableMixin {
showingIndicators: b.showingIndicators,
shadow: Shadow.lerp(a.shadow, b.shadow, t)!,
isStepLineChart: b.isStepLineChart,
isStepLineEdgesCurved: b.isStepLineEdgesCurved,
stepLineEdgesSmoothness: b.stepLineEdgesSmoothness,
lineChartStepData:
LineChartStepData.lerp(a.lineChartStepData, b.lineChartStepData, t),
);
Expand All @@ -418,6 +428,8 @@ class LineChartBarData with EquatableMixin {
List<int>? showingIndicators,
Shadow? shadow,
bool? isStepLineChart,
bool? isStepLineEdgesCurved,
double? stepLineEdgesSmoothness,
LineChartStepData? lineChartStepData,
}) {
return LineChartBarData(
Expand All @@ -441,6 +453,8 @@ class LineChartBarData with EquatableMixin {
showingIndicators: showingIndicators ?? this.showingIndicators,
shadow: shadow ?? this.shadow,
isStepLineChart: isStepLineChart ?? this.isStepLineChart,
isStepLineEdgesCurved: isStepLineEdgesCurved ?? this.isStepLineEdgesCurved,
stepLineEdgesSmoothness: stepLineEdgesSmoothness ?? this.stepLineEdgesSmoothness,
lineChartStepData: lineChartStepData ?? this.lineChartStepData,
);
}
Expand All @@ -466,6 +480,8 @@ class LineChartBarData with EquatableMixin {
dashArray,
shadow,
isStepLineChart,
isStepLineEdgesCurved,
stepLineEdgesSmoothness,
lineChartStepData,
];
}
Expand Down
10 changes: 8 additions & 2 deletions lib/src/chart/line_chart/line_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,16 @@ class LineChartPainter extends AxisChartPainter<LineChartData> {
path.lineTo(next.dx, next.dy);
} else {
final deltaX = next.dx - current.dx;
final isYIncreasing = current.dy < next.dy;

// ignore 2 last points
final smoothness = i + 2 < size ? (barData.isStepLineEdgesCurved ? barData.stepLineEdgesSmoothness : 0) : 0;

path
..lineTo(current.dx + deltaX - (deltaX * stepDirection), current.dy)
..lineTo(current.dx + deltaX - (deltaX * stepDirection), next.dy)
..lineTo(current.dx + deltaX - (deltaX * stepDirection) - smoothness, current.dy)
..quadraticBezierTo(current.dx + deltaX - (deltaX * stepDirection), current.dy, current.dx + deltaX - (deltaX * stepDirection), current.dy + (isYIncreasing ? smoothness : -smoothness))
..lineTo(current.dx + deltaX - (deltaX * stepDirection), next.dy + (isYIncreasing ? -smoothness : smoothness))
..quadraticBezierTo(current.dx + deltaX - (deltaX * stepDirection), next.dy, current.dx + deltaX - (deltaX * stepDirection) + smoothness, next.dy)
..lineTo(next.dx, next.dy);
}
}
Expand Down