增加Formatter配置Axis的AxisLabel的格式化输出

This commit is contained in:
monitor1394
2019-09-23 19:09:56 +08:00
parent 32967df5c9
commit 48c867b1ef
6 changed files with 64 additions and 17 deletions

View File

@@ -14,6 +14,7 @@ namespace XCharts
Rect drawRect = pos; Rect drawRect = pos;
drawRect.height = EditorGUIUtility.singleLineHeight; drawRect.height = EditorGUIUtility.singleLineHeight;
SerializedProperty show = prop.FindPropertyRelative("m_Show"); SerializedProperty show = prop.FindPropertyRelative("m_Show");
SerializedProperty m_Formatter = prop.FindPropertyRelative("m_Formatter");
SerializedProperty m_Inside = prop.FindPropertyRelative("m_Inside"); SerializedProperty m_Inside = prop.FindPropertyRelative("m_Inside");
SerializedProperty m_Interval = prop.FindPropertyRelative("m_Interval"); SerializedProperty m_Interval = prop.FindPropertyRelative("m_Interval");
SerializedProperty m_Rotate = prop.FindPropertyRelative("m_Rotate"); SerializedProperty m_Rotate = prop.FindPropertyRelative("m_Rotate");
@@ -22,9 +23,9 @@ namespace XCharts
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");
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;
if (ChartEditorHelper.IsToggle(m_AxisLabelToggle,prop)) if (ChartEditorHelper.IsToggle(m_AxisLabelToggle, prop))
{ {
++EditorGUI.indentLevel; ++EditorGUI.indentLevel;
EditorGUI.PropertyField(drawRect, m_Inside); EditorGUI.PropertyField(drawRect, m_Inside);
@@ -41,6 +42,8 @@ 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_Formatter);
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
--EditorGUI.indentLevel; --EditorGUI.indentLevel;
} }
} }
@@ -48,9 +51,9 @@ namespace XCharts
public override float GetPropertyHeight(SerializedProperty prop, GUIContent label) public override float GetPropertyHeight(SerializedProperty prop, GUIContent label)
{ {
float height = 0; float height = 0;
if (ChartEditorHelper.IsToggle(m_AxisLabelToggle,prop)) if (ChartEditorHelper.IsToggle(m_AxisLabelToggle, prop))
{ {
height += 7 * EditorGUIUtility.singleLineHeight + 6 * EditorGUIUtility.standardVerticalSpacing; height += 8 * EditorGUIUtility.singleLineHeight + 7 * EditorGUIUtility.standardVerticalSpacing;
} }
return height; return height;
} }

View File

@@ -440,15 +440,7 @@ namespace XCharts
{ {
value = (minValue + (maxValue - minValue) * index / (split - 1)); value = (minValue + (maxValue - minValue) * index / (split - 1));
} }
if (_cacheValue2str.ContainsKey(value)) return _cacheValue2str[value]; return m_AxisLabel.GetFormatterContent(value);
else
{
if (value - (int)value == 0)
_cacheValue2str[value] = (value).ToString();
else
_cacheValue2str[value] = (value).ToString("f1");
return _cacheValue2str[value];
}
} }
var showData = GetDataList(dataZoom); var showData = GetDataList(dataZoom);
int dataCount = showData.Count; int dataCount = showData.Count;
@@ -456,7 +448,7 @@ namespace XCharts
if (index == split - 1 && !m_BoundaryGap) if (index == split - 1 && !m_BoundaryGap)
{ {
return showData[dataCount - 1]; return m_AxisLabel.GetFormatterContent(showData[dataCount - 1]);
} }
else else
{ {
@@ -465,7 +457,7 @@ namespace XCharts
int offset = m_BoundaryGap ? (int)(rate / 2) : 0; int offset = m_BoundaryGap ? (int)(rate / 2) : 0;
int newIndex = (int)(index * rate >= dataCount - 1 ? int newIndex = (int)(index * rate >= dataCount - 1 ?
dataCount - 1 : offset + index * rate); dataCount - 1 : offset + index * rate);
return showData[newIndex]; return m_AxisLabel.GetFormatterContent(showData[newIndex]);
} }
} }

View File

