drawSegmentLabel method

void drawSegmentLabel(
  1. CanvasRenderingContext2D ctx,
  2. int idx,
  3. PieChartItem item,
  4. double start,
)

Implementation

void drawSegmentLabel(CanvasRenderingContext2D ctx, int idx, PieChartItem item, double start) {
  ctx.save();
  var angle = degreesToRadians((start / totalWeight) * 360 - 90);

  ctx.translate(centerX, centerY);
  ctx.rotate(angle);
  var dx = (min(width, height) * 0.5).floor() * 0.8 - 5;
  var dy = (height * 0.05).floor();

  ctx.textAlign = "right";
  int fontSize = min(11, (height / 25)).floor();
  ctx.font = "${item.isActive ? "bold " : ''}${fontSize}pt Helvetica";
  ctx.fillStyle = 'black'.toJS;

  ctx.fillText(item.shortLabel, dx, dy);

  ctx.restore();

  ctx.fillStyle = (item.color ?? '').toJS;
  //ctx.fillRect(width - legendWidth, height - legendHeight, legendWidth, legendHeight);
  //ctx.fillText(item.label, 10, 10);
  int legendSize = 5;
  if (legend) {
    int x = width - legendWidth + 5;
    int y = height - legendHeight + (idx * 20) + 10;
    ctx.fillStyle = (item.color ?? '').toJS;
    legendSize = item.isActive ? 7 : 5;
    ctx.fillRect(x - legendSize, y - legendSize, 2 * legendSize, 2 * legendSize);

    ctx.textAlign = "left";
    ctx.fillStyle = 'black'.toJS;
    ctx.font = (item.isActive ? "bold " : '') + legendFont;

    ctx.fillText(legendLabel(item), x + 10, y + 4);
  }
}