private static void RenderLabel(CommonElements common, ChartGraphics graph, CategoryNode node, DataPoint dataPoint, DataPointAttributes dataPointAttributes, string text, float startAngle, float sweepAngle, PointF centerAbsolute, float radiusAbsolute, int dataPointIndex, GraphicsPath sliceGraphicsPath) { if (string.IsNullOrEmpty(text)) { return; } SizeF size = graph.MeasureString(text.Replace("\\n", "\n"), dataPointAttributes.Font, new SizeF(1000f, 1000f), new StringFormat(StringFormat.GenericTypographic)); SizeF relativeSize = graph.GetRelativeSize(size); float num = relativeSize.Width / (float)text.Length; float num2 = relativeSize.Width + num; float num3 = relativeSize.Height + relativeSize.Height / 8f; float sliceCenterAngle = GetSliceCenterAngle(startAngle, sweepAngle); float labelAngle = GetLabelAngle(sliceCenterAngle); PointF sliceCenterRelative = GetSliceCenterRelative(graph, sliceCenterAngle, centerAbsolute, radiusAbsolute); RectangleF resizedRect = new RectangleF(sliceCenterRelative.X - num2 / 2f, sliceCenterRelative.Y - num3 / 2f, num2, num3); if (resizedRect.IsEmpty) { return; } int num4 = (int)labelAngle + dataPointAttributes.FontAngle; if (graph.CanLabelFitInSlice(sliceGraphicsPath, resizedRect, num4) || CanFitInResizedArea(text, dataPointAttributes.Font, relativeSize, sliceCenterRelative, graph, sliceGraphicsPath, resizedRect, num4, radiusAbsolute, out resizedRect)) { if (dataPoint != null) { graph.DrawPointLabelStringRel(common, text, dataPoint.Font, new SolidBrush(dataPoint.FontColor), resizedRect, format, (int)labelAngle + dataPoint.FontAngle, resizedRect, dataPoint.LabelBackColor, dataPoint.LabelBorderColor, dataPoint.LabelBorderWidth, dataPoint.LabelBorderStyle, dataPoint.series, dataPoint, dataPointIndex); return; } graph.DrawLabelBackground(num4, sliceCenterRelative, resizedRect, dataPointAttributes.LabelBackColor, dataPointAttributes.LabelBorderColor, dataPointAttributes.LabelBorderWidth, dataPointAttributes.LabelBorderStyle); graph.MapCategoryNodeLabel(common, node, resizedRect); graph.DrawStringRel(text, dataPointAttributes.Font, new SolidBrush(dataPointAttributes.FontColor), resizedRect, format, num4); } }
private static bool CanFitInResizedArea(string text, Font textFont, SizeF relativeSize, PointF sliceCenterRelative, ChartGraphics graph, GraphicsPath sliceGraphicsPath, RectangleF labelRelativeRect, int labelRotationAngle, float radiusAbsolute, out RectangleF resizedRect) { float num = relativeSize.Width / (float)text.Length; float num2 = relativeSize.Height / 8f; float num3 = relativeSize.Width + num; float width = labelRelativeRect.Width; float height = relativeSize.Height; resizedRect = labelRelativeRect; for (int i = 2; i <= 4; i++) { float num4 = num3 / (float)i + num; float num5 = height * (float)i + num2; labelRelativeRect = new RectangleF(sliceCenterRelative.X - num4 / 2f, sliceCenterRelative.Y - num5 / 2f, num4, num5); if (graph.CanLabelFitInSlice(sliceGraphicsPath, labelRelativeRect, labelRotationAngle)) { labelRelativeRect.Width = FindOptimalWidth(width, graph, sliceGraphicsPath, labelRelativeRect, labelRotationAngle); StringFormat stringFormat = new StringFormat(StringFormat.GenericTypographic); stringFormat.FormatFlags = format.FormatFlags; graph.MeasureString(text.Replace("\\n", "\n"), textFont, labelRelativeRect.Size, stringFormat, out int charactersFitted, out int _); if (charactersFitted == text.Length) { resizedRect = labelRelativeRect; return(true); } } } return(false); }
private static bool CanFitInResizedArea(string text, Font textFont, SizeF relativeSize, PointF sliceCenterRelative, ChartGraphics graph, GraphicsPath sliceGraphicsPath, RectangleF labelRelativeRect, int labelRotationAngle, float radiusAbsolute, out RectangleF resizedRect) { float num = relativeSize.Width / (float)text.Length; float num2 = (float)(relativeSize.Height / 8.0); float num3 = relativeSize.Width + num; float width = labelRelativeRect.Width; float height = relativeSize.Height; resizedRect = labelRelativeRect; for (int i = 2; i <= 4; i++) { float num4 = num3 / (float)i + num; float num5 = height * (float)i + num2; labelRelativeRect = new RectangleF((float)(sliceCenterRelative.X - num4 / 2.0), (float)(sliceCenterRelative.Y - num5 / 2.0), num4, num5); if (graph.CanLabelFitInSlice(sliceGraphicsPath, labelRelativeRect, labelRotationAngle)) { labelRelativeRect.Width = SunburstChart.FindOptimalWidth(width, graph, sliceGraphicsPath, labelRelativeRect, labelRotationAngle); StringFormat stringFormat = new StringFormat(StringFormat.GenericTypographic); stringFormat.FormatFlags = SunburstChart.format.FormatFlags; int num6 = default(int); int num7 = default(int); ((ChartRenderingEngine)graph).MeasureString(text.Replace("\\n", "\n"), textFont, labelRelativeRect.Size, stringFormat, out num6, out num7); if (num6 == text.Length) { resizedRect = labelRelativeRect; return(true); } } } return(false); }
private static float FindOptimalWidth(float maxWidth, ChartGraphics graph, GraphicsPath sliceGraphicsPath, RectangleF labelRelativeRect, int labelRotationAngle) { RectangleF labelRelativeRect2 = new RectangleF(labelRelativeRect.Location, labelRelativeRect.Size); float width = labelRelativeRect.Width; int num = 4; while (graph.CanLabelFitInSlice(sliceGraphicsPath, labelRelativeRect2, labelRotationAngle) && num-- >= 0) { width = labelRelativeRect2.Width; labelRelativeRect2.Width = width + (maxWidth - width) / 2f; } return(width); }