Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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