using UnityEngine; using UnityEngine.UI; namespace XCharts { /// /// DataZoom component is used for zooming a specific area, /// which enables user to investigate data in detail, /// or get an overview of the data, or get rid of outlier points. /// DataZoom 组件 用于区域缩放,从而能自由关注细节的数据信息,或者概览数据整体,或者去除离群点的影响。 /// [System.Serializable] public class DataZoom { public enum DataZoomType { /// /// DataZoom functionalities is embeded inside coordinate systems, enable user to /// zoom or roam coordinate system by mouse dragging, mouse move or finger touch (in touch screen). /// 内置于坐标系中,使用户可以在坐标系上通过鼠标拖拽、鼠标滚轮、手指滑动(触屏上)来缩放或漫游坐标系。 /// Inside, /// /// A special slider bar is provided, on which coordinate systems can be zoomed or /// roamed by mouse dragging or finger touch (in touch screen). /// 有单独的滑动条,用户在滑动条上进行缩放或漫游。 /// Slider } /// /// Generally dataZoom component zoom or roam coordinate system through data filtering /// and set the windows of axes internally. /// Its behaviours vary according to filtering mode settings. /// dataZoom 的运行原理是通过 数据过滤 来达到 数据窗口缩放 的效果。数据过滤模式的设置不同,效果也不同。 /// public enum FilterMode { /// /// data that outside the window will be filtered, which may lead to some changes of windows of other axes. /// For each data item, it will be filtered if one of the relevant dimensions is out of the window. /// 当前数据窗口外的数据,被 过滤掉。即 会 影响其他轴的数据范围。每个数据项,只要有一个维度在数据窗口外,整个数据项就会被过滤掉。 /// Filter, /// /// data that outside the window will be filtered, which may lead to some changes of windows of other axes. /// For each data item, it will be filtered only if all of the relevant dimensions are out of the same side of the window. /// 当前数据窗口外的数据,被 过滤掉。即 会 影响其他轴的数据范围。每个数据项,只有当全部维度都在数据窗口同侧外部,整个数据项才会被过滤掉。 /// WeakFilter, /// /// data that outside the window will be set to NaN, which will not lead to changes of windows of other axes. /// 当前数据窗口外的数据,被 设置为空。即 不会 影响其他轴的数据范围。 /// Empty, /// /// Do not filter data. /// 不过滤数据,只改变数轴范围。 /// None } /// /// The value type of start and end.取值类型 /// public enum RangeMode { //Value, /// /// percent value. 百分比 /// Percent } [SerializeField] private bool m_Show; [SerializeField] private DataZoomType m_Type; [SerializeField] private FilterMode m_FilterMode; [SerializeField] private Orient m_Orient; [SerializeField] private int m_XAxisIndex; [SerializeField] private int m_YAxisIndex; [SerializeField] private bool m_ShowDataShadow; [SerializeField] private bool m_ShowDetail; [SerializeField] private bool m_ZoomLock; [SerializeField] private bool m_Realtime; [SerializeField] private Color m_BackgroundColor; [SerializeField] private float m_Height; [SerializeField] private float m_Bottom; [SerializeField] private RangeMode m_RangeMode; [SerializeField] private float m_Start; [SerializeField] private float m_End; [SerializeField] private float m_StartValue; [SerializeField] private float m_EndValue; [Range(1f, 20f)] [SerializeField] private float m_ScrollSensitivity; /// /// Whether to show dataZoom. /// 是否显示缩放区域。 /// public bool show { get { return m_Show; } set { m_Show = value; } } /// /// The type of dataZoom. /// 区域缩放类型。 /// public DataZoomType type { get { return m_Type; } set { m_Type = value; } } /// /// The mode of data filter. /// 数据过滤类型。 /// public FilterMode filterMode { get { return m_FilterMode; } set { m_FilterMode = value; } } /// /// Specify whether the layout of dataZoom component is horizontal or vertical. /// 水平还是垂直显示。 /// public Orient orient { get { return m_Orient; } set { m_Orient = value; } } /// /// Specify which xAxis is controlled by the dataZoom. /// 控制哪一个 x 轴。 /// public int xAxisIndex { get { return m_XAxisIndex; } set { m_XAxisIndex = value; } } /// /// Specify which yAxis is controlled by the dataZoom. /// 控制哪一个 y 轴。 /// public int yAxisIndex { get { return m_YAxisIndex; } set { m_YAxisIndex = value; } } /// /// Whether to show data shadow, to indicate the data tendency in brief. /// default:true /// 是否显示数据阴影。数据阴影可以简单地反应数据走势。 /// public bool showDataShadow { get { return m_ShowDataShadow; } set { m_ShowDataShadow = value; } } /// /// Whether to show detail, that is, show the detailed data information when dragging. /// 是否显示detail,即拖拽时候显示详细数值信息。 /// public bool showDetail { get { return m_ShowDetail; } set { m_ShowDetail = value; } } /// /// Specify whether to lock the size of window (selected area). /// default:false /// 是否锁定选择区域(或叫做数据窗口)的大小。 /// 如果设置为 true 则锁定选择区域的大小,也就是说,只能平移,不能缩放。 /// public bool zoomLock { get { return m_ZoomLock; } set { m_ZoomLock = value; } } /// /// Whether to show data shadow in dataZoom-silder component, to indicate the data tendency in brief. /// default:true /// 拖动时,是否实时更新系列的视图。如果设置为 false,则只在拖拽结束的时候更新。 /// public bool realtime { get { return m_Realtime; } set { m_Realtime = value; } } /// /// The background color of the component. /// 组件的背景颜色。 /// public Color backgroundColor { get { return m_BackgroundColor; } set { m_BackgroundColor = value; } } /// /// Distance between dataZoom component and the bottom side of the container. /// bottom value is a instant pixel value like 10. /// default:10 /// 组件离容器下侧的距离。 /// public float bottom { get { return m_Bottom; } set { m_Bottom = value; } } /// /// The height of dataZoom component. /// height value is a instant pixel value like 10. /// default:50 /// 组件高度。 /// public float height { get { return m_Height; } set { m_Height = value; } } /// /// Use absolute value or percent value in DataZoom.start and DataZoom.end. /// default:RangeMode.Percent. /// 取绝对值还是百分比。 /// public RangeMode rangeMode { get { return m_RangeMode; } set { m_RangeMode = value; } } /// /// The start percentage of the window out of the data extent, in the range of 0 ~ 100. /// default:30 /// 数据窗口范围的起始百分比。范围是:0 ~ 100。 /// public float start { get { return m_Start; } set { m_Start = value; } } /// /// The end percentage of the window out of the data extent, in the range of 0 ~ 100. /// default:70 /// 数据窗口范围的结束百分比。范围是:0 ~ 100。 /// public float end { get { return m_End; } set { m_End = value; } } /// /// The sensitivity of dataZoom scroll. /// The larger the number, the more sensitive it is. /// default:10 /// 缩放区域组件的敏感度。值越高每次缩放所代表的数据越多。 /// public float scrollSensitivity { get { return m_ScrollSensitivity; } set { m_ScrollSensitivity = value; } } /// /// DataZoom is in draging. /// 正在拖拽组件。 /// public bool isDraging { get; set; } /// /// The start label. /// 组件的开始信息文本。 /// public Text startLabel { get; set; } /// /// The end label. /// 组件的结束信息文本。 /// public Text endLabel { get; set; } public static DataZoom defaultDataZoom { get { return new DataZoom() { m_Type = DataZoomType.Slider, filterMode = FilterMode.None, orient = Orient.Horizonal, xAxisIndex = 0, yAxisIndex = 0, showDataShadow = true, showDetail = false, zoomLock = false, realtime = true, m_Height = 50, m_Bottom = 10, rangeMode = RangeMode.Percent, start = 30, end = 70, m_ScrollSensitivity = 10, }; } } /// /// 给定的坐标是否在缩放区域内 /// /// /// /// /// public bool IsInZoom(Vector2 pos, float startX, float width) { Rect rect = Rect.MinMaxRect(startX, m_Bottom, startX + width, m_Bottom + m_Height); return rect.Contains(pos); } /// /// 给定的坐标是否在选中区域内 /// /// /// /// /// public bool IsInSelectedZoom(Vector2 pos, float startX, float width) { var start = startX + width * m_Start / 100; var end = startX + width * m_End / 100; Rect rect = Rect.MinMaxRect(start, m_Bottom, end, m_Bottom + m_Height); return rect.Contains(pos); } /// /// 给定的坐标是否在开始活动条触发区域内 /// /// /// /// /// public bool IsInStartZoom(Vector2 pos, float startX, float width) { var start = startX + width * m_Start / 100; Rect rect = Rect.MinMaxRect(start - 10, m_Bottom, start + 10, m_Bottom + m_Height); return rect.Contains(pos); } /// /// 给定的坐标是否在结束活动条触发区域内 /// /// /// /// /// public bool IsInEndZoom(Vector2 pos, float startX, float width) { var end = startX + width * m_End / 100; Rect rect = Rect.MinMaxRect(end - 10, m_Bottom, end + 10, m_Bottom + m_Height); return rect.Contains(pos); } /// /// 是否显示文本 /// /// public void SetLabelActive(bool flag) { if (startLabel && startLabel.gameObject.activeInHierarchy != flag) { startLabel.gameObject.SetActive(flag); } if (endLabel && endLabel.gameObject.activeInHierarchy != flag) { endLabel.gameObject.SetActive(flag); } } /// /// 设置开始文本内容 /// /// public void SetStartLabelText(string text) { if (startLabel) startLabel.text = text; } /// /// 设置结束文本内容 /// /// public void SetEndLabelText(string text) { if (endLabel) endLabel.text = text; } } }