diff --git a/Assets/XCharts/CHANGELOG-EN.md b/Assets/XCharts/CHANGELOG-EN.md
index ee7a0ebc..56d6d24b 100644
--- a/Assets/XCharts/CHANGELOG-EN.md
+++ b/Assets/XCharts/CHANGELOG-EN.md
@@ -1,6 +1,7 @@
# 更新日志
+* (2020.07.21) Added `formatter` of `AxisLabel` and `SerieLabel` to configure numeric formatting separately.
* (2020.07.17) Added animation completion callback interface for `SerieAnimation`.
* (2020.07.17) Optimize `Chart` under `ScrollView` without affecting the scrolling and dragging of `ScrollView`.
* (2020.07.16) Fixed a problem with `Tooltip` that would also show up if it was blocked on top. #74
diff --git a/Assets/XCharts/CHANGELOG.md b/Assets/XCharts/CHANGELOG.md
index 088aec15..9af85d34 100644
--- a/Assets/XCharts/CHANGELOG.md
+++ b/Assets/XCharts/CHANGELOG.md
@@ -1,6 +1,7 @@
# 更新日志
+* (2020.07.21) 增加`AxisLabel`和`SerieLabel`的`formatter`可单独配置数值格式化#68
* (2020.07.17) 增加`SerieAnimation`动画完成回调接口
* (2020.07.17) 优化`Chart`放在`ScrollView`下时不影响`ScrollView`的滚动和拖动
* (2020.07.16) 修复`Tooltip`在上层有遮挡还会显示的问题#74
diff --git a/Assets/XCharts/Runtime/Component/Sub/AxisLabel.cs b/Assets/XCharts/Runtime/Component/Sub/AxisLabel.cs
index 63091abe..36c7fa35 100644
--- a/Assets/XCharts/Runtime/Component/Sub/AxisLabel.cs
+++ b/Assets/XCharts/Runtime/Component/Sub/AxisLabel.cs
@@ -253,27 +253,11 @@ namespace XCharts
}
return ChartCached.NumberToStr(value, numericFormatter);
}
- else if (m_Formatter.Contains("{value"))
- {
- var content = m_Formatter;
- if (content.Contains("{value:f0}"))
- content = m_Formatter.Replace("{value:f0}", ChartCached.IntToStr((int)value));
- if (content.Contains("{value:f2}"))
- content = m_Formatter.Replace("{value:f2}", ChartCached.FloatToStr(value, string.Empty, 2));
- else if (content.Contains("{value:f1}"))
- content = m_Formatter.Replace("{value:f1}", ChartCached.FloatToStr(value, string.Empty, 1));
- else if (content.Contains("{value}"))
- {
- content = m_Formatter.Replace("{value}", ChartCached.NumberToStr((int)value, numericFormatter));
- }
-
- content = content.Replace("\\n", "\n");
- content = content.Replace("
", "\n");
- return content;
- }
else
{
- return value.ToString(m_Formatter);
+ var content = m_Formatter;
+ FormatterHelper.ReplaceAxisLabelContent(ref content, numericFormatter, value);
+ return content;
}
}
}
diff --git a/Assets/XCharts/Runtime/Component/Sub/SerieAnimation.cs b/Assets/XCharts/Runtime/Component/Sub/SerieAnimation.cs
index d319fe3b..0a6e006d 100644
--- a/Assets/XCharts/Runtime/Component/Sub/SerieAnimation.cs
+++ b/Assets/XCharts/Runtime/Component/Sub/SerieAnimation.cs
@@ -205,7 +205,6 @@ namespace XCharts
if (fadeInFinishCallback != null)
{
fadeInFinishCallback();
- fadeInFinishCallback = null;
}
}
if (m_FadeOut)
@@ -215,7 +214,6 @@ namespace XCharts
if (fadeOutFinishCallback != null)
{
fadeOutFinishCallback();
- fadeOutFinishCallback = null;
}
}
}
diff --git a/Assets/XCharts/Runtime/Helper/FormatterHelper.cs b/Assets/XCharts/Runtime/Helper/FormatterHelper.cs
index 3330fed8..665d53c3 100644
--- a/Assets/XCharts/Runtime/Helper/FormatterHelper.cs
+++ b/Assets/XCharts/Runtime/Helper/FormatterHelper.cs
@@ -21,6 +21,10 @@ namespace XCharts
private static Regex s_RegexN_N = new Regex(@"\d+-\d+", RegexOptions.IgnoreCase);
private static Regex s_RegexFn = new Regex(@"[c-g|x|p|r]\d*", RegexOptions.IgnoreCase);
private static Regex s_RegexNewLine = new Regex(@"[\\|/]+n", RegexOptions.IgnoreCase);
+ private static Regex s_RegexForAxisLabel = new Regex(@"{value(:[c-g|x|p|r]\d*)?}", RegexOptions.IgnoreCase);
+ private static Regex s_RegexSubForAxisLabel = new Regex(@"(value)|([c-g|x|p|r]\d*)", RegexOptions.IgnoreCase);
+ private static Regex s_RegexForSerieLabel = new Regex(@"{[a-d](:[c-g|x|p|r]\d*)?}", RegexOptions.IgnoreCase);
+ private static Regex s_RegexSubForSerieLabel = new Regex(@"([a-d])|([c-g|x|p|r]\d*)", RegexOptions.IgnoreCase);
///
/// 替换字符串中的通配符,支持的通配符有{.}、{a}、{b}、{c}、{d}。
@@ -175,5 +179,63 @@ namespace XCharts
{
return s_RegexNewLine.Replace(content.Trim(), PH_NN);
}
+
+ public static void ReplaceAxisLabelContent(ref string content, string numericFormatter, float value)
+ {
+ var mc = s_RegexForAxisLabel.Matches(content);
+ foreach (var m in mc)
+ {
+ var old = m.ToString();
+ var args = s_RegexSubForAxisLabel.Matches(m.ToString());
+ var argsCount = args.Count;
+ if (argsCount <= 0) continue;
+ if (argsCount >= 2)
+ {
+ numericFormatter = args[1].ToString();
+ }
+ content = content.Replace(old, ChartCached.FloatToStr(value, numericFormatter));
+ }
+ content = TrimAndReplaceLine(content);
+ }
+
+ public static void ReplaceSerieLabelContent(ref string content, string numericFormatter, float value, float total,
+ string serieName, string dataName)
+ {
+ var mc = s_RegexForSerieLabel.Matches(content);
+ foreach (var m in mc)
+ {
+ var old = m.ToString();
+ var args = s_RegexSubForSerieLabel.Matches(old);
+ var argsCount = args.Count;
+ if (argsCount <= 0) continue;
+ var p = args[0].ToString().ElementAt(0);
+ if (argsCount >= 2)
+ {
+ numericFormatter = args[1].ToString();
+ }
+ if (p == 'a' || p == 'A')
+ {
+ content = content.Replace(old, serieName);
+ }
+ else if (p == 'b' || p == 'B')
+ {
+ content = content.Replace(old, dataName);
+ }
+ else if (p == 'c' || p == 'C' || p == 'd' || p == 'D')
+ {
+ var isPercent = p == 'd' || p == 'D';
+ if (isPercent)
+ {
+ var percent = total == 0 ? 0 : value / total * 100;
+ content = content.Replace(old, ChartCached.FloatToStr(percent, numericFormatter));
+ }
+ else
+ {
+ content = content.Replace(old, ChartCached.FloatToStr(value, numericFormatter));
+ }
+ }
+ }
+ content = TrimAndReplaceLine(content);
+ }
}
}
\ No newline at end of file
diff --git a/Assets/XCharts/Runtime/Internal/Helper/SerieLabelHelper.cs b/Assets/XCharts/Runtime/Internal/Helper/SerieLabelHelper.cs
index 63d8724b..7aba8ca3 100644
--- a/Assets/XCharts/Runtime/Internal/Helper/SerieLabelHelper.cs
+++ b/Assets/XCharts/Runtime/Internal/Helper/SerieLabelHelper.cs
@@ -104,19 +104,9 @@ namespace XCharts
return ChartCached.NumberToStr(dataValue, numericFormatter);
else
{
- var content = serieLabel.formatter.Replace("{a}", serieName);
- content = content.Replace("{b}", dataName);
- content = content.Replace("{c}", ChartCached.NumberToStr(dataValue, numericFormatter));
- content = content.Replace("{c:f0}", ChartCached.IntToStr((int)Mathf.Round(dataValue)));
- content = content.Replace("{c:f1}", ChartCached.FloatToStr(dataValue, string.Empty, 1));
- content = content.Replace("{c:f2}", ChartCached.FloatToStr(dataValue, string.Empty, 2));
- var percent = dataValue == 0 && dataTotal == 0 ? 0 : dataValue / dataTotal * 100;
- content = content.Replace("{d}", ChartCached.NumberToStr(percent, numericFormatter));
- content = content.Replace("{d:f0}", ChartCached.IntToStr((int)Mathf.Round(percent)));
- content = content.Replace("{d:f1}", ChartCached.FloatToStr(percent, string.Empty, 1));
- content = content.Replace("{d:f2}", ChartCached.FloatToStr(percent, string.Empty, 2));
- content = content.Replace("\\n", "\n");
- content = content.Replace("
", "\n");
+ var content = serieLabel.formatter;
+ FormatterHelper.ReplaceSerieLabelContent(ref content, numericFormatter, dataValue,
+ dataTotal, serieName, dataName);
return content;
}
}