增加numericFormatter参数可配置数值格式化显示,去掉forceENotation参数

This commit is contained in:
monitor1394
2020-05-04 13:29:56 +08:00
parent 9bfc1fc9a6
commit f6c614e8c4
19 changed files with 211 additions and 149 deletions

View File

@@ -27,7 +27,7 @@ namespace XCharts
[SerializeField] private Color m_Color;
[SerializeField] private int m_FontSize;
[SerializeField] private FontStyle m_FontStyle;
[SerializeField] private bool m_ForceENotation = false;
[SerializeField] private string m_NumericFormatter = "";
[SerializeField] private bool m_ShowAsPositiveNumber = false;
[SerializeField] private bool m_OnZero = false;
[SerializeField] private TextLimit m_TextLimit = new TextLimit();
@@ -106,20 +106,25 @@ namespace XCharts
}
/// <summary>
/// 图例内容字符串模版格式器。支持用 \n 换行。
/// 模板变量为图例名称 {value},支持{value:f0}{value:f1}{value:f2}
/// 模板变量为图例名称 {value}
/// </summary>
public string formatter
{
get { return m_Formatter; }
set { if (PropertyUtility.SetClass(ref m_Formatter, value)) SetComponentDirty(); }
}
/// <summary>
/// 是否强制使用科学计数法格式化显示数值。默认为false当小数精度大于3时才采用科学计数法。
/// Standard numeric format strings.
/// 标准数字格式字符串。用于将数值格式化显示为字符串。
/// 使用Axx的形式A是格式说明符的单字符支持C货币、D十进制、E指数、F定点数、G常规、N数字、P百分比、R往返、X十六进制的。xx是精度说明从0-99。
/// 参考https://docs.microsoft.com/zh-cn/dotnet/standard/base-types/standard-numeric-format-strings
/// </summary>
public bool forceENotation
/// <value></value>
public string numericFormatter
{
get { return m_ForceENotation; }
set { if (PropertyUtility.SetStruct(ref m_ForceENotation, value)) SetComponentDirty(); }
get { return m_NumericFormatter; }
set { if (PropertyUtility.SetClass(ref m_NumericFormatter, value)) SetComponentDirty(); }
}
/// <summary>
@@ -186,7 +191,7 @@ namespace XCharts
axisLable.margin = margin;
axisLable.color = color;
axisLable.fontSize = fontSize;
axisLable.forceENotation = forceENotation;
axisLable.numericFormatter = numericFormatter;
axisLable.textLimit = textLimit.Clone();
return axisLable;
}
@@ -201,7 +206,7 @@ namespace XCharts
margin = axisLable.margin;
color = axisLable.color;
fontSize = axisLable.fontSize;
forceENotation = axisLable.forceENotation;
numericFormatter = axisLable.numericFormatter;
textLimit.Copy(axisLable.textLimit);
}
@@ -236,10 +241,7 @@ namespace XCharts
{
if (isLog)
{
if (value - (int)value == 0)
return ChartCached.IntToStr((int)value);
else
return ChartCached.FloatToStr(value);
return ChartCached.NumberToStr(value, numericFormatter);
}
if (minValue >= -1 && minValue <= 1 && maxValue >= -1 && maxValue <= 1)
{
@@ -247,12 +249,9 @@ namespace XCharts
int maxAcc = ChartHelper.GetFloatAccuracy(maxValue);
int curAcc = ChartHelper.GetFloatAccuracy(value);
int acc = Mathf.Max(Mathf.Max(minAcc, maxAcc), curAcc);
return ChartCached.FloatToStr(value, acc, m_ForceENotation);
return ChartCached.FloatToStr(value, numericFormatter, acc);
}
else if (value - (int)value == 0)
return ChartCached.IntToStr((int)value);
else
return ChartCached.FloatToStr(value, 1);
return ChartCached.NumberToStr(value, numericFormatter);
}
else if (m_Formatter.Contains("{value"))
{
@@ -260,15 +259,15 @@ namespace XCharts
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, 2));
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, 1));
content = m_Formatter.Replace("{value:f1}", ChartCached.FloatToStr(value, string.Empty, 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 = m_Formatter.Replace("{value}", ChartCached.FloatToStr(value, string.Empty, 1));
}
content = content.Replace("\\n", "\n");

View File

@@ -47,6 +47,7 @@ namespace XCharts
[SerializeField] private Color m_BorderColor;
[SerializeField] [Range(0, 1)] private float m_Opacity = 1;
[SerializeField] private string m_TooltipFormatter;
[SerializeField] private string m_NumericFormatter = "";
[SerializeField] private float[] m_CornerRadius = new float[] { 0, 0, 0, 0 };
/// <summary>
@@ -147,6 +148,18 @@ namespace XCharts
set { if (PropertyUtility.SetClass(ref m_TooltipFormatter, value)) SetVerticesDirty(); }
}
/// <summary>
/// Standard numeric format strings.
/// 标准数字格式字符串。用于将数值格式化显示为字符串。
/// 使用Axx的形式A是格式说明符的单字符支持C货币、D十进制、E指数、F定点数、G常规、N数字、P百分比、R往返、X十六进制的。xx是精度说明从0-99。
/// 参考https://docs.microsoft.com/zh-cn/dotnet/standard/base-types/standard-numeric-format-strings
/// </summary>
/// <value></value>
public string numericFormatter
{
get { return m_NumericFormatter; }
set { if (PropertyUtility.SetClass(ref m_NumericFormatter, value)) SetComponentDirty(); }
}
/// <summary>
/// The radius of rounded corner. Its unit is px. Use array to respectively specify the 4 corner radiuses((clockwise upper left, upper right, bottom right and bottom left)).
/// 圆角半径。用数组分别指定4个圆角半径顺时针左上右上右下左下
/// </summary>

