diff --git a/CHANGELOG-EN.md b/CHANGELOG-EN.md
index 13d99ad4..2669cb64 100644
--- a/CHANGELOG-EN.md
+++ b/CHANGELOG-EN.md
@@ -38,6 +38,8 @@
## master
+* (2021.06.21) Add `iconStyle`'s `AutoHideWhenLabelEmpty` to set whether the icon is automatically hidden when `label` is empty
+
# # v2.2.3
* (2021.06.20) Release `v2.2.3` version
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5df14777..8771c8f6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -38,6 +38,8 @@
## master
+* (2021.06.21) 增加`IconStyle`的`autoHideWhenLabelEmpty`参数设置当`label`为空时是否自动隐藏图标
+
## v2.2.3
* (2021.06.20) 发布`v2.2.3`版本
diff --git a/Editor/PropertyDrawers/IconStyleDrawer.cs b/Editor/PropertyDrawers/IconStyleDrawer.cs
index 36a3bfb8..c203de66 100644
--- a/Editor/PropertyDrawers/IconStyleDrawer.cs
+++ b/Editor/PropertyDrawers/IconStyleDrawer.cs
@@ -28,6 +28,7 @@ namespace XCharts
PropertyField(prop, "m_Width");
PropertyField(prop, "m_Height");
PropertyField(prop, "m_Offset");
+ PropertyField(prop, "m_AutoHideWhenLabelEmpty");
--EditorGUI.indentLevel;
}
}
diff --git a/Runtime/Component/Sub/IconStyle.cs b/Runtime/Component/Sub/IconStyle.cs
index 4e0fc70d..03392f01 100644
--- a/Runtime/Component/Sub/IconStyle.cs
+++ b/Runtime/Component/Sub/IconStyle.cs
@@ -26,6 +26,7 @@ namespace XCharts
[SerializeField] private float m_Width = 20;
[SerializeField] private float m_Height = 20;
[SerializeField] private Vector3 m_Offset;
+ [SerializeField] private bool m_AutoHideWhenLabelEmpty = false;
public void Reset()
{
@@ -36,6 +37,7 @@ namespace XCharts
m_Width = 20;
m_Height = 20;
m_Offset = Vector3.zero;
+ m_AutoHideWhenLabelEmpty = false;
}
///
/// Whether the data icon is show.
@@ -71,6 +73,10 @@ namespace XCharts
/// 水平方向对齐方式。
///
public Align align { get { return m_Align; } set { m_Align = value; } }
+ ///
+ /// 当label内容为空时是否自动隐藏图标
+ ///
+ public bool autoHideWhenLabelEmpty { get { return m_AutoHideWhenLabelEmpty; } set { m_AutoHideWhenLabelEmpty = value; } }
public IconStyle Clone()
{
var iconStyle = new IconStyle();
@@ -82,6 +88,7 @@ namespace XCharts
iconStyle.height = height;
iconStyle.offset = offset;
iconStyle.align = align;
+ iconStyle.autoHideWhenLabelEmpty = autoHideWhenLabelEmpty;
return iconStyle;
}
@@ -95,6 +102,7 @@ namespace XCharts
height = iconStyle.height;
offset = iconStyle.offset;
align = iconStyle.align;
+ autoHideWhenLabelEmpty = iconStyle.autoHideWhenLabelEmpty;
}
}
}
diff --git a/Runtime/Internal/CoordinateChart.cs b/Runtime/Internal/CoordinateChart.cs
index 0363c835..7a586892 100644
--- a/Runtime/Internal/CoordinateChart.cs
+++ b/Runtime/Internal/CoordinateChart.cs
@@ -576,14 +576,14 @@ namespace XCharts
{
txt = ChartHelper.AddAxisLabelObject(i, objName + i, axisObj.transform, Vector2.zero,
Vector2.zero, new Vector2(0, 0.5f), new Vector2(textWidth, textHeight), yAxis, theme.axis,
- labelName, true);
+ labelName);
txt.label.SetAlignment(axisLabelTextStyle.GetAlignment(TextAnchor.MiddleLeft));
}
else
{
txt = ChartHelper.AddAxisLabelObject(i, objName + i, axisObj.transform, Vector2.zero,
Vector2.zero, new Vector2(1, 0.5f), new Vector2(textWidth, textHeight), yAxis, theme.axis,
- labelName, true);
+ labelName);
txt.label.SetAlignment(axisLabelTextStyle.GetAlignment(TextAnchor.MiddleRight));
}
var labelWidth = AxisHelper.GetScaleWidth(yAxis, grid.runtimeHeight, i + 1, dataZoom);
@@ -698,7 +698,7 @@ namespace XCharts
xAxis.runtimeMaxValue, dataZoom, isPercentStack);
var label = ChartHelper.AddAxisLabelObject(i, ChartCached.GetXAxisName(xAxisIndex, i), axisObj.transform,
new Vector2(0, 1), new Vector2(0, 1), new Vector2(1, 0.5f), new Vector2(textWidth, textHeight),
- xAxis, theme.axis,labelName, true);
+ xAxis, theme.axis, labelName);
if (i == 0) xAxis.axisLabel.SetRelatedText(label.label, labelWidth);
label.label.SetAlignment(axisLabelTextStyle.GetAlignment(TextAnchor.MiddleCenter));
diff --git a/Runtime/Internal/Object/ChartLabel.cs b/Runtime/Internal/Object/ChartLabel.cs
index 341059f8..1659321e 100644
--- a/Runtime/Internal/Object/ChartLabel.cs
+++ b/Runtime/Internal/Object/ChartLabel.cs
@@ -12,6 +12,7 @@ namespace XCharts
{
public class ChartLabel : ChartObject
{
+ private bool m_AutoHideIconWhenLabelEmpty = false;
private bool m_LabelAutoSize = true;
private float m_LabelPaddingLeftRight = 3f;
private float m_LabelPaddingTopBottom = 3f;
@@ -48,6 +49,9 @@ namespace XCharts
}
}
+ public bool autoHideIconWhenLabelEmpty { set { m_AutoHideIconWhenLabelEmpty = value; } }
+ public bool isIconActive { get; private set; }
+
public ChartLabel()
{
}
@@ -91,24 +95,21 @@ namespace XCharts
public void UpdateIcon(IconStyle iconStyle, Sprite sprite = null)
{
if (m_IconImage == null) return;
+ SetIconActive(iconStyle.show);
if (iconStyle.show)
{
- ChartHelper.SetActive(m_IconImage.gameObject, true);
m_IconImage.sprite = sprite == null ? iconStyle.sprite : sprite;
m_IconImage.color = iconStyle.color;
m_IconRect.sizeDelta = new Vector2(iconStyle.width, iconStyle.height);
m_IconOffest = iconStyle.offset;
m_Align = iconStyle.align;
+ m_AutoHideIconWhenLabelEmpty = iconStyle.autoHideWhenLabelEmpty;
AdjustIconPos();
if (iconStyle.layer == IconStyle.Layer.UnderLabel)
m_IconRect.SetSiblingIndex(0);
else
m_IconRect.SetSiblingIndex(m_GameObject.transform.childCount - 1);
}
- else
- {
- ChartHelper.SetActive(m_IconImage.gameObject, false);
- }
}
public float GetLabelWidth()
@@ -156,6 +157,7 @@ namespace XCharts
}
public void SetIconActive(bool flag)
{
+ isIconActive = flag;
if (m_IconImage) ChartHelper.SetActive(m_IconImage, flag);
}
@@ -179,13 +181,17 @@ namespace XCharts
return sizeChange;
}
AdjustIconPos();
+ if (m_AutoHideIconWhenLabelEmpty && isIconActive)
+ {
+ ChartHelper.SetActive(m_IconImage.gameObject, !string.IsNullOrEmpty(text));
+ }
}
return false;
}
private void AdjustIconPos()
{
- if (m_IconImage && m_IconImage.sprite != null && m_IconRect)
+ if (m_IconImage && m_IconRect)
{
var iconX = 0f;
switch (m_Align)
diff --git a/Runtime/Internal/Utility/ChartHelper.cs b/Runtime/Internal/Utility/ChartHelper.cs
index e5edfeae..735a155e 100644
--- a/Runtime/Internal/Utility/ChartHelper.cs
+++ b/Runtime/Internal/Utility/ChartHelper.cs
@@ -400,7 +400,7 @@ namespace XCharts
}
public static ChartLabel AddAxisLabelObject(int index, string name, Transform parent, Vector2 anchorMin, Vector2 anchorMax,
- Vector2 pivot, Vector2 sizeDelta, Axis axis, ComponentTheme theme, string content, bool autoHideWhenContentEmpty = false)
+ Vector2 pivot, Vector2 sizeDelta, Axis axis, ComponentTheme theme, string content)
{
var textStyle = axis.axisLabel.textStyle;
var iconStyle = axis.iconStyle;
@@ -414,10 +414,6 @@ namespace XCharts
GameObject.DestroyImmediate(oldText);
}
var labelShow = axis.axisLabel.show && (axis.axisLabel.interval == 0 || index % (axis.axisLabel.interval + 1) == 0);
- if (autoHideWhenContentEmpty && string.IsNullOrEmpty(content))
- {
- labelShow &= false;
- }
label.label = AddTextObject("Text", label.gameObject.transform, anchorMin, anchorMax, pivot, sizeDelta, textStyle, theme);
label.icon = ChartHelper.AddIcon("Icon", label.gameObject.transform, iconStyle.width, iconStyle.height);
label.SetAutoSize(false);