drawLegend method

void drawLegend(
  1. Canvas canvas,
  2. int petalNum,
  3. double scaleFactor
)

Implementation

void drawLegend(Canvas canvas, int petalNum, double scaleFactor) {
  double startAngleInRad =
      (petalNum * rotateAngle - initialRotateOffset) * DEGREE_IN_RAD;
  double halfRotateAngleInRad = rotateAngle * DEGREE_IN_RAD / 2;

  double startX = canvasCx +
      (scaleFactor *
          petalInnerRadius *
          math.cos(startAngleInRad + halfRotateAngleInRad));
  double startY = canvasCy +
      (scaleFactor *
          petalInnerRadius *
          math.sin(startAngleInRad + halfRotateAngleInRad));

  double endX = startX +
      (1.2 - scaleFactor) * petalOuterRadius * math.cos(startAngleInRad);
  double endY = startY +
      (1.2 - scaleFactor) * petalOuterRadius * math.sin(startAngleInRad);

  double rdx = textMeasureRect.width / 2 + 10;
  if (endX < canvasCx) {
    rdx = -rdx;
  }

  legendPath.reset();
  legendPath.moveTo(startX, startY);
  legendPath.lineTo(endX, endY);
  legendPath.relativeLineTo(rdx, 0);

  if (legendColor != 0) {
    legendLinePaint.color = Color(legendColor.toInt());
  } else {
    legendLinePaint.color = shadeColor(endColors[petalNum],
        viewStyle == ViewStyle.DOMINANT_PURPLE ? 0.05 : 0.55);
    legendLinePaint.style = PaintingStyle.stroke;
    legendLinePaint.strokeWidth = 2;
  }
  canvas.drawPath(legendPath, legendLinePaint);

  if (percentageTextColor != 0) {
    solidColorPaint.color = Color(percentageTextColor.toInt());
  } else {
    solidColorPaint.color = endColors[petalNum];
  }

  TextPainter percentagePainter = TextPainter(
    textAlign: TextAlign.center,
    text: TextSpan(
        text: "${subjectPercentages[petalNum]} %",
        style: TextStyle(
            fontSize: 12,
            fontWeight: FontWeight.normal,
            color: solidColorPaint.color)),
    textDirection: TextDirection.ltr,
  );
  percentagePainter.layout();
  percentagePainter.paint(
    canvas,
    Offset(endX + rdx - 10, endY - (TextFontSize)),
  );

  if (subjectTextColor != 0) {
    solidColorPaint.color = Color(subjectTextColor.toInt());
  } else {
    solidColorPaint.color = Colors.black54;
  }

  TextPainter subjectPainter = TextPainter(
    textAlign: TextAlign.center,
    text: TextSpan(
        text: subjects[petalNum],
        style: TextStyle(
            fontSize: 10,
            fontWeight: FontWeight.normal,
            color: solidColorPaint.color)),
    textDirection: TextDirection.ltr,
  );
  subjectPainter.layout();
  subjectPainter.paint(
    canvas,
    Offset(endX + rdx - 20, endY + (TextFontSize * 0.15)),
  );
}