From 80c77fc69a590a6f5ffc7b2ed254768d7ce13d3f Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Thu, 7 Apr 2022 08:35:03 +0800 Subject: [PATCH] [optimize] Pie support startAngle --- Editor/Series/PieEditor.cs | 1 + Runtime/Helper/SerieLabelHelper.cs | 11 +++++---- Runtime/Serie/Pie/PieHandler.cs | 38 ++++++++++++++++++++++++++---- Runtime/Serie/SerieContext.cs | 1 + 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/Editor/Series/PieEditor.cs b/Editor/Series/PieEditor.cs index 393b85c5..4d909ad0 100644 --- a/Editor/Series/PieEditor.cs +++ b/Editor/Series/PieEditor.cs @@ -11,6 +11,7 @@ public override void OnCustomInspectorGUI() PropertyField("m_Gap"); PropertyTwoFiled("m_Center"); PropertyTwoFiled("m_Radius"); + //PropertyField("m_StartAngle"); PropertyFiledMore(() => { diff --git a/Runtime/Helper/SerieLabelHelper.cs b/Runtime/Helper/SerieLabelHelper.cs index f9993d05..984eae2a 100644 --- a/Runtime/Helper/SerieLabelHelper.cs +++ b/Runtime/Helper/SerieLabelHelper.cs @@ -79,6 +79,7 @@ public static void SetGaugeLabelText(Serie serie) public static void UpdatePieLabelPosition(Serie serie, SerieData serieData) { if (serieData.labelObject == null) return; + var startAngle = serie.context.startAngle; var currAngle = serieData.context.halfAngle; var currRad = currAngle * Mathf.Deg2Rad; var offsetRadius = serieData.context.offsetRadius; @@ -105,7 +106,7 @@ public static void UpdatePieLabelPosition(Serie serie, SerieData serieData) var currSin = Mathf.Sin(currRad); var currCos = Mathf.Cos(currRad); var pos0 = new Vector3(serie.context.center.x + radius3 * currSin, serie.context.center.y + radius3 * currCos); - if (currAngle > 180) + if ((currAngle - startAngle) % 360 > 180) { currSin = Mathf.Sin((360 - currAngle) * Mathf.Deg2Rad); currCos = Mathf.Cos((360 - currAngle) * Mathf.Deg2Rad); @@ -113,7 +114,7 @@ public static void UpdatePieLabelPosition(Serie serie, SerieData serieData) var r4 = Mathf.Sqrt(radius1 * radius1 - Mathf.Pow(currCos * radius3, 2)) - currSin * radius3; r4 += labelLine.lineLength1 + labelLine.lineWidth * 4; r4 += serieData.labelObject.label.GetPreferredWidth() / 2; - serieData.context.labelPosition = pos0 + (currAngle > 180 ? Vector3.left : Vector3.right) * r4; + serieData.context.labelPosition = pos0 + ((currAngle - startAngle) % 360 > 180 ? Vector3.left : Vector3.right) * r4; } else { @@ -184,11 +185,11 @@ private static void CheckSerieDataLabel(Serie serie, SerieData serieData, bool i serieData.context.labelPosition = new Vector3(x1, y1); } lastCheckPos = serieData.context.labelPosition; - serieData.labelObject.SetPosition(SerieLabelHelper.GetRealLabelPosition(serieData, serieLabel, labelLine)); + serieData.labelObject.SetPosition(SerieLabelHelper.GetRealLabelPosition(serie, serieData, serieLabel, labelLine)); } } - public static Vector3 GetRealLabelPosition(SerieData serieData, LabelStyle label, LabelLine labelLine) + public static Vector3 GetRealLabelPosition(Serie serie, SerieData serieData, LabelStyle label, LabelLine labelLine) { if (label == null || labelLine == null) return serieData.context.labelPosition; @@ -197,7 +198,7 @@ public static Vector3 GetRealLabelPosition(SerieData serieData, LabelStyle label { var currAngle = serieData.context.halfAngle; var offset = labelLine.lineLength2 + serieData.labelObject.GetLabelWidth() / 2; - if (currAngle > 180) + if ((currAngle - serie.context.startAngle) % 360 > 180) return serieData.context.labelPosition + new Vector3(-offset, 0, 0); else return serieData.context.labelPosition + new Vector3(offset, 0, 0); diff --git a/Runtime/Serie/Pie/PieHandler.cs b/Runtime/Serie/Pie/PieHandler.cs index 8f928f7b..9c95ba4b 100644 --- a/Runtime/Serie/Pie/PieHandler.cs +++ b/Runtime/Serie/Pie/PieHandler.cs @@ -34,7 +34,7 @@ public override void UpdateTooltipSerieParams(int dataIndex, bool showCategory, public override Vector3 GetSerieDataLabelPosition(SerieData serieData, LabelStyle label) { var labelLine = SerieHelper.GetSerieLabelLine(serie, serieData); - return SerieLabelHelper.GetRealLabelPosition(serieData, label, labelLine); + return SerieLabelHelper.GetRealLabelPosition(serie, serieData, label, labelLine); } public override void OnLegendButtonClick(int index, string legendName, bool show) @@ -151,11 +151,12 @@ private void UpdateRuntimeData(Serie serie) { var data = serie.data; serie.context.dataMax = serie.yMax; + serie.context.startAngle = GetStartAngle(serie); var runtimePieDataTotal = serie.yTotal; SerieHelper.UpdateCenter(serie, chart.chartPosition, chart.chartWidth, chart.chartHeight); + float startDegree = serie.context.startAngle; float totalDegree = 0; - float startDegree = 0; float zeroReplaceValue = 0; int showdataCount = 0; foreach (var sd in serie.data) @@ -253,7 +254,7 @@ private void UpdateRuntimeData(Serie serie) private double GetTotalAngle(Serie serie, double dataTotal, ref float totalAngle) { - totalAngle = 360f; + totalAngle = serie.context.startAngle + 360f; if (serie.minAngle > 0) { var rate = serie.minAngle / 360; @@ -435,6 +436,7 @@ private void DrawPieLabelLine(VertexHelper vh, Serie serie, SerieData serieData, Vector3 pos4, pos6; var horizontalLineCircleRadius = labelLine.lineWidth * 4f; var lineCircleDiff = horizontalLineCircleRadius - 0.3f; + var startAngle = serie.context.startAngle; if (currAngle < 90) { var r4 = Mathf.Sqrt(radius1 * radius1 - Mathf.Pow(currCos * radius3, 2)) - currSin * radius3; @@ -467,7 +469,8 @@ private void DrawPieLabelLine(VertexHelper vh, Serie serie, SerieData serieData, pos6 = pos0 + Vector3.left * lineCircleDiff; pos4 = pos6 + Vector3.left * r4; } - var pos5X = currAngle > 180 ? pos2.x - labelLine.lineLength2 : pos2.x + labelLine.lineLength2; + var pos5X = (currAngle - startAngle) % 360 > 180 + ? pos2.x - labelLine.lineLength2 : pos2.x + labelLine.lineLength2; var pos5 = new Vector3(pos5X, pos2.y); switch (labelLine.lineType) { @@ -526,5 +529,32 @@ private bool PointerIsInPieSerie(Serie serie, Vector2 local) return false; } + + private float GetStartAngle(Serie serie) + { + return serie.clockwise ? (serie.startAngle + 360) % 360 : 360 - serie.startAngle; + } + + private float GetToAngle(Serie serie, float angle) + { + var toAngle = angle + serie.startAngle; + if (!serie.clockwise) + { + toAngle = 360 - angle - serie.startAngle; + } + if (!serie.animation.IsFinish()) + { + var currAngle = serie.animation.GetCurrDetail(); + if (serie.clockwise) + { + toAngle = toAngle > currAngle ? currAngle : toAngle; + } + else + { + toAngle = toAngle < 360 - currAngle ? 360 - currAngle : toAngle; + } + } + return toAngle; + } } } \ No newline at end of file diff --git a/Runtime/Serie/SerieContext.cs b/Runtime/Serie/SerieContext.cs index 98ad76ce..d549f635 100644 --- a/Runtime/Serie/SerieContext.cs +++ b/Runtime/Serie/SerieContext.cs @@ -44,6 +44,7 @@ public class SerieContext /// 外半径 /// public float outsideRadius; + public float startAngle; /// /// 最大值 ///