Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 5 additions & 108 deletions lib/src/sparkline.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ class Sparkline extends StatelessWidget {
this.fillGradient,
this.fallbackHeight = 100.0,
this.fallbackWidth = 300.0,
this.enableGridLines = false,
this.gridLineColor = Colors.grey,
this.gridLineAmount = 5,
this.gridLineWidth = 0.5,
this.gridLineLabelColor = Colors.grey,
this.labelPrefix = "\$",
}) : assert(data != null),
assert(lineWidth != null),
assert(lineColor != null),
Expand Down Expand Up @@ -166,22 +160,6 @@ class Sparkline extends StatelessWidget {
/// * [fallbackWidth], the same but horizontally.
final double fallbackHeight;

/// Enable or disable grid lines
final bool enableGridLines;

/// Color of grid lines and label text
final Color gridLineColor;
final Color gridLineLabelColor;

/// Number of grid lines
final int gridLineAmount;

/// Width of grid lines
final double gridLineWidth;

/// Symbol prefix for grid line labels
final String labelPrefix;

@override
Widget build(BuildContext context) {
return new LimitedBox(
Expand All @@ -201,12 +179,6 @@ class Sparkline extends StatelessWidget {
pointsMode: pointsMode,
pointSize: pointSize,
pointColor: pointColor,
enableGridLines: enableGridLines,
gridLineColor: gridLineColor,
gridLineAmount: gridLineAmount,
gridLineLabelColor: gridLineLabelColor,
gridLineWidth: gridLineWidth,
labelPrefix: labelPrefix
),
),
);
Expand All @@ -226,14 +198,8 @@ class _SparklinePainter extends CustomPainter {
@required this.pointsMode,
@required this.pointSize,
@required this.pointColor,
@required this.enableGridLines,
this.gridLineColor,
this.gridLineAmount,
this.gridLineWidth,
this.gridLineLabelColor,
this.labelPrefix
}) : _max = dataPoints.reduce(math.max),
_min = dataPoints.reduce(math.min);
}) : _max = dataPoints.reduce(math.max),
_min = dataPoints.reduce(math.min);

final List<double> dataPoints;

Expand All @@ -254,82 +220,18 @@ class _SparklinePainter extends CustomPainter {
final double _max;
final double _min;

final bool enableGridLines;
final Color gridLineColor;
final int gridLineAmount;
final double gridLineWidth;
final Color gridLineLabelColor;
final String labelPrefix;

List<TextPainter> gridLineTextPainters = [];

update() {
if (enableGridLines) {
double gridLineValue;
for (int i = 0; i < gridLineAmount; i++) {
// 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();
}

gridLineTextPainters.add(new TextPainter(
text: new TextSpan(
text: labelPrefix + gridLineText,
style: new TextStyle(
color: gridLineLabelColor,
fontSize: 10.0,
fontWeight: FontWeight.bold)),
textDirection: TextDirection.ltr));
gridLineTextPainters[i].layout();
}
}
}

@override
void paint(Canvas canvas, Size size) {
double width = size.width - lineWidth;
final double width = size.width - lineWidth;
final double height = size.height - lineWidth;
final double widthNormalizer = width / (dataPoints.length - 1);
final double heightNormalizer = height / (_max - _min);

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

Offset startPoint;

if (gridLineTextPainters.isEmpty) {
update();
}

if (enableGridLines) {
width = size.width - gridLineTextPainters[0].text.text.length * 6;
Paint gridPaint = new Paint()
..color = gridLineColor
..strokeWidth = gridLineWidth;

double gridLineDist = height / (gridLineAmount - 1);
double gridLineY;

// Draw grid lines
for (int i = 0; i < gridLineAmount; i++) {
gridLineY = (gridLineDist * i).round().toDouble();
canvas.drawLine(new Offset(0.0, gridLineY),
new Offset(width, gridLineY), gridPaint);

// Label grid lines
gridLineTextPainters[i]
.paint(canvas, new Offset(width + 2.0, gridLineY - 6.0));
}
}

final double widthNormalizer = width / dataPoints.length;

for (int i = 0; i < dataPoints.length; i++) {
double x = i * widthNormalizer + lineWidth / 2;
double y =
Expand Down Expand Up @@ -413,11 +315,6 @@ class _SparklinePainter extends CustomPainter {
fillGradient != old.fillGradient ||
pointsMode != old.pointsMode ||
pointSize != old.pointSize ||
pointColor != old.pointColor ||
enableGridLines != old.enableGridLines ||
gridLineColor != old.gridLineColor ||
gridLineAmount != old.gridLineAmount ||
gridLineWidth != old.gridLineWidth ||
gridLineLabelColor != old.gridLineLabelColor;
pointColor != old.pointColor;
}
}