From 48c867b1efc03db062ee95ed8bba3add25d9a809 Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Mon, 23 Sep 2019 19:09:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Formatter=E9=85=8D=E7=BD=AEAx?= =?UTF-8?q?is=E7=9A=84AxisLabel=E7=9A=84=E6=A0=BC=E5=BC=8F=E5=8C=96?= =?UTF-8?q?=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Editor/PropertyDrawers/AxisLabelDrawer.cs | 11 ++-- Assets/XCharts/Scripts/UI/Component/Axis.cs | 14 ++--- Assets/XCharts/Scripts/UI/Component/Legend.cs | 2 +- .../XCharts/Scripts/UI/Internal/AxisLabel.cs | 52 ++++++++++++++++++- .../XCharts/Scripts/UI/Utility/ChartCached.cs | 1 + README.md | 1 + 6 files changed, 64 insertions(+), 17 deletions(-) diff --git a/Assets/XCharts/Scripts/Editor/PropertyDrawers/AxisLabelDrawer.cs b/Assets/XCharts/Scripts/Editor/PropertyDrawers/AxisLabelDrawer.cs index d55cbacf..6f420d5d 100644 --- a/Assets/XCharts/Scripts/Editor/PropertyDrawers/AxisLabelDrawer.cs +++ b/Assets/XCharts/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/Assets/XCharts/Scripts/UI/Component/Axis.cs b/Assets/XCharts/Scripts/UI/Component/Axis.cs index b620abb2..d194cbbd 100644 --- a/Assets/XCharts/Scripts/UI/Component/Axis.cs +++ b/Assets/XCharts/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/Assets/XCharts/Scripts/UI/Component/Legend.cs b/Assets/XCharts/Scripts/UI/Component/Legend.cs index a726d058..bebb6721 100644 --- a/Assets/XCharts/Scripts/UI/Component/Legend.cs +++ b/Assets/XCharts/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/Assets/XCharts/Scripts/UI/Internal/AxisLabel.cs b/Assets/XCharts/Scripts/UI/Internal/AxisLabel.cs index c26ff44c..980878fb 100644 --- a/Assets/XCharts/Scripts/UI/Internal/AxisLabel.cs +++ b/Assets/XCharts/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/Assets/XCharts/Scripts/UI/Utility/ChartCached.cs b/Assets/XCharts/Scripts/UI/Utility/ChartCached.cs index 2a8192d0..b47f63a2 100644 --- a/Assets/XCharts/Scripts/UI/Utility/ChartCached.cs +++ b/Assets/XCharts/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; diff --git a/README.md b/README.md index 75b25806..3cc6e4f0 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ QQ交流群:XCharts交流群(202030963) ## 更新日志 +* (2019.09.23)增加`Formatter`配置`Axis`的`AxisLabel`的格式化输出 * (2019.09.23)增加`Tooltip`的`FontSize`、`FontStyle`配置字体大小和样式 * (2019.09.23)增加`Formatter`配置`SerieLabel`、`Legend`、`Tooltip`的格式化输出 * (2019.09.19)增加`LineArrow`配置带箭头曲线