2019-05-11 04:33:54 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System;
|
2019-05-15 09:44:18 +08:00
|
|
|
|
using UnityEngine.EventSystems;
|
2019-05-11 04:33:54 +08:00
|
|
|
|
|
|
|
|
|
|
namespace XCharts
|
|
|
|
|
|
{
|
2019-06-03 23:15:23 +08:00
|
|
|
|
public enum Orient
|
|
|
|
|
|
{
|
|
|
|
|
|
Horizonal,
|
|
|
|
|
|
Vertical
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-18 09:42:36 +08:00
|
|
|
|
public class BaseChart : Graphic, IPointerDownHandler, IPointerUpHandler,
|
2019-06-13 09:53:03 +08:00
|
|
|
|
IPointerEnterHandler, IPointerExitHandler, IBeginDragHandler,
|
|
|
|
|
|
IDragHandler, IEndDragHandler, IScrollHandler
|
2019-05-11 04:33:54 +08:00
|
|
|
|
{
|
|
|
|
|
|
private static readonly string s_TitleObjectName = "title";
|
|
|
|
|
|
private static readonly string s_LegendObjectName = "legend";
|
|
|
|
|
|
|
2019-07-15 19:21:22 +08:00
|
|
|
|
[SerializeField] protected float m_ChartWidth;
|
|
|
|
|
|
[SerializeField] protected float m_ChartHeight;
|
2019-05-11 04:33:54 +08:00
|
|
|
|
[SerializeField] protected Theme m_Theme = Theme.Default;
|
|
|
|
|
|
[SerializeField] protected ThemeInfo m_ThemeInfo;
|
|
|
|
|
|
[SerializeField] protected Title m_Title = Title.defaultTitle;
|
|
|
|
|
|
[SerializeField] protected Legend m_Legend = Legend.defaultLegend;
|
|
|
|
|
|
[SerializeField] protected Tooltip m_Tooltip = Tooltip.defaultTooltip;
|
2019-07-13 16:38:38 +08:00
|
|
|
|
[SerializeField] protected Series m_Series = Series.defaultSeries;
|
2019-05-11 04:33:54 +08:00
|
|
|
|
|
|
|
|
|
|
[SerializeField] protected bool m_Large;
|
|
|
|
|
|
[SerializeField] protected int m_MinShowDataNumber;
|
|
|
|
|
|
[SerializeField] protected int m_MaxShowDataNumber;
|
|
|
|
|
|
[SerializeField] protected int m_MaxCacheDataNumber;
|
|
|
|
|
|
|
|
|
|
|
|
[NonSerialized] private Theme m_CheckTheme = 0;
|
|
|
|
|
|
[NonSerialized] private Title m_CheckTitle = Title.defaultTitle;
|
|
|
|
|
|
[NonSerialized] private Legend m_CheckLegend = Legend.defaultLegend;
|
|
|
|
|
|
[NonSerialized] private float m_CheckWidth = 0;
|
|
|
|
|
|
[NonSerialized] private float m_CheckHeight = 0;
|
2019-06-13 09:53:03 +08:00
|
|
|
|
[NonSerialized] private bool m_RefreshChart = false;
|
2019-05-11 04:33:54 +08:00
|
|
|
|
[NonSerialized] protected List<Text> m_LegendTextList = new List<Text>();
|
|
|
|
|
|
|
|
|
|
|
|
protected Vector2 chartAnchorMax { get { return rectTransform.anchorMax; } }
|
|
|
|
|
|
protected Vector2 chartAnchorMin { get { return rectTransform.anchorMin; } }
|
|
|
|
|
|
protected Vector2 chartPivot { get { return rectTransform.pivot; } }
|
|
|
|
|
|
|
|
|
|
|
|
public Title title { get { return m_Title; } }
|
|
|
|
|
|
public Legend legend { get { return m_Legend; } }
|
|
|
|
|
|
public Tooltip tooltip { get { return m_Tooltip; } }
|
|
|
|
|
|
public Series series { get { return m_Series; } }
|
|
|
|
|
|
|
2019-07-15 19:21:22 +08:00
|
|
|
|
public float chartWidth { get { return m_ChartWidth; } }
|
|
|
|
|
|
public float chartHeight { get { return m_ChartHeight; } }
|
|
|
|
|
|
|
2019-07-15 00:24:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The min number of data to show in chart.
|
|
|
|
|
|
/// </summary>
|
2019-05-11 04:33:54 +08:00
|
|
|
|
public int minShowDataNumber
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_MinShowDataNumber; }
|
|
|
|
|
|
set { m_MinShowDataNumber = value; if (m_MinShowDataNumber < 0) m_MinShowDataNumber = 0; }
|
|
|
|
|
|
}
|
2019-07-15 00:24:04 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The max number of data to show in chart.
|
|
|
|
|
|
/// </summary>
|
2019-05-11 04:33:54 +08:00
|
|
|
|
public int maxShowDataNumber
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_MaxShowDataNumber; }
|
|
|
|
|
|
set { m_MaxShowDataNumber = value; if (m_MaxShowDataNumber < 0) m_MaxShowDataNumber = 0; }
|
|
|
|
|
|
}
|
2019-07-15 00:24:04 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The max number of serie and axis data cache.
|
|
|
|
|
|
/// The first data will be remove when the size of serie and axis data is larger then maxCacheDataNumber.
|
|
|
|
|
|
/// default:0,unlimited.
|
|
|
|
|
|
/// </summary>
|
2019-05-11 04:33:54 +08:00
|
|
|
|
public int maxCacheDataNumber
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_MaxCacheDataNumber; }
|
|
|
|
|
|
set { m_MaxCacheDataNumber = value; if (m_MaxCacheDataNumber < 0) m_MaxCacheDataNumber = 0; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-15 19:21:22 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Set the size of chart.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="width">width</param>
|
|
|
|
|
|
/// <param name="height">height</param>
|
|
|
|
|
|
public virtual void SetSize(float width, float height)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_ChartWidth = width;
|
|
|
|
|
|
m_ChartHeight = height;
|
|
|
|
|
|
m_CheckWidth = width;
|
|
|
|
|
|
m_CheckHeight = height;
|
|
|
|
|
|
rectTransform.sizeDelta = new Vector2(m_ChartWidth, m_ChartHeight);
|
|
|
|
|
|
OnSizeChanged();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-07-15 00:24:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Remove all series and legend data.
|
|
|
|
|
|
/// It just emptying all of serie's data without emptying the list of series.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public virtual void ClearData()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_Series.ClearData();
|
|
|
|
|
|
m_Legend.ClearData();
|
|
|
|
|
|
RefreshChart();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Remove legend and serie by name.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="serieName">the name of serie</param>
|
|
|
|
|
|
public virtual void RemoveData(string serieName)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_Series.Remove(serieName);
|
|
|
|
|
|
m_Legend.RemoveData(serieName);
|
|
|
|
|
|
RefreshChart();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Remove all data from series and legend.
|
|
|
|
|
|
/// The series list is also cleared.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public virtual void RemoveData()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_Legend.ClearData();
|
|
|
|
|
|
m_Series.RemoveAll();
|
|
|
|
|
|
RefreshChart();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Add a serie to serie list.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="serieName">the name of serie</param>
|
|
|
|
|
|
/// <param name="type">the type of serie</param>
|
|
|
|
|
|
/// <param name="show">whether to show this serie</param>
|
|
|
|
|
|
/// <returns>the added serie</returns>
|
|
|
|
|
|
public virtual Serie AddSerie(string serieName, SerieType type, bool show = true)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_Legend.AddData(serieName);
|
|
|
|
|
|
return m_Series.AddSerie(serieName, type);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Add a data to serie.
|
|
|
|
|
|
/// If serie doesn't exist,will be add to series.
|
|
|
|
|
|
/// If serieName doesn't exist in legend,will be add to legend.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="serieName">the name of serie</param>
|
|
|
|
|
|
/// <param name="value">the data to add</param>
|
|
|
|
|
|
public virtual void AddData(string serieName, float value)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_Legend.AddData(serieName);
|
|
|
|
|
|
m_Series.AddData(serieName, value, m_MaxCacheDataNumber);
|
|
|
|
|
|
RefreshChart();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Add a data to serie.
|
|
|
|
|
|
/// If serie doesn't exist, the data is ignored.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="serieIndex">the index of serie</param>
|
|
|
|
|
|
/// <param name="value">the data to add</param>
|
|
|
|
|
|
public virtual void AddData(int serieIndex, float value)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_Series.AddData(serieIndex, value, m_MaxCacheDataNumber);
|
|
|
|
|
|
RefreshChart();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Update serie data by serie name.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="serieName">the name of serie</param>
|
|
|
|
|
|
/// <param name="value">the data will be update</param>
|
|
|
|
|
|
/// <param name="dataIndex">the index of data</param>
|
|
|
|
|
|
public virtual void UpdateData(string serieName, float value, int dataIndex = 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_Series.UpdateData(serieName, value, dataIndex);
|
|
|
|
|
|
RefreshChart();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Update serie data by serie index.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="serieIndex">the index of serie</param>
|
|
|
|
|
|
/// <param name="value">the data will be update</param>
|
|
|
|
|
|
/// <param name="dataIndex">the index of data</param>
|
|
|
|
|
|
public virtual void UpdateData(int serieIndex, float value, int dataIndex = 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_Series.UpdateData(serieIndex, value, dataIndex);
|
|
|
|
|
|
RefreshChart();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Whether to show serie and legend.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="serieName">the name of serie</param>
|
|
|
|
|
|
/// <param name="active">Active or not</param>
|
|
|
|
|
|
public virtual void SetActive(string serieName, bool active)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_Legend.SetActive(serieName, active);
|
|
|
|
|
|
m_Series.SetActive(serieName, active);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Whether to show serie and legend.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="serieIndex">the index of serie</param>
|
|
|
|
|
|
/// <param name="active">Active or not</param>
|
|
|
|
|
|
public virtual void SetActive(int serieIndex, bool active)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_Legend.SetActive(serieIndex, active);
|
|
|
|
|
|
m_Series.SetActive(serieIndex, active);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Whether serie is activated.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="serieName">the name of serie</param>
|
|
|
|
|
|
/// <returns>True when activated</returns>
|
|
|
|
|
|
public virtual bool IsActive(string serieName)
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_Legend.IsActive(serieName) || m_Series.IsActive(serieName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Whether serie is activated.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="serieIndex">the index of serie</param>
|
|
|
|
|
|
/// <returns>True when activated</returns>
|
|
|
|
|
|
public virtual bool IsActive(int serieIndex)
|
|
|
|
|
|
{
|
2019-07-18 09:42:36 +08:00
|
|
|
|
return m_Legend.IsActive(serieIndex) && m_Series.IsActive(serieIndex);
|
2019-07-15 00:24:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Redraw chart next frame.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void RefreshChart()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_RefreshChart = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Update chart theme
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="theme">theme</param>
|
|
|
|
|
|
public void UpdateTheme(Theme theme)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.m_Theme = theme;
|
|
|
|
|
|
OnThemeChanged();
|
|
|
|
|
|
RefreshChart();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-05-11 04:33:54 +08:00
|
|
|
|
protected override void Awake()
|
|
|
|
|
|
{
|
2019-06-17 04:29:19 +08:00
|
|
|
|
if (m_ThemeInfo == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_ThemeInfo = ThemeInfo.Default;
|
|
|
|
|
|
}
|
2019-06-13 09:53:03 +08:00
|
|
|
|
raycastTarget = false;
|
2019-05-11 04:33:54 +08:00
|
|
|
|
rectTransform.anchorMax = Vector2.zero;
|
|
|
|
|
|
rectTransform.anchorMin = Vector2.zero;
|
|
|
|
|
|
rectTransform.pivot = Vector2.zero;
|
2019-07-15 19:21:22 +08:00
|
|
|
|
m_ChartWidth = rectTransform.sizeDelta.x;
|
|
|
|
|
|
m_ChartHeight = rectTransform.sizeDelta.y;
|
|
|
|
|
|
m_CheckWidth = m_ChartWidth;
|
|
|
|
|
|
m_CheckHeight = m_ChartHeight;
|
2019-05-11 04:33:54 +08:00
|
|
|
|
m_CheckTheme = m_Theme;
|
|
|
|
|
|
InitTitle();
|
|
|
|
|
|
InitLegend();
|
|
|
|
|
|
InitTooltip();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void Update()
|
|
|
|
|
|
{
|
|
|
|
|
|
CheckSize();
|
|
|
|
|
|
CheckTheme();
|
|
|
|
|
|
CheckTile();
|
|
|
|
|
|
CheckLegend();
|
|
|
|
|
|
CheckTooltip();
|
2019-06-13 09:53:03 +08:00
|
|
|
|
CheckRefreshChart();
|
2019-05-11 04:33:54 +08:00
|
|
|
|
}
|
2019-06-14 09:41:15 +08:00
|
|
|
|
|
2019-07-18 09:42:36 +08:00
|
|
|
|
protected override void OnEnable()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.OnEnable();
|
|
|
|
|
|
Awake();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnDisable()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.OnDisable();
|
|
|
|
|
|
ChartHelper.DestoryAllChilds(transform);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-05-16 09:39:58 +08:00
|
|
|
|
#if UNITY_EDITOR
|
2019-05-11 04:33:54 +08:00
|
|
|
|
protected override void Reset()
|
|
|
|
|
|
{
|
2019-07-15 19:21:22 +08:00
|
|
|
|
var sizeDelta = rectTransform.sizeDelta;
|
|
|
|
|
|
if (sizeDelta.x < 580 && sizeDelta.y < 300)
|
|
|
|
|
|
{
|
|
|
|
|
|
rectTransform.sizeDelta = new Vector2(580, 300);
|
|
|
|
|
|
}
|
2019-05-11 04:33:54 +08:00
|
|
|
|
ChartHelper.DestoryAllChilds(transform);
|
2019-06-17 04:29:19 +08:00
|
|
|
|
m_ThemeInfo = ThemeInfo.Default;
|
2019-05-11 04:33:54 +08:00
|
|
|
|
m_Title = Title.defaultTitle;
|
|
|
|
|
|
m_Legend = Legend.defaultLegend;
|
|
|
|
|
|
m_Tooltip = Tooltip.defaultTooltip;
|
|
|
|
|
|
m_Series = Series.defaultSeries;
|
2019-06-17 04:29:19 +08:00
|
|
|
|
Awake();
|
2019-05-11 04:33:54 +08:00
|
|
|
|
}
|
2019-05-16 09:39:58 +08:00
|
|
|
|
#endif
|
2019-05-11 04:33:54 +08:00
|
|
|
|
|
|
|
|
|
|
protected override void OnDestroy()
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int i = transform.childCount - 1; i >= 0; i--)
|
|
|
|
|
|
{
|
|
|
|
|
|
DestroyImmediate(transform.GetChild(i).gameObject);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void InitTitle()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_Title.OnChanged();
|
|
|
|
|
|
TextAnchor anchor = m_Title.location.textAnchor;
|
|
|
|
|
|
Vector2 anchorMin = m_Title.location.anchorMin;
|
|
|
|
|
|
Vector2 anchorMax = m_Title.location.anchorMax;
|
|
|
|
|
|
Vector2 pivot = m_Title.location.pivot;
|
|
|
|
|
|
Vector3 titlePosition = m_Title.location.GetPosition(chartWidth, chartHeight);
|
|
|
|
|
|
Vector3 subTitlePosition = -new Vector3(0, m_Title.textFontSize + m_Title.itemGap, 0);
|
|
|
|
|
|
float titleWid = chartWidth;
|
|
|
|
|
|
|
|
|
|
|
|
var titleObject = ChartHelper.AddObject(s_TitleObjectName, transform, anchorMin, anchorMax,
|
|
|
|
|
|
pivot, new Vector2(chartWidth, chartHeight));
|
|
|
|
|
|
titleObject.transform.localPosition = titlePosition;
|
|
|
|
|
|
ChartHelper.HideAllObject(titleObject, s_TitleObjectName);
|
|
|
|
|
|
|
|
|
|
|
|
Text titleText = ChartHelper.AddTextObject(s_TitleObjectName, titleObject.transform,
|
|
|
|
|
|
m_ThemeInfo.font, m_ThemeInfo.textColor, anchor, anchorMin, anchorMax, pivot,
|
|
|
|
|
|
new Vector2(titleWid, m_Title.textFontSize), m_Title.textFontSize);
|
|
|
|
|
|
|
|
|
|
|
|
titleText.alignment = anchor;
|
|
|
|
|
|
titleText.gameObject.SetActive(m_Title.show);
|
|
|
|
|
|
titleText.transform.localPosition = Vector2.zero;
|
|
|
|
|
|
titleText.text = m_Title.text;
|
|
|
|
|
|
|
|
|
|
|
|
Text subText = ChartHelper.AddTextObject(s_TitleObjectName + "_sub", titleObject.transform,
|
|
|
|
|
|
m_ThemeInfo.font, m_ThemeInfo.textColor, anchor, anchorMin, anchorMax, pivot,
|
|
|
|
|
|
new Vector2(titleWid, m_Title.subTextFontSize), m_Title.subTextFontSize);
|
|
|
|
|
|
|
|
|
|
|
|
subText.alignment = anchor;
|
|
|
|
|
|
subText.gameObject.SetActive(m_Title.show && !string.IsNullOrEmpty(m_Title.subText));
|
|
|
|
|
|
subText.transform.localPosition = subTitlePosition;
|
|
|
|
|
|
subText.text = m_Title.subText;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void InitLegend()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_Legend.OnChanged();
|
|
|
|
|
|
ChartHelper.HideAllObject(transform, s_LegendObjectName);
|
|
|
|
|
|
TextAnchor anchor = m_Legend.location.textAnchor;
|
|
|
|
|
|
Vector2 anchorMin = m_Legend.location.anchorMin;
|
|
|
|
|
|
Vector2 anchorMax = m_Legend.location.anchorMax;
|
|
|
|
|
|
Vector2 pivot = m_Legend.location.pivot;
|
|
|
|
|
|
|
|
|
|
|
|
var legendObject = ChartHelper.AddObject(s_LegendObjectName, transform, anchorMin, anchorMax,
|
|
|
|
|
|
pivot, new Vector2(chartWidth, chartHeight));
|
|
|
|
|
|
legendObject.transform.localPosition = m_Legend.location.GetPosition(chartWidth, chartHeight);
|
|
|
|
|
|
ChartHelper.HideAllObject(legendObject, s_LegendObjectName);
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < m_Legend.data.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
Button btn = ChartHelper.AddButtonObject(s_LegendObjectName + "_" + i, legendObject.transform,
|
|
|
|
|
|
m_ThemeInfo.font, m_Legend.itemFontSize, m_ThemeInfo.legendTextColor, anchor,
|
|
|
|
|
|
anchorMin, anchorMax, pivot, new Vector2(m_Legend.itemWidth, m_Legend.itemHeight));
|
|
|
|
|
|
|
|
|
|
|
|
m_Legend.SetButton(i, btn);
|
2019-05-16 09:39:58 +08:00
|
|
|
|
m_Legend.SetActive(i, IsActive(i));
|
2019-06-14 09:41:15 +08:00
|
|
|
|
m_Legend.UpdateButtonColor(i, m_ThemeInfo.GetColor(i), m_ThemeInfo.legendUnableColor);
|
2019-05-11 04:33:54 +08:00
|
|
|
|
btn.GetComponentInChildren<Text>().text = m_Legend.data[i];
|
2019-05-15 09:44:18 +08:00
|
|
|
|
ChartHelper.AddEventListener(btn.gameObject, EventTriggerType.PointerDown, (data) =>
|
2019-05-11 04:33:54 +08:00
|
|
|
|
{
|
2019-06-14 09:41:15 +08:00
|
|
|
|
int count = (data as PointerEventData).clickCount;
|
|
|
|
|
|
int index = int.Parse(data.selectedObject.name.Split('_')[1]);
|
|
|
|
|
|
SetActive(index, !m_Legend.IsActive(index));
|
2019-06-17 04:29:19 +08:00
|
|
|
|
m_Legend.UpdateButtonColor(index, m_ThemeInfo.GetColor(index),
|
|
|
|
|
|
m_ThemeInfo.legendUnableColor);
|
2019-06-14 09:41:15 +08:00
|
|
|
|
OnYMaxValueChanged();
|
|
|
|
|
|
OnLegendButtonClicked();
|
|
|
|
|
|
RefreshChart();
|
2019-05-11 04:33:54 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void InitTooltip()
|
|
|
|
|
|
{
|
2019-05-29 09:53:30 +08:00
|
|
|
|
var tooltipObject = ChartHelper.AddObject("tooltip", transform, chartAnchorMin,
|
|
|
|
|
|
chartAnchorMax, chartPivot, new Vector2(chartWidth, chartHeight));
|
|
|
|
|
|
tooltipObject.transform.localPosition = Vector3.zero;
|
|
|
|
|
|
DestroyImmediate(tooltipObject.GetComponent<Image>());
|
2019-06-17 04:29:19 +08:00
|
|
|
|
var parent = tooltipObject.transform;
|
2019-05-29 09:53:30 +08:00
|
|
|
|
ChartHelper.HideAllObject(tooltipObject.transform);
|
2019-06-17 04:29:19 +08:00
|
|
|
|
GameObject content = ChartHelper.AddTooltipContent("content", parent, m_ThemeInfo.font);
|
2019-05-29 09:53:30 +08:00
|
|
|
|
m_Tooltip.SetObj(tooltipObject);
|
|
|
|
|
|
m_Tooltip.SetContentObj(content);
|
|
|
|
|
|
m_Tooltip.SetContentBackgroundColor(m_ThemeInfo.tooltipBackgroundColor);
|
|
|
|
|
|
m_Tooltip.SetContentTextColor(m_ThemeInfo.tooltipTextColor);
|
2019-05-11 04:33:54 +08:00
|
|
|
|
m_Tooltip.SetActive(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Vector3 GetLegendPosition(int i)
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_Legend.location.GetPosition(chartWidth, chartHeight);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected float GetMaxValue(int index)
|
|
|
|
|
|
{
|
2019-07-13 16:38:38 +08:00
|
|
|
|
return m_Series.GetMaxValue(index);
|
2019-05-11 04:33:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void CheckSize()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_CheckWidth != chartWidth || m_CheckHeight != chartHeight)
|
|
|
|
|
|
{
|
2019-07-15 19:21:22 +08:00
|
|
|
|
SetSize(chartWidth, chartHeight);
|
|
|
|
|
|
}
|
|
|
|
|
|
var sizeDelta = rectTransform.sizeDelta;
|
|
|
|
|
|
if (m_CheckWidth != sizeDelta.x || m_CheckHeight != sizeDelta.y)
|
|
|
|
|
|
{
|
|
|
|
|
|
SetSize(sizeDelta.x, sizeDelta.y);
|
2019-05-11 04:33:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void CheckTheme()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_CheckTheme != m_Theme)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_CheckTheme = m_Theme;
|
|
|
|
|
|
OnThemeChanged();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void CheckTile()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_CheckTitle.Equals(m_Title))
|
|
|
|
|
|
{
|
|
|
|
|
|
m_CheckTitle.Copy(m_Title);
|
|
|
|
|
|
OnTitleChanged();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void CheckLegend()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_CheckLegend != m_Legend)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_CheckLegend.Copy(m_Legend);
|
|
|
|
|
|
OnLegendChanged();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void CheckTooltip()
|
|
|
|
|
|
{
|
2019-06-01 15:58:44 +08:00
|
|
|
|
if (!m_Tooltip.show || !m_Tooltip.isInited)
|
|
|
|
|
|
{
|
2019-07-18 09:42:36 +08:00
|
|
|
|
|
|
|
|
|
|
if (m_Tooltip.dataIndex[0] != 0 || m_Tooltip.dataIndex[1] != 0)
|
2019-06-01 15:58:44 +08:00
|
|
|
|
{
|
2019-07-18 09:42:36 +08:00
|
|
|
|
m_Tooltip.dataIndex[0] = m_Tooltip.dataIndex[1] = -1;
|
2019-06-01 15:58:44 +08:00
|
|
|
|
m_Tooltip.SetActive(false);
|
|
|
|
|
|
RefreshChart();
|
|
|
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2019-07-18 09:42:36 +08:00
|
|
|
|
m_Tooltip.dataIndex[0] = m_Tooltip.dataIndex[1] = -1;
|
2019-05-11 04:33:54 +08:00
|
|
|
|
Vector2 local;
|
2019-07-18 09:42:36 +08:00
|
|
|
|
if (canvas == null) return;
|
2019-05-11 04:33:54 +08:00
|
|
|
|
|
|
|
|
|
|
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform,
|
2019-06-21 07:38:29 +08:00
|
|
|
|
Input.mousePosition, canvas.worldCamera, out local))
|
2019-06-01 15:58:44 +08:00
|
|
|
|
{
|
|
|
|
|
|
m_Tooltip.SetActive(false);
|
2019-06-03 23:15:23 +08:00
|
|
|
|
RefreshChart();
|
2019-05-11 04:33:54 +08:00
|
|
|
|
return;
|
2019-06-01 15:58:44 +08:00
|
|
|
|
}
|
2019-05-11 04:33:54 +08:00
|
|
|
|
if (local.x < 0 || local.x > chartWidth ||
|
|
|
|
|
|
local.y < 0 || local.y > chartHeight)
|
2019-06-01 15:58:44 +08:00
|
|
|
|
{
|
|
|
|
|
|
m_Tooltip.SetActive(false);
|
2019-06-03 23:15:23 +08:00
|
|
|
|
RefreshChart();
|
2019-05-11 04:33:54 +08:00
|
|
|
|
return;
|
2019-06-01 15:58:44 +08:00
|
|
|
|
}
|
2019-05-29 09:53:30 +08:00
|
|
|
|
m_Tooltip.pointerPos = local;
|
2019-05-11 04:33:54 +08:00
|
|
|
|
CheckTootipArea(local);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void CheckTootipArea(Vector2 localPostion)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-06-13 09:53:03 +08:00
|
|
|
|
protected void CheckRefreshChart()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_RefreshChart)
|
|
|
|
|
|
{
|
|
|
|
|
|
int tempWid = (int)chartWidth;
|
|
|
|
|
|
rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, tempWid - 1);
|
|
|
|
|
|
rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, tempWid);
|
|
|
|
|
|
m_RefreshChart = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-05-11 04:33:54 +08:00
|
|
|
|
protected virtual void OnSizeChanged()
|
|
|
|
|
|
{
|
|
|
|
|
|
InitTitle();
|
|
|
|
|
|
InitLegend();
|
2019-07-13 16:38:38 +08:00
|
|
|
|
InitTooltip();
|
2019-05-11 04:33:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void OnThemeChanged()
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (m_Theme)
|
|
|
|
|
|
{
|
|
|
|
|
|
case Theme.Dark:
|
|
|
|
|
|
m_ThemeInfo.Copy(ThemeInfo.Dark);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case Theme.Default:
|
|
|
|
|
|
m_ThemeInfo.Copy(ThemeInfo.Default);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case Theme.Light:
|
|
|
|
|
|
m_ThemeInfo.Copy(ThemeInfo.Light);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
InitTitle();
|
|
|
|
|
|
InitLegend();
|
2019-05-29 09:53:30 +08:00
|
|
|
|
InitTooltip();
|
2019-05-11 04:33:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void OnTitleChanged()
|
|
|
|
|
|
{
|
|
|
|
|
|
InitTitle();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void OnLegendChanged()
|
|
|
|
|
|
{
|
|
|
|
|
|
InitLegend();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void OnYMaxValueChanged()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void OnLegendButtonClicked()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void RefreshTooltip()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnPopulateMesh(VertexHelper vh)
|
|
|
|
|
|
{
|
|
|
|
|
|
vh.Clear();
|
|
|
|
|
|
DrawBackground(vh);
|
|
|
|
|
|
DrawChart(vh);
|
|
|
|
|
|
DrawTooltip(vh);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void DrawChart(VertexHelper vh)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void DrawTooltip(VertexHelper vh)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void DrawBackground(VertexHelper vh)
|
|
|
|
|
|
{
|
|
|
|
|
|
// draw bg
|
|
|
|
|
|
Vector3 p1 = new Vector3(0, chartHeight);
|
|
|
|
|
|
Vector3 p2 = new Vector3(chartWidth, chartHeight);
|
|
|
|
|
|
Vector3 p3 = new Vector3(chartWidth, 0);
|
|
|
|
|
|
Vector3 p4 = new Vector3(0, 0);
|
|
|
|
|
|
ChartHelper.DrawPolygon(vh, p1, p2, p3, p4, m_ThemeInfo.backgroundColor);
|
|
|
|
|
|
}
|
2019-06-13 09:53:03 +08:00
|
|
|
|
|
|
|
|
|
|
public virtual void OnPointerDown(PointerEventData eventData)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void OnPointerUp(PointerEventData eventData)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void OnPointerEnter(PointerEventData eventData)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void OnPointerExit(PointerEventData eventData)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void OnBeginDrag(PointerEventData eventData)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void OnEndDrag(PointerEventData eventData)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void OnDrag(PointerEventData eventData)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void OnScroll(PointerEventData eventData)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
2019-05-11 04:33:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|