protected virtual void ComputeSize() { var longest = XAxis.GetLongestLabel(); AxisLabelPaint.Typeface = XAxis.Typeface; AxisLabelPaint.TextSize = XAxis.TextSize; var labelSize = AxisLabelPaint.Measure(longest); float labelWidth = labelSize.Width; float labelHeight = AxisLabelPaint.MeasureHeight("Q"); var labelRotatedSize = ChartUtil.GetSizeOfRotatedRectangleByDegrees( labelWidth, labelHeight, XAxis.LabelRotationAngle); XAxis.LabelWidth = (int)Math.Round(labelWidth); XAxis.LabelHeight = (int)Math.Round(labelHeight); XAxis.LabelRotatedWidth = (int)MathF.Round(labelRotatedSize.Width); XAxis.LabelRotatedHeight = (int)MathF.Round(labelRotatedSize.Height); }
public static void DrawXAxisValue(SKCanvas c, string text, float x, float y, SKPaint paint, SKPoint anchor, float angleDegrees) { float drawOffsetX = 0.0f; float drawOffsetY = 0.0f; SKRect bounds = SKRect.Empty; float lineHeight = paint.GetFontMetrics(out SKFontMetrics fontMatrics); paint.MeasureText(text, ref bounds); // Android sometimes has pre-padding drawOffsetX -= bounds.Left; // Android does not snap the bounds to line boundaries, // and draws from bottom to top. // And we want to normalize it. drawOffsetY += -fontMatrics.Ascent; // To have a consistent point of reference, we always draw left-aligned var originalTextAlign = paint.TextAlign; paint.TextAlign = SKTextAlign.Left; if (angleDegrees != 0.0f) { // Move the text drawing rect in a way that it always rotates around its center drawOffsetX -= bounds.Width * 0.5f; drawOffsetY -= lineHeight * 0.5f; float translateX = x; float translateY = y; // Move the "outer" rect relative to the anchor, assuming its centered if (anchor.X != 0.5f || anchor.Y != 0.5f) { var rotatedSize = ChartUtil.GetSizeOfRotatedRectangleByDegrees( bounds.Width, lineHeight, angleDegrees); translateX -= rotatedSize.Width * (anchor.X - 0.5f); translateY -= rotatedSize.Height * (anchor.Y - 0.5f); } c.Save(); c.Translate(translateX, translateY); c.RotateDegrees(angleDegrees); c.DrawText(text, drawOffsetX, drawOffsetY, paint); c.Restore(); } else { if (anchor.X != 0.0f || anchor.Y != 0.0f) { drawOffsetX -= bounds.Width * anchor.X; drawOffsetY -= lineHeight * anchor.Y; } drawOffsetX += x; drawOffsetY += y; c.DrawText(text, drawOffsetX, drawOffsetY, paint); } paint.TextAlign = originalTextAlign; }