@@ -90,7 +90,7 @@ namespace XCharts
/// <summary> /// <summary>
/// 图例内容字符串模版格式器。支持用 \n 换行。 /// 图例内容字符串模版格式器。支持用 \n 换行。
/// 模板变量为图例名称 {name} /// 模板变量为图例名称 {name}
/// </example> /// </summary>
public string formatter { get { return m_Formatter; } set { m_Formatter = value; } } public string formatter { get { return m_Formatter; } set { m_Formatter = value; } }
/// <summary> /// <summary>
/// Data array of legend. An array item is usually a name representing string. (If it is a pie chart, /// Data array of legend. An array item is usually a name representing string. (If it is a pie chart,

View File

@@ -11,6 +11,7 @@ namespace XCharts
public class AxisLabel public class AxisLabel
{ {
[SerializeField] private bool m_Show = true; [SerializeField] private bool m_Show = true;
[SerializeField] private string m_Formatter;
[SerializeField] private int m_Interval = 0; [SerializeField] private int m_Interval = 0;
[SerializeField] private bool m_Inside = false; [SerializeField] private bool m_Inside = false;
[SerializeField] private float m_Rotate; [SerializeField] private float m_Rotate;
@@ -59,6 +60,11 @@ 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>
/// 图例内容字符串模版格式器。支持用 \n 换行。
/// 模板变量为图例名称 {value}{value:f1} 表示取1为小数
/// </summary>
public string formatter { get { return m_Formatter; } set { m_Formatter = value; } }
public static AxisLabel defaultAxisLabel public static AxisLabel defaultAxisLabel
{ {
@@ -87,6 +93,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_Formatter = other.formatter;
} }
public override bool Equals(object obj) public override bool Equals(object obj)
@@ -103,12 +110,55 @@ namespace XCharts
m_Margin == other.margin && m_Margin == other.margin &&
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_Formatter == other.formatter;
} }
public override int GetHashCode() public override int GetHashCode()
{ {
return base.GetHashCode(); return base.GetHashCode();
} }
public string GetFormatterContent(string category)
{
if (string.IsNullOrEmpty(m_Formatter))
return category;
else
{
var content = m_Formatter.Replace("{value}", category);
content = content.Replace("\\n", "\n");
content = content.Replace("<br/>", "\n");
return content;
}
}
public string GetFormatterContent(float value)
{
if (string.IsNullOrEmpty(m_Formatter))
if (value - (int)value == 0)
return ChartCached.IntToStr((int)value);
else
return ChartCached.FloatToStr(value, 1);
else
{
var content = m_Formatter;
if (content.Contains("{value:f2}"))
content = m_Formatter.Replace("{value:f2}", ChartCached.FloatToStr(value, 2));
else if (content.Contains("{value:f1}"))
content = m_Formatter.Replace("{value:f1}", ChartCached.FloatToStr(value, 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 = content.Replace("\\n", "\n");
content = content.Replace("<br/>", "\n");
return content;
}
}
} }
} }

View File

@@ -12,6 +12,7 @@ namespace XCharts
public static string FloatToStr(float value, int f = 0) public static string FloatToStr(float value, int f = 0)
{ {
if (f > 2) f = 2;
Dictionary<float, string> valueDic; Dictionary<float, string> valueDic;
if (f == 1) valueDic = s_ValueToF1Str; if (f == 1) valueDic = s_ValueToF1Str;
else if (f == 2) valueDic = s_ValueToF2Str; else if (f == 2) valueDic = s_ValueToF2Str;

View File

@@ -23,6 +23,7 @@ QQ交流群XCharts交流群202030963
## 更新日志 ## 更新日志
* 2019.09.23)增加`Formatter`配置`Axis``AxisLabel`的格式化输出
* 2019.09.23)增加`Tooltip``FontSize``FontStyle`配置字体大小和样式 * 2019.09.23)增加`Tooltip``FontSize``FontStyle`配置字体大小和样式
* 2019.09.23)增加`Formatter`配置`SerieLabel``Legend``Tooltip`的格式化输出 * 2019.09.23)增加`Formatter`配置`SerieLabel``Legend``Tooltip`的格式化输出
* 2019.09.19)增加`LineArrow`配置带箭头曲线 * 2019.09.19)增加`LineArrow`配置带箭头曲线