增加AxisLabel和SerieLabel的formatter委托方法 #145

This commit is contained in:
monitor1394
2021-06-27 12:26:20 +08:00
parent 78923ede3a
commit e7be2adbc9
9 changed files with 86 additions and 16 deletions

View File

@@ -38,8 +38,9 @@
## master ## master
* (2021.06.27) Add `DataZoom`'s `orient` parameter to set horizontal or vertical styles * (2021.06.27) Added `formatter` delegate method to `AxisLabel` and `SerieLabel` (#145)
* (2021.06.21) Add `iconStyle`'s `AutoHideWhenLabelEmpty` to set whether the icon is automatically hidden when `label` is empty * (2021.06.27) Added `DataZoom`'s `orient` parameter to set horizontal or vertical styles
* (2021.06.21) Added `iconStyle`'s `AutoHideWhenLabelEmpty` to set whether the icon is automatically hidden when `label` is empty
# # v2.2.3 # # v2.2.3
@@ -58,7 +59,7 @@
* (2021.06.13) Release `v2.2.1` version * (2021.06.13) Release `v2.2.1` version
* (2021.06.13) Improved support for multiple screens * (2021.06.13) Improved support for multiple screens
* (2021.06.12) Add `iconStyle` `align` parameter to set the horizontal alignment of the icon * (2021.06.12) Added `iconStyle` `align` parameter to set the horizontal alignment of the icon
* (2021.06.12) Improve `Theme` import (#148) * (2021.06.12) Improve `Theme` import (#148)
* (2021.06.10) Fixed compatibility issues with `Unity` version (#154) * (2021.06.10) Fixed compatibility issues with `Unity` version (#154)
* (2021.06.05) Improved Candlestickchart support for inverse (#152) * (2021.06.05) Improved Candlestickchart support for inverse (#152)

View File

@@ -38,6 +38,7 @@
## master ## master
* (2021.06.27) 增加`AxisLabel``SerieLabel``formatter`委托方法 (#145)
* (2021.06.27) 增加`DataZoom``orient`参数设置水平或垂直样式 * (2021.06.27) 增加`DataZoom``orient`参数设置水平或垂直样式
* (2021.06.21) 增加`IconStyle``autoHideWhenLabelEmpty`参数设置当`label`为空时是否自动隐藏图标 * (2021.06.21) 增加`IconStyle``autoHideWhenLabelEmpty`参数设置当`label`为空时是否自动隐藏图标

View File

@@ -30,6 +30,7 @@ namespace XCharts
[SerializeField] private float m_Height = 0f; [SerializeField] private float m_Height = 0f;
[SerializeField] private TextLimit m_TextLimit = new TextLimit(); [SerializeField] private TextLimit m_TextLimit = new TextLimit();
[SerializeField] private TextStyle m_TextStyle = new TextStyle(); [SerializeField] private TextStyle m_TextStyle = new TextStyle();
private DelegateAxisLabelFormatter m_FormatterFunction;
/// <summary> /// <summary>
/// Set this to false to prevent the axis label from appearing. /// Set this to false to prevent the axis label from appearing.
@@ -144,6 +145,11 @@ namespace XCharts
set { if (PropertyUtil.SetClass(ref m_TextStyle, value)) SetComponentDirty(); } set { if (PropertyUtil.SetClass(ref m_TextStyle, value)) SetComponentDirty(); }
} }
public DelegateAxisLabelFormatter formatterFunction
{
set { m_FormatterFunction = value; }
}
public override bool componentDirty { get { return m_ComponentDirty || m_TextLimit.componentDirty; } } public override bool componentDirty { get { return m_ComponentDirty || m_TextLimit.componentDirty; } }
public override void ClearComponentDirty() public override void ClearComponentDirty()
{ {
@@ -201,8 +207,12 @@ namespace XCharts
m_TextLimit.SetRelatedText(txt, labelWidth); m_TextLimit.SetRelatedText(txt, labelWidth);
} }
public string GetFormatterContent(string category) public string GetFormatterContent(int labelIndex, string category)
{ {
if (m_FormatterFunction != null)
{
return m_FormatterFunction(labelIndex, 0, category);
}
if (string.IsNullOrEmpty(category)) return category; if (string.IsNullOrEmpty(category)) return category;
if (string.IsNullOrEmpty(m_Formatter)) if (string.IsNullOrEmpty(m_Formatter))
{ {
@@ -216,12 +226,16 @@ namespace XCharts
} }
} }
public string GetFormatterContent(float value, float minValue, float maxValue, bool isLog = false) public string GetFormatterContent(int labelIndex, float value, float minValue, float maxValue, bool isLog = false)
{ {
if (showAsPositiveNumber && value < 0) if (showAsPositiveNumber && value < 0)
{ {
value = Mathf.Abs(value); value = Mathf.Abs(value);
} }
if (m_FormatterFunction != null)
{
return m_FormatterFunction(labelIndex, value, null);
}
if (string.IsNullOrEmpty(m_Formatter)) if (string.IsNullOrEmpty(m_Formatter))
{ {
if (isLog) if (isLog)
@@ -246,8 +260,14 @@ namespace XCharts
} }
} }
public string GetFormatterDateTime(DateTime dateTime) public string GetFormatterDateTime(int labelIndex, float value)
{ {
if (m_FormatterFunction != null)
{
return m_FormatterFunction(labelIndex, value, null);
}
var timestamp = (int)value;
var dateTime = DateTimeUtil.GetDateTime(timestamp);
var format = string.IsNullOrEmpty(numericFormatter) ? "yyyy/M/d" : numericFormatter; var format = string.IsNullOrEmpty(numericFormatter) ? "yyyy/M/d" : numericFormatter;
if (!string.IsNullOrEmpty(m_Formatter)) if (!string.IsNullOrEmpty(m_Formatter))
{ {

View File

@@ -100,6 +100,7 @@ namespace XCharts
[SerializeField] private string m_NumericFormatter = ""; [SerializeField] private string m_NumericFormatter = "";
[SerializeField] private bool m_AutoOffset = false; [SerializeField] private bool m_AutoOffset = false;
[SerializeField] private TextStyle m_TextStyle = new TextStyle(); [SerializeField] private TextStyle m_TextStyle = new TextStyle();
private DelegateSerieLabelFormatter m_FormatterFunction;
public void Reset() public void Reset()
{ {
@@ -337,6 +338,12 @@ namespace XCharts
set { if (PropertyUtil.SetClass(ref m_TextStyle, value)) SetAllDirty(); } set { if (PropertyUtil.SetClass(ref m_TextStyle, value)) SetAllDirty(); }
} }
public DelegateSerieLabelFormatter formatterFunction
{
get { return m_FormatterFunction; }
set { m_FormatterFunction = value; }
}
public bool IsInside() public bool IsInside()
{ {
return position == Position.Inside || position == Position.Center; return position == Position.Inside || position == Position.Center;

View File

@@ -138,7 +138,7 @@ namespace XCharts
maxValue = -maxValue; maxValue = -maxValue;
} }
if (forcePercent) return string.Format("{0}%", (int)value); if (forcePercent) return string.Format("{0}%", (int)value);
else return axis.axisLabel.GetFormatterContent(value, minValue, maxValue); else return axis.axisLabel.GetFormatterContent(index, value, minValue, maxValue);
} }
else if (axis.type == Axis.AxisType.Log) else if (axis.type == Axis.AxisType.Log)
{ {
@@ -150,7 +150,7 @@ namespace XCharts
minValue = -minValue; minValue = -minValue;
maxValue = -maxValue; maxValue = -maxValue;
} }
return axis.axisLabel.GetFormatterContent(value, minValue, maxValue, true); return axis.axisLabel.GetFormatterContent(index, value, minValue, maxValue, true);
} }
else if (axis.type == Axis.AxisType.Time) else if (axis.type == Axis.AxisType.Time)
{ {
@@ -165,9 +165,7 @@ namespace XCharts
{ {
value = minValue + (maxValue - minValue) * index / split; value = minValue + (maxValue - minValue) * index / split;
} }
var timestamp = (int)value; return axis.axisLabel.GetFormatterDateTime(index, value);
var dateTime = DateTimeUtil.GetDateTime(timestamp);
return axis.axisLabel.GetFormatterDateTime(dateTime);
} }
var showData = axis.GetDataList(dataZoom); var showData = axis.GetDataList(dataZoom);
int dataCount = showData.Count; int dataCount = showData.Count;
@@ -181,12 +179,12 @@ namespace XCharts
var residue = (dataCount - 1) - split * rate; var residue = (dataCount - 1) - split * rate;
var newIndex = residue + (index - 1) * rate; var newIndex = residue + (index - 1) * rate;
if (newIndex < 0) newIndex = 0; if (newIndex < 0) newIndex = 0;
return axis.axisLabel.GetFormatterContent(showData[newIndex]); return axis.axisLabel.GetFormatterContent(newIndex, showData[newIndex]);
} }
else else
{ {
if (axis.boundaryGap && coordinateWidth / dataCount > 5) return string.Empty; if (axis.boundaryGap && coordinateWidth / dataCount > 5) return string.Empty;
else return axis.axisLabel.GetFormatterContent(showData[0]); else return axis.axisLabel.GetFormatterContent(0, showData[0]);
} }
} }
else else
@@ -194,12 +192,12 @@ namespace XCharts
int newIndex = index * rate; int newIndex = index * rate;
if (newIndex < dataCount) if (newIndex < dataCount)
{ {
return axis.axisLabel.GetFormatterContent(showData[newIndex]); return axis.axisLabel.GetFormatterContent(newIndex, showData[newIndex]);
} }
else else
{ {
if (axis.boundaryGap && coordinateWidth / dataCount > 5) return string.Empty; if (axis.boundaryGap && coordinateWidth / dataCount > 5) return string.Empty;
else return axis.axisLabel.GetFormatterContent(showData[dataCount - 1]); else return axis.axisLabel.GetFormatterContent(dataCount - 1, showData[dataCount - 1]);
} }
} }
} }

