forked from imaNNeo/fl_chart
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove path_drawing dependency, we now use the dashPath() code directly.
- Loading branch information
imaN Khoshabi
committed
Mar 22, 2021
1 parent
962b1e5
commit fc64cde
Showing
3 changed files
with
91 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import 'dart:ui'; | ||
|
||
/// Came from [flutter_path_drawing](https://github.com/dnfield/flutter_path_drawing) library. | ||
/// Creates a new path that is drawn from the segments of `source`. | ||
/// | ||
/// Dash intervals are controled by the `dashArray` - see [CircularIntervalList] | ||
/// for examples. | ||
/// | ||
/// `dashOffset` specifies an initial starting point for the dashing. | ||
/// | ||
/// Passing a `source` that is an empty path will return an empty path. | ||
Path dashPath( | ||
Path source, { | ||
required CircularIntervalList<double> dashArray, | ||
DashOffset? dashOffset, | ||
}) { | ||
assert(dashArray != null); // ignore: unnecessary_null_comparison | ||
|
||
dashOffset = dashOffset ?? const DashOffset.absolute(0.0); | ||
// TODO: Is there some way to determine how much of a path would be visible today? | ||
|
||
final dest = Path(); | ||
for (final metric in source.computeMetrics()) { | ||
var distance = dashOffset._calculate(metric.length); | ||
var draw = true; | ||
while (distance < metric.length) { | ||
final len = dashArray.next; | ||
if (draw) { | ||
dest.addPath(metric.extractPath(distance, distance + len), Offset.zero); | ||
} | ||
distance += len; | ||
draw = !draw; | ||
} | ||
} | ||
|
||
return dest; | ||
} | ||
|
||
enum _DashOffsetType { Absolute, Percentage } | ||
|
||
/// Specifies the starting position of a dash array on a path, either as a | ||
/// percentage or absolute value. | ||
/// | ||
/// The internal value will be guaranteed to not be null. | ||
class DashOffset { | ||
/// Create a DashOffset that will be measured as a percentage of the length | ||
/// of the segment being dashed. | ||
/// | ||
/// `percentage` will be clamped between 0.0 and 1.0. | ||
DashOffset.percentage(double percentage) | ||
: _rawVal = percentage.clamp(0.0, 1.0), | ||
_dashOffsetType = _DashOffsetType.Percentage; | ||
|
||
/// Create a DashOffset that will be measured in terms of absolute pixels | ||
/// along the length of a [Path] segment. | ||
const DashOffset.absolute(double start) | ||
: _rawVal = start, | ||
_dashOffsetType = _DashOffsetType.Absolute; | ||
|
||
final double _rawVal; | ||
final _DashOffsetType _dashOffsetType; | ||
|
||
double _calculate(double length) { | ||
return _dashOffsetType == _DashOffsetType.Absolute ? _rawVal : length * _rawVal; | ||
} | ||
} | ||
|
||
/// A circular array of dash offsets and lengths. | ||
/// | ||
/// For example, the array `[5, 10]` would result in dashes 5 pixels long | ||
/// followed by blank spaces 10 pixels long. The array `[5, 10, 5]` would | ||
/// result in a 5 pixel dash, a 10 pixel gap, a 5 pixel dash, a 5 pixel gap, | ||
/// a 10 pixel dash, etc. | ||
/// | ||
/// Note that this does not quite conform to an [Iterable<T>], because it does | ||
/// not have a moveNext. | ||
class CircularIntervalList<T> { | ||
CircularIntervalList(this._values); | ||
|
||
final List<T> _values; | ||
int _idx = 0; | ||
|
||
T get next { | ||
if (_idx >= _values.length) { | ||
_idx = 0; | ||
} | ||
return _values[_idx++]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters