Skip to content

Commit

Permalink
增加SerieavoidLabelOverlap参数避免饼图标签堆叠的情况#56
Browse files Browse the repository at this point in the history
  • Loading branch information
monitor1394 committed Jun 16, 2020
1 parent 590a136 commit 5835a37
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 103 deletions.
2 changes: 2 additions & 0 deletions Assets/XCharts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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}`表示忽略不显示标题
Expand Down
1 change: 1 addition & 0 deletions Assets/XCharts/Documentation/XCharts配置项手册.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@
* `roundCap`:是否启用圆弧效果。
* `ignore`:是否开启忽略数据。当为 `true` 时,数据值为 `ignoreValue` 时不进行绘制,对应的`Label``Legend`也不会显示。
* `ignoreValue`:忽略数据的默认值。默认值默认为0,当 `ignore``true` 才有效。
* `avoidLabelOverlap`:在饼图且标签外部显示的情况下,是否启用防止标签重叠策略,默认关闭,在标签拥挤重叠的情况下会挪动各个标签的位置,防止标签间的重叠。
* `label`:图形上的文本标签 [SerieLabel](#SerieLabel),可用于说明图形的一些数据信息,比如值,名称等。
* `emphasis`:高亮样式 [Emphasis](#Emphasis)
* `animation`:起始动画 [SerieAnimation](#SerieAnimation)
Expand Down
5 changes: 4 additions & 1 deletion Assets/XCharts/Editor/PropertyDrawers/SerieDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"));
Expand Down
9 changes: 9 additions & 0 deletions Assets/XCharts/Runtime/Component/Main/Serie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SerieData> m_Data = new List<SerieData>();
Expand Down Expand Up @@ -787,6 +788,14 @@ public int largeThreshold
}
}
/// <summary>
/// 在饼图且标签外部显示的情况下,是否启用防止标签重叠策略,默认关闭,在标签拥挤重叠的情况下会挪动各个标签的位置,防止标签间的重叠。
/// </summary>
public bool avoidLabelOverlap
{
get { return m_AvoidLabelOverlap; }
set { if (PropertyUtility.SetStruct(ref m_AvoidLabelOverlap, value)) SetVerticesDirty(); }
}
/// <summary>
/// 系列中的数据内容数组。SerieData可以设置1到n维数据。
/// </summary>
public List<SerieData> data { get { return m_Data; } }
Expand Down
129 changes: 129 additions & 0 deletions Assets/XCharts/Runtime/Internal/Helper/SerieLabelHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
9 changes: 8 additions & 1 deletion Assets/XCharts/Runtime/Internal/Object/LabelObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; } }
Expand All @@ -36,6 +38,7 @@ public void SetLabel(GameObject labelObj, bool autoSize, float paddingLeftRight,
m_LabelPaddingTopBottom = paddingTopBottom;
m_LabelText = labelObj.GetComponentInChildren<Text>();
m_LabelRect = m_LabelText.GetComponent<RectTransform>();
m_ObjectRect = labelObj.GetComponent<RectTransform>();
}

public void SetIcon(Image image)
Expand Down Expand Up @@ -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;
}
}
Expand Down
Loading

0 comments on commit 5835a37

Please sign in to comment.