增加BarrealtimeSort支持实时排序

This commit is contained in:
monitor1394
2025-02-23 21:47:15 +08:00
parent a54c50d947
commit 063b5529d7
12 changed files with 105 additions and 22 deletions

View File

@@ -79,6 +79,7 @@ slug: /changelog
## master ## master
* (2025.02.23) 增加`Bar``realtimeSort`支持实时排序
* (2025.02.19) 增加`Tooltip``itemFormatter``\n`换行的支持 * (2025.02.19) 增加`Tooltip``itemFormatter``\n`换行的支持
* (2025.02.18) 优化`Tooltip`的对齐方式 * (2025.02.18) 优化`Tooltip`的对齐方式
* (2025.02.09) 修复`SaveAsImage`保存图片时不支持透明度的问题 (#337) * (2025.02.09) 修复`SaveAsImage`保存图片时不支持透明度的问题 (#337)

View File

@@ -22,6 +22,11 @@ namespace XCharts.Editor
PropertyField("m_BarWidth"); PropertyField("m_BarWidth");
PropertyField("m_BarGap"); PropertyField("m_BarGap");
PropertyField("m_BarMaxWidth"); PropertyField("m_BarMaxWidth");
PropertyField("m_RealtimeSort");
if(serie.useSortData)
{
PropertyField("m_DataSortType");
}
if (serie.IsUseCoord<PolarCoord>()) if (serie.IsUseCoord<PolarCoord>())
{ {
PropertyField("m_RoundCap"); PropertyField("m_RoundCap");

View File

@@ -53,7 +53,7 @@ namespace XCharts.Runtime
if (axis.context.labelObjectList.Count <= 0) if (axis.context.labelObjectList.Count <= 0)
InitAngleAxis(axis); InitAngleAxis(axis);
else else
axis.UpdateLabelText(runtimeWidth, null, false); UpdateLabelText(axis, runtimeWidth, null, false);
} }
private void InitAngleAxis(AngleAxis axis) private void InitAngleAxis(AngleAxis axis)

View File

@@ -836,22 +836,6 @@ namespace XCharts.Runtime
return IsCategory() ? GetDataList(dataZoom).Count : 0; return IsCategory() ? GetDataList(dataZoom).Count : 0;
} }
/// <summary>
/// 更新刻度标签文字
/// </summary>
/// <param name="dataZoom"></param>
internal void UpdateLabelText(float coordinateWidth, DataZoom dataZoom, bool forcePercent)
{
for (int i = 0; i < context.labelObjectList.Count; i++)
{
if (context.labelObjectList[i] != null)
{
var text = AxisHelper.GetLabelName(this, coordinateWidth, i, context.destMinValue, context.destMaxValue, dataZoom, forcePercent);
context.labelObjectList[i].SetText(text);
}
}
}
internal Vector3 GetLabelObjectPosition(int index) internal Vector3 GetLabelObjectPosition(int index)
{ {
if (context.labelObjectList != null && index < context.labelObjectList.Count) if (context.labelObjectList != null && index < context.labelObjectList.Count)

View File

@@ -69,6 +69,7 @@ namespace XCharts.Runtime
public List<string> runtimeData { get { return m_RuntimeData; } } public List<string> runtimeData { get { return m_RuntimeData; } }
public List<double> labelValueList { get { return m_LabelValueList; } } public List<double> labelValueList { get { return m_LabelValueList; } }
public List<ChartLabel> labelObjectList { get { return m_AxisLabelList; } } public List<ChartLabel> labelObjectList { get { return m_AxisLabelList; } }
public List<int> sortedDataIndices { get { return m_SortedDataIndices; } }
public int dataZoomStartIndex; public int dataZoomStartIndex;
/// <summary> /// <summary>
/// 添加过的历史数据总数 /// 添加过的历史数据总数
@@ -86,6 +87,7 @@ namespace XCharts.Runtime
private List<ChartLabel> m_AxisLabelList = new List<ChartLabel>(); private List<ChartLabel> m_AxisLabelList = new List<ChartLabel>();
private List<double> m_LabelValueList = new List<double>(); private List<double> m_LabelValueList = new List<double>();
private List<string> m_RuntimeData = new List<string>(); private List<string> m_RuntimeData = new List<string>();
private List<int> m_SortedDataIndices = new List<int>();
internal void Clear() internal void Clear()
{ {

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
using XCharts.Runtime; using XCharts.Runtime;
@@ -149,6 +150,10 @@ namespace XCharts
axis.context.minValue = 0; axis.context.minValue = 0;
axis.context.maxValue = axis.data.Count > 0 ? axis.data.Count - 1 : SeriesHelper.GetMaxSerieDataCount(chart.series) - 1; axis.context.maxValue = axis.data.Count > 0 ? axis.data.Count - 1 : SeriesHelper.GetMaxSerieDataCount(chart.series) - 1;
axis.context.minMaxRange = axis.context.maxValue; axis.context.minMaxRange = axis.context.maxValue;
if (chart.HasRealtimeSortSerie())
{
UpdateAxisLabelText(axis);
}
return; return;
} }
@@ -236,7 +241,59 @@ namespace XCharts
var isPercentStack = SeriesHelper.IsPercentStack<Bar>(chart.series); var isPercentStack = SeriesHelper.IsPercentStack<Bar>(chart.series);
var dataZoom = chart.GetDataZoomOfAxis(axis); var dataZoom = chart.GetDataZoomOfAxis(axis);
axis.UpdateLabelText(runtimeWidth, dataZoom, isPercentStack); UpdateLabelText(axis, runtimeWidth, dataZoom, isPercentStack);
}
internal void UpdateLabelText(Axis axis, float coordinateWidth, DataZoom dataZoom, bool forcePercent)
{
var context = axis.context;
var destMaxValue = context.destMaxValue;
var destMinValue = context.destMinValue;
var isCategory = axis.IsCategory();
var serie = chart.GetSerie(0);
if (isCategory && serie != null && serie.useSortData)
{
var showData = serie.GetDataList(dataZoom);
var isChanged = CheckSortedDataChanged(axis, showData);
if (isChanged)
{
for (int i = 0; i < context.labelObjectList.Count; i++)
{
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);
context.labelObjectList[i].SetText(text);
}
}
axis.context.sortedDataIndices.Clear();
for (int i = 0; i < showData.Count; i++)
{
axis.context.sortedDataIndices.Add(showData[i].index);
}
}
}
else
{
for (int i = 0; i < context.labelObjectList.Count; i++)
{
if (context.labelObjectList[i] != null)
{
var text = AxisHelper.GetLabelName(axis, coordinateWidth, i, destMinValue, destMaxValue, dataZoom, forcePercent);
context.labelObjectList[i].SetText(text);
}
}
}
}
private bool CheckSortedDataChanged(Axis axis, List<SerieData> dataList)
{
if (dataList.Count != axis.context.sortedDataIndices.Count) return true;
for (int i = 0; i < dataList.Count; i++)
{
if (dataList[i].index != axis.context.sortedDataIndices[i]) return true;
}
return false;
} }
internal void UpdateAxisTickValueList(Axis axis) internal void UpdateAxisTickValueList(Axis axis)
@@ -558,8 +615,8 @@ namespace XCharts
} }
if (axis.axisName.show) if (axis.axisName.show)
{ {
ChartLabel label = null; ChartLabel label;
var relativedDist = (relativedAxis == null ? 0 : relativedAxis.context.offset); var relativedDist = relativedAxis == null ? 0 : relativedAxis.context.offset;
var zeroPos = new Vector3(axisStartX, axisStartY + relativedDist); var zeroPos = new Vector3(axisStartX, axisStartY + relativedDist);
var offset = axis.axisName.labelStyle.offset; var offset = axis.axisName.labelStyle.offset;
var autoColor = axis.axisLine.GetColor(chart.theme.axis.lineColor); var autoColor = axis.axisLine.GetColor(chart.theme.axis.lineColor);

View File

@@ -80,7 +80,7 @@ namespace XCharts.Runtime
InitRadiusAxis(axis); InitRadiusAxis(axis);
else else
{ {
axis.UpdateLabelText(polar.context.radius, null, false); UpdateLabelText(axis, polar.context.radius, null, false);
} }
} }

