drawValues method

  1. @override
void drawValues(
  1. Canvas c
)
override

Draws the value and the entry label of each slice, either inside the slice or outside it at the end of a two-part value line, and the entry icon at the label radius.

Implementation

@override
void drawValues(ui.Canvas c) {
  final center = chart.centerCircleBox;

  final radius = chart.radius;
  var rotationAngle = chart.rotationAngle;
  final drawAngles = chart.drawAngles;
  final absoluteAngles = chart.absoluteAngles;

  final phaseX = animator.phaseX;
  final phaseY = animator.phaseY;

  final roundedRadius = (radius - (radius * chart.holeRadius / 100)) / 2;
  final holeRadiusPercent = chart.holeRadius / 100;
  var labelRadiusOffset = radius / 10 * 3.6;

  if (chart.isDrawHoleEnabled) {
    labelRadiusOffset = (radius - (radius * holeRadiusPercent)) / 2;

    if (!chart.isDrawSlicesUnderHoleEnabled &&
        chart.isDrawRoundedSlicesEnabled) {
      // shift by the rounded slice so the label sits inside it
      final roundedAngle = roundedRadius * 360 / (math.pi * 2 * radius);
      rotationAngle += chart.isRoundedSlicesReversed
          ? -roundedAngle
          : roundedAngle;
    }
  }

  final labelRadius = radius - labelRadiusOffset;

  final data = chart.data;
  if (data == null) {
    MPPointF.recycleInstance(center);
    return;
  }
  final dataSets = data.dataSets;

  final yValueSum = chart.absoluteValueSum;

  final drawEntryLabels = chart.isDrawEntryLabelsEnabled;

  double angle;
  var xIndex = 0;

  c.save();

  final offset = Utils.convertDpToPixel(5);

  for (var i = 0; i < dataSets.length; i++) {
    final dataSet = dataSets[i];

    final drawValues = dataSet.isDrawValuesEnabled;

    if (!drawValues && !drawEntryLabels) continue;

    final xValuePosition = dataSet.xValuePosition;
    final yValuePosition = dataSet.yValuePosition;

    applyValueTextStyle(dataSet);

    final lineHeight =
        Utils.calcTextHeight(valuePaint, 'Q') + Utils.convertDpToPixel(4);

    final formatter = dataSet.valueFormatter;

    final entryCount = dataSet.entryCount;

    final isUseValueColorForLineEnabled =
        dataSet.isUseValueColorForLineEnabled;
    final valueLineColor = dataSet.valueLineColor;

    valueLinePaint.strokeWidth = Utils.convertDpToPixel(
      dataSet.valueLineWidth,
    );

    final sliceSpace = getSliceSpace(dataSet);

    final iconsOffset = MPPointF.getInstanceFrom(dataSet.iconsOffset);
    iconsOffset.x = Utils.convertDpToPixel(iconsOffset.x);
    iconsOffset.y = Utils.convertDpToPixel(iconsOffset.y);

    for (var j = 0; j < entryCount; j++) {
      if (xIndex >= drawAngles.length || xIndex >= absoluteAngles.length) {
        break;
      }

      final entry = dataSet.getEntryForIndex(j);

      angle = xIndex == 0 ? 0 : absoluteAngles[xIndex - 1] * phaseX;

      final sliceAngle = drawAngles[xIndex];
      final sliceSpaceMiddleAngle =
          sliceSpace / (Utils.FDEG2RAD * labelRadius);

      final angleOffset = (sliceAngle - sliceSpaceMiddleAngle / 2) / 2;

      angle += angleOffset;

      final transformedAngle = rotationAngle + angle * phaseY;

      final value = !chart.isUsePercentValuesEnabled
          ? entry.y
          : yValueSum == 0
          ? 0.0
          : entry.y / yValueSum * 100;
      final entryLabel = entry.label;

      final sliceXBase = math.cos(transformedAngle * Utils.FDEG2RAD);
      final sliceYBase = math.sin(transformedAngle * Utils.FDEG2RAD);

      final drawXOutside =
          drawEntryLabels && xValuePosition == ValuePosition.OUTSIDE_SLICE;
      final drawYOutside =
          drawValues && yValuePosition == ValuePosition.OUTSIDE_SLICE;
      final drawXInside =
          drawEntryLabels && xValuePosition == ValuePosition.INSIDE_SLICE;
      final drawYInside =
          drawValues && yValuePosition == ValuePosition.INSIDE_SLICE;

      if (drawXOutside || drawYOutside) {
        final valueLineLength1 = dataSet.valueLinePart1Length;
        final valueLineLength2 = dataSet.valueLinePart2Length;
        final valueLinePart1OffsetPercentage =
            dataSet.valueLinePart1OffsetPercentage / 100;

        final double pt2x;
        final double pt2y;
        final double labelPtx;
        final double labelPty;

        final line1Radius = chart.isDrawHoleEnabled
            ? (radius - (radius * holeRadiusPercent)) *
                      valueLinePart1OffsetPercentage +
                  (radius * holeRadiusPercent)
            : radius * valueLinePart1OffsetPercentage;

        final polyline2Width = dataSet.isValueLineVariableLength
            ? labelRadius *
                  valueLineLength2 *
                  math.sin(transformedAngle * Utils.FDEG2RAD).abs()
            : labelRadius * valueLineLength2;

        final pt0x = line1Radius * sliceXBase + center.x;
        final pt0y = line1Radius * sliceYBase + center.y;

        final pt1x =
            labelRadius * (1 + valueLineLength1) * sliceXBase + center.x;
        final pt1y =
            labelRadius * (1 + valueLineLength1) * sliceYBase + center.y;

        if (transformedAngle.remainder(360.0) >= 90.0 &&
            transformedAngle.remainder(360.0) <= 270.0) {
          pt2x = pt1x - polyline2Width;
          pt2y = pt1y;

          valuePaint.textAlign = PaintAlign.RIGHT;

          if (drawXOutside) paintEntryLabels.textAlign = PaintAlign.RIGHT;

          labelPtx = pt2x - offset;
          labelPty = pt2y;
        } else {
          pt2x = pt1x + polyline2Width;
          pt2y = pt1y;
          valuePaint.textAlign = PaintAlign.LEFT;

          if (drawXOutside) paintEntryLabels.textAlign = PaintAlign.LEFT;

          labelPtx = pt2x + offset;
          labelPty = pt2y;
        }

        var lineColor = ColorTemplate.COLOR_NONE;

        if (isUseValueColorForLineEnabled) {
          lineColor = dataSet.getColor(j);
        } else if (valueLineColor != ColorTemplate.COLOR_NONE) {
          lineColor = valueLineColor;
        }

        if (lineColor != ColorTemplate.COLOR_NONE) {
          valueLinePaint.color = lineColor;
          _drawLine(c, pt0x, pt0y, pt1x, pt1y, valueLinePaint);
          _drawLine(c, pt1x, pt1y, pt2x, pt2y, valueLinePaint);
        }

        if (drawXOutside && drawYOutside) {
          drawValue(
            c,
            formatter,
            value,
            entry,
            0,
            labelPtx,
            labelPty,
            dataSet.getValueTextColor(j),
          );

          if (j < data.entryCount && entryLabel != null) {
            final labelY =
                labelPty +
                lineHeight +
                _extraLineShift(formatter, value, entry, entryLabel);
            drawEntryLabel(c, entryLabel, labelPtx, labelY);
          }
        } else if (drawXOutside) {
          if (j < data.entryCount && entryLabel != null) {
            drawEntryLabel(
              c,
              entryLabel,
              labelPtx,
              labelPty + lineHeight / 2,
            );
          }
        } else if (drawYOutside) {
          drawValue(
            c,
            formatter,
            value,
            entry,
            0,
            labelPtx,
            labelPty + lineHeight / 2,
            dataSet.getValueTextColor(j),
          );
        }
      }

      if (drawXInside || drawYInside) {
        final x = labelRadius * sliceXBase + center.x;
        final y = labelRadius * sliceYBase + center.y;

        valuePaint.textAlign = PaintAlign.CENTER;

        if (drawXInside && drawYInside) {
          drawValue(
            c,
            formatter,
            value,
            entry,
            0,
            x,
            y,
            dataSet.getValueTextColor(j),
          );

          if (j < data.entryCount && entryLabel != null) {
            drawEntryLabel(
              c,
              entryLabel,
              x,
              y +
                  lineHeight +
                  _extraLineShift(formatter, value, entry, entryLabel),
            );
          }
        } else if (drawXInside) {
          if (j < data.entryCount && entryLabel != null) {
            drawEntryLabel(c, entryLabel, x, y + lineHeight / 2);
          }
        } else if (drawYInside) {
          drawValue(
            c,
            formatter,
            value,
            entry,
            0,
            x,
            y + lineHeight / 2,
            dataSet.getValueTextColor(j),
          );
        }
      }

      final icon = entry.icon;
      if (icon != null && dataSet.isDrawIconsEnabled) {
        final x = (labelRadius + iconsOffset.y) * sliceXBase + center.x;
        var y = (labelRadius + iconsOffset.y) * sliceYBase + center.y;
        y += iconsOffset.x;

        Utils.drawImage(
          c,
          icon,
          truncateToDevicePx(x),
          truncateToDevicePx(y),
          icon.intrinsicWidth,
          icon.intrinsicHeight,
        );
      }

      xIndex++;
    }

    MPPointF.recycleInstance(iconsOffset);
  }
  MPPointF.recycleInstance(center);
  c.restore();
}