forked from google/charts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
line_painter.dart
248 lines (213 loc) · 8.98 KB
/
line_painter.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright 2018 the Charts project authors. Please see the AUTHORS file
// for details.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'dart:math' show Point, Rectangle;
import 'package:charts_flutter/src/util/monotonex.dart';
import 'package:flutter/material.dart';
import 'package:charts_common/common.dart' as common show Color;
/// Draws a simple line.
///
/// Lines may be styled with dash patterns similar to stroke-dasharray in SVG
/// path elements. Dash patterns are currently only supported between vertical
/// or horizontal line segments at this time.
class LinePainter {
/// Draws a simple line.
///
/// [dashPattern] controls the pattern of dashes and gaps in a line. It is a
/// list of lengths of alternating dashes and gaps. The rendering is similar
/// to stroke-dasharray in SVG path elements. An odd number of values in the
/// pattern will be repeated to derive an even number of values. "1,2,3" is
/// equivalent to "1,2,3,1,2,3."
void draw(
{Canvas canvas,
Paint paint,
List<Point> points,
Rectangle<num> clipBounds,
common.Color fill,
common.Color stroke,
bool smoothLine,
bool roundEndCaps,
double strokeWidthPx,
List<int> dashPattern}) {
if (points.isEmpty) {
return;
}
// Apply clip bounds as a clip region.
if (clipBounds != null) {
canvas
..save()
..clipRect(new Rect.fromLTWH(
clipBounds.left.toDouble(),
clipBounds.top.toDouble(),
clipBounds.width.toDouble(),
clipBounds.height.toDouble()));
}
paint.color = new Color.fromARGB(stroke.a, stroke.r, stroke.g, stroke.b);
// If the line has a single point, draw a circle.
if (points.length == 1) {
final point = points.first;
paint.style = PaintingStyle.fill;
canvas.drawCircle(new Offset(point.x, point.y), strokeWidthPx, paint);
} else {
if (strokeWidthPx != null) {
paint.strokeWidth = strokeWidthPx;
}
paint.strokeJoin = StrokeJoin.round;
paint.style = PaintingStyle.stroke;
if (smoothLine ?? false) {
_drawSmoothLine(canvas, paint, points);
} else if (dashPattern == null || dashPattern.isEmpty) {
if (roundEndCaps == true) {
paint.strokeCap = StrokeCap.round;
}
_drawSolidLine(canvas, paint, points);
} else {
_drawDashedLine(canvas, paint, points, dashPattern);
}
}
if (clipBounds != null) {
canvas.restore();
}
}
/// Draws smooth lines between each point.
void _drawSmoothLine(Canvas canvas, Paint paint, List<Point> points) {
final path = new Path()
..moveTo(points.first.x.toDouble(), points.first.y.toDouble());
MonotoneX.addCurve(path, points);
canvas.drawPath(path, paint);
}
/// Draws solid lines between each point.
void _drawSolidLine(Canvas canvas, Paint paint, List<Point> points) {
// TODO: Extract a native line component which constructs the
// appropriate underlying data structures to avoid conversion.
final path = new Path()
..moveTo(points.first.x.toDouble(), points.first.y.toDouble());
for (var point in points) {
path.lineTo(point.x.toDouble(), point.y.toDouble());
}
canvas.drawPath(path, paint);
}
/// Draws dashed lines lines between each point.
void _drawDashedLine(
Canvas canvas, Paint paint, List<Point> points, List<int> dashPattern) {
final localDashPattern = new List.from(dashPattern);
// If an odd number of parts are defined, repeat the pattern to get an even
// number.
if (dashPattern.length % 2 == 1) {
localDashPattern.addAll(dashPattern);
}
// Stores the previous point in the series.
var previousSeriesPoint = _getOffset(points.first);
var remainder = 0;
var solid = true;
var dashPatternIndex = 0;
// Gets the next segment in the dash pattern, looping back to the
// beginning once the end has been reached.
var getNextDashPatternSegment = () {
final dashSegment = localDashPattern[dashPatternIndex];
dashPatternIndex = (dashPatternIndex + 1) % localDashPattern.length;
return dashSegment;
};
// Array of points that is used to draw a connecting path when only a
// partial dash pattern segment can be drawn in the remaining length of a
// line segment (between two defined points in the shape).
var remainderPoints;
// Draw the path through all the rest of the points in the series.
for (var pointIndex = 1; pointIndex < points.length; pointIndex++) {
// Stores the current point in the series.
final seriesPoint = _getOffset(points[pointIndex]);
if (previousSeriesPoint == seriesPoint) {
// Bypass dash pattern handling if the points are the same.
} else {
// Stores the previous point along the current series line segment where
// we rendered a dash (or left a gap).
var previousPoint = previousSeriesPoint;
var d = _getOffsetDistance(previousSeriesPoint, seriesPoint);
while (d > 0) {
var dashSegment =
remainder > 0 ? remainder : getNextDashPatternSegment();
remainder = 0;
// Create a unit vector in the direction from previous to next point.
final v = seriesPoint - previousPoint;
final u = new Offset(v.dx / v.distance, v.dy / v.distance);
// If the remaining distance is less than the length of the dash
// pattern segment, then cut off the pattern segment for this portion
// of the overall line.
final distance = d < dashSegment ? d : dashSegment.toDouble();
// Compute a vector representing the length of dash pattern segment to
// be drawn.
final nextPoint = previousPoint + (u * distance);
// If we are in a solid portion of the dash pattern, draw a line.
// Else, move on.
if (solid) {
if (remainderPoints != null) {
// If we had a partial un-drawn dash from the previous point along
// the line, draw a path that includes it and the end of the dash
// pattern segment in the current line segment.
remainderPoints.add(new Offset(nextPoint.dx, nextPoint.dy));
final path = new Path()
..moveTo(remainderPoints.first.dx, remainderPoints.first.dy);
for (var p in remainderPoints) {
path.lineTo(p.dx, p.dy);
}
canvas.drawPath(path, paint);
remainderPoints = null;
} else {
if (d < dashSegment && pointIndex < points.length - 1) {
// If the remaining distance d is too small to fit this dash,
// and we have more points in the line, save off a series of
// remainder points so that we can draw a path segment moving in
// the direction of the next point.
//
// Note that we don't need to save anything off for the "blank"
// portions of the pattern because we still take the remaining
// distance into account before starting the next dash in the
// next line segment.
remainderPoints = [
new Offset(previousPoint.dx, previousPoint.dy),
new Offset(nextPoint.dx, nextPoint.dy)
];
} else {
// Otherwise, draw a simple line segment for this dash.
canvas.drawLine(previousPoint, nextPoint, paint);
}
}
}
solid = !solid;
previousPoint = nextPoint;
d = d - dashSegment;
}
// Save off the remaining distance so that we can continue the dash (or
// gap) into the next line segment.
remainder = -d.round();
// If we have a remaining un-drawn distance for the current dash (or
// gap), revert the last change to "solid" so that we will continue
// either drawing a dash or leaving a gap.
if (remainder > 0) {
solid = !solid;
}
}
previousSeriesPoint = seriesPoint;
}
}
/// Converts a [Point] into an [Offset].
Offset _getOffset(Point point) =>
new Offset(point.x.toDouble(), point.y.toDouble());
/// Computes the distance between two [Offset]s, as if they were [Point]s.
num _getOffsetDistance(Offset o1, Offset o2) {
final p1 = new Point(o1.dx, o1.dy);
final p2 = new Point(o2.dx, o2.dy);
return p1.distanceTo(p2);
}
}