layout method

  1. @override
void layout(
  1. Context context,
  2. BoxConstraints constraints, {
  3. bool parentUsesSize = false,
})
override

First widget pass to calculate the children layout and bounding box

Implementation

@override
void layout(Context context, BoxConstraints constraints,
    {bool parentUsesSize = false}) {
  super.layout(context, constraints, parentUsesSize: parentUsesSize);

  final datasets = Chart.of(context).datasets;
  final size = constraints.biggest;

  final _gridBox = PdfRect(0, 0, size.x, size.y);

  var _total = 0.0;

  for (final dataset in datasets) {
    assert(dataset is PieDataSet, 'Use only PieDataset with a PieGrid');
    if (dataset is PieDataSet) {
      _total += dataset.value;
    }
  }

  final unit = pi / _total * 2;
  var angle = startAngle;

  for (final dataset in datasets) {
    if (dataset is PieDataSet) {
      dataset.angleStart = angle;
      angle += dataset.value * unit;
      dataset.angleEnd = angle;
    }
  }

  _radius = min(_gridBox.width / 2, _gridBox.height / 2);
  var reduce = false;

  do {
    reduce = false;
    for (final dataset in datasets) {
      if (dataset is PieDataSet) {
        dataset.layout(context, BoxConstraints.tight(_gridBox.size));
        assert(dataset.box != null);
        if (_radius > 20 &&
            (dataset.box!.width > _gridBox.width ||
                dataset.box!.height > _gridBox.height)) {
          _radius -= 10;
          reduce = true;
          break;
        }
      }
    }
  } while (reduce);
}