From 5835a372f5e96efd82d9f606978c5626b38ecc7d Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Tue, 16 Jun 2020 09:33:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0`Serie`=E7=9A=84`avoidLabelOv?= =?UTF-8?q?erlap`=E5=8F=82=E6=95=B0=E9=81=BF=E5=85=8D=E9=A5=BC=E5=9B=BE?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E5=A0=86=E5=8F=A0=E7=9A=84=E6=83=85=E5=86=B5?= =?UTF-8?q?#56?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/XCharts/CHANGELOG.md | 2 + ...56\351\241\271\346\211\213\345\206\214.md" | 1 + .../Editor/PropertyDrawers/SerieDrawer.cs | 5 +- .../XCharts/Runtime/Component/Main/Serie.cs | 9 ++ .../Internal/Helper/SerieLabelHelper.cs | 129 ++++++++++++++++++ .../Runtime/Internal/Object/LabelObject.cs | 9 +- Assets/XCharts/Runtime/PieChart.cs | 129 ++++-------------- Assets/XCharts/Runtime/Utility/ChartHelper.cs | 17 +++ 8 files changed, 198 insertions(+), 103 deletions(-) diff --git a/Assets/XCharts/CHANGELOG.md b/Assets/XCharts/CHANGELOG.md index b1db248f..c550a4f4 100644 --- a/Assets/XCharts/CHANGELOG.md +++ b/Assets/XCharts/CHANGELOG.md @@ -1,6 +1,8 @@ # 更新日志 +* (2020.06.16) 增加`Serie`的`avoidLabelOverlap`参数避免饼图标签堆叠的情况#56 +* (2020.06.15) 修复`SerieLabel`单独控制显示时可能错乱的问题 * (2020.06.11) 修复`Check warning`不生效的问题 * (2020.06.11) 修复`PieChart`和`RingChart`在数据占比很小时不显示的问题 * (2020.06.11) 增加`Tooltip`的`titleFormatter`支持配置占位符`{i}`表示忽略不显示标题 diff --git "a/Assets/XCharts/Documentation/XCharts\351\205\215\347\275\256\351\241\271\346\211\213\345\206\214.md" "b/Assets/XCharts/Documentation/XCharts\351\205\215\347\275\256\351\241\271\346\211\213\345\206\214.md" index 752d8ad4..4d7ae167 100644 --- "a/Assets/XCharts/Documentation/XCharts\351\205\215\347\275\256\351\241\271\346\211\213\345\206\214.md" +++ "b/Assets/XCharts/Documentation/XCharts\351\205\215\347\275\256\351\241\271\346\211\213\345\206\214.md" @@ -574,6 +574,7 @@ * `roundCap`:是否启用圆弧效果。 * `ignore`:是否开启忽略数据。当为 `true` 时,数据值为 `ignoreValue` 时不进行绘制,对应的`Label`和`Legend`也不会显示。 * `ignoreValue`:忽略数据的默认值。默认值默认为0,当 `ignore` 为 `true` 才有效。 +* `avoidLabelOverlap`:在饼图且标签外部显示的情况下,是否启用防止标签重叠策略,默认关闭,在标签拥挤重叠的情况下会挪动各个标签的位置,防止标签间的重叠。 * `label`:图形上的文本标签 [SerieLabel](#SerieLabel),可用于说明图形的一些数据信息,比如值,名称等。 * `emphasis`:高亮样式 [Emphasis](#Emphasis)。 * `animation`:起始动画 [SerieAnimation](#SerieAnimation)。 diff --git a/Assets/XCharts/Editor/PropertyDrawers/SerieDrawer.cs b/Assets/XCharts/Editor/PropertyDrawers/SerieDrawer.cs index 417995f8..06014413 100644 --- a/Assets/XCharts/Editor/PropertyDrawers/SerieDrawer.cs +++ b/Assets/XCharts/Editor/PropertyDrawers/SerieDrawer.cs @@ -77,6 +77,7 @@ public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label) SerializedProperty m_ShowAsPositiveNumber = prop.FindPropertyRelative("m_ShowAsPositiveNumber"); SerializedProperty m_Large = prop.FindPropertyRelative("m_Large"); SerializedProperty m_LargeThreshold = prop.FindPropertyRelative("m_LargeThreshold"); + SerializedProperty m_AvoidLabelOverlap = prop.FindPropertyRelative("m_AvoidLabelOverlap"); SerializedProperty m_Datas = prop.FindPropertyRelative("m_Data"); int index = InitToggle(prop); @@ -213,6 +214,8 @@ public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label) drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; EditorGUI.PropertyField(drawRect, m_IgnoreValue); drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; + EditorGUI.PropertyField(drawRect, m_AvoidLabelOverlap); + drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; EditorGUI.PropertyField(drawRect, m_ItemStyle); drawRect.y += EditorGUI.GetPropertyHeight(m_ItemStyle); EditorGUI.PropertyField(drawRect, m_Label); @@ -532,7 +535,7 @@ public override float GetPropertyHeight(SerializedProperty prop, GUIContent labe height += EditorGUI.GetPropertyHeight(prop.FindPropertyRelative("m_Animation")); break; case SerieType.Pie: - height += 11 * EditorGUIUtility.singleLineHeight + 10 * EditorGUIUtility.standardVerticalSpacing; + height += 12 * EditorGUIUtility.singleLineHeight + 11 * EditorGUIUtility.standardVerticalSpacing; height += EditorGUI.GetPropertyHeight(prop.FindPropertyRelative("m_ItemStyle")); height += EditorGUI.GetPropertyHeight(prop.FindPropertyRelative("m_Label")); height += EditorGUI.GetPropertyHeight(prop.FindPropertyRelative("m_Emphasis")); diff --git a/Assets/XCharts/Runtime/Component/Main/Serie.cs b/Assets/XCharts/Runtime/Component/Main/Serie.cs index 87a6a115..9b20269d 100644 --- a/Assets/XCharts/Runtime/Component/Main/Serie.cs +++ b/Assets/XCharts/Runtime/Component/Main/Serie.cs @@ -273,6 +273,7 @@ public class Serie : MainComponent [SerializeField] private bool m_ShowAsPositiveNumber = false; [SerializeField] private bool m_Large = true; [SerializeField] private int m_LargeThreshold = 200; + [SerializeField] private bool m_AvoidLabelOverlap = false; [SerializeField] private RadarType m_RadarType = RadarType.Multiple; [SerializeField] private List m_Data = new List(); @@ -787,6 +788,14 @@ public int largeThreshold } } /// + /// 在饼图且标签外部显示的情况下,是否启用防止标签重叠策略,默认关闭,在标签拥挤重叠的情况下会挪动各个标签的位置,防止标签间的重叠。 + /// + public bool avoidLabelOverlap + { + get { return m_AvoidLabelOverlap; } + set { if (PropertyUtility.SetStruct(ref m_AvoidLabelOverlap, value)) SetVerticesDirty(); } + } + /// /// 系列中的数据内容数组。SerieData可以设置1到n维数据。 /// public List data { get { return m_Data; } } diff --git a/Assets/XCharts/Runtime/Internal/Helper/SerieLabelHelper.cs b/Assets/XCharts/Runtime/Internal/Helper/SerieLabelHelper.cs index ff7bb4b3..abf94ff9 100644 --- a/Assets/XCharts/Runtime/Internal/Helper/SerieLabelHelper.cs +++ b/Assets/XCharts/Runtime/Internal/Helper/SerieLabelHelper.cs @@ -175,5 +175,134 @@ private static void SetRingLabelText(Serie serie, ThemeInfo themeInfo) } } } + + public static void UpdatePieLabelPosition(Serie serie, SerieData serieData) + { + if (serieData.labelObject == null) return; + var currAngle = serieData.runtimePieHalfAngle; + var currRad = currAngle * Mathf.Deg2Rad; + var offsetRadius = serieData.runtimePieOffsetRadius; + var insideRadius = serieData.runtimePieInsideRadius; + var outsideRadius = serieData.runtimePieOutsideRadius; + var serieLabel = SerieHelper.GetSerieLabel(serie, serieData); + switch (serieLabel.position) + { + case SerieLabel.Position.Center: + serieData.labelPosition = serie.runtimeCenterPos; + break; + case SerieLabel.Position.Inside: + var labelRadius = offsetRadius + insideRadius + (outsideRadius - insideRadius) / 2; + var labelCenter = new Vector2(serie.runtimeCenterPos.x + labelRadius * Mathf.Sin(currRad), + serie.runtimeCenterPos.y + labelRadius * Mathf.Cos(currRad)); + serieData.labelPosition = labelCenter; + break; + case SerieLabel.Position.Outside: + if (serieLabel.lineType == SerieLabel.LineType.HorizontalLine) + { + var radius1 = serie.runtimeOutsideRadius; + var radius3 = insideRadius + (outsideRadius - insideRadius) / 2; + var currSin = Mathf.Sin(currRad); + var currCos = Mathf.Cos(currRad); + var pos0 = new Vector3(serie.runtimeCenterPos.x + radius3 * currSin, serie.runtimeCenterPos.y + radius3 * currCos); + if (currAngle > 180) + { + currSin = Mathf.Sin((360 - currAngle) * Mathf.Deg2Rad); + currCos = Mathf.Cos((360 - currAngle) * Mathf.Deg2Rad); + } + var r4 = Mathf.Sqrt(radius1 * radius1 - Mathf.Pow(currCos * radius3, 2)) - currSin * radius3; + r4 += serieLabel.lineLength1 + serieLabel.lineWidth * 4; + r4 += serieData.labelObject.label.preferredWidth / 2; + serieData.labelPosition = pos0 + (currAngle > 180 ? Vector3.left : Vector3.right) * r4; + } + else + { + labelRadius = serie.runtimeOutsideRadius + serieLabel.lineLength1; + labelCenter = new Vector2(serie.runtimeCenterPos.x + labelRadius * Mathf.Sin(currRad), + serie.runtimeCenterPos.y + labelRadius * Mathf.Cos(currRad)); + float labelWidth = serieData.labelObject.label.preferredWidth; + serieData.labelPosition = labelCenter; + } + break; + } + } + + internal static void AvoidLabelOverlap(Serie serie) + { + if (!serie.avoidLabelOverlap) return; + var lastCheckPos = Vector3.zero; + var data = serie.data; + var splitCount = 0; + for (int n = 0; n < data.Count; n++) + { + var serieData = data[n]; + if (serieData.labelPosition.x != 0 && serieData.labelPosition.x < serie.runtimeCenterPos.x) + { + splitCount = n; + break; + } + } + if (splitCount <= 0) return; + for (int n = 0; n < splitCount; n++) + { + var serieData = data[n]; + CheckSerieDataLabel(serie, serieData, false, ref lastCheckPos); + } + lastCheckPos = Vector3.zero; + for (int n = data.Count - 1; n >= splitCount; n--) + { + var serieData = data[n]; + CheckSerieDataLabel(serie, serieData, true, ref lastCheckPos); + } + } + + private static void CheckSerieDataLabel(Serie serie, SerieData serieData, bool isLeft, ref Vector3 lastCheckPos) + { + if (!serieData.canShowLabel) + { + serieData.SetLabelActive(false); + return; + } + if (!serieData.show) return; + var serieLabel = SerieHelper.GetSerieLabel(serie, serieData); + if (serieLabel.position != SerieLabel.Position.Outside) return; + if (lastCheckPos == Vector3.zero) + { + lastCheckPos = serieData.labelPosition; + } + else if (serieData.labelPosition.x != 0) + { + float hig = serieLabel.fontSize; + if (lastCheckPos.y - serieData.labelPosition.y < hig) + { + var labelRadius = serie.runtimeOutsideRadius + serieLabel.lineLength1; + var y1 = lastCheckPos.y - hig; + var cy = serie.runtimeCenterPos.y; + var diff = Mathf.Abs(y1 - cy); + var diffX = labelRadius * labelRadius - diff * diff; + diffX = diffX <= 0 ? 0 : diffX; + var x1 = serie.runtimeCenterPos.x + Mathf.Sqrt(diffX) * (isLeft ? -1 : 1); + serieData.labelPosition = new Vector3(x1, y1); + } + lastCheckPos = serieData.labelPosition; + serieData.labelObject.SetPosition(SerieLabelHelper.GetRealLabelPosition(serieData, serieLabel)); + } + } + + internal static Vector3 GetRealLabelPosition(SerieData serieData, SerieLabel label) + { + if (label.position == SerieLabel.Position.Outside && label.lineType != SerieLabel.LineType.HorizontalLine) + { + var currAngle = serieData.runtimePieHalfAngle; + var offset = label.lineLength2 + serieData.labelObject.GetLabelWidth() / 2; + if (currAngle > 180) + return serieData.labelPosition + new Vector3(-offset, 0, 0); + else + return serieData.labelPosition + new Vector3(offset, 0, 0); + } + else + { + return serieData.labelPosition; + } + } } } \ No newline at end of file diff --git a/Assets/XCharts/Runtime/Internal/Object/LabelObject.cs b/Assets/XCharts/Runtime/Internal/Object/LabelObject.cs index e0101c7f..c9623a11 100644 --- a/Assets/XCharts/Runtime/Internal/Object/LabelObject.cs +++ b/Assets/XCharts/Runtime/Internal/Object/LabelObject.cs @@ -18,6 +18,8 @@ public class LabelObject : ChartObject private Text m_LabelText; private RectTransform m_LabelRect; private RectTransform m_IconRect; + private RectTransform m_ObjectRect; + private Image m_IconImage; public GameObject gameObject { get { return m_GameObject; } } @@ -36,6 +38,7 @@ public void SetLabel(GameObject labelObj, bool autoSize, float paddingLeftRight, m_LabelPaddingTopBottom = paddingTopBottom; m_LabelText = labelObj.GetComponentInChildren(); m_LabelRect = m_LabelText.GetComponent(); + m_ObjectRect = labelObj.GetComponent(); } public void SetIcon(Image image) @@ -137,7 +140,11 @@ public bool SetText(string text) new Vector2(m_LabelText.preferredWidth + m_LabelPaddingLeftRight * 2, m_LabelText.preferredHeight + m_LabelPaddingTopBottom * 2); var sizeChange = newSize.x != m_LabelRect.sizeDelta.x || newSize.y != m_LabelRect.sizeDelta.y; - if (sizeChange) m_LabelRect.sizeDelta = newSize; + if (sizeChange) + { + m_LabelRect.sizeDelta = newSize; + m_ObjectRect.sizeDelta = newSize; + } return sizeChange; } } diff --git a/Assets/XCharts/Runtime/PieChart.cs b/Assets/XCharts/Runtime/PieChart.cs index 0db31f2e..ef6d0dda 100644 --- a/Assets/XCharts/Runtime/PieChart.cs +++ b/Assets/XCharts/Runtime/PieChart.cs @@ -5,7 +5,6 @@ /* */ /******************************************/ -using System.Text; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; @@ -203,17 +202,15 @@ private void DrawLabelLine(VertexHelper vh) { foreach (var serie in m_Series.list) { - if (serie.type == SerieType.Pie) + if (serie.type != SerieType.Pie) continue; + foreach (var serieData in serie.data) { - foreach (var serieData in serie.data) + var serieLabel = SerieHelper.GetSerieLabel(serie, serieData); + if (SerieLabelHelper.CanShowLabel(serie, serieData, serieLabel, 1)) { - var serieLabel = SerieHelper.GetSerieLabel(serie, serieData); - if (SerieLabelHelper.CanShowLabel(serie, serieData, serieLabel, 1)) - { - int colorIndex = m_LegendRealShowName.IndexOf(serieData.name); - Color color = m_ThemeInfo.GetColor(colorIndex); - DrawLabelLine(vh, serie, serieData, color); - } + int colorIndex = m_LegendRealShowName.IndexOf(serieData.name); + Color color = m_ThemeInfo.GetColor(colorIndex); + DrawLabelLine(vh, serie, serieData, color); } } } @@ -223,16 +220,14 @@ private void DrawLabelBackground(VertexHelper vh) { foreach (var serie in m_Series.list) { - if (serie.type == SerieType.Pie) + if (serie.type != SerieType.Pie) continue; + foreach (var serieData in serie.data) { - foreach (var serieData in serie.data) + var serieLabel = SerieHelper.GetSerieLabel(serie, serieData); + if (SerieLabelHelper.CanShowLabel(serie, serieData, serieLabel, 1)) { - var serieLabel = SerieHelper.GetSerieLabel(serie, serieData); - if (SerieLabelHelper.CanShowLabel(serie, serieData, serieLabel, 1)) - { - UpdateLabelPostion(serie, serieData); - DrawLabelBackground(vh, serie, serieData); - } + SerieLabelHelper.UpdatePieLabelPosition(serie, serieData); + DrawLabelBackground(vh, serie, serieData); } } } @@ -261,7 +256,11 @@ private void DrawLabelLine(VertexHelper vh, Serie serie, SerieData serieData, Co radius1 -= 0.1f; var pos0 = new Vector3(center.x + radius3 * currSin, center.y + radius3 * currCos); var pos1 = new Vector3(center.x + radius1 * currSin, center.y + radius1 * currCos); - var pos2 = new Vector3(center.x + radius2 * currSin, center.y + radius2 * currCos); + var pos2 = serieData.labelPosition; + if (pos2.x == 0) + { + pos2 = new Vector3(center.x + radius2 * currSin, center.y + radius2 * currCos); + } float tx, ty; Vector3 pos3, pos4, pos6; var horizontalLineCircleRadius = serieLabel.lineWidth * 4f; @@ -337,7 +336,6 @@ protected override void OnRefreshLabel() serie.index = i; if (!serie.show) continue; var data = serie.data; - for (int n = 0; n < data.Count; n++) { var serieData = data[n]; @@ -349,9 +347,9 @@ protected override void OnRefreshLabel() if (!serieData.show) continue; serieNameCount = m_LegendRealShowName.IndexOf(serieData.name); Color color = m_ThemeInfo.GetColor(serieNameCount); - DrawLabel(serie, n, serieData, color); } + SerieLabelHelper.AvoidLabelOverlap(serie); } } @@ -392,8 +390,7 @@ private void DrawLabel(Serie serie, int dataIndex, SerieData serieData, Color se serieData.labelObject.label.fontSize = fontSize; serieData.labelObject.label.fontStyle = fontStyle; serieData.labelObject.SetLabelRotate(rotate); - - UpdateLabelPostion(serie, serieData); + SerieLabelHelper.UpdatePieLabelPosition(serie, serieData); if (!string.IsNullOrEmpty(serieLabel.formatter)) { var value = serieData.data[1]; @@ -405,7 +402,7 @@ private void DrawLabel(Serie serie, int dataIndex, SerieData serieData, Color se { if (serieData.labelObject.SetText(serieData.name)) RefreshChart(); } - serieData.labelObject.SetPosition(serieData.labelPosition); + serieData.labelObject.SetPosition(SerieLabelHelper.GetRealLabelPosition(serieData, serieLabel)); if (showLabel) serieData.labelObject.SetLabelPosition(serieLabel.offset); else serieData.SetLabelActive(false); } @@ -416,63 +413,6 @@ private void DrawLabel(Serie serie, int dataIndex, SerieData serieData, Color se serieData.labelObject.UpdateIcon(serieData.iconStyle); } - protected void UpdateLabelPostion(Serie serie, SerieData serieData) - { - if (serieData.labelObject == null) return; - var currAngle = serieData.runtimePieHalfAngle; - var currRad = currAngle * Mathf.Deg2Rad; - var offsetRadius = serieData.runtimePieOffsetRadius; - var insideRadius = serieData.runtimePieInsideRadius; - var outsideRadius = serieData.runtimePieOutsideRadius; - var serieLabel = SerieHelper.GetSerieLabel(serie, serieData); - switch (serieLabel.position) - { - case SerieLabel.Position.Center: - serieData.labelPosition = serie.runtimeCenterPos; - break; - case SerieLabel.Position.Inside: - var labelRadius = offsetRadius + insideRadius + (outsideRadius - insideRadius) / 2; - var labelCenter = new Vector2(serie.runtimeCenterPos.x + labelRadius * Mathf.Sin(currRad), - serie.runtimeCenterPos.y + labelRadius * Mathf.Cos(currRad)); - serieData.labelPosition = labelCenter; - break; - case SerieLabel.Position.Outside: - if (serieLabel.lineType == SerieLabel.LineType.HorizontalLine) - { - var radius1 = serie.runtimeOutsideRadius; - var radius3 = insideRadius + (outsideRadius - insideRadius) / 2; - var currSin = Mathf.Sin(currRad); - var currCos = Mathf.Cos(currRad); - var pos0 = new Vector3(serie.runtimeCenterPos.x + radius3 * currSin, serie.runtimeCenterPos.y + radius3 * currCos); - if (currAngle > 180) - { - currSin = Mathf.Sin((360 - currAngle) * Mathf.Deg2Rad); - currCos = Mathf.Cos((360 - currAngle) * Mathf.Deg2Rad); - } - var r4 = Mathf.Sqrt(radius1 * radius1 - Mathf.Pow(currCos * radius3, 2)) - currSin * radius3; - r4 += serieLabel.lineLength1 + serieLabel.lineWidth * 4; - r4 += serieData.labelObject.label.preferredWidth / 2; - serieData.labelPosition = pos0 + (currAngle > 180 ? Vector3.left : Vector3.right) * r4; - } - else - { - labelRadius = serie.runtimeOutsideRadius + serieLabel.lineLength1; - labelCenter = new Vector2(serie.runtimeCenterPos.x + labelRadius * Mathf.Sin(currRad), - serie.runtimeCenterPos.y + labelRadius * Mathf.Cos(currRad)); - float labelWidth = serieData.labelObject.label.preferredWidth; - if (currAngle > 180) - { - serieData.labelPosition = new Vector2(labelCenter.x - serieLabel.lineLength2 - 5 - labelWidth / 2, labelCenter.y); - } - else - { - serieData.labelPosition = new Vector2(labelCenter.x + serieLabel.lineLength2 + 5 + labelWidth / 2, labelCenter.y); - } - } - break; - } - } - protected override void OnLegendButtonClick(int index, string legendName, bool show) { LegendHelper.CheckDataShow(m_Series, legendName, show); @@ -532,7 +472,7 @@ private int GetPosPieIndex(Serie serie, Vector2 local) var dist = Vector2.Distance(local, serie.runtimeCenterPos); if (dist < serie.runtimeInsideRadius || dist > serie.runtimeOutsideRadius) return -1; Vector2 dir = local - new Vector2(serie.runtimeCenterPos.x, serie.runtimeCenterPos.y); - float angle = VectorAngle(Vector2.up, dir); + float angle = ChartHelper.GetAngle360(Vector2.up, dir); for (int i = 0; i < serie.data.Count; i++) { var serieData = serie.data[i]; @@ -544,17 +484,6 @@ private int GetPosPieIndex(Serie serie, Vector2 local) return -1; } - float VectorAngle(Vector2 from, Vector2 to) - { - float angle; - - Vector3 cross = Vector3.Cross(from, to); - angle = Vector2.Angle(from, to); - angle = cross.z > 0 ? -angle : angle; - angle = (angle + 360) % 360; - return angle; - } - protected override void UpdateTooltip() { base.UpdateTooltip(); @@ -583,16 +512,14 @@ public override void OnPointerDown(PointerEventData eventData) for (int i = 0; i < m_Series.Count; i++) { var serie = m_Series.GetSerie(i); - if (serie.type == SerieType.Pie) + if (serie.type != SerieType.Pie) continue; + var index = GetPosPieIndex(serie, local); + if (index >= 0) { - var index = GetPosPieIndex(serie, local); - if (index >= 0) + for (int j = 0; j < serie.data.Count; j++) { - for (int j = 0; j < serie.data.Count; j++) - { - if (j == index) serie.data[j].selected = !serie.data[j].selected; - else serie.data[j].selected = false; - } + if (j == index) serie.data[j].selected = !serie.data[j].selected; + else serie.data[j].selected = false; } } } diff --git a/Assets/XCharts/Runtime/Utility/ChartHelper.cs b/Assets/XCharts/Runtime/Utility/ChartHelper.cs index 39771763..601ef77b 100644 --- a/Assets/XCharts/Runtime/Utility/ChartHelper.cs +++ b/Assets/XCharts/Runtime/Utility/ChartHelper.cs @@ -742,5 +742,22 @@ public static Vector3 GetPosition(Vector3 center, float angle, float radius) var py = Mathf.Cos(rad) * radius; return center + new Vector3(px, py); } + + /// + /// 获得0-360的角度(12点钟方向为0度) + /// + /// + /// + /// + public static float GetAngle360(Vector2 from, Vector2 to) + { + float angle; + + Vector3 cross = Vector3.Cross(from, to); + angle = Vector2.Angle(from, to); + angle = cross.z > 0 ? -angle : angle; + angle = (angle + 360) % 360; + return angle; + } } } \ No newline at end of file