View File

@@ -101,7 +101,7 @@ namespace XCharts
[SerializeField] private bool m_Border = false;
[SerializeField] private float m_BorderWidth = 0.5f;
[SerializeField] private Color m_BorderColor = Color.grey;
[SerializeField] private bool m_ForceENotation = false;
[SerializeField] private string m_NumericFormatter = "";
/// <summary>
/// Whether the label is showed.
@@ -321,38 +321,16 @@ namespace XCharts
set { if (PropertyUtility.SetStruct(ref m_BorderColor, value)) SetVerticesDirty(); }
}
/// <summary>
/// 是否强制使用科学计数法格式化显示数值。默认为false当小数精度大于3时才采用科学计数法。
/// Standard numeric format strings.
/// 标准数字格式字符串。用于将数值格式化显示为字符串。
/// 使用Axx的形式A是格式说明符的单字符支持C货币、D十进制、E指数、F定点数、G常规、N数字、P百分比、R往返、X十六进制的。xx是精度说明从0-99。
/// 参考https://docs.microsoft.com/zh-cn/dotnet/standard/base-types/standard-numeric-format-strings
/// </summary>
public bool forceENotation
/// <value></value>
public string numericFormatter
{
get { return m_ForceENotation; }
set { if (PropertyUtility.SetStruct(ref m_ForceENotation, value)) SetVerticesDirty(); }
}
public string GetFormatterContent(string serieName, string dataName, float dataValue, float dataTotal = 0)
{
if (string.IsNullOrEmpty(m_Formatter))
return ChartCached.FloatToStr(dataValue, 0, m_ForceENotation);
else
{
var content = m_Formatter.Replace("{a}", serieName);
content = content.Replace("{b}", dataName);
content = content.Replace("{c}", ChartCached.FloatToStr(dataValue, 0, m_ForceENotation));
content = content.Replace("{c:f0}", ChartCached.IntToStr((int)Mathf.Round(dataValue)));
content = content.Replace("{c:f1}", ChartCached.FloatToStr(dataValue, 1));
content = content.Replace("{c:f2}", ChartCached.FloatToStr(dataValue, 2));
if (dataTotal > 0)
{
var percent = dataValue / dataTotal * 100;
content = content.Replace("{d}", ChartCached.FloatToStr(percent, 1));
content = content.Replace("{d:f0}", ChartCached.IntToStr((int)Mathf.Round(percent)));
content = content.Replace("{d:f1}", ChartCached.FloatToStr(percent, 1));
content = content.Replace("{d:f2}", ChartCached.FloatToStr(percent, 2));
}
content = content.Replace("\\n", "\n");
content = content.Replace("<br/>", "\n");
return content;
}
get { return m_NumericFormatter; }
set { if (PropertyUtility.SetClass(ref m_NumericFormatter, value)) SetComponentDirty(); }
}
}
}