dataLabelPosition method

Offset dataLabelPosition(
  1. CircularDataLabelBoxParentData current,
  2. Size size
)

Implementation

Offset dataLabelPosition(CircularDataLabelBoxParentData current, Size size) {
  final num angle = dataLabelSettings.angle;
  final int pointIndex = current.dataPointIndex;
  Offset labelLocation;
  const int labelPadding = 2;
  TextStyle dataLabelStyle = parent!.themeData!.textTheme.bodySmall!
    ..merge(chartThemeData!.dataLabelTextStyle)
    ..merge(dataLabelSettings.textStyle);
  final CircularChartPoint point = current.point!;
  if (point.isExplode) {
    point.center = calculateExplodingCenter(point.midAngle!,
        point.outerRadius!.toDouble(), point.center!, point.explodeOffset);
  }
  if (point.isVisible && (point.y != 0 || dataLabelSettings.showZeroValue)) {
    dataLabelStyle = dataLabelStyle.copyWith(
        color: dataLabelStyle.color ??
            saturatedTextColor(
                findThemeColor(this, point, dataLabelSettings)));

    if (dataLabelSettings.labelPosition == ChartDataLabelPosition.inside) {
      labelLocation = calculateOffset(point.midAngle!,
          (point.innerRadius! + point.outerRadius!) / 2, point.center!);
      labelLocation = Offset(
          labelLocation.dx -
              (size.width / 2) +
              (angle == 0 ? 0 : size.width / 2),
          labelLocation.dy -
              (size.height / 2) +
              (angle == 0 ? 0 : size.height / 2));
      point.labelRect = Rect.fromLTWH(
          labelLocation.dx - labelPadding,
          labelLocation.dy - labelPadding,
          size.width + (2 * labelPadding),
          size.height + (2 * labelPadding));
      bool isDataLabelCollide =
          findingCollision(point.labelRect, renderDataLabelRegions);
      // TODO(Lavanya): Recheck here.
      if (dataLabelSettings.labelIntersectAction ==
          LabelIntersectAction.hide) {
        point.isVisible = !isDataLabelCollide;
      }

      if (dataLabelSettings.builder == null) {
        // TODO(Lavanya): Get text from DataLabelBoxParentData;
        String label = point.text!;
        point.overflowTrimmedText = point.overflowTrimmedText ?? label;

        if (dataLabelSettings.overflowMode == OverflowMode.shift) {
          final String labelText = segmentOverflowTrimmedText(
              this,
              point.text!,
              size,
              point,
              point.labelRect,
              center,
              labelLocation,
              dataLabelSettings.overflowMode,
              dataLabelStyle);
          if (labelText.contains('...') || labelText.isEmpty) {
            isDataLabelCollide = true;
            point.renderPosition = ChartDataLabelPosition.outside;
          }
          point.text = isDataLabelCollide ? point.text : labelText;
        } else if (dataLabelSettings.overflowMode == OverflowMode.trim &&
            !point.text!.contains('...')) {
          point.text = segmentOverflowTrimmedText(
              this,
              point.text!,
              size,
              point,
              point.labelRect,
              center,
              labelLocation,
              dataLabelSettings.overflowMode,
              dataLabelStyle);
          label = point.text!;
          final Size trimmedTextSize = measureText(label, dataLabelStyle);
          labelLocation = calculateOffset(point.midAngle!,
              (point.innerRadius! + point.outerRadius!) / 2, point.center!);
          labelLocation = Offset(
              labelLocation.dx -
                  (trimmedTextSize.width / 2) +
                  (angle == 0 ? 0 : trimmedTextSize.width / 2),
              labelLocation.dy -
                  (trimmedTextSize.height / 2) +
                  (angle == 0 ? 0 : trimmedTextSize.height / 2));
          point.labelRect = Rect.fromLTWH(
              labelLocation.dx - labelPadding,
              labelLocation.dy - labelPadding,
              trimmedTextSize.width + (2 * labelPadding),
              trimmedTextSize.height + (2 * labelPadding));
        }
      }

      if (dataLabelSettings.labelIntersectAction ==
              LabelIntersectAction.shift &&
          isDataLabelCollide &&
          dataLabelSettings.overflowMode != OverflowMode.trim) {
        point.saturationRegionOutside = true;
        point.renderPosition = ChartDataLabelPosition.outside;
        renderOutsideDataLabel(point, size, pointIndex, this, index,
            dataLabelStyle, renderDataLabelRegions);
      } else if (((dataLabelSettings.labelIntersectAction ==
                      LabelIntersectAction.shift &&
                  dataLabelSettings.overflowMode == OverflowMode.none) &&
              isDataLabelCollide &&
              dataLabelSettings.overflowMode != OverflowMode.trim) ||
          (isDataLabelCollide &&
              dataLabelSettings.overflowMode == OverflowMode.shift)) {
        point.saturationRegionOutside = true;
        point.renderPosition = ChartDataLabelPosition.outside;
        renderOutsideDataLabel(point, size, pointIndex, this, index,
            dataLabelStyle, renderDataLabelRegions);
      } else if (!isDataLabelCollide ||
          (dataLabelSettings.labelIntersectAction ==
                  LabelIntersectAction.none &&
              dataLabelSettings.overflowMode == OverflowMode.none)) {
        point.renderPosition = ChartDataLabelPosition.inside;
        // TODO(Lavanya): Apply saturation color with
        //DataLabelRender callback.

        if (!isDataLabelCollide &&
            (dataLabelSettings.labelIntersectAction ==
                    LabelIntersectAction.shift &&
                dataLabelSettings.overflowMode != OverflowMode.hide)) {
          renderDataLabelRegions.add(point.labelRect);
          point.labelLocation = labelLocation;
        } else if (!isDataLabelCollide &&
            (dataLabelSettings.labelIntersectAction ==
                    LabelIntersectAction.hide ||
                dataLabelSettings.overflowMode == OverflowMode.hide)) {
          if (point.renderPosition == ChartDataLabelPosition.inside &&
              (dataLabelSettings.overflowMode == OverflowMode.hide)) {
            point.text = segmentOverflowTrimmedText(
                this,
                point.text!,
                size,
                point,
                point.labelRect,
                center,
                labelLocation,
                dataLabelSettings.overflowMode,
                dataLabelStyle);
            // label = point.text!;
          }

          point.labelLocation = labelLocation;
          // TODO(Lavanya): drawLabel method add renderDataLabelRegions.
          if (dataLabelSettings.labelIntersectAction !=
              LabelIntersectAction.shift) {
            renderDataLabelRegions.add(point.labelRect);
          }
        } else {
          point.labelLocation = labelLocation;
          // TODO(Lavanya): drawLabel method add renderDataLabelRegions.
          if (dataLabelSettings.labelIntersectAction !=
              LabelIntersectAction.shift) {
            renderDataLabelRegions.add(point.labelRect);
          }
        }
      }
    } else {
      point.renderPosition = ChartDataLabelPosition.outside;
      dataLabelStyle = dataLabelStyle.copyWith(
          color: dataLabelStyle.color ??
              saturatedTextColor(
                  findThemeColor(this, point, dataLabelSettings)));
      renderOutsideDataLabel(point, size, pointIndex, this, index,
          dataLabelStyle, renderDataLabelRegions);
    }
  } else {
    point.labelRect = Rect.zero;
    point.isVisible = false;
  }

  return point.labelLocation;
}