Skip to content

Commit 208e375

Browse files
trentpiercyxqwzts
authored andcommitted
Add grid lines with labels (xqwzts#5)
1 parent 740e353 commit 208e375

File tree

1 file changed

+108
-5
lines changed

1 file changed

+108
-5
lines changed

lib/src/sparkline.dart

Lines changed: 108 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ class Sparkline extends StatelessWidget {
6868
this.fillGradient,
6969
this.fallbackHeight = 100.0,
7070
this.fallbackWidth = 300.0,
71+
this.enableGridLines = false,
72+
this.gridLineColor = Colors.grey,
73+
this.gridLineAmount = 5,
74+
this.gridLineWidth = 0.5,
75+
this.gridLineLabelColor = Colors.grey,
76+
this.labelPrefix = "\$",
7177
}) : assert(data != null),
7278
assert(lineWidth != null),
7379
assert(lineColor != null),
@@ -160,6 +166,22 @@ class Sparkline extends StatelessWidget {
160166
/// * [fallbackWidth], the same but horizontally.
161167
final double fallbackHeight;
162168

169+
/// Enable or disable grid lines
170+
final bool enableGridLines;
171+
172+
/// Color of grid lines and label text
173+
final Color gridLineColor;
174+
final Color gridLineLabelColor;
175+
176+
/// Number of grid lines
177+
final int gridLineAmount;
178+
179+
/// Width of grid lines
180+
final double gridLineWidth;
181+
182+
/// Symbol prefix for grid line labels
183+
final String labelPrefix;
184+
163185
@override
164186
Widget build(BuildContext context) {
165187
return new LimitedBox(
@@ -179,6 +201,12 @@ class Sparkline extends StatelessWidget {
179201
pointsMode: pointsMode,
180202
pointSize: pointSize,
181203
pointColor: pointColor,
204+
enableGridLines: enableGridLines,
205+
gridLineColor: gridLineColor,
206+
gridLineAmount: gridLineAmount,
207+
gridLineLabelColor: gridLineLabelColor,
208+
gridLineWidth: gridLineWidth,
209+
labelPrefix: labelPrefix
182210
),
183211
),
184212
);
@@ -198,8 +226,14 @@ class _SparklinePainter extends CustomPainter {
198226
@required this.pointsMode,
199227
@required this.pointSize,
200228
@required this.pointColor,
201-
}) : _max = dataPoints.reduce(math.max),
202-
_min = dataPoints.reduce(math.min);
229+
@required this.enableGridLines,
230+
this.gridLineColor,
231+
this.gridLineAmount,
232+
this.gridLineWidth,
233+
this.gridLineLabelColor,
234+
this.labelPrefix
235+
}) : _max = dataPoints.reduce(math.max),
236+
_min = dataPoints.reduce(math.min);
203237

204238
final List<double> dataPoints;
205239

@@ -220,18 +254,82 @@ class _SparklinePainter extends CustomPainter {
220254
final double _max;
221255
final double _min;
222256

257+
final bool enableGridLines;
258+
final Color gridLineColor;
259+
final int gridLineAmount;
260+
final double gridLineWidth;
261+
final Color gridLineLabelColor;
262+
final String labelPrefix;
263+
264+
List<TextPainter> gridLineTextPainters = [];
265+
266+
update() {
267+
if (enableGridLines) {
268+
double gridLineValue;
269+
for (int i = 0; i < gridLineAmount; i++) {
270+
// Label grid lines
271+
gridLineValue = _max - (((_max - _min) / (gridLineAmount - 1)) * i);
272+
273+
String gridLineText;
274+
if (gridLineValue < 1) {
275+
gridLineText = gridLineValue.toStringAsPrecision(4);
276+
} else if (gridLineValue < 999) {
277+
gridLineText = gridLineValue.toStringAsFixed(2);
278+
} else {
279+
gridLineText = gridLineValue.round().toString();
280+
}
281+
282+
gridLineTextPainters.add(new TextPainter(
283+
text: new TextSpan(
284+
text: labelPrefix + gridLineText,
285+
style: new TextStyle(
286+
color: gridLineLabelColor,
287+
fontSize: 10.0,
288+
fontWeight: FontWeight.bold)),
289+
textDirection: TextDirection.ltr));
290+
gridLineTextPainters[i].layout();
291+
}
292+
}
293+
}
294+
223295
@override
224296
void paint(Canvas canvas, Size size) {
225-
final double width = size.width - lineWidth;
297+
double width = size.width - lineWidth;
226298
final double height = size.height - lineWidth;
227-
final double widthNormalizer = width / (dataPoints.length - 1);
228299
final double heightNormalizer = height / (_max - _min);
229300

230301
final Path path = new Path();
231302
final List<Offset> points = <Offset>[];
232303

233304
Offset startPoint;
234305

306+
if (gridLineTextPainters.isEmpty) {
307+
update();
308+
}
309+
310+
if (enableGridLines) {
311+
width = size.width - gridLineTextPainters[0].text.text.length * 6;
312+
Paint gridPaint = new Paint()
313+
..color = gridLineColor
314+
..strokeWidth = gridLineWidth;
315+
316+
double gridLineDist = height / (gridLineAmount - 1);
317+
double gridLineY;
318+
319+
// Draw grid lines
320+
for (int i = 0; i < gridLineAmount; i++) {
321+
gridLineY = (gridLineDist * i).round().toDouble();
322+
canvas.drawLine(new Offset(0.0, gridLineY),
323+
new Offset(width, gridLineY), gridPaint);
324+
325+
// Label grid lines
326+
gridLineTextPainters[i]
327+
.paint(canvas, new Offset(width + 2.0, gridLineY - 6.0));
328+
}
329+
}
330+
331+
final double widthNormalizer = width / dataPoints.length;
332+
235333
for (int i = 0; i < dataPoints.length; i++) {
236334
double x = i * widthNormalizer + lineWidth / 2;
237335
double y =
@@ -315,6 +413,11 @@ class _SparklinePainter extends CustomPainter {
315413
fillGradient != old.fillGradient ||
316414
pointsMode != old.pointsMode ||
317415
pointSize != old.pointSize ||
318-
pointColor != old.pointColor;
416+
pointColor != old.pointColor ||
417+
enableGridLines != old.enableGridLines ||
418+
gridLineColor != old.gridLineColor ||
419+
gridLineAmount != old.gridLineAmount ||
420+
gridLineWidth != old.gridLineWidth ||
421+
gridLineLabelColor != old.gridLineLabelColor;
319422
}
320423
}

0 commit comments

Comments
 (0)