drawSegment method

void drawSegment(
  1. Canvas canvas,
  2. FastChartData datum,
  3. double startAngle,
  4. double sweepAngle,
  5. Paint paint,
  6. Rect rect,
  7. double radius,
  8. double labelRadius,
)

Implementation

void drawSegment(
  Canvas canvas,
  FastChartData datum,
  double startAngle,
  double sweepAngle,
  Paint paint,
  Rect rect,
  double radius,
  double labelRadius,
) {
  canvas.drawArc(
    rect,
    degreesToRadian(startAngle),
    degreesToRadian(sweepAngle),
    paint.style == PaintingStyle.fill,
    paint,
  );

  if (showLabel && datum.value >= labelValueThreshold) {
    // Draw the percentage text in the middle of the segment
    final textPainter = TextPainter(
      text: TextSpan(
        text: formatPercentage(
          value: datum.value,
          maximumFractionDigits: 1,
          minimumFractionDigits: 0,
        ),
        style: const TextStyle(
          fontWeight: kFastFontWeightLight,
          fontSize: kFastFontSize10,
          color: Colors.white,
        ),
      ),
      textDirection: TextDirection.ltr,
    )..layout();

    final labelAngle = startAngle + sweepAngle / 2;
    final labelX = radius + labelRadius * cos(degreesToRadian(labelAngle));
    final labelY = radius + labelRadius * sin(degreesToRadian(labelAngle));

    textPainter.paint(
      canvas,
      Offset(labelX - textPainter.width / 2, labelY - textPainter.height / 2),
    );
  }
}