View File

@@ -641,6 +641,7 @@ namespace XCharts.Runtime
private bool GetAxisCategory(int gridIndex, ref int dataIndex, ref string category, ref int timestamp, ref double axisRange) 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) foreach (var component in chart.components)
{ {
if (component is Axis) if (component is Axis)
@@ -653,7 +654,12 @@ namespace XCharts.Runtime
dataIndex = double.IsNaN(axis.context.pointerValue) dataIndex = double.IsNaN(axis.context.pointerValue)
? axis.context.dataZoomStartIndex ? axis.context.dataZoomStartIndex
: (int)axis.context.axisTooltipValue; : (int)axis.context.axisTooltipValue;
category = axis.GetData(dataIndex); int axisIndex = dataIndex;
if (needSort && axisIndex < axis.context.sortedDataIndices.Count)
{
axisIndex = axis.context.sortedDataIndices[axisIndex];
}
category = axis.GetData(axisIndex);
return true; return true;
} }
else if (axis.IsTime()) else if (axis.IsTime())

View File

@@ -101,6 +101,16 @@ namespace XCharts.Runtime
return false; return false;
} }
public bool HasRealtimeSortSerie()
{
foreach (var serie in m_Series)
{
if (serie.useSortData)
return true;
}
return false;
}
public T GetSerie<T>() where T : Serie public T GetSerie<T>() where T : Serie
{ {
foreach (var serie in m_Series) foreach (var serie in m_Series)

View File

@@ -1,3 +1,5 @@
using UnityEngine;
namespace XCharts.Runtime namespace XCharts.Runtime
{ {
[System.Serializable] [System.Serializable]
@@ -11,6 +13,8 @@ namespace XCharts.Runtime
[SerieDataExtraField("m_Ignore")] [SerieDataExtraField("m_Ignore")]
public class Bar : Serie, INeedSerieContainer public class Bar : Serie, INeedSerieContainer
{ {
public override bool useSortData { get { return realtimeSort; } }
public int containerIndex { get; internal set; } public int containerIndex { get; internal set; }
public int containterInstanceId { get; internal set; } public int containterInstanceId { get; internal set; }

View File

@@ -161,6 +161,10 @@ namespace XCharts.Runtime
m_SerieGrid = chart.GetChartComponent<GridCoord>(axis.gridIndex); m_SerieGrid = chart.GetChartComponent<GridCoord>(axis.gridIndex);
if (m_SerieGrid == null) if (m_SerieGrid == null)
return; return;
if(serie.useSortData)
{
SerieHelper.UpdateSerieRuntimeFilterData(serie);
}
var dataZoom = chart.GetDataZoomOfAxis(axis); var dataZoom = chart.GetDataZoomOfAxis(axis);
var showData = serie.GetDataList(dataZoom); var showData = serie.GetDataList(dataZoom);

View File

@@ -305,6 +305,7 @@ namespace XCharts.Runtime
[SerializeField] private float m_Top; [SerializeField] private float m_Top;
[SerializeField] private float m_Bottom; [SerializeField] private float m_Bottom;
[SerializeField] private bool m_InsertDataToHead; [SerializeField] private bool m_InsertDataToHead;
[SerializeField][Since("v3.14.0")] private bool m_RealtimeSort = false;
[SerializeField] private LineStyle m_LineStyle = new LineStyle(); [SerializeField] private LineStyle m_LineStyle = new LineStyle();
[SerializeField] private SerieSymbol m_Symbol = new SerieSymbol(); [SerializeField] private SerieSymbol m_Symbol = new SerieSymbol();
@@ -983,6 +984,15 @@ namespace XCharts.Runtime
set { if (PropertyUtil.SetStruct(ref m_MinShowLabelValue, value)) { SetVerticesDirty(); } } set { if (PropertyUtil.SetStruct(ref m_MinShowLabelValue, value)) { SetVerticesDirty(); } }
} }
/// <summary> /// <summary>
/// Whether to enable realtime sorting, which is used for bar-racing effect. Currently only available in Bar.
/// ||是否开启实时排序用来实现动态排序图效果。目前仅在Bar中生效。
/// </summary>
public bool realtimeSort
{
get { return m_RealtimeSort; }
set { if (PropertyUtil.SetStruct(ref m_RealtimeSort, value)) SetVerticesDirty(); }
}
/// <summary>
/// 系列中的数据内容数组。SerieData可以设置1到n维数据。 /// 系列中的数据内容数组。SerieData可以设置1到n维数据。
/// </summary> /// </summary>
public List<SerieData> data { get { return m_Data; } } public List<SerieData> data { get { return m_Data; } }