diff --git a/Documentation~/zh/changelog.md b/Documentation~/zh/changelog.md index 5cde791c..6cad70a9 100644 --- a/Documentation~/zh/changelog.md +++ b/Documentation~/zh/changelog.md @@ -79,6 +79,8 @@ slug: /changelog ## master +* (2025.03.01) 优化`Comment`的组件刷新 +* (2025.02.23) 增加`Axis`的`Label`的`formatter`支持`{index}``{index-1}``{-index}``{-index-1}`通配符 * (2025.02.23) 增加`Bar`的`realtimeSort`支持实时排序 * (2025.02.19) 增加`Tooltip`的`itemFormatter`对`\n`换行的支持 * (2025.02.18) 优化`Tooltip`的对齐方式 diff --git a/Runtime/Component/Axis/AxisHandler.cs b/Runtime/Component/Axis/AxisHandler.cs index 239102fc..7129ea9e 100644 --- a/Runtime/Component/Axis/AxisHandler.cs +++ b/Runtime/Component/Axis/AxisHandler.cs @@ -253,7 +253,7 @@ namespace XCharts var serie = chart.GetSerie(0); if (isCategory && serie != null && serie.useSortData) { - var showData = serie.GetDataList(dataZoom); + var showData = serie.GetDataList(dataZoom, true); var isChanged = CheckSortedDataChanged(axis, showData); if (isChanged) { @@ -262,15 +262,11 @@ namespace XCharts if (context.labelObjectList[i] != null) { var index = i < showData.Count ? showData[i].index : i; - var text = AxisHelper.GetLabelName(axis, coordinateWidth, index, destMinValue, destMaxValue, dataZoom, forcePercent); + var text = AxisHelper.GetLabelName(axis, coordinateWidth, index, destMinValue, destMaxValue, dataZoom, forcePercent, i); context.labelObjectList[i].SetText(text); } } - axis.context.sortedDataIndices.Clear(); - for (int i = 0; i < showData.Count; i++) - { - axis.context.sortedDataIndices.Add(showData[i].index); - } + SaveSortedDataIndex(axis, showData); } } else @@ -296,6 +292,15 @@ namespace XCharts return false; } + private void SaveSortedDataIndex(Axis axis, List dataList) + { + axis.context.sortedDataIndices.Clear(); + for (int i = 0; i < dataList.Count; i++) + { + axis.context.sortedDataIndices.Add(dataList[i].index); + } + } + internal void UpdateAxisTickValueList(Axis axis) { if (axis.IsTime()) @@ -473,13 +478,21 @@ namespace XCharts if (axis.IsCategory() && axis.boundaryGap) splitNumber -= 1; axis.context.aligment = defaultAlignment; + var sortSerie = chart.GetRealtimeSortSerie(); + if (sortSerie != null) + { + SerieHelper.UpdateSerieRuntimeFilterData(sortSerie); + } + var showData = sortSerie != null ? sortSerie.GetDataList(dataZoom, true) : null; + for (int i = 0; i < splitNumber; i++) { var labelWidth = AxisHelper.GetScaleWidth(axis, axisLength, i + 1, dataZoom); - var labelName = AxisHelper.GetLabelName(axis, axisLength, i, + var sortIndex = sortSerie != null ? (i < showData.Count ? showData[i].index : i) : i; + var labelName = AxisHelper.GetLabelName(axis, axisLength, sortIndex, axis.context.destMinValue, axis.context.destMaxValue, - dataZoom, isPercentStack); + dataZoom, isPercentStack, i); var label = ChartHelper.AddAxisLabelObject(splitNumber, i, ChartCached.GetAxisLabelName(i), @@ -585,13 +598,20 @@ namespace XCharts if (axis.IsCategory() && axis.boundaryGap) splitNumber -= 1; axis.context.aligment = defaultAlignment; + var sortSerie = chart.GetRealtimeSortSerie(); + if (sortSerie != null) + { + SerieHelper.UpdateSerieRuntimeFilterData(sortSerie); + } + var showData = sortSerie != null ? sortSerie.GetDataList(dataZoom, true) : null; for (int i = 0; i < splitNumber; i++) { var labelWidth = AxisHelper.GetScaleWidth(axis, axisLength, i + 1, dataZoom); - var labelName = AxisHelper.GetLabelName(axis, axisLength, i, + var sortIndex = sortSerie != null ? (i < showData.Count ? showData[i].index : i) : i; + var labelName = AxisHelper.GetLabelName(axis, axisLength, sortIndex, axis.context.destMinValue, axis.context.destMaxValue, - dataZoom, isPercentStack); + dataZoom, isPercentStack, i); var label = ChartHelper.AddAxisLabelObject(splitNumber, i, ChartCached.GetAxisLabelName(i), diff --git a/Runtime/Component/Axis/AxisHelper.cs b/Runtime/Component/Axis/AxisHelper.cs index 5f2c12f9..7c294b7f 100644 --- a/Runtime/Component/Axis/AxisHelper.cs +++ b/Runtime/Component/Axis/AxisHelper.cs @@ -116,9 +116,10 @@ namespace XCharts.Runtime /// /// public static string GetLabelName(Axis axis, float coordinateWidth, int index, double minValue, double maxValue, - DataZoom dataZoom, bool forcePercent) + DataZoom dataZoom, bool forcePercent, int sortIndex = -1) { int split = GetSplitNumber(axis, coordinateWidth, dataZoom); + if(sortIndex == -1) sortIndex = index; if (axis.type == Axis.AxisType.Value) { if (minValue == 0 && maxValue == 0) @@ -137,7 +138,7 @@ namespace XCharts.Runtime if (forcePercent) return string.Format("{0}%", (int)value); else - return axis.axisLabel.GetFormatterContent(index, value, minValue, maxValue); + return axis.axisLabel.GetFormatterContent(sortIndex, axis.context.labelValueList.Count, value, minValue, maxValue); } else if (axis.type == Axis.AxisType.Log) { @@ -150,7 +151,7 @@ namespace XCharts.Runtime minValue = -minValue; maxValue = -maxValue; } - return axis.axisLabel.GetFormatterContent(index, value, minValue, maxValue, true); + return axis.axisLabel.GetFormatterContent(sortIndex, 0, value, minValue, maxValue, true); } else if (axis.type == Axis.AxisType.Time) { @@ -160,7 +161,7 @@ namespace XCharts.Runtime return string.Empty; var value = axis.GetLabelValue(index); - return axis.axisLabel.GetFormatterDateTime(index, value, minValue, maxValue); + return axis.axisLabel.GetFormatterDateTime(sortIndex, axis.context.labelValueList.Count, value, minValue, maxValue); } var showData = axis.GetDataList(dataZoom); int dataCount = showData.Count; @@ -172,18 +173,18 @@ namespace XCharts.Runtime { if (index > 0) { - var residue = (dataCount - 1) - split * rate; + var residue = dataCount - 1 - split * rate; var newIndex = residue + (index - 1) * rate; if (newIndex < 0) newIndex = 0; - return axis.axisLabel.GetFormatterContent(newIndex, showData[newIndex]); + return axis.axisLabel.GetFormatterContent(sortIndex, dataCount, showData[newIndex]); } else { if (axis.boundaryGap && coordinateWidth / dataCount > 5) return string.Empty; else - return axis.axisLabel.GetFormatterContent(0, showData[0]); + return axis.axisLabel.GetFormatterContent(sortIndex, dataCount, showData[0]); } } else @@ -191,7 +192,7 @@ namespace XCharts.Runtime int newIndex = index * rate; if (newIndex < dataCount) { - return axis.axisLabel.GetFormatterContent(newIndex, showData[newIndex]); + return axis.axisLabel.GetFormatterContent(sortIndex, dataCount, showData[newIndex]); } else { @@ -199,7 +200,7 @@ namespace XCharts.Runtime if (axis.boundaryGap && ((diff > 0 && diff / rate < 0.4f) || dataCount >= axis.data.Count)) return string.Empty; else - return axis.axisLabel.GetFormatterContent(dataCount - 1, showData[dataCount - 1]); + return axis.axisLabel.GetFormatterContent(sortIndex, dataCount, showData[dataCount - 1]); } } } diff --git a/Runtime/Component/Axis/AxisLabel.cs b/Runtime/Component/Axis/AxisLabel.cs index 86cc37e4..6479a141 100644 --- a/Runtime/Component/Axis/AxisLabel.cs +++ b/Runtime/Component/Axis/AxisLabel.cs @@ -145,7 +145,7 @@ namespace XCharts.Runtime m_TextLimit.SetRelatedText(txt, labelWidth); } - public override string GetFormatterContent(int labelIndex, string category) + public override string GetFormatterContent(int labelIndex, int totalIndex, string category) { if (string.IsNullOrEmpty(category)) return GetFormatterFunctionContent(labelIndex, category, category); @@ -157,18 +157,18 @@ namespace XCharts.Runtime else { var content = m_Formatter; - FormatterHelper.ReplaceAxisLabelContent(ref content, category); + FormatterHelper.ReplaceAxisLabelContent(ref content, category, labelIndex, totalIndex); return GetFormatterFunctionContent(labelIndex, category, m_TextLimit.GetLimitContent(content)); } } - public override string GetFormatterContent(int labelIndex, double value, double minValue, double maxValue, bool isLog = false) + public override string GetFormatterContent(int labelIndex, int totalIndex, double value, double minValue, double maxValue, bool isLog = false) { if (showAsPositiveNumber && value < 0) { value = Math.Abs(value); } - return base.GetFormatterContent(labelIndex, value, minValue, maxValue, isLog); + return base.GetFormatterContent(labelIndex, totalIndex, value, minValue, maxValue, isLog); } public bool IsNeedShowLabel(int index, int total) diff --git a/Runtime/Component/Comment/CommentHander.cs b/Runtime/Component/Comment/CommentHander.cs index b0d06eb5..7520aa29 100644 --- a/Runtime/Component/Comment/CommentHander.cs +++ b/Runtime/Component/Comment/CommentHander.cs @@ -37,6 +37,7 @@ namespace XCharts.Runtime label.SetActive(comment.show && item.show, true); label.SetPosition(labelPos); label.text.SetLocalPosition(labelStyle.offset); + item.labelObject = label; } }; comment.refreshComponent(); diff --git a/Runtime/Component/Comment/CommentItem.cs b/Runtime/Component/Comment/CommentItem.cs index d568db57..10363288 100644 --- a/Runtime/Component/Comment/CommentItem.cs +++ b/Runtime/Component/Comment/CommentItem.cs @@ -15,7 +15,9 @@ namespace XCharts.Runtime [SerializeField] private Rect m_MarkRect; [SerializeField] private CommentMarkStyle m_MarkStyle = new CommentMarkStyle() { show = false }; [SerializeField] private LabelStyle m_LabelStyle = new LabelStyle() { show = false }; - [SerializeField] [Since("v3.5.0")]private Location m_Location = new Location() { align = Location.Align.TopLeft, top = 0.125f }; + [SerializeField][Since("v3.5.0")] private Location m_Location = new Location() { align = Location.Align.TopLeft, top = 0.125f }; + + public ChartLabel labelObject { get; set; } /// @@ -27,7 +29,18 @@ namespace XCharts.Runtime /// content of comment. /// ||注解的文本内容。支持模板参数,可以参考Tooltip的itemFormatter。 /// - public string content { get { return m_Content; } set { if (PropertyUtil.SetClass(ref m_Content, value)) SetComponentDirty(); } } + public string content + { + get { return m_Content; } + set + { + if (PropertyUtil.SetClass(ref m_Content, value)) + { + if (labelObject != null) labelObject.SetText(value); + else SetComponentDirty(); + } + } + } /// /// the mark rect of comment. /// ||注解区域。 diff --git a/Runtime/Component/Label/LabelStyle.cs b/Runtime/Component/Label/LabelStyle.cs index 743a8a7a..d6ed4875 100644 --- a/Runtime/Component/Label/LabelStyle.cs +++ b/Runtime/Component/Label/LabelStyle.cs @@ -131,7 +131,8 @@ namespace XCharts.Runtime /// `{g}` : indicates the total number of data.
/// `{h}` : hexadecimal color value.
/// `{y}` : category value of y axis.
- /// `{value}` : The value of the axis or legend.
+ /// `{value}` : the value of the axis or legend.
+ /// `{index}` : the index of the axis.
/// The following placeholder apply to `UITable` components:
/// `{name}` : indicates the row name of the table.
/// `{index}` : indicates the row number of the table.
@@ -161,6 +162,7 @@ namespace XCharts.Runtime /// `{h}`:十六进制颜色值。
/// `{y}`:Y轴的类目名。
/// `{value}`:坐标轴或图例的值。
+ /// `{index}`:坐标轴编号。
/// 以下通配符适用UITable组件:
/// `{name}`: 表格的行名。
/// `{index}`:表格的行号。
@@ -398,7 +400,7 @@ namespace XCharts.Runtime m_TextStyle.Copy(label.m_TextStyle); } - public virtual string GetFormatterContent(int labelIndex, string category) + public virtual string GetFormatterContent(int labelIndex, int totalIndex, string category) { if (string.IsNullOrEmpty(category)) return GetFormatterFunctionContent(labelIndex, category, category); @@ -410,12 +412,12 @@ namespace XCharts.Runtime else { var content = m_Formatter; - FormatterHelper.ReplaceAxisLabelContent(ref content, category); + FormatterHelper.ReplaceAxisLabelContent(ref content, category, labelIndex, totalIndex); return GetFormatterFunctionContent(labelIndex, category, category); } } - public virtual string GetFormatterContent(int labelIndex, double value, double minValue, double maxValue, bool isLog = false) + public virtual string GetFormatterContent(int labelIndex, int totalIndex, double value, double minValue, double maxValue, bool isLog = false) { var newNumericFormatter = numericFormatter; if (value == 0 && !DateTimeUtil.IsDateOrTimeRegex(newNumericFormatter)) @@ -452,14 +454,14 @@ namespace XCharts.Runtime else { var content = m_Formatter; - FormatterHelper.ReplaceAxisLabelContent(ref content, newNumericFormatter, value); + FormatterHelper.ReplaceAxisLabelContent(ref content, newNumericFormatter, value, labelIndex, totalIndex); return GetFormatterFunctionContent(labelIndex, value, content); } } private static bool isDateFormatter = false; private static string newFormatter = null; - public string GetFormatterDateTime(int labelIndex, double value, double minValue, double maxValue) + public string GetFormatterDateTime(int labelIndex, int totalIndex, double value, double minValue, double maxValue) { var timestamp = (int)value; var dateTime = DateTimeUtil.GetDateTime(timestamp); @@ -472,9 +474,9 @@ namespace XCharts.Runtime { try { - if(DateTimeUtil.IsDateOrTimeRegex(numericFormatter, ref isDateFormatter, ref newFormatter)) + if (DateTimeUtil.IsDateOrTimeRegex(numericFormatter, ref isDateFormatter, ref newFormatter)) { - if(isDateFormatter) + if (isDateFormatter) dateString = ChartCached.NumberToDateStr(timestamp, newFormatter); else dateString = ChartCached.NumberToTimeStr(timestamp, newFormatter); @@ -492,7 +494,7 @@ namespace XCharts.Runtime if (!string.IsNullOrEmpty(m_Formatter)) { var content = m_Formatter; - FormatterHelper.ReplaceAxisLabelContent(ref content, dateString); + FormatterHelper.ReplaceAxisLabelContent(ref content, dateString, labelIndex, totalIndex); return GetFormatterFunctionContent(labelIndex, value, content); } else diff --git a/Runtime/Component/Radar/RadarCoord.cs b/Runtime/Component/Radar/RadarCoord.cs index 6967ec6e..222a240e 100644 --- a/Runtime/Component/Radar/RadarCoord.cs +++ b/Runtime/Component/Radar/RadarCoord.cs @@ -464,16 +464,16 @@ namespace XCharts.Runtime indicatorList.Clear(); } - public string GetFormatterIndicatorContent(int indicatorIndex) + public string GetFormatterIndicatorContent(int indicatorIndex, int totalIndex) { var indicator = GetIndicator(indicatorIndex); if (indicator == null) return string.Empty; else - return GetFormatterIndicatorContent(indicator.name); + return GetFormatterIndicatorContent(indicator.name, indicatorIndex, totalIndex); } - public string GetFormatterIndicatorContent(string indicatorName) + public string GetFormatterIndicatorContent(string indicatorName, int index, int totalIndex) { if (string.IsNullOrEmpty(indicatorName)) return indicatorName; @@ -485,7 +485,7 @@ namespace XCharts.Runtime else { var content = m_AxisName.labelStyle.formatter; - FormatterHelper.ReplaceAxisLabelContent(ref content, indicatorName); + FormatterHelper.ReplaceAxisLabelContent(ref content, indicatorName, index, totalIndex); return content; } } diff --git a/Runtime/Component/Radar/RadarCoordHandler.cs b/Runtime/Component/Radar/RadarCoordHandler.cs index 3b820d39..abf5b437 100644 --- a/Runtime/Component/Radar/RadarCoordHandler.cs +++ b/Runtime/Component/Radar/RadarCoordHandler.cs @@ -50,9 +50,9 @@ namespace XCharts.Runtime var indicator = radar.indicatorList[i]; var pos = radar.GetIndicatorPosition(i); var objName = INDICATOR_TEXT + "_" + i; - + var content = radar.GetFormatterIndicatorContent(i, radar.indicatorList.Count); var label = ChartHelper.AddChartLabel(objName, radarObject.transform, radar.axisName.labelStyle, - chart.theme.common, radar.GetFormatterIndicatorContent(i), Color.clear, TextAnchor.MiddleCenter); + chart.theme.common, content, Color.clear, TextAnchor.MiddleCenter); label.SetActive(radar.axisName.show && radar.indicator && radar.axisName.labelStyle.show, true); AxisHelper.AdjustCircleLabelPos(label, pos, radar.context.center, txtHig, radar.axisName.labelStyle.offset); } diff --git a/Runtime/Component/Tooltip/TooltipHandler.cs b/Runtime/Component/Tooltip/TooltipHandler.cs index e9b63fab..3ff95e7a 100644 --- a/Runtime/Component/Tooltip/TooltipHandler.cs +++ b/Runtime/Component/Tooltip/TooltipHandler.cs @@ -264,15 +264,15 @@ namespace XCharts.Runtime var index = (int)axis.context.pointerValue; var dataZoom = chart.GetDataZoomOfAxis(axis); var category = axis.GetData(index, dataZoom); - label.SetText(axis.indicatorLabel.GetFormatterContent(index, category)); + label.SetText(axis.indicatorLabel.GetFormatterContent(index, 0, category)); } else if (axis.IsTime()) { - label.SetText(axis.indicatorLabel.GetFormatterDateTime(0, axis.context.pointerValue, axis.context.minValue, axis.context.maxValue)); + label.SetText(axis.indicatorLabel.GetFormatterDateTime(0, 0, axis.context.pointerValue, axis.context.minValue, axis.context.maxValue)); } else { - label.SetText(axis.indicatorLabel.GetFormatterContent(0, axis.context.pointerValue, axis.context.minValue, axis.context.maxValue, axis.IsLog())); + label.SetText(axis.indicatorLabel.GetFormatterContent(0, 0, axis.context.pointerValue, axis.context.minValue, axis.context.maxValue, axis.IsLog())); } var textColor = axis.axisLabel.textStyle.GetColor(chart.theme.axis.textColor); if (ChartHelper.IsClearColor(axis.indicatorLabel.background.color)) @@ -356,6 +356,7 @@ namespace XCharts.Runtime if (isTriggerAxis) { var index = serie.context.dataZoomStartIndex + (int)yAxis.context.pointerValue; + if(serie.useSortData) index = yAxis.context.sortedDataIndices[index]; serie.context.pointerEnter = true; serie.context.pointerAxisDataIndexs.Add(index); serie.context.pointerItemDataIndex = index; @@ -375,6 +376,7 @@ namespace XCharts.Runtime if (isTriggerAxis) { var index = serie.context.dataZoomStartIndex + (int)xAxis.context.pointerValue; + if(serie.useSortData) index = xAxis.context.sortedDataIndices[index]; if (chart.isTriggerOnClick) { if (serie.insertDataToHead) @@ -452,7 +454,7 @@ namespace XCharts.Runtime var dataCount = serie.dataCount; var themeSymbolSize = chart.theme.serie.scatterSymbolSize; var data = serie.data; - if (!isTimeAxis) + if (!isTimeAxis)// || serie.useSortData) { serie.context.sortedData.Clear(); for (int i = 0; i < dataCount; i++) @@ -641,7 +643,6 @@ namespace XCharts.Runtime private bool GetAxisCategory(int gridIndex, ref int dataIndex, ref string category, ref int timestamp, ref double axisRange) { - var needSort = chart.HasRealtimeSortSerie(); foreach (var component in chart.components) { if (component is Axis) @@ -654,12 +655,7 @@ namespace XCharts.Runtime dataIndex = double.IsNaN(axis.context.pointerValue) ? axis.context.dataZoomStartIndex : (int)axis.context.axisTooltipValue; - int axisIndex = dataIndex; - if (needSort && axisIndex < axis.context.sortedDataIndices.Count) - { - axisIndex = axis.context.sortedDataIndices[axisIndex]; - } - category = axis.GetData(axisIndex); + category = axis.GetData(dataIndex); return true; } else if (axis.IsTime()) diff --git a/Runtime/Helper/FormatterHelper.cs b/Runtime/Helper/FormatterHelper.cs index 2ab5a4da..57ae9d48 100644 --- a/Runtime/Helper/FormatterHelper.cs +++ b/Runtime/Helper/FormatterHelper.cs @@ -18,6 +18,7 @@ namespace XCharts.Runtime private static Regex s_RegexSubForAxisLabel = new Regex(@"(value)|([c-g|x|p|r]\d*)", RegexOptions.IgnoreCase); private static Regex s_RegexForSerieLabel = new Regex(@"{[a-h|\.|y]\d*(:[c-g|x|p|r]\d*)?}", RegexOptions.IgnoreCase); private static Regex s_RegexSubForSerieLabel = new Regex(@"(\.)|([a-h|y]\d*)|([c-g|x|p|r]\d*)", RegexOptions.IgnoreCase); + private static Regex s_RegexForAxisIndex = new Regex(@"\{(-?)index([+-]\d+)?\}", RegexOptions.IgnoreCase); public static bool NeedFormat(string content) { @@ -334,7 +335,7 @@ namespace XCharts.Runtime return s_RegexNewLine.Replace(content.Trim(), PH_NN); } - public static void ReplaceAxisLabelContent(ref string content, string numericFormatter, double value) + public static void ReplaceAxisLabelContent(ref string content, string numericFormatter, double value, int index, int totalIndex) { var mc = s_RegexForAxisLabel.Matches(content); foreach (var m in mc) @@ -349,10 +350,11 @@ namespace XCharts.Runtime } content = content.Replace(old, ChartCached.FloatToStr(value, numericFormatter)); } + ReplaceIndexContent(ref content, index, totalIndex); content = TrimAndReplaceLine(content); } - public static void ReplaceAxisLabelContent(ref string content, string value) + public static void ReplaceAxisLabelContent(ref string content, string value, int index, int totalIndex) { var mc = s_RegexForAxisLabel.Matches(content); foreach (var m in mc) @@ -363,8 +365,26 @@ namespace XCharts.Runtime if (argsCount <= 0) continue; content = content.Replace(old, value); } + ReplaceIndexContent(ref content, index, totalIndex); content = TrimAndReplaceLine(content); } + public static void ReplaceIndexContent(ref string content, int currIndex, int totalIndex) + { + if (totalIndex <= 0) return; + content = s_RegexForAxisIndex.Replace(content, (match) => + { + bool isNegative = match.Groups[1].Value == "-"; + int offset = 0; + int parsedOffset = 0; + if (match.Groups[2].Success && + int.TryParse(match.Groups[2].Value, out parsedOffset)) + { + offset = parsedOffset; + } + int baseValue = isNegative ? totalIndex - currIndex : currIndex + 1; + return (baseValue + offset).ToString(); + }); + } } } \ No newline at end of file diff --git a/Runtime/Internal/BaseChart.Serie.cs b/Runtime/Internal/BaseChart.Serie.cs index 9e00ba58..640ce59b 100644 --- a/Runtime/Internal/BaseChart.Serie.cs +++ b/Runtime/Internal/BaseChart.Serie.cs @@ -111,6 +111,16 @@ namespace XCharts.Runtime return false; } + public Serie GetRealtimeSortSerie() + { + foreach (var serie in m_Series) + { + if (serie.useSortData) + return serie; + } + return null; + } + public T GetSerie() where T : Serie { foreach (var serie in m_Series) diff --git a/Runtime/Serie/Bar/BarHandler.cs b/Runtime/Serie/Bar/BarHandler.cs index ff26d233..3b68293d 100644 --- a/Runtime/Serie/Bar/BarHandler.cs +++ b/Runtime/Serie/Bar/BarHandler.cs @@ -167,7 +167,7 @@ namespace XCharts.Runtime } var dataZoom = chart.GetDataZoomOfAxis(axis); - var showData = serie.GetDataList(dataZoom); + var showData = serie.GetDataList(dataZoom, true); if (showData.Count <= 0) return; diff --git a/Runtime/Serie/Serie.cs b/Runtime/Serie/Serie.cs index 8ae952c7..ac3f47e3 100644 --- a/Runtime/Serie/Serie.cs +++ b/Runtime/Serie/Serie.cs @@ -1750,7 +1750,7 @@ namespace XCharts.Runtime ///
/// /// - public List GetDataList(DataZoom dataZoom = null) + public List GetDataList(DataZoom dataZoom = null, bool sorted = false) { if (dataZoom != null && dataZoom.enable && (dataZoom.IsContainsXAxis(xAxisIndex) || dataZoom.IsContainsYAxis(yAxisIndex))) @@ -1760,7 +1760,7 @@ namespace XCharts.Runtime } else { - return useSortData && context.sortedData.Count > 0 ? context.sortedData : m_Data; + return useSortData && sorted && context.sortedData.Count > 0 ? context.sortedData : m_Data; } } diff --git a/Runtime/Serie/SerieData.cs b/Runtime/Serie/SerieData.cs index c0e79546..e7032350 100644 --- a/Runtime/Serie/SerieData.cs +++ b/Runtime/Serie/SerieData.cs @@ -59,6 +59,7 @@ namespace XCharts.Runtime public ChartLabel labelObject { get; set; } public ChartLabel titleObject { get; set; } + public int sortIndex { get; set; } private bool m_Show = true; /// diff --git a/Runtime/Serie/SerieHelper.cs b/Runtime/Serie/SerieHelper.cs index 2ceb1137..eb560218 100644 --- a/Runtime/Serie/SerieHelper.cs +++ b/Runtime/Serie/SerieHelper.cs @@ -955,6 +955,10 @@ namespace XCharts.Runtime public static void UpdateSerieRuntimeFilterData(Serie serie, bool filterInvisible = true) { + var realtimeData = true; + var dataChangeDuration = serie.animation.GetChangeDuration(); + var dataAddDuration = serie.animation.GetAdditionDuration(); + var unscaledTime = serie.animation.unscaledTime; serie.context.sortedData.Clear(); foreach (var serieData in serie.data) { @@ -966,8 +970,12 @@ namespace XCharts.Runtime case SerieDataSortType.Ascending: serie.context.sortedData.Sort(delegate (SerieData data1, SerieData data2) { - var value1 = data1.GetData(1); - var value2 = data2.GetData(1); + var value1 = realtimeData ? + data1.GetCurrData(1, dataAddDuration, dataChangeDuration, false, 0, 0, unscaledTime) : + data1.GetData(1); + var value2 = realtimeData ? + data2.GetCurrData(1, dataAddDuration, dataChangeDuration, false, 0, 0, unscaledTime) : + data2.GetData(1); if (value1 == value2) return 0; else if (value1 > value2) return 1; else return -1; @@ -976,8 +984,12 @@ namespace XCharts.Runtime case SerieDataSortType.Descending: serie.context.sortedData.Sort(delegate (SerieData data1, SerieData data2) { - var value1 = data1.GetData(1); - var value2 = data2.GetData(1); + var value1 = realtimeData ? + data1.GetCurrData(1, dataAddDuration, dataChangeDuration, false, 0, 0, unscaledTime) : + data1.GetData(1); + var value2 = realtimeData ? + data2.GetCurrData(1, dataAddDuration, dataChangeDuration, false, 0, 0, unscaledTime) : + data2.GetData(1); if (value1 == value2) return 0; else if (value1 > value2) return -1; else return 1; @@ -986,6 +998,10 @@ namespace XCharts.Runtime case SerieDataSortType.None: break; } + for (int i = 0; i < serie.context.sortedData.Count; i++) + { + serie.context.sortedData[i].sortIndex = i; + } } public static T CloneSerie(Serie serie) where T : Serie