drawSegment method

dynamic drawSegment(
  1. CanvasRenderingContext2D ctx,
  2. PieChartItem item,
  3. double start,
  4. double total,
)

Implementation

drawSegment(CanvasRenderingContext2D ctx, PieChartItem item, double start, double total) {
  ctx.save();
  var centerX = (width / 2).floor();
  var centerY = (height / 2).floor();

  var radius = (min(width, height) / 2) * 0.8;
  var startingAngle = degreesToRadians((start / total) * 360 - 90.0);
  var arcSize = degreesToRadians((item.weight / total) * 360);
  var endingAngle = startingAngle + arcSize;

  ctx.beginPath();
  ctx.moveTo(centerX, centerY);
  ctx.arc(centerX, centerY, radius,
  startingAngle, endingAngle, false);
  ctx.closePath();

  ctx.fillStyle = item.color;
  ctx.fill();
  ctx.restore();

  this.drawSegmentLabel(ctx, item, start, total);
}