@@ -68,6 +68,12 @@ class Sparkline extends StatelessWidget {
68
68
this .fillGradient,
69
69
this .fallbackHeight = 100.0 ,
70
70
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 = "\$ " ,
71
77
}) : assert (data != null ),
72
78
assert (lineWidth != null ),
73
79
assert (lineColor != null ),
@@ -160,6 +166,22 @@ class Sparkline extends StatelessWidget {
160
166
/// * [fallbackWidth] , the same but horizontally.
161
167
final double fallbackHeight;
162
168
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
+
163
185
@override
164
186
Widget build (BuildContext context) {
165
187
return new LimitedBox (
@@ -179,6 +201,12 @@ class Sparkline extends StatelessWidget {
179
201
pointsMode: pointsMode,
180
202
pointSize: pointSize,
181
203
pointColor: pointColor,
204
+ enableGridLines: enableGridLines,
205
+ gridLineColor: gridLineColor,
206
+ gridLineAmount: gridLineAmount,
207
+ gridLineLabelColor: gridLineLabelColor,
208
+ gridLineWidth: gridLineWidth,
209
+ labelPrefix: labelPrefix
182
210
),
183
211
),
184
212
);
@@ -198,8 +226,14 @@ class _SparklinePainter extends CustomPainter {
198
226
@required this .pointsMode,
199
227
@required this .pointSize,
200
228
@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);
203
237
204
238
final List <double > dataPoints;
205
239
@@ -220,18 +254,82 @@ class _SparklinePainter extends CustomPainter {
220
254
final double _max;
221
255
final double _min;
222
256
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
+
223
295
@override
224
296
void paint (Canvas canvas, Size size) {
225
- final double width = size.width - lineWidth;
297
+ double width = size.width - lineWidth;
226
298
final double height = size.height - lineWidth;
227
- final double widthNormalizer = width / (dataPoints.length - 1 );
228
299
final double heightNormalizer = height / (_max - _min);
229
300
230
301
final Path path = new Path ();
231
302
final List <Offset > points = < Offset > [];
232
303
233
304
Offset startPoint;
234
305
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
+
235
333
for (int i = 0 ; i < dataPoints.length; i++ ) {
236
334
double x = i * widthNormalizer + lineWidth / 2 ;
237
335
double y =
@@ -315,6 +413,11 @@ class _SparklinePainter extends CustomPainter {
315
413
fillGradient != old.fillGradient ||
316
414
pointsMode != old.pointsMode ||
317
415
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;
319
422
}
320
423
}
0 commit comments