calculateWidthForRotatedLabel method

double calculateWidthForRotatedLabel(
  1. int rotation,
  2. double labelHeight,
  3. double labelLength
)

Returns the width of a rotated labels on a domain axis.

Implementation

double calculateWidthForRotatedLabel(
    int rotation, double labelHeight, double labelLength) {
  if (rotation == 0) return labelLength;
  var rotationRadian = _degToRad(rotation.toDouble());

  // Imagine a right triangle with a base that is parallel to the axis
  // baseline. The side of this triangle that is perpendicular to the baseline
  // is the height of the axis we wish to calculate. The hypotenuse of the
  // triangle is the given length of the tick labels, labelLength. The angle
  // between the perpendicular line and the hypotenuse (the tick label) is 90
  // - the label rotation angle, since the tick label transformation is
  // applied relative to the axis baseline. Given this triangle, we can
  // calculate the height of the axis by using the cosine of this angle.

  // The triangle assumes a zero-height line for the labels, but the actual
  // rendered text will be drawn above and below this center line. To account
  // for this, extend the label length by using a triangle with half the
  // height of the label.
  labelLength += labelHeight / 2.0 * tan(rotationRadian);

  // To compute the label width, we need the angle between the label and a
  // line perpendicular to the axis baseline, in radians.
  return labelLength * cos(rotationRadian);
}