Skip to content

Instantly share code, notes, and snippets.

@Indy9000
Created June 13, 2021 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Indy9000/9d49d9cf50329e96b9596a59b0393062 to your computer and use it in GitHub Desktop.
Save Indy9000/9d49d9cf50329e96b9596a59b0393062 to your computer and use it in GitHub Desktop.
line-chart-widget
List<String> _computeLabels() {
return _Y.map((yp) => "${yp.toStringAsFixed(1)}").toList();
}
void _drawYLabels(Canvas canvas, List<String> labels, List<Offset> points,
double boxW, double top) {
var i = 0;
labels.forEach((label) {
final dp = points[i];
final dy = (dp.dy - 15.0) < top ? 15.0 : -15.0;
final ly = dp + Offset(0, dy);
drawTextCentered(canvas, ly, label, yLabelStyle, boxW);
i++;
});
}
void _drawXLabels(Canvas canvas, Offset c, double boxW) {
_X.forEach((xp) {
drawTextCentered(canvas, c, xp, xLabelStyle, boxW);
c += Offset(boxW, 0);
});
}
TextPainter measureText(
String s, TextStyle style, double maxWidth, TextAlign align) {
final span = TextSpan(text: s, style: style);
final tp = TextPainter(
text: span, textAlign: align, textDirection: TextDirection.ltr);
tp.layout(minWidth: 0, maxWidth: maxWidth);
return tp;
}
Size drawTextCentered(
Canvas canvas, Offset c, String text, TextStyle style, double maxWidth) {
final tp = measureText(text, style, maxWidth, TextAlign.center);
final offset = c + Offset(-tp.width / 2.0, -tp.height / 2.0);
tp.paint(canvas, offset);
return tp.size;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment