From 6a54eff247b4762dac6429eb3b3e8e2e3918f1fd Mon Sep 17 00:00:00 2001 From: Marius Gripsgard Date: Wed, 17 Apr 2024 02:19:41 +0200 Subject: [PATCH] Add curved step line edges --- lib/src/chart/line_chart/line_chart_data.dart | 16 ++++++++++++++++ lib/src/chart/line_chart/line_chart_painter.dart | 10 ++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/src/chart/line_chart/line_chart_data.dart b/lib/src/chart/line_chart/line_chart_data.dart index c24127acf..831819e18 100644 --- a/lib/src/chart/line_chart/line_chart_data.dart +++ b/lib/src/chart/line_chart/line_chart_data.dart @@ -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), @@ -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; @@ -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), ); @@ -418,6 +428,8 @@ class LineChartBarData with EquatableMixin { List? showingIndicators, Shadow? shadow, bool? isStepLineChart, + bool? isStepLineEdgesCurved, + double? stepLineEdgesSmoothness, LineChartStepData? lineChartStepData, }) { return LineChartBarData( @@ -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, ); } @@ -466,6 +480,8 @@ class LineChartBarData with EquatableMixin { dashArray, shadow, isStepLineChart, + isStepLineEdgesCurved, + stepLineEdgesSmoothness, lineChartStepData, ]; } diff --git a/lib/src/chart/line_chart/line_chart_painter.dart b/lib/src/chart/line_chart/line_chart_painter.dart index b0f7c6cbd..d61da2355 100644 --- a/lib/src/chart/line_chart/line_chart_painter.dart +++ b/lib/src/chart/line_chart/line_chart_painter.dart @@ -590,10 +590,16 @@ class LineChartPainter extends AxisChartPainter { 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); } }