View File

@@ -98,6 +98,10 @@ namespace XCharts
var numericFormatter = serieLabel == null ? serie.label.numericFormatter : serieLabel.numericFormatter; var numericFormatter = serieLabel == null ? serie.label.numericFormatter : serieLabel.numericFormatter;
var serieName = serie.name; var serieName = serie.name;
var dataName = serieData != null ? serieData.name : null; var dataName = serieData != null ? serieData.name : null;
if (serieLabel.formatterFunction != null)
{
return serieLabel.formatterFunction(serieData.index, dataValue);
}
if (string.IsNullOrEmpty(serieLabel.formatter)) if (string.IsNullOrEmpty(serieLabel.formatter))
return ChartCached.NumberToStr(dataValue, numericFormatter); return ChartCached.NumberToStr(dataValue, numericFormatter);
else else

View File

@@ -448,7 +448,7 @@ namespace XCharts
string legendName = legend.GetFormatterContent(datas[i]); string legendName = legend.GetFormatterContent(datas[i]);
var readIndex = m_LegendRealShowName.IndexOf(datas[i]); var readIndex = m_LegendRealShowName.IndexOf(datas[i]);
var active = IsActiveByLegend(datas[i]); var active = IsActiveByLegend(datas[i]);
var bgColor = LegendHelper.GetIconColor(this, readIndex , datas[i], active); var bgColor = LegendHelper.GetIconColor(this, readIndex, datas[i], active);
var item = LegendHelper.AddLegendItem(legend, i, datas[i], legendObject.transform, m_Theme, var item = LegendHelper.AddLegendItem(legend, i, datas[i], legendObject.transform, m_Theme,
legendName, bgColor, active); legendName, bgColor, active);
legend.SetButton(legendName, item, totalLegend); legend.SetButton(legendName, item, totalLegend);

View File

@@ -0,0 +1,28 @@
/************************************************/
/* */
/* Copyright (c) 2018 - 2021 monitor1394 */
/* https://github.com/monitor1394 */
/* */
/************************************************/
namespace XCharts
{
/// <summary>
/// The delegate function for AxisLabel's formatter. |
/// AxisLabel的formatter自定义委托。
/// </summary>
/// <param name="labelIndex">label索引</param>
/// <param name="value">当前label对应的数值数据Value轴或Time轴有效</param>
/// <param name="category">当前label对应的类目数据Category轴有效</param>
/// <returns>最终显示的文本内容</returns>
public delegate string DelegateAxisLabelFormatter(int labelIndex, float value, string category);
/// <summary>
/// The delegate function for SerieLabels formatter.
/// SerieLabel的formatter自定义委托。
/// </summary>
/// <param name="dataIndex">数据索引</param>
/// <param name="value">数值</param>
/// <returns>最终显示的文本内容</returns>
public delegate string DelegateSerieLabelFormatter(int dataIndex, float value);
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8d9ec774e2e5b4d9ba407a27f60b6d71
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: