drawLabel method
void
drawLabel()
Draws a label for a bar with a rounded background box.
Parameters:
canvas: The canvas to draw onsize: The size to draw the labelbarWidth: The width of each bar to calculate label positionmaxHeight: The max height to calculate lbel positiondataset: The data itens for this chartgreaterDataset: The grater dataset to calculate label position
Implementation
void drawLabel(
Canvas canvas,
Size size,
double barWidth,
double maxHeight,
List<DataItem> dataset,
DataItem greaterDataset,
) {
for (int i = 0; i < dataset.length; i++) {
double heightFactor = greaterDataset.value / (maxHeight - 150);
double barHeight = maxHeight - 10 - dataset[i].value / heightFactor;
double x = (i * barWidth * 1.1) + (barWidth / 2) + 15;
TextSpan label = TextSpan(
text: dataset[i].label,
style: TextStyle(fontSize: 14, color: ColorSeed.values[i].color),
);
final labelPainter = TextPainter(
text: label,
textDirection: TextDirection.ltr,
);
labelPainter.layout(minWidth: 0, maxWidth: size.width);
Offset center = Offset(x, barHeight);
canvas.save();
canvas.translate(center.dx, center.dy);
canvas.rotate(-math.pi / 3);
canvas.translate(-center.dx, -center.dy);
labelPainter.paint(canvas, Offset(center.dx, center.dy));
canvas.restore();
}
}