paintForeground method

  1. @override
void paintForeground(
  1. Context context
)
override

Implementation

@override
void paintForeground(Context context) {
  super.paintForeground(context);

  if (data.isEmpty) {
    return;
  }

  final grid = Chart.of(context).grid;

  if (drawPoints) {
    if (shape == null) {
      for (final value in data) {
        final p = grid.toChart(value.point);
        context.canvas.drawEllipse(p.x, p.y, pointSize, pointSize);
      }

      context.canvas
        ..setColor(color)
        ..fillPath();
    } else {
      for (final value in data) {
        final p = grid.toChart(value.point);

        Widget.draw(
          SizedBox.square(
            dimension: pointSize * 2,
            child: shape!(context),
          ),
          offset: p,
          alignment: Alignment.center,
          context: context,
        );
      }
    }
  }

  if (buildValue != null) {
    PdfPoint? previous;
    var index = 1;

    for (final value in data) {
      final p = grid.toChart(value.point);

      final size = Widget.measure(
        buildValue!(context, value),
        context: context,
      );

      final PdfPoint offset;
      var pos = valuePosition;
      if (pos == ValuePosition.auto) {
        final next =
            index < data.length ? grid.toChart(data[index++].point) : null;
        pos = automaticValuePosition(p, size, previous, next);
      }

      switch (pos) {
        case ValuePosition.left:
          offset = PdfPoint(p.x - size.x / 2 - pointSize - delta, p.y);
          break;
        case ValuePosition.top:
          offset = PdfPoint(p.x, p.y + size.y / 2 + pointSize + delta);
          break;
        case ValuePosition.right:
          offset = PdfPoint(p.x + size.x / 2 + pointSize + delta, p.y);
          break;
        case ValuePosition.bottom:
          offset = PdfPoint(p.x, p.y - size.y / 2 - pointSize - delta);
          break;
        case ValuePosition.auto:
          assert(false, 'We have an issue here');
          offset = p;
          break;
      }

      Widget.draw(
        buildValue!(context, value),
        offset: offset,
        alignment: Alignment.center,
        context: context,
      );

      previous = p;
    }
  }
}