diff --git a/README.md b/README.md index 65f1a07..d9db198 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,30 @@ new Sparkline( ![fill above example screenshot](screenshots/example_fill_gradient.png) +### Grid Lines + +| Property | Default | +|------------------------|:---------------------:| +| enableGridLines | false | +| gridLineColor | Colors.grey | +| gridLineLabelColor | Colors.grey | +| gridLinelabelPrefix | "" | +| gridLineAmount | 5 | +| gridLineWidth | 0.5 | +| gridLineLabelPrecision | 3 | + +Example: + +```dart +new Sparkline( + data: data, + enableGridLines: true, + gridLineAmount: 3, +), +``` + +![grid line example screenshot](screenshots/example_grid_lines.png) + --- ### Todo: diff --git a/lib/src/sparkline.dart b/lib/src/sparkline.dart index 30b497b..aceb91a 100644 --- a/lib/src/sparkline.dart +++ b/lib/src/sparkline.dart @@ -73,7 +73,8 @@ class Sparkline extends StatelessWidget { this.gridLineAmount = 5, this.gridLineWidth = 0.5, this.gridLineLabelColor = Colors.grey, - this.labelPrefix = "\$", + this.gridLinelabelPrefix = "", + this.gridLineLabelPrecision = 3 }) : assert(data != null), assert(lineWidth != null), assert(lineColor != null), @@ -180,7 +181,10 @@ class Sparkline extends StatelessWidget { final double gridLineWidth; /// Symbol prefix for grid line labels - final String labelPrefix; + final String gridLinelabelPrefix; + + /// Digit precision of grid line labels + final int gridLineLabelPrecision; @override Widget build(BuildContext context) { @@ -206,7 +210,8 @@ class Sparkline extends StatelessWidget { gridLineAmount: gridLineAmount, gridLineLabelColor: gridLineLabelColor, gridLineWidth: gridLineWidth, - labelPrefix: labelPrefix + gridLinelabelPrefix: gridLinelabelPrefix, + gridLineLabelPrecision: gridLineLabelPrecision ), ), ); @@ -231,7 +236,8 @@ class _SparklinePainter extends CustomPainter { this.gridLineAmount, this.gridLineWidth, this.gridLineLabelColor, - this.labelPrefix + this.gridLinelabelPrefix, + this.gridLineLabelPrecision }) : _max = dataPoints.reduce(math.max), _min = dataPoints.reduce(math.min); @@ -259,7 +265,8 @@ class _SparklinePainter extends CustomPainter { final int gridLineAmount; final double gridLineWidth; final Color gridLineLabelColor; - final String labelPrefix; + final String gridLinelabelPrefix; + final int gridLineLabelPrecision; List gridLineTextPainters = []; @@ -270,18 +277,11 @@ class _SparklinePainter extends CustomPainter { // Label grid lines gridLineValue = _max - (((_max - _min) / (gridLineAmount - 1)) * i); - String gridLineText; - if (gridLineValue < 1) { - gridLineText = gridLineValue.toStringAsPrecision(4); - } else if (gridLineValue < 999) { - gridLineText = gridLineValue.toStringAsFixed(2); - } else { - gridLineText = gridLineValue.round().toString(); - } + String gridLineText = gridLineValue.toStringAsPrecision(gridLineLabelPrecision); gridLineTextPainters.add(new TextPainter( text: new TextSpan( - text: labelPrefix + gridLineText, + text: gridLinelabelPrefix + gridLineText, style: new TextStyle( color: gridLineLabelColor, fontSize: 10.0, @@ -418,6 +418,8 @@ class _SparklinePainter extends CustomPainter { gridLineColor != old.gridLineColor || gridLineAmount != old.gridLineAmount || gridLineWidth != old.gridLineWidth || - gridLineLabelColor != old.gridLineLabelColor; + gridLineLabelColor != old.gridLineLabelColor || + gridLinelabelPrefix != old.gridLinelabelPrefix || + gridLineLabelPrecision != old.gridLineLabelPrecision; } } diff --git a/screenshots/example_grid_lines.png b/screenshots/example_grid_lines.png new file mode 100644 index 0000000..131044a Binary files /dev/null and b/screenshots/example_grid_lines.png differ