mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-24 01:40:06 +00:00
增加科学计数法显示数值的支持
This commit is contained in:
@@ -55,6 +55,7 @@ namespace XCharts
|
||||
[SerializeField] private float m_MinHeight = 0;
|
||||
[SerializeField] private int m_FontSize = 18;
|
||||
[SerializeField] private FontStyle m_FontStyle = FontStyle.Normal;
|
||||
[SerializeField] private bool m_ForceENotation = false;
|
||||
|
||||
private GameObject m_GameObject;
|
||||
private GameObject m_Content;
|
||||
@@ -114,6 +115,10 @@ namespace XCharts
|
||||
/// 文字的字体风格。
|
||||
/// </summary>
|
||||
public FontStyle fontStyle { get { return m_FontStyle; } set { m_FontStyle = value; } }
|
||||
/// <summary>
|
||||
/// 是否强制使用科学计数法格式化显示数值。默认为false,当小数精度大于3时才采用科学计数法。
|
||||
/// </summary>
|
||||
public bool forceENotation { get { return m_ForceENotation; } set { m_ForceENotation = value; } }
|
||||
|
||||
/// <summary>
|
||||
/// The data index currently indicated by Tooltip.
|
||||
@@ -355,7 +360,7 @@ namespace XCharts
|
||||
{
|
||||
content = content.Replace("{a}", serie.name);
|
||||
content = content.Replace("{b}", needCategory ? category : serieData.name);
|
||||
content = content.Replace("{c}", ChartCached.FloatToStr(serieData.GetData(1)));
|
||||
content = content.Replace("{c}", ChartCached.FloatToStr(serieData.GetData(1), 0, m_ForceENotation));
|
||||
//if (serie.type == SerieType.Pie)
|
||||
{
|
||||
var percent = serieData.GetData(1) / serie.yTotal * 100;
|
||||
@@ -364,7 +369,7 @@ namespace XCharts
|
||||
}
|
||||
content = content.Replace("{a" + i + "}", serie.name);
|
||||
content = content.Replace("{b" + i + "}", needCategory ? category : serieData.name);
|
||||
content = content.Replace("{c" + i + "}", ChartCached.FloatToStr(serieData.GetData(1)));
|
||||
content = content.Replace("{c" + i + "}", ChartCached.FloatToStr(serieData.GetData(1), 0, m_ForceENotation));
|
||||
//if (serie.type == SerieType.Pie)
|
||||
{
|
||||
var percent = serieData.GetData(1) / serie.yTotal * 100;
|
||||
@@ -381,12 +386,12 @@ namespace XCharts
|
||||
public string GetFormatterContent(string serieName, string dataName, float dataValue)
|
||||
{
|
||||
if (string.IsNullOrEmpty(m_Formatter))
|
||||
return ChartCached.FloatToStr(dataValue);
|
||||
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));
|
||||
content = content.Replace("{c}", ChartCached.FloatToStr(dataValue, 0, m_ForceENotation));
|
||||
content = content.Replace("\\n", "\n");
|
||||
content = content.Replace("<br/>", "\n");
|
||||
return content;
|
||||
|
||||
@@ -26,6 +26,8 @@ namespace XCharts
|
||||
[SerializeField] private Color m_Color;
|
||||
[SerializeField] private int m_FontSize;
|
||||
[SerializeField] private FontStyle m_FontStyle;
|
||||
[SerializeField] private bool m_ForceENotation = false;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Set this to false to prevent the axis label from appearing.
|
||||
@@ -72,6 +74,10 @@ namespace XCharts
|
||||
/// 模板变量为图例名称 {value},{value:f1} 表示取1为小数
|
||||
/// </summary>
|
||||
public string formatter { get { return m_Formatter; } set { m_Formatter = value; } }
|
||||
/// <summary>
|
||||
/// 是否强制使用科学计数法格式化显示数值。默认为false,当小数精度大于3时才采用科学计数法。
|
||||
/// </summary>
|
||||
public bool forceENotation { get { return m_ForceENotation; } set { m_ForceENotation = value; } }
|
||||
|
||||
public static AxisLabel defaultAxisLabel
|
||||
{
|
||||
@@ -118,6 +124,7 @@ namespace XCharts
|
||||
m_Color == other.color &&
|
||||
m_FontSize == other.fontSize &&
|
||||
m_FontStyle == other.fontStyle &&
|
||||
m_ForceENotation == other.forceENotation &&
|
||||
m_Formatter == other.formatter;
|
||||
}
|
||||
|
||||
@@ -139,13 +146,22 @@ namespace XCharts
|
||||
}
|
||||
}
|
||||
|
||||
public string GetFormatterContent(float value)
|
||||
public string GetFormatterContent(float value, float minValue, float maxValue)
|
||||
{
|
||||
if (string.IsNullOrEmpty(m_Formatter))
|
||||
if (value - (int)value == 0)
|
||||
{
|
||||
if (minValue >= -1 && minValue <= 1 && maxValue >= -1 && maxValue <= 1)
|
||||
{
|
||||
int minAcc = ChartHelper.GetFloatAccuracy(minValue);
|
||||
int maxAcc = ChartHelper.GetFloatAccuracy(maxValue);
|
||||
int acc = Mathf.Max(minAcc, maxAcc);
|
||||
return ChartCached.FloatToStr(value, acc, m_ForceENotation);
|
||||
}
|
||||
else if (value - (int)value == 0)
|
||||
return ChartCached.IntToStr((int)value);
|
||||
else
|
||||
return ChartCached.FloatToStr(value, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
var content = m_Formatter;
|
||||
|
||||
@@ -99,6 +99,7 @@ namespace XCharts
|
||||
[SerializeField] private bool m_Border = true;
|
||||
[SerializeField] private float m_BorderWidth = 0.5f;
|
||||
[SerializeField] private Color m_BorderColor = Color.grey;
|
||||
[SerializeField] private bool m_ForceENotation = false;
|
||||
/// <summary>
|
||||
/// Whether the label is showed.
|
||||
/// 是否显示文本标签。
|
||||
@@ -220,16 +221,20 @@ namespace XCharts
|
||||
/// 边框颜色。
|
||||
/// </summary>
|
||||
public Color borderColor { get { return m_BorderColor; } set { m_BorderColor = value; } }
|
||||
/// <summary>
|
||||
/// 是否强制使用科学计数法格式化显示数值。默认为false,当小数精度大于3时才采用科学计数法。
|
||||
/// </summary>
|
||||
public bool forceENotation { get { return m_ForceENotation; } set { m_ForceENotation = value; } }
|
||||
|
||||
public string GetFormatterContent(string serieName, string dataName, float dataValue, float dataTotal = 0)
|
||||
{
|
||||
if (string.IsNullOrEmpty(m_Formatter))
|
||||
return ChartCached.FloatToStr(dataValue);
|
||||
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));
|
||||
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));
|
||||
|
||||
Reference in New Issue
Block a user