diff --git a/Assets/XCharts/CHANGELOG-EN.md b/Assets/XCharts/CHANGELOG-EN.md index 1617603e..31bc7848 100644 --- a/Assets/XCharts/CHANGELOG-EN.md +++ b/Assets/XCharts/CHANGELOG-EN.md @@ -38,8 +38,9 @@ ## master -* (2021.06.27) Add `DataZoom`'s `orient` parameter to set horizontal or vertical styles -* (2021.06.21) Add `iconStyle`'s `AutoHideWhenLabelEmpty` to set whether the icon is automatically hidden when `label` is empty +* (2021.06.27) Added `formatter` delegate method to `AxisLabel` and `SerieLabel` (#145) +* (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 @@ -58,7 +59,7 @@ * (2021.06.13) Release `v2.2.1` version * (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.10) Fixed compatibility issues with `Unity` version (#154) * (2021.06.05) Improved Candlestickchart support for inverse (#152) diff --git a/Assets/XCharts/CHANGELOG.md b/Assets/XCharts/CHANGELOG.md index 47f3e702..71e138ce 100644 --- a/Assets/XCharts/CHANGELOG.md +++ b/Assets/XCharts/CHANGELOG.md @@ -38,6 +38,7 @@ ## master +* (2021.06.27) 增加`AxisLabel`和`SerieLabel`的`formatter`委托方法 (#145) * (2021.06.27) 增加`DataZoom`的`orient`参数设置水平或垂直样式 * (2021.06.21) 增加`IconStyle`的`autoHideWhenLabelEmpty`参数设置当`label`为空时是否自动隐藏图标 diff --git a/Assets/XCharts/Runtime/Component/Sub/AxisLabel.cs b/Assets/XCharts/Runtime/Component/Sub/AxisLabel.cs index eee25c00..b67d6dc2 100644 --- a/Assets/XCharts/Runtime/Component/Sub/AxisLabel.cs +++ b/Assets/XCharts/Runtime/Component/Sub/AxisLabel.cs @@ -30,6 +30,7 @@ namespace XCharts [SerializeField] private float m_Height = 0f; [SerializeField] private TextLimit m_TextLimit = new TextLimit(); [SerializeField] private TextStyle m_TextStyle = new TextStyle(); + private DelegateAxisLabelFormatter m_FormatterFunction; /// /// 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(); } } + public DelegateAxisLabelFormatter formatterFunction + { + set { m_FormatterFunction = value; } + } + public override bool componentDirty { get { return m_ComponentDirty || m_TextLimit.componentDirty; } } public override void ClearComponentDirty() { @@ -201,8 +207,12 @@ namespace XCharts 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(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) { value = Mathf.Abs(value); } + if (m_FormatterFunction != null) + { + return m_FormatterFunction(labelIndex, value, null); + } if (string.IsNullOrEmpty(m_Formatter)) { 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; if (!string.IsNullOrEmpty(m_Formatter)) { diff --git a/Assets/XCharts/Runtime/Component/Sub/SerieLabel.cs b/Assets/XCharts/Runtime/Component/Sub/SerieLabel.cs index 32c9503e..9bed43ca 100644 --- a/Assets/XCharts/Runtime/Component/Sub/SerieLabel.cs +++ b/Assets/XCharts/Runtime/Component/Sub/SerieLabel.cs @@ -100,6 +100,7 @@ namespace XCharts [SerializeField] private string m_NumericFormatter = ""; [SerializeField] private bool m_AutoOffset = false; [SerializeField] private TextStyle m_TextStyle = new TextStyle(); + private DelegateSerieLabelFormatter m_FormatterFunction; public void Reset() { @@ -337,6 +338,12 @@ namespace XCharts set { if (PropertyUtil.SetClass(ref m_TextStyle, value)) SetAllDirty(); } } + public DelegateSerieLabelFormatter formatterFunction + { + get { return m_FormatterFunction; } + set { m_FormatterFunction = value; } + } + public bool IsInside() { return position == Position.Inside || position == Position.Center; diff --git a/Assets/XCharts/Runtime/Helper/AxisHelper.cs b/Assets/XCharts/Runtime/Helper/AxisHelper.cs index a1fe0b2f..6335173f 100644 --- a/Assets/XCharts/Runtime/Helper/AxisHelper.cs +++ b/Assets/XCharts/Runtime/Helper/AxisHelper.cs @@ -138,7 +138,7 @@ namespace XCharts maxValue = -maxValue; } 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) { @@ -150,7 +150,7 @@ namespace XCharts minValue = -minValue; 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) { @@ -165,9 +165,7 @@ namespace XCharts { value = minValue + (maxValue - minValue) * index / split; } - var timestamp = (int)value; - var dateTime = DateTimeUtil.GetDateTime(timestamp); - return axis.axisLabel.GetFormatterDateTime(dateTime); + return axis.axisLabel.GetFormatterDateTime(index, value); } var showData = axis.GetDataList(dataZoom); int dataCount = showData.Count; @@ -181,12 +179,12 @@ namespace XCharts var residue = (dataCount - 1) - split * rate; var newIndex = residue + (index - 1) * rate; if (newIndex < 0) newIndex = 0; - return axis.axisLabel.GetFormatterContent(showData[newIndex]); + return axis.axisLabel.GetFormatterContent(newIndex, showData[newIndex]); } else { 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 @@ -194,12 +192,12 @@ namespace XCharts int newIndex = index * rate; if (newIndex < dataCount) { - return axis.axisLabel.GetFormatterContent(showData[newIndex]); + return axis.axisLabel.GetFormatterContent(newIndex, showData[newIndex]); } else { 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]); } } } diff --git a/Assets/XCharts/Runtime/Helper/SerieLabelHelper.cs b/Assets/XCharts/Runtime/Helper/SerieLabelHelper.cs index e10e6497..3ce28102 100644 --- a/Assets/XCharts/Runtime/Helper/SerieLabelHelper.cs +++ b/Assets/XCharts/Runtime/Helper/SerieLabelHelper.cs @@ -98,6 +98,10 @@ namespace XCharts var numericFormatter = serieLabel == null ? serie.label.numericFormatter : serieLabel.numericFormatter; var serieName = serie.name; var dataName = serieData != null ? serieData.name : null; + if (serieLabel.formatterFunction != null) + { + return serieLabel.formatterFunction(serieData.index, dataValue); + } if (string.IsNullOrEmpty(serieLabel.formatter)) return ChartCached.NumberToStr(dataValue, numericFormatter); else diff --git a/Assets/XCharts/Runtime/Internal/BaseChart.cs b/Assets/XCharts/Runtime/Internal/BaseChart.cs index 154a683c..11285af5 100644 --- a/Assets/XCharts/Runtime/Internal/BaseChart.cs +++ b/Assets/XCharts/Runtime/Internal/BaseChart.cs @@ -448,7 +448,7 @@ namespace XCharts string legendName = legend.GetFormatterContent(datas[i]); var readIndex = m_LegendRealShowName.IndexOf(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, legendName, bgColor, active); legend.SetButton(legendName, item, totalLegend); diff --git a/Assets/XCharts/Runtime/Internal/Interface/DelegateFunction.cs b/Assets/XCharts/Runtime/Internal/Interface/DelegateFunction.cs new file mode 100644 index 00000000..971b3872 --- /dev/null +++ b/Assets/XCharts/Runtime/Internal/Interface/DelegateFunction.cs @@ -0,0 +1,28 @@ + +/************************************************/ +/* */ +/* Copyright (c) 2018 - 2021 monitor1394 */ +/* https://github.com/monitor1394 */ +/* */ +/************************************************/ + +namespace XCharts +{ + /// + /// The delegate function for AxisLabel's formatter. | + /// AxisLabel的formatter自定义委托。 + /// + /// label索引 + /// 当前label对应的数值数据,Value轴或Time轴有效 + /// 当前label对应的类目数据,Category轴有效 + /// 最终显示的文本内容 + public delegate string DelegateAxisLabelFormatter(int labelIndex, float value, string category); + /// + /// The delegate function for SerieLabel‘s formatter. + /// SerieLabel的formatter自定义委托。 + /// + /// 数据索引 + /// 数值 + /// 最终显示的文本内容 + public delegate string DelegateSerieLabelFormatter(int dataIndex, float value); +} \ No newline at end of file diff --git a/Assets/XCharts/Runtime/Internal/Interface/DelegateFunction.cs.meta b/Assets/XCharts/Runtime/Internal/Interface/DelegateFunction.cs.meta new file mode 100644 index 00000000..27b82fbd --- /dev/null +++ b/Assets/XCharts/Runtime/Internal/Interface/DelegateFunction.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8d9ec774e2e5b4d9ba407a27f60b6d71 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: