增加科学计数法显示数值的支持

This commit is contained in:
monitor1394
2019-10-26 05:02:32 +08:00
parent 8c4ef814c8
commit bdf46ea785
9 changed files with 78 additions and 23 deletions

View File

@@ -29,6 +29,7 @@ namespace XCharts
SerializedProperty m_Color = prop.FindPropertyRelative("m_Color"); SerializedProperty m_Color = prop.FindPropertyRelative("m_Color");
SerializedProperty m_FontSize = prop.FindPropertyRelative("m_FontSize"); SerializedProperty m_FontSize = prop.FindPropertyRelative("m_FontSize");
SerializedProperty m_FontStyle = prop.FindPropertyRelative("m_FontStyle"); SerializedProperty m_FontStyle = prop.FindPropertyRelative("m_FontStyle");
SerializedProperty m_ForceENotation = prop.FindPropertyRelative("m_ForceENotation");
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; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
@@ -51,6 +52,8 @@ namespace XCharts
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
EditorGUI.PropertyField(drawRect, m_Formatter); EditorGUI.PropertyField(drawRect, m_Formatter);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
EditorGUI.PropertyField(drawRect, m_ForceENotation);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
--EditorGUI.indentLevel; --EditorGUI.indentLevel;
} }
} }
@@ -60,7 +63,7 @@ namespace XCharts
float height = 0; float height = 0;
if (ChartEditorHelper.IsToggle(m_AxisLabelToggle, prop)) if (ChartEditorHelper.IsToggle(m_AxisLabelToggle, prop))
{ {
height += 8 * EditorGUIUtility.singleLineHeight + 7 * EditorGUIUtility.standardVerticalSpacing; height += 9 * EditorGUIUtility.singleLineHeight + 10 * EditorGUIUtility.standardVerticalSpacing;
} }
return height; return height;
} }

View File

@@ -42,6 +42,7 @@ namespace XCharts
SerializedProperty m_Border = prop.FindPropertyRelative("m_Border"); SerializedProperty m_Border = prop.FindPropertyRelative("m_Border");
SerializedProperty m_BorderWidth = prop.FindPropertyRelative("m_BorderWidth"); SerializedProperty m_BorderWidth = prop.FindPropertyRelative("m_BorderWidth");
SerializedProperty m_BorderColor = prop.FindPropertyRelative("m_BorderColor"); SerializedProperty m_BorderColor = prop.FindPropertyRelative("m_BorderColor");
SerializedProperty m_ForceENotation = prop.FindPropertyRelative("m_ForceENotation");
ChartEditorHelper.MakeFoldout(ref drawRect, ref m_SerieLabelToggle, prop, null, show, false); ChartEditorHelper.MakeFoldout(ref drawRect, ref m_SerieLabelToggle, prop, null, show, false);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
@@ -100,6 +101,8 @@ namespace XCharts
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
EditorGUI.PropertyField(drawRect, m_LineLength2); EditorGUI.PropertyField(drawRect, m_LineLength2);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
EditorGUI.PropertyField(drawRect, m_ForceENotation);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
--EditorGUI.indentLevel; --EditorGUI.indentLevel;
} }
} }
@@ -109,7 +112,7 @@ namespace XCharts
float height = 0; float height = 0;
if (ChartEditorHelper.IsToggle(m_SerieLabelToggle, prop)) if (ChartEditorHelper.IsToggle(m_SerieLabelToggle, prop))
{ {
height += 22 * EditorGUIUtility.singleLineHeight + 21 * EditorGUIUtility.standardVerticalSpacing; height += 23 * EditorGUIUtility.singleLineHeight + 22 * EditorGUIUtility.standardVerticalSpacing;
} }
else else
{ {

View File

@@ -28,6 +28,7 @@ namespace XCharts
SerializedProperty m_MinHeight = prop.FindPropertyRelative("m_MinHeight"); SerializedProperty m_MinHeight = prop.FindPropertyRelative("m_MinHeight");
SerializedProperty m_FontSize = prop.FindPropertyRelative("m_FontSize"); SerializedProperty m_FontSize = prop.FindPropertyRelative("m_FontSize");
SerializedProperty m_FontStyle = prop.FindPropertyRelative("m_FontStyle"); SerializedProperty m_FontStyle = prop.FindPropertyRelative("m_FontStyle");
SerializedProperty m_ForceENotation = prop.FindPropertyRelative("m_ForceENotation");
ChartEditorHelper.MakeFoldout(ref drawRect, ref m_TooltipModuleToggle, "Tooltip", show); ChartEditorHelper.MakeFoldout(ref drawRect, ref m_TooltipModuleToggle, "Tooltip", show);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
@@ -49,13 +50,15 @@ namespace XCharts
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
EditorGUI.PropertyField(drawRect, m_FontStyle); EditorGUI.PropertyField(drawRect, m_FontStyle);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
EditorGUI.PropertyField(drawRect, m_ForceENotation);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
} }
} }
public override float GetPropertyHeight(SerializedProperty prop, GUIContent label) public override float GetPropertyHeight(SerializedProperty prop, GUIContent label)
{ {
if (m_TooltipModuleToggle) if (m_TooltipModuleToggle)
return 9 * EditorGUIUtility.singleLineHeight + 8 * EditorGUIUtility.standardVerticalSpacing; return 10 * EditorGUIUtility.singleLineHeight + 9 * EditorGUIUtility.standardVerticalSpacing;
else else
return EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; return EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
} }

View File

@@ -55,6 +55,7 @@ namespace XCharts
[SerializeField] private float m_MinHeight = 0; [SerializeField] private float m_MinHeight = 0;
[SerializeField] private int m_FontSize = 18; [SerializeField] private int m_FontSize = 18;
[SerializeField] private FontStyle m_FontStyle = FontStyle.Normal; [SerializeField] private FontStyle m_FontStyle = FontStyle.Normal;
[SerializeField] private bool m_ForceENotation = false;
private GameObject m_GameObject; private GameObject m_GameObject;
private GameObject m_Content; private GameObject m_Content;
@@ -114,6 +115,10 @@ namespace XCharts
/// 文字的字体风格。 /// 文字的字体风格。
/// </summary> /// </summary>
public FontStyle fontStyle { get { return m_FontStyle; } set { m_FontStyle = value; } } 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> /// <summary>
/// The data index currently indicated by Tooltip. /// The data index currently indicated by Tooltip.
@@ -355,7 +360,7 @@ namespace XCharts
{ {
content = content.Replace("{a}", serie.name); content = content.Replace("{a}", serie.name);
content = content.Replace("{b}", needCategory ? category : serieData.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) //if (serie.type == SerieType.Pie)
{ {
var percent = serieData.GetData(1) / serie.yTotal * 100; var percent = serieData.GetData(1) / serie.yTotal * 100;
@@ -364,7 +369,7 @@ namespace XCharts
} }
content = content.Replace("{a" + i + "}", serie.name); content = content.Replace("{a" + i + "}", serie.name);
content = content.Replace("{b" + i + "}", needCategory ? category : serieData.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) //if (serie.type == SerieType.Pie)
{ {
var percent = serieData.GetData(1) / serie.yTotal * 100; var percent = serieData.GetData(1) / serie.yTotal * 100;
@@ -381,12 +386,12 @@ namespace XCharts
public string GetFormatterContent(string serieName, string dataName, float dataValue) public string GetFormatterContent(string serieName, string dataName, float dataValue)
{ {
if (string.IsNullOrEmpty(m_Formatter)) if (string.IsNullOrEmpty(m_Formatter))
return ChartCached.FloatToStr(dataValue); return ChartCached.FloatToStr(dataValue, 0, m_ForceENotation);
else else
{ {
var content = m_Formatter.Replace("{a}", serieName); var content = m_Formatter.Replace("{a}", serieName);
content = content.Replace("{b}", dataName); 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("\\n", "\n");
content = content.Replace("<br/>", "\n"); content = content.Replace("<br/>", "\n");
return content; return content;

View File

@@ -26,6 +26,8 @@ namespace XCharts
[SerializeField] private Color m_Color; [SerializeField] private Color m_Color;
[SerializeField] private int m_FontSize; [SerializeField] private int m_FontSize;
[SerializeField] private FontStyle m_FontStyle; [SerializeField] private FontStyle m_FontStyle;
[SerializeField] private bool m_ForceENotation = false;
/// <summary> /// <summary>
/// Set this to false to prevent the axis label from appearing. /// Set this to false to prevent the axis label from appearing.
@@ -72,6 +74,10 @@ namespace XCharts
/// 模板变量为图例名称 {value}{value:f1} 表示取1为小数 /// 模板变量为图例名称 {value}{value:f1} 表示取1为小数
/// </summary> /// </summary>
public string formatter { get { return m_Formatter; } set { m_Formatter = value; } } 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 public static AxisLabel defaultAxisLabel
{ {
@@ -118,6 +124,7 @@ namespace XCharts
m_Color == other.color && m_Color == other.color &&
m_FontSize == other.fontSize && m_FontSize == other.fontSize &&
m_FontStyle == other.fontStyle && m_FontStyle == other.fontStyle &&
m_ForceENotation == other.forceENotation &&
m_Formatter == other.formatter; 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 (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); return ChartCached.IntToStr((int)value);
else else
return ChartCached.FloatToStr(value, 1); return ChartCached.FloatToStr(value, 1);
}
else else
{ {
var content = m_Formatter; var content = m_Formatter;

View File

@@ -99,6 +99,7 @@ namespace XCharts
[SerializeField] private bool m_Border = true; [SerializeField] private bool m_Border = true;
[SerializeField] private float m_BorderWidth = 0.5f; [SerializeField] private float m_BorderWidth = 0.5f;
[SerializeField] private Color m_BorderColor = Color.grey; [SerializeField] private Color m_BorderColor = Color.grey;
[SerializeField] private bool m_ForceENotation = false;
/// <summary> /// <summary>
/// Whether the label is showed. /// Whether the label is showed.
/// 是否显示文本标签。 /// 是否显示文本标签。
@@ -220,16 +221,20 @@ namespace XCharts
/// 边框颜色。 /// 边框颜色。
/// </summary> /// </summary>
public Color borderColor { get { return m_BorderColor; } set { m_BorderColor = value; } } 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) public string GetFormatterContent(string serieName, string dataName, float dataValue, float dataTotal = 0)
{ {
if (string.IsNullOrEmpty(m_Formatter)) if (string.IsNullOrEmpty(m_Formatter))
return ChartCached.FloatToStr(dataValue); return ChartCached.FloatToStr(dataValue, 0, m_ForceENotation);
else else
{ {
var content = m_Formatter.Replace("{a}", serieName); var content = m_Formatter.Replace("{a}", serieName);
content = content.Replace("{b}", dataName); 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:f0}", ChartCached.IntToStr((int)Mathf.Round(dataValue)));
content = content.Replace("{c:f1}", ChartCached.FloatToStr(dataValue, 1)); content = content.Replace("{c:f1}", ChartCached.FloatToStr(dataValue, 1));
content = content.Replace("{c:f2}", ChartCached.FloatToStr(dataValue, 2)); content = content.Replace("{c:f2}", ChartCached.FloatToStr(dataValue, 2));

View File

@@ -556,7 +556,7 @@ namespace XCharts
sb.Append(serie.name).Append("\n"); sb.Append(serie.name).Append("\n");
} }
sb.Append("<color=#").Append(m_ThemeInfo.GetColorStr(index)).Append(">● </color>") sb.Append("<color=#").Append(m_ThemeInfo.GetColorStr(index)).Append(">● </color>")
.Append(key).Append(": ").Append(ChartCached.FloatToStr(value)); .Append(key).Append(": ").Append(ChartCached.FloatToStr(value, 0, m_Tooltip.forceENotation));
m_Tooltip.UpdateContentText(sb.ToString()); m_Tooltip.UpdateContentText(sb.ToString());
} }
else else

View File

@@ -213,7 +213,7 @@ namespace XCharts
int indicatorNum = radar.indicatorList.Count; int indicatorNum = radar.indicatorList.Count;
var angle = 2 * Mathf.PI / indicatorNum; var angle = 2 * Mathf.PI / indicatorNum;
Vector3 p = radar.centerPos; Vector3 p = radar.centerPos;
serie.animation.InitProgress(1,0,1); serie.animation.InitProgress(1, 0, 1);
if (!IsActive(i)) if (!IsActive(i))
{ {
continue; continue;
@@ -507,7 +507,7 @@ namespace XCharts
string key = radar.indicatorList[i].name; string key = radar.indicatorList[i].name;
float value = serieData.GetData(i); float value = serieData.GetData(i);
sb.Append("\n"); sb.Append("\n");
sb.AppendFormat("{0}: {1}", key, value); sb.AppendFormat("{0}: {1}", key, ChartCached.FloatToStr(value, 0, m_Tooltip.forceENotation));
} }
m_Tooltip.UpdateContentText(sb.ToString()); m_Tooltip.UpdateContentText(sb.ToString());
var pos = m_Tooltip.GetContentPos(); var pos = m_Tooltip.GetContentPos();

View File

@@ -13,31 +13,51 @@ namespace XCharts
public static class ChartCached public static class ChartCached
{ {
private static Dictionary<float, string> s_ValueToF1Str = new Dictionary<float, string>(1000); private static Dictionary<float, string> s_ValueToF1Str = new Dictionary<float, string>(1000);
private static Dictionary<float, string> s_ValueToE1Str = new Dictionary<float, string>(1000);
private static Dictionary<float, string> s_ValueToF2Str = new Dictionary<float, string>(1000); private static Dictionary<float, string> s_ValueToF2Str = new Dictionary<float, string>(1000);
private static Dictionary<float, string> s_ValueToStr = new Dictionary<float, string>(1000); private static Dictionary<float, string> s_ValueToE2Str = new Dictionary<float, string>(1000);
private static Dictionary<float, string> s_ValueToFnStr = new Dictionary<float, string>(1000);
private static Dictionary<float, string> s_ValueToEnStr = new Dictionary<float, string>(1000);
private static Dictionary<float, string> s_ValueToFStr = new Dictionary<float, string>(1000);
private static Dictionary<float, string> s_ValueToEStr = new Dictionary<float, string>(1000);
private static Dictionary<int, string> s_IntToStr = new Dictionary<int, string>(1000); private static Dictionary<int, string> s_IntToStr = new Dictionary<int, string>(1000);
private static Dictionary<int, string> s_IntToFn = new Dictionary<int, string>(20);
private static Dictionary<Color, string> s_ColorToStr = new Dictionary<Color, string>(1000); private static Dictionary<Color, string> s_ColorToStr = new Dictionary<Color, string>(1000);
public static string FloatToStr(float value, int f = 0) public static string FloatToStr(float value, int f = 0, bool forceE = false)
{ {
if (f > 2) f = 2;
Dictionary<float, string> valueDic; Dictionary<float, string> valueDic;
if (f == 1) valueDic = s_ValueToF1Str; if (f == 0) valueDic = forceE ? s_ValueToEStr : s_ValueToFStr;
else if (f == 2) valueDic = s_ValueToF2Str; if (f == 1) valueDic = forceE ? s_ValueToE1Str : s_ValueToF1Str;
else valueDic = s_ValueToStr; else if (f == 2) valueDic = forceE ? s_ValueToE2Str : s_ValueToF2Str;
else valueDic = forceE ? s_ValueToEnStr : s_ValueToFnStr;
if (valueDic.ContainsKey(value)) if (valueDic.ContainsKey(value))
{ {
return valueDic[value]; return valueDic[value];
} }
else else
{ {
if (f == 1) valueDic[value] = value.ToString("f1"); if (f == 0) valueDic[value] = forceE ? value.ToString("E") : value.ToString();
else if (f == 1) valueDic[value] = value.ToString("f1");
else if (f == 2) valueDic[value] = value.ToString("f2"); else if (f == 2) valueDic[value] = value.ToString("f2");
else valueDic[value] = value.ToString(); else valueDic[value] = (f > 3 || forceE) ? value.ToString("E1") : value.ToString(GetFn(f));
return valueDic[value]; return valueDic[value];
} }
} }
private static string GetFn(int f)
{
if (s_IntToFn.ContainsKey(f))
{
return s_IntToFn[f];
}
else
{
s_IntToFn[f] = "f" + f;
return s_IntToFn[f];
}
}
public static string IntToStr(int value) public static string IntToStr(int value)
{ {
if (s_IntToStr.ContainsKey(value)) if (s_IntToStr.ContainsKey(value))