diff --git a/Scripts/Editor/PropertyDrawers/AxisLabelDrawer.cs b/Scripts/Editor/PropertyDrawers/AxisLabelDrawer.cs
index d55cbacf..6f420d5d 100644
--- a/Scripts/Editor/PropertyDrawers/AxisLabelDrawer.cs
+++ b/Scripts/Editor/PropertyDrawers/AxisLabelDrawer.cs
@@ -14,6 +14,7 @@ namespace XCharts
Rect drawRect = pos;
drawRect.height = EditorGUIUtility.singleLineHeight;
SerializedProperty show = prop.FindPropertyRelative("m_Show");
+ SerializedProperty m_Formatter = prop.FindPropertyRelative("m_Formatter");
SerializedProperty m_Inside = prop.FindPropertyRelative("m_Inside");
SerializedProperty m_Interval = prop.FindPropertyRelative("m_Interval");
SerializedProperty m_Rotate = prop.FindPropertyRelative("m_Rotate");
@@ -22,9 +23,9 @@ namespace XCharts
SerializedProperty m_FontSize = prop.FindPropertyRelative("m_FontSize");
SerializedProperty m_FontStyle = prop.FindPropertyRelative("m_FontStyle");
- ChartEditorHelper.MakeFoldout(ref drawRect, ref m_AxisLabelToggle,prop, "Axis Label", show, false);
+ ChartEditorHelper.MakeFoldout(ref drawRect, ref m_AxisLabelToggle, prop, "Axis Label", show, false);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
- if (ChartEditorHelper.IsToggle(m_AxisLabelToggle,prop))
+ if (ChartEditorHelper.IsToggle(m_AxisLabelToggle, prop))
{
++EditorGUI.indentLevel;
EditorGUI.PropertyField(drawRect, m_Inside);
@@ -41,6 +42,8 @@ namespace XCharts
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
EditorGUI.PropertyField(drawRect, m_FontStyle);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
+ EditorGUI.PropertyField(drawRect, m_Formatter);
+ drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
--EditorGUI.indentLevel;
}
}
@@ -48,9 +51,9 @@ namespace XCharts
public override float GetPropertyHeight(SerializedProperty prop, GUIContent label)
{
float height = 0;
- if (ChartEditorHelper.IsToggle(m_AxisLabelToggle,prop))
+ if (ChartEditorHelper.IsToggle(m_AxisLabelToggle, prop))
{
- height += 7 * EditorGUIUtility.singleLineHeight + 6 * EditorGUIUtility.standardVerticalSpacing;
+ height += 8 * EditorGUIUtility.singleLineHeight + 7 * EditorGUIUtility.standardVerticalSpacing;
}
return height;
}
diff --git a/Scripts/UI/Component/Axis.cs b/Scripts/UI/Component/Axis.cs
index b620abb2..d194cbbd 100644
--- a/Scripts/UI/Component/Axis.cs
+++ b/Scripts/UI/Component/Axis.cs
@@ -440,15 +440,7 @@ namespace XCharts
{
value = (minValue + (maxValue - minValue) * index / (split - 1));
}
- if (_cacheValue2str.ContainsKey(value)) return _cacheValue2str[value];
- else
- {
- if (value - (int)value == 0)
- _cacheValue2str[value] = (value).ToString();
- else
- _cacheValue2str[value] = (value).ToString("f1");
- return _cacheValue2str[value];
- }
+ return m_AxisLabel.GetFormatterContent(value);
}
var showData = GetDataList(dataZoom);
int dataCount = showData.Count;
@@ -456,7 +448,7 @@ namespace XCharts
if (index == split - 1 && !m_BoundaryGap)
{
- return showData[dataCount - 1];
+ return m_AxisLabel.GetFormatterContent(showData[dataCount - 1]);
}
else
{
@@ -465,7 +457,7 @@ namespace XCharts
int offset = m_BoundaryGap ? (int)(rate / 2) : 0;
int newIndex = (int)(index * rate >= dataCount - 1 ?
dataCount - 1 : offset + index * rate);
- return showData[newIndex];
+ return m_AxisLabel.GetFormatterContent(showData[newIndex]);
}
}
diff --git a/Scripts/UI/Component/Legend.cs b/Scripts/UI/Component/Legend.cs
index a726d058..bebb6721 100644
--- a/Scripts/UI/Component/Legend.cs
+++ b/Scripts/UI/Component/Legend.cs
@@ -90,7 +90,7 @@ namespace XCharts
///
/// 图例内容字符串模版格式器。支持用 \n 换行。
/// 模板变量为图例名称 {name}
- ///
+ ///
public string formatter { get { return m_Formatter; } set { m_Formatter = value; } }
///
/// Data array of legend. An array item is usually a name representing string. (If it is a pie chart,
diff --git a/Scripts/UI/Internal/AxisLabel.cs b/Scripts/UI/Internal/AxisLabel.cs
index c26ff44c..980878fb 100644
--- a/Scripts/UI/Internal/AxisLabel.cs
+++ b/Scripts/UI/Internal/AxisLabel.cs
@@ -11,6 +11,7 @@ namespace XCharts
public class AxisLabel
{
[SerializeField] private bool m_Show = true;
+ [SerializeField] private string m_Formatter;
[SerializeField] private int m_Interval = 0;
[SerializeField] private bool m_Inside = false;
[SerializeField] private float m_Rotate;
@@ -59,6 +60,11 @@ namespace XCharts
/// 文字字体的风格。
///
public FontStyle fontStyle { get { return m_FontStyle; } set { m_FontStyle = value; } }
+ ///
+ /// 图例内容字符串模版格式器。支持用 \n 换行。
+ /// 模板变量为图例名称 {value},{value:f1} 表示取1为小数
+ ///
+ public string formatter { get { return m_Formatter; } set { m_Formatter = value; } }
public static AxisLabel defaultAxisLabel
{
@@ -87,6 +93,7 @@ namespace XCharts
m_Color = other.color;
m_FontSize = other.fontSize;
m_FontStyle = other.fontStyle;
+ m_Formatter = other.formatter;
}
public override bool Equals(object obj)
@@ -103,12 +110,55 @@ namespace XCharts
m_Margin == other.margin &&
m_Color == other.color &&
m_FontSize == other.fontSize &&
- m_FontStyle == other.fontStyle;
+ m_FontStyle == other.fontStyle &&
+ m_Formatter == other.formatter;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
+
+ public string GetFormatterContent(string category)
+ {
+ if (string.IsNullOrEmpty(m_Formatter))
+ return category;
+ else
+ {
+ var content = m_Formatter.Replace("{value}", category);
+ content = content.Replace("\\n", "\n");
+ content = content.Replace("
", "\n");
+ return content;
+ }
+ }
+
+ public string GetFormatterContent(float value)
+ {
+ if (string.IsNullOrEmpty(m_Formatter))
+ if (value - (int)value == 0)
+ return ChartCached.IntToStr((int)value);
+ else
+ return ChartCached.FloatToStr(value, 1);
+ else
+ {
+ var content = m_Formatter;
+ if (content.Contains("{value:f2}"))
+ content = m_Formatter.Replace("{value:f2}", ChartCached.FloatToStr(value, 2));
+ else if (content.Contains("{value:f1}"))
+ content = m_Formatter.Replace("{value:f1}", ChartCached.FloatToStr(value, 1));
+ else if (content.Contains("{value}"))
+ {
+ if (value - (int)value == 0)
+
+ content = m_Formatter.Replace("{value}", ChartCached.IntToStr((int)value));
+ else
+ content = m_Formatter.Replace("{value}", ChartCached.FloatToStr(value, 1));
+ }
+
+ content = content.Replace("\\n", "\n");
+ content = content.Replace("
", "\n");
+ return content;
+ }
+ }
}
}
\ No newline at end of file
diff --git a/Scripts/UI/Utility/ChartCached.cs b/Scripts/UI/Utility/ChartCached.cs
index 2a8192d0..b47f63a2 100644
--- a/Scripts/UI/Utility/ChartCached.cs
+++ b/Scripts/UI/Utility/ChartCached.cs
@@ -12,6 +12,7 @@ namespace XCharts
public static string FloatToStr(float value, int f = 0)
{
+ if (f > 2) f = 2;
Dictionary valueDic;
if (f == 1) valueDic = s_ValueToF1Str;
else if (f == 2) valueDic = s_ValueToF2Str;