-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathflutter_sparkline.dart
93 lines (74 loc) · 2.19 KB
/
flutter_sparkline.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
library flutter_sparkline;
import 'dart:math' as math;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
math.Random random = new math.Random();
List<double> _generateRandomData(int count) {
List<double> result = <double>[];
for (int i = 0; i < count; i++) {
result.add(random.nextDouble() * 100);
}
return result;
}
class Sparkline extends StatelessWidget {
final List<double> data = _generateRandomData(5);
final double lineWidth;
final Color lineColor;
Sparkline({this.lineWidth = 15.0, this.lineColor = Colors.green});
@override
Widget build(BuildContext context) {
return new Container(
color: Colors.red,
width: 200.0,
height: 100.0,
child: new CustomPaint(
painter: new _SparklinePainter(
data,
lineWidth: lineWidth,
lineColor: lineColor,
),
),
);
}
}
class _SparklinePainter extends CustomPainter {
_SparklinePainter(
this.dataPoints, {
@required this.lineWidth,
@required this.lineColor,
}) : _max = dataPoints.reduce(math.max),
_min = dataPoints.reduce(math.min);
final List<double> dataPoints;
final double lineWidth;
final Color lineColor;
final double _max;
final double _min;
@override
void paint(Canvas canvas, Size size) {
print('max: $_max, min: $_min: size: $size');
final double widthNormalizer = size.width / (dataPoints.length - 1);
final double heightNormalizer = size.height / (_max - _min);
print('wn: $widthNormalizer, hn: $heightNormalizer');
final Path path = new Path();
for (int i = 0; i < dataPoints.length; i++) {
double x = i * widthNormalizer;
double y = size.height - (dataPoints[i] - _min) * heightNormalizer;
print('painting: ${dataPoints[i]} ($x, $y)');
if (i == 0) {
path.moveTo(x, y);
} else {
path.lineTo(x, y);
}
}
Paint paint = new Paint()
..strokeWidth = lineWidth
..color = lineColor
..strokeCap = StrokeCap.round
..style = PaintingStyle.stroke;
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(_SparklinePainter old) {
return dataPoints != old.dataPoints;
}
}