computePieDataBounds function

Rect computePieDataBounds(
  1. List<DataPoint> pies,
  2. double offset, {
  3. double eps = 0.0001,
})

Data-space bounds of pie centerlines and offsets.

Visual thickness, borders, corner radii, and markers are intentionally not included, matching the bounds behavior of line and bar charts. eps is the minimum radius and angular span to include.

Implementation

Rect computePieDataBounds(
  List<DataPoint> pies,
  double offset, {
  double eps = 0.0001,
}) {
  Rect? bounds;

  for (final pie in pies) {
    if (pie.x <= eps || pie.dy <= eps) {
      continue;
    }

    final center = toCartesian(pie.pieOffset?.pieOffset ?? offset, pie);
    final start = pie.y;
    final end = pie.fy;
    final angles = <double>[start, end];

    if (end - start >= 2 * pi) {
      angles.addAll(const [0.0, pi / 2, pi, 3 * pi / 2]);
    } else {
      final firstQuarter = (start / (pi / 2)).ceil();
      final lastQuarter = (end / (pi / 2)).floor();
      for (var quarter = firstQuarter; quarter <= lastQuarter; quarter++) {
        angles.add(quarter * pi / 2);
      }
    }

    for (final angle in angles) {
      final point = center + Offset(pie.x * cos(angle), pie.x * sin(angle));
      final pointBounds = Rect.fromLTWH(point.dx, point.dy, 0, 0);
      bounds =
          bounds == null ? pointBounds : bounds.expandToInclude(pointBounds);
    }
  }

  return bounds ?? const Rect.fromLTRB(0, 0, 1, 1);
}