dataLabelPosition method

  1. @override
Offset dataLabelPosition(
  1. ChartElementParentData current,
  2. ChartDataLabelAlignment alignment,
  3. Size size
)
override

Implementation

@override
Offset dataLabelPosition(ChartElementParentData current,
    ChartDataLabelAlignment alignment, Size size) {
  final num x = current.x! + (sbsInfo.maximum + sbsInfo.minimum) / 2;
  num y = current.y!;
  final int dataPointIndex = current.dataPointIndex;
  final num bottom = _lowValues[dataPointIndex];
  final bool isNegative = yValues[dataPointIndex] < 0;

  if (alignment == ChartDataLabelAlignment.bottom) {
    y = bottom;
  } else if (alignment == ChartDataLabelAlignment.middle) {
    y = (y + bottom) / 2;
  }
  final EdgeInsets margin = dataLabelSettings.margin;
  double translationX = 0.0;
  double translationY = 0.0;
  switch (alignment) {
    case ChartDataLabelAlignment.auto:
    case ChartDataLabelAlignment.outer:
      if (isTransposed) {
        translationX = isNegative
            ? -(dataLabelPadding + size.width + margin.horizontal)
            : dataLabelPadding;
        translationY = -margin.top;
      } else {
        translationX = -margin.left;
        translationY = isNegative
            ? 0
            : -(dataLabelPadding + size.height + margin.vertical);
      }
      return translateTransform(x, y, translationX, translationY);

    case ChartDataLabelAlignment.bottom:
      if (isTransposed) {
        translationX = isNegative
            ? -(dataLabelPadding + size.width + margin.horizontal)
            : dataLabelPadding;
        translationY = -margin.top;
      } else {
        translationX = -margin.left;
        translationY = isNegative
            ? dataLabelPadding
            : -(dataLabelPadding + size.height + margin.vertical);
      }
      return translateTransform(x, y, translationX, translationY);

    case ChartDataLabelAlignment.top:
      if (isTransposed) {
        translationX = isNegative
            ? 0
            : -(dataLabelPadding + size.width + margin.horizontal);
        translationY = -margin.top;
      } else {
        translationX = -margin.left;
        translationY = isNegative
            ? -(dataLabelPadding + size.height + margin.vertical)
            : dataLabelPadding;
      }
      return translateTransform(x, y, translationX, translationY);

    case ChartDataLabelAlignment.middle:
      final WaterfallSegment<T, D> segment =
          segments[dataPointIndex] as WaterfallSegment<T, D>;
      final Offset center = segment.segmentRect!.center;
      if (isTransposed) {
        translationX = -margin.left - size.width / 2;
        translationY = -margin.top;
      } else {
        translationX = -margin.left;
        translationY = -margin.top - size.height / 2;
      }
      return center.translate(translationX, translationY);
  }
}