增加AxisLabelSerieLabelformatter可单独配置数值格式化#68

This commit is contained in:
monitor1394
2020-07-21 09:13:51 +08:00
parent 3860d1c862
commit 2cafbc7706
6 changed files with 70 additions and 34 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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("<br/>", "\n");
return content;
}
else
{
return value.ToString(m_Formatter);
var content = m_Formatter;
FormatterHelper.ReplaceAxisLabelContent(ref content, numericFormatter, value);
return content;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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);
/// <summary>
/// 替换字符串中的通配符,支持的通配符有{.}、{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);
}
}
}

View File

@@ -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("<br/>", "\n");
var content = serieLabel.formatter;
FormatterHelper.ReplaceSerieLabelContent(ref content, numericFormatter, dataValue,
dataTotal, serieName, dataName);
return content;
}
}