| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 | using System;using System.Collections.Generic;using UnityEngine;namespace XCharts.Runtime{    /// <summary>    /// Legend component.The legend component shows different sets of tags, colors, and names.     /// You can control which series are not displayed by clicking on the legend.    /// ||图例组件。    /// 图例组件展现了不同系列的标记,颜色和名字。可以通过点击图例控制哪些系列不显示。    /// </summary>    [System.Serializable]    [ComponentHandler(typeof(LegendHandler), true)]    public class Legend : MainComponent, IPropertyChanged    {        public enum Type        {            /// <summary>            /// 自动匹配。            /// </summary>            Auto,            /// <summary>            /// 自定义图标。            /// </summary>            Custom,            /// <summary>            /// 空心圆。            /// </summary>            EmptyCircle,            /// <summary>            /// 圆形。            /// </summary>            Circle,            /// <summary>            /// 正方形。可通过Setting的legendIconCornerRadius参数调整圆角。            /// </summary>            Rect,            /// <summary>            /// 三角形。            /// </summary>            Triangle,            /// <summary>            /// 菱形。            /// </summary>            Diamond,            /// <summary>            /// 烛台(可用于K线图)。            /// </summary>            Candlestick,        }        /// <summary>        /// Selected mode of legend, which controls whether series can be toggled displaying by clicking legends.        /// ||图例选择的模式,控制是否可以通过点击图例改变系列的显示状态。默认开启图例选择,可以设成 None 关闭。        /// </summary>        public enum SelectedMode        {            /// <summary>            /// 多选。            /// </summary>            Multiple,            /// <summary>            /// 单选。            /// </summary>            Single,            /// <summary>            /// 无法选择。            /// </summary>            None        }        [SerializeField] private bool m_Show = true;        [SerializeField] private Type m_IconType = Type.Auto;        [SerializeField] private SelectedMode m_SelectedMode = SelectedMode.Multiple;        [SerializeField] private Orient m_Orient = Orient.Horizonal;        [SerializeField] private Location m_Location = new Location() { align = Location.Align.TopCenter, top = 0.125f };        [SerializeField] private float m_ItemWidth = 25.0f;        [SerializeField] private float m_ItemHeight = 12.0f;        [SerializeField] private float m_ItemGap = 10f;        [SerializeField] private bool m_ItemAutoColor = true;        [SerializeField] private float m_ItemOpacity = 1;        [SerializeField] private string m_Formatter;        [SerializeField] private LabelStyle m_LabelStyle = new LabelStyle();        [SerializeField][Since("v3.10.0")] private TextLimit m_TextLimit = new TextLimit();        [SerializeField] private List<string> m_Data = new List<string>();        [SerializeField] private List<Sprite> m_Icons = new List<Sprite>();        [SerializeField] private List<Color> m_Colors = new List<Color>();        [SerializeField][Since("v3.1.0")] protected ImageStyle m_Background = new ImageStyle() { show = false };        [SerializeField][Since("v3.1.0")] protected Padding m_Padding = new Padding();        [SerializeField][Since("v3.6.0")] private List<Vector3> m_Positions = new List<Vector3>();        public LegendContext context = new LegendContext();        /// <summary>        /// Whether to show legend component.        /// ||是否显示图例组件。        /// </summary>        public bool show        {            get { return m_Show; }            set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetComponentDirty(); }        }        /// <summary>        /// Type of legend.        /// ||图例类型。        /// </summary>        public Type iconType        {            get { return m_IconType; }            set { if (PropertyUtil.SetStruct(ref m_IconType, value)) SetAllDirty(); }        }        /// <summary>        /// Selected mode of legend, which controls whether series can be toggled displaying by clicking legends.        /// ||选择模式。控制是否可以通过点击图例改变系列的显示状态。默认开启图例选择,可以设成 None 关闭。        /// </summary>        public SelectedMode selectedMode        {            get { return m_SelectedMode; }            set { if (PropertyUtil.SetStruct(ref m_SelectedMode, value)) SetComponentDirty(); }        }        /// <summary>        /// Specify whether the layout of legend component is horizontal or vertical.        /// ||布局方式是横还是竖。        /// </summary>        public Orient orient        {            get { return m_Orient; }            set { if (PropertyUtil.SetStruct(ref m_Orient, value)) SetComponentDirty(); }        }        /// <summary>        /// The location of legend.        /// ||图例显示的位置。        /// </summary>        public Location location        {            get { return m_Location; }            set { if (PropertyUtil.SetClass(ref m_Location, value)) SetComponentDirty(); }        }        /// <summary>        /// Image width of legend symbol.        /// ||图例标记的图形宽度。        /// </summary>        public float itemWidth        {            get { return m_ItemWidth; }            set { if (PropertyUtil.SetStruct(ref m_ItemWidth, value)) SetComponentDirty(); }        }        /// <summary>        /// Image height of legend symbol.        /// ||图例标记的图形高度。        /// </summary>        public float itemHeight        {            get { return m_ItemHeight; }            set { if (PropertyUtil.SetStruct(ref m_ItemHeight, value)) SetComponentDirty(); }        }        /// <summary>        /// The distance between each legend, horizontal distance in horizontal layout, and vertical distance in vertical layout.        /// ||图例每项之间的间隔。横向布局时为水平间隔,纵向布局时为纵向间隔。        /// </summary>        public float itemGap        {            get { return m_ItemGap; }            set { if (PropertyUtil.SetStruct(ref m_ItemGap, value)) SetComponentDirty(); }        }        /// <summary>        /// Whether the legend symbol matches the color automatically.        /// ||图例标记的图形是否自动匹配颜色。        /// </summary>        public bool itemAutoColor        {            get { return m_ItemAutoColor; }            set { if (PropertyUtil.SetStruct(ref m_ItemAutoColor, value)) SetComponentDirty(); }        }        /// <summary>        /// the opacity of item color.        /// ||图例标记的图形的颜色透明度。        /// </summary>        public float itemOpacity        {            get { return m_ItemOpacity; }            set { if (PropertyUtil.SetStruct(ref m_ItemOpacity, value)) SetComponentDirty(); }        }        /// <summary>        /// No longer used, the use of LabelStyle.formatter instead.        /// ||不再使用,使用LabelStyle.formatter代替。        /// </summary>        [Obsolete("Use LabelStyle.formatter instead.", false)]        public string formatter        {            get { return m_Formatter; }            set { if (PropertyUtil.SetClass(ref m_Formatter, value)) SetComponentDirty(); }        }        /// <summary>        /// the style of text.        /// ||文本样式。        /// </summary>        public LabelStyle labelStyle        {            get { return m_LabelStyle; }            set { if (PropertyUtil.SetClass(ref m_LabelStyle, value)) SetComponentDirty(); }        }        /// <summary>        /// the limit of text.         /// ||文本限制。        /// </summary>        public TextLimit textLimit        {            get { return m_TextLimit; }            set { if (value != null) { m_TextLimit = value; SetComponentDirty(); } }        }        /// <summary>        /// the sytle of background.        /// ||背景图样式。        /// </summary>        public ImageStyle background        {            get { return m_Background; }            set { if (PropertyUtil.SetClass(ref m_Background, value)) SetAllDirty(); }        }        /// <summary>        /// the paddinng of item and background.        /// ||图例标记和背景的间距。        /// </summary>        public Padding padding        {            get { return m_Padding; }            set { if (PropertyUtil.SetClass(ref m_Padding, value)) SetAllDirty(); }        }        /// <summary>        /// Data array of legend. An array item is usually a name representing string. (If it is a pie chart,         /// it could also be the name of a single data in the pie chart) of a series.        /// If data is not specified, it will be auto collected from series.        /// ||图例的数据数组。数组项通常为一个字符串,每一项代表一个系列的 name(如果是饼图,也可以是饼图单个数据的 name)。        /// 如果 data 没有被指定,会自动从当前系列中获取。指定data时里面的数据项和serie匹配时才会生效。        /// </summary>        public List<string> data        {            get { return m_Data; }            set { if (value != null) { m_Data = value; SetComponentDirty(); } }        }        /// <summary>        /// 自定义的图例标记图形。        /// </summary>        public List<Sprite> icons        {            get { return m_Icons; }            set { if (value != null) { m_Icons = value; SetComponentDirty(); } }        }        /// <summary>        /// the colors of legend item.        /// ||图例标记的颜色列表。        /// </summary>        public List<Color> colors        {            get { return m_Colors; }            set { if (value != null) { m_Colors = value; SetAllDirty(); } }        }        /// <summary>        /// the custom positions of legend item.        /// ||图例标记的自定义位置列表。        /// </summary>        public List<Vector3> positions        {            get { return m_Positions; }            set { if (value != null) { m_Positions = value; SetAllDirty(); } }        }        /// <summary>        /// 图表是否需要刷新(图例组件不需要刷新图表)        /// </summary>        public override bool vertsDirty { get { return false; } }        /// <summary>        /// 组件是否需要刷新        /// </summary>        public override bool componentDirty        {            get { return m_ComponentDirty || location.componentDirty || labelStyle.componentDirty || textLimit.componentDirty; }        }        public override void ClearComponentDirty()        {            base.ClearComponentDirty();            location.ClearComponentDirty();            labelStyle.ClearComponentDirty();            textLimit.ClearComponentDirty();        }        /// <summary>        /// Clear legend data.        /// ||清空。        /// </summary>        public override void ClearData()        {            m_Data.Clear();            SetComponentDirty();        }        /// <summary>        /// Whether include in legend data by the specified name.        /// ||是否包括由指定名字的图例        /// </summary>        /// <param name="name"></param>        /// <returns></returns>        public bool ContainsData(string name)        {            return m_Data.Contains(name);        }        /// <summary>        /// Removes the legend with the specified name.        /// ||移除指定名字的图例。        /// </summary>        /// <param name="name"></param>        public void RemoveData(string name)        {            if (m_Data.Contains(name))            {                m_Data.Remove(name);                SetComponentDirty();            }        }        /// <summary>        /// Add legend data.        /// ||添加图例。        /// </summary>        /// <param name="name"></param>        public void AddData(string name)        {            if (!m_Data.Contains(name) && !string.IsNullOrEmpty(name))            {                m_Data.Add(name);                SetComponentDirty();            }        }        /// <summary>        /// Gets the legend for the specified index.        /// ||获得指定索引的图例。        /// </summary>        /// <param name="index"></param>        /// <returns></returns>        public string GetData(int index)        {            if (index >= 0 && index < m_Data.Count)            {                return m_Data[index];            }            return null;        }        /// <summary>        /// Gets the index of the specified legend.        /// ||获得指定图例的索引。        /// </summary>        /// <param name="legendName"></param>        /// <returns></returns>        public int GetIndex(string legendName)        {            return m_Data.IndexOf(legendName);        }        /// <summary>        /// Remove all legend buttons.        /// ||移除所有图例按钮。        /// </summary>        public void RemoveButton()        {            context.buttonList.Clear();        }        /// <summary>        /// Bind buttons to legends.        /// ||给图例绑定按钮。        /// </summary>        /// <param name="name"></param>        /// <param name="btn"></param>        /// <param name="total"></param>        public void SetButton(string name, LegendItem item, int total)        {            context.buttonList[name] = item;            int index = context.buttonList.Values.Count;            item.SetIconActive(iconType == Type.Custom);            item.SetActive(show);        }        /// <summary>        /// Update the legend button color.        /// ||更新图例按钮颜色。        /// </summary>        /// <param name="name"></param>        /// <param name="color"></param>        public void UpdateButtonColor(string name, Color color)        {            if (context.buttonList.ContainsKey(name))            {                context.buttonList[name].SetIconColor(color);            }        }        /// <summary>        /// Update the text color of legend.        /// ||更新图例文字颜色。        /// </summary>        /// <param name="name"></param>        /// <param name="color"></param>        public void UpdateContentColor(string name, Color color)        {            if (context.buttonList.ContainsKey(name))            {                context.buttonList[name].SetContentColor(color);            }        }        /// <summary>        /// Gets the legend button for the specified index.        /// ||获得指定索引的图例按钮。        /// </summary>        /// <param name="index"></param>        /// <returns></returns>        public Sprite GetIcon(int index)        {            if (index >= 0 && index < m_Icons.Count)            {                return m_Icons[index];            }            else            {                return null;            }        }        public Color GetColor(int index)        {            if (index >= 0 && index < m_Colors.Count)                return m_Colors[index];            else                return Color.white;        }        public Vector3 GetPosition(int index, Vector3 defaultPos)        {            if (index >= 0 && index < m_Positions.Count)                return m_Positions[index];            else                return defaultPos;        }        /// <summary>        /// Callback handling when parameters change.        /// ||参数变更时的回调处理。        /// </summary>        public void OnChanged()        {            m_Location.OnChanged();        }    }}
 |