paint method

  1. @override
void paint(
  1. Canvas canvas,
  2. Size size,
  3. WorldToScreen worldToScreen,
  4. ScreenToWorld screenToWorld,
  5. double scale,
)
override

Called by the canvas to paint this item onto the screen.

The canvas and size are provided by the Flutter framework. Use worldToScreen to translate mathematical coordinates into on-screen pixels.

Implementation

@override
void paint(
  Canvas canvas,
  Size size,
  WorldToScreen worldToScreen,
  ScreenToWorld screenToWorld,
  double scale,
) {
  if (points.isEmpty) return;

  final screenPoints = points.map((p) => worldToScreen(p.x, p.y)).toList();

  if (connectPoints && screenPoints.length > 1) {
    final path = Path()..moveTo(screenPoints[0].dx, screenPoints[0].dy);
    for (int i = 1; i < screenPoints.length; i++) {
      path.lineTo(screenPoints[i].dx, screenPoints[i].dy);
    }

    canvas.drawPath(
      path.shift(const Offset(0, 2)),
      Paint()
        ..color = color.withOpacity(0.2)
        ..strokeWidth = lineWidth
        ..style = PaintingStyle.stroke
        ..strokeCap = lineCap
        ..strokeJoin = StrokeJoin.round
        ..maskFilter = const MaskFilter.blur(BlurStyle.normal, 2.0),
    );

    canvas.drawPath(
      path,
      Paint()
        ..color = color
        ..strokeWidth = lineWidth
        ..style = PaintingStyle.stroke
        ..strokeCap = lineCap
        ..strokeJoin = StrokeJoin.round,
    );
  }

  if (showPoints) {
    for (int i = 0; i < points.length; i++) {
      final currentPoint = points[i];
      final currentScreenPoint = screenPoints[i];
      final currentRadius = currentPoint.radius ?? pointRadius;
      final currentColor = currentPoint.color ?? color;

      canvas.drawCircle(
        currentScreenPoint,
        currentRadius,
        Paint()..color = currentColor,
      );
      canvas.drawCircle(
        currentScreenPoint,
        currentRadius,
        Paint()
          ..color = Colors.white
          ..style = PaintingStyle.stroke
          ..strokeWidth = 1.2,
      );

      if (currentPoint.label != null) {
        final textPainter = TextPainter(
          text: TextSpan(
            text: currentPoint.label,
            style: TextStyle(
              fontSize: 11,
              color: currentColor,
              fontWeight: FontWeight.w600,
            ),
          ),
          textDirection: TextDirection.ltr,
        )..layout();
        textPainter.paint(canvas, currentScreenPoint + const Offset(8, -14));
      }
    }
  }
}