drawLabel method

void drawLabel(
  1. Canvas canvas,
  2. Size size,
  3. double barWidth,
  4. double maxHeight,
  5. List<DataItem> dataset,
  6. DataItem greaterDataset,
)

Draws a label for a bar with a rounded background box.

Parameters:

  • canvas : The canvas to draw on
  • size : The size to draw the label
  • barWidth : The width of each bar to calculate label position
  • maxHeight : The max height to calculate lbel position
  • dataset : The data itens for this chart
  • greaterDataset : 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();
  }
}