Files
XCharts/Runtime/Serie/SerieData.cs

471 lines
17 KiB
C#
Raw Normal View History

2021-12-24 13:33:09 +08:00

2021-12-23 13:23:18 +08:00
using System;
using System.Collections.Generic;
2019-07-28 00:44:53 +08:00
using UnityEngine;
2022-02-19 22:37:57 +08:00
namespace XCharts.Runtime
2019-07-28 00:44:53 +08:00
{
/// <summary>
/// A data item of serie.
2021-12-09 07:12:15 +08:00
/// 系列中的一个数据项。可存储数据名和1-n维个数据。
2019-07-28 00:44:53 +08:00
/// </summary>
[System.Serializable]
2021-11-23 13:20:07 +08:00
public class SerieData : ChildComponent
2019-07-28 00:44:53 +08:00
{
2022-03-04 22:17:32 +08:00
[SerializeField] private int m_Index;
2019-07-28 00:44:53 +08:00
[SerializeField] private string m_Name;
2022-03-04 22:17:32 +08:00
[SerializeField] private string m_Id;
[SerializeField] private string m_ParentId;
2021-11-23 13:20:07 +08:00
[SerializeField] private List<ItemStyle> m_ItemStyles = new List<ItemStyle>();
[SerializeField] private List<LabelStyle> m_Labels = new List<LabelStyle>();
[SerializeField] private List<LabelLine> m_LabelLines = new List<LabelLine>();
[SerializeField] private List<Emphasis> m_Emphases = new List<Emphasis>();
[SerializeField] private List<SymbolStyle> m_Symbols = new List<SymbolStyle>();
[SerializeField] private List<IconStyle> m_IconStyles = new List<IconStyle>();
2022-01-22 21:08:26 +08:00
[SerializeField] private List<LineStyle> m_LineStyles = new List<LineStyle>();
[SerializeField] private List<AreaStyle> m_AreaStyles = new List<AreaStyle>();
2022-02-19 17:35:22 +08:00
[SerializeField] private List<TitleStyle> m_TitleStyles = new List<TitleStyle>();
[SerializeField] private List<double> m_Data = new List<double>();
2021-11-23 13:20:07 +08:00
2021-12-23 13:23:18 +08:00
[NonSerialized] public SerieDataContext context = new SerieDataContext();
[NonSerialized] public InteractData interact = new InteractData();
2022-03-04 22:17:32 +08:00
[NonSerialized] private bool m_Ignore = false;
[NonSerialized] private bool m_Selected;
[NonSerialized] private float m_Radius;
2021-01-11 08:54:28 +08:00
public ChartLabel labelObject { get; set; }
2022-02-19 17:35:22 +08:00
public ChartLabel titleObject { get; set; }
2019-07-28 00:44:53 +08:00
private bool m_Show = true;
2022-03-04 22:17:32 +08:00
public override int index { get { return m_Index; } set { m_Index = value; } }
2019-07-28 00:44:53 +08:00
/// <summary>
/// the name of data item.
/// 数据项名称。
2019-07-28 00:44:53 +08:00
/// </summary>
public string name { get { return m_Name; } set { m_Name = value; } }
/// <summary>
2021-11-23 13:20:07 +08:00
/// 数据项的唯一id。唯一id不是必须设置的。
/// </summary>
2022-03-04 22:17:32 +08:00
public string id { get { return m_Id; } set { m_Id = value; } }
public string parentId { get { return m_ParentId; } set { m_ParentId = value; } }
2021-11-23 13:20:07 +08:00
/// <summary>
/// 数据项图例名称。当数据项名称不为空时图例名称即为系列名称反之则为索引index。
/// </summary>
/// <value></value>
public string legendName { get { return string.IsNullOrEmpty(name) ? ChartCached.IntToStr(index) : name; } }
/// <summary>
/// 自定义半径。可用在饼图中自定义某个数据项的半径。
/// </summary>
public float radius { get { return m_Radius; } set { m_Radius = value; } }
/// <summary>
/// Whether the data item is selected.
/// 该数据项是否被选中。
2019-07-28 00:44:53 +08:00
/// </summary>
public bool selected { get { return m_Selected; } set { m_Selected = value; } }
/// <summary>
/// the icon of data.
/// 数据项图标样式。
/// </summary>
2021-11-23 13:20:07 +08:00
public IconStyle iconStyle { get { return m_IconStyles.Count > 0 ? m_IconStyles[0] : null; } }
2019-11-30 21:24:04 +08:00
/// <summary>
/// 单个数据项的标签设置。
/// </summary>
2021-11-23 13:20:07 +08:00
public LabelStyle label { get { return m_Labels.Count > 0 ? m_Labels[0] : null; } }
public LabelLine labelLine { get { return m_LabelLines.Count > 0 ? m_LabelLines[0] : null; } }
/// <summary>
/// 单个数据项的样式设置。
/// </summary>
2021-11-23 13:20:07 +08:00
public ItemStyle itemStyle { get { return m_ItemStyles.Count > 0 ? m_ItemStyles[0] : null; } }
/// <summary>
/// 单个数据项的高亮样式设置。
/// </summary>
2021-11-23 13:20:07 +08:00
public Emphasis emphasis { get { return m_Emphases.Count > 0 ? m_Emphases[0] : null; } }
/// <summary>
/// 单个数据项的标记设置。
/// </summary>
2021-11-23 13:20:07 +08:00
public SymbolStyle symbol { get { return m_Symbols.Count > 0 ? m_Symbols[0] : null; } }
2022-01-22 21:08:26 +08:00
public LineStyle lineStyle { get { return m_LineStyles.Count > 0 ? m_LineStyles[0] : null; } }
public AreaStyle areaStyle { get { return m_AreaStyles.Count > 0 ? m_AreaStyles[0] : null; } }
2022-02-19 17:35:22 +08:00
public TitleStyle titleStyle { get { return m_TitleStyles.Count > 0 ? m_TitleStyles[0] : null; } }
/// <summary>
/// 是否忽略数据。当为 true 时,数据不进行绘制。
/// </summary>
public bool ignore
{
get { return m_Ignore; }
set { if (PropertyUtil.SetStruct(ref m_Ignore, value)) SetVerticesDirty(); }
}
/// <summary>
/// An arbitrary dimension data list of data item.
/// 可指定任意维数的数值列表。
2019-07-28 00:44:53 +08:00
/// </summary>
public List<double> data { get { return m_Data; } set { m_Data = value; } }
2019-07-28 00:44:53 +08:00
/// <summary>
/// [default:true] Whether the data item is showed.
/// 该数据项是否要显示。
2019-07-28 00:44:53 +08:00
/// </summary>
public bool show { get { return m_Show; } set { m_Show = value; } }
private List<double> m_PreviousData = new List<double>();
2019-12-03 07:49:37 +08:00
private List<float> m_DataUpdateTime = new List<float>();
private List<bool> m_DataUpdateFlag = new List<bool>();
2021-05-29 22:07:09 +08:00
private List<Vector2> m_PolygonPoints = new List<Vector2>();
2019-11-30 21:24:04 +08:00
public void Reset()
2020-05-09 09:42:41 +08:00
{
index = 0;
2022-03-04 22:17:32 +08:00
m_Id = null;
m_ParentId = null;
labelObject = null;
2020-05-09 09:42:41 +08:00
m_Name = string.Empty;
m_Show = true;
2020-05-09 09:42:41 +08:00
m_Selected = false;
2021-12-09 07:12:15 +08:00
context.canShowLabel = true;
2021-12-11 18:26:28 +08:00
context.highlight = false;
2022-03-04 22:17:32 +08:00
context.children.Clear();
context.dataPoints.Clear();
2020-05-09 09:42:41 +08:00
m_Radius = 0;
2021-12-23 13:23:18 +08:00
interact.Reset();
2020-05-09 09:42:41 +08:00
m_Data.Clear();
m_PreviousData.Clear();
m_DataUpdateTime.Clear();
m_DataUpdateFlag.Clear();
2021-11-23 13:20:07 +08:00
m_IconStyles.Clear();
m_Labels.Clear();
m_LabelLines.Clear();
m_ItemStyles.Clear();
m_Emphases.Clear();
m_Symbols.Clear();
2022-01-22 21:08:26 +08:00
m_LineStyles.Clear();
m_AreaStyles.Clear();
2022-02-19 17:35:22 +08:00
m_TitleStyles.Clear();
2020-05-09 09:42:41 +08:00
}
2021-11-23 13:20:07 +08:00
public T GetOrAddComponent<T>() where T : ChildComponent
{
var type = typeof(T);
if (type == typeof(ItemStyle))
{
if (m_ItemStyles.Count == 0)
m_ItemStyles.Add(new ItemStyle() { show = true });
return m_ItemStyles[0] as T;
}
else if (type == typeof(IconStyle))
{
if (m_IconStyles.Count == 0)
m_IconStyles.Add(new IconStyle() { show = true });
return m_IconStyles[0] as T;
}
else if (type == typeof(LabelStyle))
{
if (m_Labels.Count == 0)
m_Labels.Add(new LabelStyle() { show = true });
return m_Labels[0] as T;
}
else if (type == typeof(LabelLine))
{
if (m_LabelLines.Count == 0)
m_LabelLines.Add(new LabelLine() { show = true });
return m_LabelLines[0] as T;
}
else if (type == typeof(Emphasis))
{
if (m_Emphases.Count == 0)
m_Emphases.Add(new Emphasis() { show = true });
return m_Emphases[0] as T;
}
else if (type == typeof(SymbolStyle))
{
if (m_Symbols.Count == 0)
m_Symbols.Add(new SymbolStyle() { show = true });
return m_Symbols[0] as T;
}
2022-01-22 21:08:26 +08:00
else if (type == typeof(LineStyle))
{
if (m_LineStyles.Count == 0)
m_LineStyles.Add(new LineStyle() { show = true });
return m_LineStyles[0] as T;
}
else if (type == typeof(AreaStyle))
{
if (m_AreaStyles.Count == 0)
m_AreaStyles.Add(new AreaStyle() { show = true });
return m_AreaStyles[0] as T;
}
2022-02-19 17:35:22 +08:00
else if (type == typeof(TitleStyle))
{
if (m_TitleStyles.Count == 0)
m_TitleStyles.Add(new TitleStyle() { show = true });
return m_TitleStyles[0] as T;
}
2021-11-23 13:20:07 +08:00
else
{
throw new System.Exception("SerieData not support component:" + type);
}
}
public void RemoveAllComponent()
{
m_ItemStyles.Clear();
m_IconStyles.Clear();
m_Labels.Clear();
m_LabelLines.Clear();
m_Symbols.Clear();
m_Emphases.Clear();
2022-01-22 21:08:26 +08:00
m_LineStyles.Clear();
m_AreaStyles.Clear();
2022-02-19 17:35:22 +08:00
m_TitleStyles.Clear();
2021-11-23 13:20:07 +08:00
}
public void RemoveComponent<T>() where T : ISerieDataComponent
{
var type = typeof(T);
if (type == typeof(ItemStyle))
m_ItemStyles.Clear();
else if (type == typeof(IconStyle))
m_IconStyles.Clear();
else if (type == typeof(LabelStyle))
m_Labels.Clear();
else if (type == typeof(LabelLine))
m_LabelLines.Clear();
else if (type == typeof(Emphasis))
m_Emphases.Clear();
else if (type == typeof(SymbolStyle))
m_Symbols.Clear();
2022-01-22 21:08:26 +08:00
else if (type == typeof(LineStyle))
m_LineStyles.Clear();
else if (type == typeof(AreaStyle))
m_AreaStyles.Clear();
2022-02-19 17:35:22 +08:00
else if (type == typeof(TitleStyle))
m_TitleStyles.Clear();
2021-11-23 13:20:07 +08:00
else
throw new System.Exception("SerieData not support component:" + type);
}
public double GetData(int index, bool inverse = false)
{
2019-11-30 21:24:04 +08:00
if (index >= 0 && index < m_Data.Count)
{
return inverse ? -m_Data[index] : m_Data[index];
2019-11-30 21:24:04 +08:00
}
else return 0;
}
2019-08-16 00:13:01 +08:00
public double GetData(int index, double min, double max)
2021-05-22 15:29:55 +08:00
{
if (index >= 0 && index < m_Data.Count)
{
var value = m_Data[index];
if (value < min) return min;
else if (value > max) return max;
else return value;
}
else return 0;
}
public double GetPreviousData(int index, bool inverse = false)
2019-12-03 07:49:37 +08:00
{
2020-03-08 10:47:48 +08:00
if (index >= 0 && index < m_PreviousData.Count)
2019-12-03 07:49:37 +08:00
{
return inverse ? -m_PreviousData[index] : m_PreviousData[index];
2019-12-03 07:49:37 +08:00
}
else return 0;
}
public double GetFirstData(float animationDuration = 500f)
2020-03-08 10:47:48 +08:00
{
if (m_Data.Count > 0) return GetCurrData(0, animationDuration);
return 0;
}
public double GetLastData()
2020-03-08 10:47:48 +08:00
{
if (m_Data.Count > 0) return m_Data[m_Data.Count - 1];
return 0;
}
public double GetCurrData(int index, float animationDuration = 500f, bool inverse = false)
{
return GetCurrData(index, animationDuration, inverse, 0, 0);
}
public double GetCurrData(int index, float animationDuration, bool inverse, double min, double max)
2019-11-30 21:24:04 +08:00
{
2019-12-03 07:49:37 +08:00
if (index < m_DataUpdateFlag.Count && m_DataUpdateFlag[index] && animationDuration > 0)
2019-11-30 21:24:04 +08:00
{
2019-12-03 07:49:37 +08:00
var time = Time.time - m_DataUpdateTime[index];
2019-11-30 21:24:04 +08:00
var total = animationDuration / 1000;
var rate = time / total;
if (rate > 1) rate = 1;
if (rate < 1)
2019-11-30 21:24:04 +08:00
{
2019-12-03 07:49:37 +08:00
CheckLastData();
var curr = MathUtil.Lerp(GetPreviousData(index), GetData(index), rate);
if (min != 0 || max != 0)
{
if (inverse)
{
var temp = min;
min = -max;
max = -temp;
}
var pre = m_PreviousData[index];
if (pre < min)
{
m_PreviousData[index] = min;
curr = min;
}
else if (pre > max)
{
m_PreviousData[index] = max;
curr = max;
}
}
curr = inverse ? -curr : curr;
2019-11-30 21:24:04 +08:00
return curr;
}
else
{
2019-12-03 07:49:37 +08:00
m_DataUpdateFlag[index] = false;
return GetData(index, inverse);
2019-11-30 21:24:04 +08:00
}
}
else
{
return GetData(index, inverse);
2019-11-30 21:24:04 +08:00
}
}
/// <summary>
/// the maxinum value.
/// 最大值。
/// </summary>
public double GetMaxData(bool inverse = false)
{
if (m_Data.Count == 0) return 0;
var temp = double.MinValue;
for (int i = 0; i < m_Data.Count; i++)
{
var value = GetData(i, inverse);
if (value > temp) temp = value;
}
return temp;
}
/// <summary>
/// the mininum value.
/// 最小值。
/// </summary>
public double GetMinData(bool inverse = false)
{
if (m_Data.Count == 0) return 0;
var temp = double.MaxValue;
for (int i = 0; i < m_Data.Count; i++)
{
var value = GetData(i, inverse);
if (value < temp) temp = value;
}
return temp;
}
public bool UpdateData(int dimension, double value, bool updateAnimation, float animationDuration = 500f)
2019-11-30 21:24:04 +08:00
{
2019-12-03 07:49:37 +08:00
if (dimension >= 0 && dimension < data.Count)
{
CheckLastData();
m_PreviousData[dimension] = GetCurrData(dimension, animationDuration);
//m_PreviousData[dimension] = data[dimension];;
2019-12-03 07:49:37 +08:00
m_DataUpdateTime[dimension] = Time.time;
m_DataUpdateFlag[dimension] = updateAnimation;
2019-12-03 07:49:37 +08:00
data[dimension] = value;
return true;
2019-12-03 07:49:37 +08:00
}
return false;
2019-11-30 21:24:04 +08:00
}
2021-11-23 13:20:07 +08:00
public bool UpdateData(int dimension, double value)
{
if (dimension >= 0 && dimension < data.Count)
{
data[dimension] = value;
return true;
}
return false;
}
2019-12-03 07:49:37 +08:00
private void CheckLastData()
2019-11-30 21:24:04 +08:00
{
2020-03-08 10:47:48 +08:00
if (m_PreviousData.Count != m_Data.Count)
2019-11-30 21:24:04 +08:00
{
2020-03-08 10:47:48 +08:00
m_PreviousData.Clear();
2019-12-20 09:17:15 +08:00
m_DataUpdateTime.Clear();
m_DataUpdateFlag.Clear();
2019-12-03 07:49:37 +08:00
for (int i = 0; i < m_Data.Count; i++)
2019-11-30 21:24:04 +08:00
{
2020-03-08 10:47:48 +08:00
m_PreviousData.Add(m_Data[i]);
2019-12-03 07:49:37 +08:00
m_DataUpdateTime.Add(Time.time);
m_DataUpdateFlag.Add(false);
2019-11-30 21:24:04 +08:00
}
}
}
public bool IsDataChanged()
{
2022-03-20 18:52:50 +08:00
for (int i = 0; i < m_DataUpdateFlag.Count; i++)
if (m_DataUpdateFlag[i]) return true;
2019-12-03 07:49:37 +08:00
return false;
2019-11-30 21:24:04 +08:00
}
public float GetLabelWidth()
{
if (labelObject != null) return labelObject.GetLabelWidth();
else return 0;
}
public float GetLabelHeight()
{
if (labelObject != null) return labelObject.GetLabelHeight();
return 0;
}
public void SetLabelActive(bool flag)
2019-11-30 21:24:04 +08:00
{
if (labelObject != null) labelObject.SetLabelActive(flag);
2019-11-30 21:24:04 +08:00
}
public void SetIconActive(bool flag)
{
if (labelObject != null) labelObject.SetIconActive(flag);
}
2021-05-29 22:07:09 +08:00
public void SetPolygon(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4)
{
m_PolygonPoints.Clear();
m_PolygonPoints.Add(p1);
m_PolygonPoints.Add(p2);
m_PolygonPoints.Add(p3);
m_PolygonPoints.Add(p4);
}
2022-02-19 17:35:22 +08:00
public void SetPolygon(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, Vector2 p5)
{
SetPolygon(p1, p2, p3, p4);
m_PolygonPoints.Add(p5);
}
2021-05-29 22:07:09 +08:00
public bool IsInPolygon(Vector2 p)
{
if (m_PolygonPoints.Count == 0) return false;
var inside = false;
var j = m_PolygonPoints.Count - 1;
for (int i = 0; i < m_PolygonPoints.Count; j = i++)
{
var pi = m_PolygonPoints[i];
var pj = m_PolygonPoints[j];
if (((pi.y <= p.y && p.y < pj.y) || (pj.y <= p.y && p.y < pi.y)) &&
(p.x < (pj.x - pi.x) * (p.y - pi.y) / (pj.y - pi.y) + pi.x))
inside = !inside;
}
return inside;
}
2019-07-28 00:44:53 +08:00
}
}