using System; using System.Collections.Generic; using System.Reflection; using UnityEngine; namespace XCharts.Runtime { public partial class Serie { public static Dictionary extraComponentMap = new Dictionary { { typeof(LabelStyle), "m_Labels" }, { typeof(LabelLine), "m_LabelLines" }, { typeof(EndLabelStyle), "m_EndLabels" }, { typeof(LineArrow), "m_LineArrows" }, { typeof(AreaStyle), "m_AreaStyles" }, { typeof(TitleStyle), "m_TitleStyles" }, { typeof(EmphasisItemStyle), "m_EmphasisItemStyles" }, { typeof(EmphasisLabelStyle), "m_EmphasisLabels" }, { typeof(EmphasisLabelLine), "m_EmphasisLabelLines" }, }; [SerializeField][IgnoreDoc] private List m_Labels = new List(); [SerializeField][IgnoreDoc] private List m_LabelLines = new List(); [SerializeField][IgnoreDoc] private List m_EndLabels = new List(); [SerializeField][IgnoreDoc] private List m_LineArrows = new List(); [SerializeField][IgnoreDoc] private List m_AreaStyles = new List(); [SerializeField][IgnoreDoc] private List m_TitleStyles = new List(); [SerializeField][IgnoreDoc] private List m_EmphasisItemStyles = new List(); [SerializeField][IgnoreDoc] private List m_EmphasisLabels = new List(); [SerializeField][IgnoreDoc] private List m_EmphasisLabelLines = new List(); /// /// The style of area. /// |区域填充样式。 /// public AreaStyle areaStyle { get { return m_AreaStyles.Count > 0 ? m_AreaStyles[0] : null; } } /// /// Text label of graphic element,to explain some data information about graphic item like value, name and so on. /// |图形上的文本标签,可用于说明图形的一些数据信息,比如值,名称等。 /// public LabelStyle label { get { return m_Labels.Count > 0 ? m_Labels[0] : null; } } public LabelStyle endLabel { get { return m_EndLabels.Count > 0 ? m_EndLabels[0] : null; } } /// /// The line of label. /// |标签上的视觉引导线。 /// public LabelLine labelLine { get { return m_LabelLines.Count > 0 ? m_LabelLines[0] : null; } } /// /// The arrow of line. /// |折线图的箭头。 /// public LineArrow lineArrow { get { return m_LineArrows.Count > 0 ? m_LineArrows[0] : null; } } /// /// 高亮的图形样式 /// public EmphasisItemStyle emphasisItemStyle { get { return m_EmphasisItemStyles.Count > 0 ? m_EmphasisItemStyles[0] : null; } } /// /// 高亮时的标签样式 /// public EmphasisLabelStyle emphasisLabel { get { return m_EmphasisLabels.Count > 0 ? m_EmphasisLabels[0] : null; } } /// /// 高亮时的标签引导线样式 /// public EmphasisLabelLine emphasisLabelLine { get { return m_EmphasisLabelLines.Count > 0 ? m_EmphasisLabelLines[0] : null; } } /// /// the icon of data. /// |数据项标题样式。 /// public TitleStyle titleStyle { get { return m_TitleStyles.Count > 0 ? m_TitleStyles[0] : null; } } public void RemoveAllExtraComponent() { var serieType = GetType(); foreach (var kv in extraComponentMap) { ReflectionUtil.InvokeListClear(this, serieType.GetField(kv.Value)); } SetAllDirty(); } public T AddExtraComponent() where T : ChildComponent, ISerieExtraComponent { return AddExtraComponent(typeof(T)) as T; } public ISerieExtraComponent AddExtraComponent(Type type) { if (GetType().IsDefined(typeof(SerieExtraComponentAttribute), false)) { var attr = GetType().GetAttribute(); if (attr.Contains(type)) { var fieldName = string.Empty; if (extraComponentMap.TryGetValue(type, out fieldName)) { var field = typeof(Serie).GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); if (ReflectionUtil.InvokeListCount(this, field) <= 0) { var extraComponent = Activator.CreateInstance(type) as ISerieExtraComponent; ReflectionUtil.InvokeListAdd(this, field, extraComponent); SetAllDirty(); return extraComponent; } else { return ReflectionUtil.InvokeListGet(this, field, 0); } } } } throw new System.Exception(string.Format("Serie {0} not support extra component: {1}", GetType().Name, type.Name)); } public void RemoveExtraComponent() where T : ISerieExtraComponent { RemoveExtraComponent(typeof(T)); } public void RemoveExtraComponent(Type type) { if (GetType().IsDefined(typeof(SerieExtraComponentAttribute), false)) { var attr = GetType().GetAttribute(); if (attr.Contains(type)) { var fieldName = string.Empty; if (extraComponentMap.TryGetValue(type, out fieldName)) { var field = typeof(Serie).GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic); ReflectionUtil.InvokeListClear(this, field); SetAllDirty(); return; } } } throw new System.Exception(string.Format("Serie {0} not support extra component: {1}", GetType().Name, type.Name)); } private void RemoveExtraComponentList(List list) where T : ISerieExtraComponent { if (list.Count > 0) { list.Clear(); SetAllDirty(); } } } }