BaseChart.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. using XUGL;
  9. namespace XCharts.Runtime
  10. {
  11. [AddComponentMenu("XCharts/EmptyChart", 10)]
  12. [ExecuteInEditMode]
  13. [RequireComponent(typeof(RectTransform), typeof(CanvasRenderer))]
  14. [DisallowMultipleComponent]
  15. public partial class BaseChart : BaseGraph, ISerializationCallbackReceiver
  16. {
  17. [SerializeField] protected string m_ChartName;
  18. [SerializeField] protected ThemeStyle m_Theme = new ThemeStyle();
  19. [SerializeField] protected Settings m_Settings;
  20. [SerializeField] protected DebugInfo m_DebugInfo = new DebugInfo();
  21. [SerializeField] protected bool m_ChartInited = false;
  22. #pragma warning disable 0414
  23. [SerializeField][ListForComponent(typeof(AngleAxis))] private List<AngleAxis> m_AngleAxes = new List<AngleAxis>();
  24. [SerializeField][ListForComponent(typeof(Background))] private List<Background> m_Backgrounds = new List<Background>();
  25. [SerializeField][ListForComponent(typeof(DataZoom))] private List<DataZoom> m_DataZooms = new List<DataZoom>();
  26. [SerializeField][ListForComponent(typeof(GridCoord))] private List<GridCoord> m_Grids = new List<GridCoord>();
  27. [SerializeField][ListForComponent(typeof(GridLayout))] private List<GridLayout> m_GridsLayout = new List<GridLayout>();
  28. [SerializeField][ListForComponent(typeof(Legend))] private List<Legend> m_Legends = new List<Legend>();
  29. [SerializeField][ListForComponent(typeof(MarkLine))] private List<MarkLine> m_MarkLines = new List<MarkLine>();
  30. [SerializeField][ListForComponent(typeof(MarkArea))] private List<MarkArea> m_MarkAreas = new List<MarkArea>();
  31. [SerializeField][ListForComponent(typeof(PolarCoord))] private List<PolarCoord> m_Polars = new List<PolarCoord>();
  32. [SerializeField][ListForComponent(typeof(RadarCoord))] private List<RadarCoord> m_Radars = new List<RadarCoord>();
  33. [SerializeField][ListForComponent(typeof(RadiusAxis))] private List<RadiusAxis> m_RadiusAxes = new List<RadiusAxis>();
  34. [SerializeField][ListForComponent(typeof(Title))] private List<Title> m_Titles = new List<Title>();
  35. [SerializeField][ListForComponent(typeof(Tooltip))] private List<Tooltip> m_Tooltips = new List<Tooltip>();
  36. [SerializeField][ListForComponent(typeof(VisualMap))] private List<VisualMap> m_VisualMaps = new List<VisualMap>();
  37. [SerializeField][ListForComponent(typeof(XAxis))] private List<XAxis> m_XAxes = new List<XAxis>();
  38. [SerializeField][ListForComponent(typeof(YAxis))] private List<YAxis> m_YAxes = new List<YAxis>();
  39. [SerializeField][ListForComponent(typeof(SingleAxis))] private List<SingleAxis> m_SingleAxes = new List<SingleAxis>();
  40. [SerializeField][ListForComponent(typeof(ParallelCoord))] private List<ParallelCoord> m_Parallels = new List<ParallelCoord>();
  41. [SerializeField][ListForComponent(typeof(ParallelAxis))] private List<ParallelAxis> m_ParallelAxes = new List<ParallelAxis>();
  42. [SerializeField][ListForComponent(typeof(Comment))] private List<Comment> m_Comments = new List<Comment>();
  43. [SerializeField][ListForSerie(typeof(Bar))] private List<Bar> m_SerieBars = new List<Bar>();
  44. [SerializeField][ListForSerie(typeof(Candlestick))] private List<Candlestick> m_SerieCandlesticks = new List<Candlestick>();
  45. [SerializeField][ListForSerie(typeof(EffectScatter))] private List<EffectScatter> m_SerieEffectScatters = new List<EffectScatter>();
  46. [SerializeField][ListForSerie(typeof(Heatmap))] private List<Heatmap> m_SerieHeatmaps = new List<Heatmap>();
  47. [SerializeField][ListForSerie(typeof(Line))] private List<Line> m_SerieLines = new List<Line>();
  48. [SerializeField][ListForSerie(typeof(Pie))] private List<Pie> m_SeriePies = new List<Pie>();
  49. [SerializeField][ListForSerie(typeof(Radar))] private List<Radar> m_SerieRadars = new List<Radar>();
  50. [SerializeField][ListForSerie(typeof(Ring))] private List<Ring> m_SerieRings = new List<Ring>();
  51. [SerializeField][ListForSerie(typeof(Scatter))] private List<Scatter> m_SerieScatters = new List<Scatter>();
  52. [SerializeField][ListForSerie(typeof(Parallel))] private List<Parallel> m_SerieParallels = new List<Parallel>();
  53. [SerializeField][ListForSerie(typeof(SimplifiedLine))] private List<SimplifiedLine> m_SerieSimplifiedLines = new List<SimplifiedLine>();
  54. [SerializeField][ListForSerie(typeof(SimplifiedBar))] private List<SimplifiedBar> m_SerieSimplifiedBars = new List<SimplifiedBar>();
  55. [SerializeField][ListForSerie(typeof(SimplifiedCandlestick))] private List<SimplifiedCandlestick> m_SerieSimplifiedCandlesticks = new List<SimplifiedCandlestick>();
  56. #pragma warning restore 0414
  57. protected List<Serie> m_Series = new List<Serie>();
  58. protected List<MainComponent> m_Components = new List<MainComponent>();
  59. protected Dictionary<Type, FieldInfo> m_TypeListForComponent = new Dictionary<Type, FieldInfo>();
  60. protected Dictionary<Type, FieldInfo> m_TypeListForSerie = new Dictionary<Type, FieldInfo>();
  61. protected Dictionary<Type, List<MainComponent>> m_ComponentMaps = new Dictionary<Type, List<MainComponent>>();
  62. public Dictionary<Type, FieldInfo> typeListForComponent { get { return m_TypeListForComponent; } }
  63. public Dictionary<Type, FieldInfo> typeListForSerie { get { return m_TypeListForSerie; } }
  64. public List<MainComponent> components { get { return m_Components; } }
  65. public List<Serie> series { get { return m_Series; } }
  66. public DebugInfo debug { get { return m_DebugInfo; } }
  67. public override HideFlags chartHideFlags { get { return m_DebugInfo.showAllChartObject ? HideFlags.None : HideFlags.HideInHierarchy; } }
  68. protected float m_ChartWidth;
  69. protected float m_ChartHeight;
  70. protected float m_ChartX;
  71. protected float m_ChartY;
  72. protected Vector3 m_ChartPosition = Vector3.zero;
  73. protected Vector2 m_ChartMinAnchor;
  74. protected Vector2 m_ChartMaxAnchor;
  75. protected Vector2 m_ChartPivot;
  76. protected Vector2 m_ChartSizeDelta;
  77. protected Rect m_ChartRect = new Rect(0, 0, 0, 0);
  78. protected Action m_OnInit;
  79. protected Action m_OnUpdate;
  80. protected Action<VertexHelper> m_OnDrawBase;
  81. protected Action<VertexHelper> m_OnDrawUpper;
  82. protected Action<VertexHelper> m_OnDrawTop;
  83. protected Action<VertexHelper, Serie> m_OnDrawSerieBefore;
  84. protected Action<VertexHelper, Serie> m_OnDrawSerieAfter;
  85. protected Action<SerieEventData> m_OnSerieClick;
  86. protected Action<SerieEventData> m_OnSerieDown;
  87. protected Action<SerieEventData> m_OnSerieEnter;
  88. protected Action<SerieEventData> m_OnSerieExit;
  89. protected Action<int, int> m_OnPointerEnterPie;
  90. protected Action<Axis, double> m_OnAxisPointerValueChanged;
  91. protected Action<Legend, int, string, bool> m_OnLegendClick;
  92. protected Action<Legend, int, string> m_OnLegendEnter;
  93. protected Action<Legend, int, string> m_OnLegendExit;
  94. protected CustomDrawGaugePointerFunction m_CustomDrawGaugePointerFunction;
  95. internal bool m_CheckAnimation = false;
  96. internal protected List<string> m_LegendRealShowName = new List<string>();
  97. protected List<Painter> m_PainterList = new List<Painter>();
  98. internal Painter m_PainterUpper;
  99. internal Painter m_PainterTop;
  100. internal int m_BasePainterVertCount;
  101. internal int m_UpperPainterVertCount;
  102. internal int m_TopPainterVertCount;
  103. private ThemeType m_CheckTheme = 0;
  104. protected List<MainComponentHandler> m_ComponentHandlers = new List<MainComponentHandler>();
  105. protected List<SerieHandler> m_SerieHandlers = new List<SerieHandler>();
  106. protected virtual void DefaultChart() { }
  107. protected override void InitComponent()
  108. {
  109. base.InitComponent();
  110. SeriesHelper.UpdateSerieNameList(this, ref m_LegendRealShowName);
  111. foreach (var handler in m_ComponentHandlers)
  112. handler.InitComponent();
  113. foreach (var handler in m_SerieHandlers)
  114. handler.InitComponent();
  115. m_DebugInfo.Init(this);
  116. }
  117. protected override void Awake()
  118. {
  119. if (m_Settings == null)
  120. m_Settings = Settings.DefaultSettings;
  121. CheckTheme(true);
  122. base.Awake();
  123. InitComponentHandlers();
  124. InitSerieHandlers();
  125. AnimationReset();
  126. AnimationFadeIn();
  127. XChartsMgr.AddChart(this);
  128. }
  129. protected void OnInit()
  130. {
  131. RemoveAllChartComponent();
  132. OnBeforeSerialize();
  133. EnsureChartComponent<Title>();
  134. EnsureChartComponent<Tooltip>();
  135. EnsureChartComponent<Title>().text = GetType().Name;
  136. var background = EnsureChartComponent<Background>();
  137. background.borderStyle.show = true;
  138. background.borderStyle.cornerRadius = new float[] { 10, 10, 10, 10 };
  139. if (m_Theme.sharedTheme != null)
  140. m_Theme.sharedTheme.CopyTheme(ThemeType.Default);
  141. else
  142. m_Theme.sharedTheme = XCThemeMgr.GetTheme(ThemeType.Default);
  143. var sizeDelta = rectTransform.sizeDelta;
  144. if (sizeDelta.x < 580 && sizeDelta.y < 300)
  145. {
  146. m_GraphWidth = 580;
  147. m_GraphHeight = 300;
  148. m_ChartWidth = m_GraphWidth;
  149. m_ChartHeight = m_GraphHeight;
  150. rectTransform.sizeDelta = new Vector2(m_ChartWidth, m_ChartHeight);
  151. }
  152. ChartHelper.HideAllObject(transform);
  153. m_ChartInited = true;
  154. if (m_OnInit != null)
  155. m_OnInit();
  156. }
  157. protected void CheckChartInit()
  158. {
  159. if (!m_ChartInited)
  160. {
  161. OnInit();
  162. DefaultChart();
  163. }
  164. }
  165. #if UNITY_EDITOR
  166. protected override void Reset()
  167. {
  168. base.Reset();
  169. OnInit();
  170. DefaultChart();
  171. Awake();
  172. }
  173. protected override void OnValidate()
  174. {
  175. base.OnValidate();
  176. foreach (var handler in m_SerieHandlers) handler.ForceUpdateSerieContext();
  177. }
  178. #endif
  179. protected override void Start()
  180. {
  181. RefreshChart();
  182. }
  183. protected override void Update()
  184. {
  185. CheckTheme();
  186. base.Update();
  187. CheckPainter();
  188. CheckRefreshChart();
  189. Internal_CheckAnimation();
  190. foreach (var handler in m_SerieHandlers) handler.BeforeUpdate();
  191. foreach (var handler in m_ComponentHandlers) handler.BeforceSerieUpdate();
  192. foreach (var handler in m_SerieHandlers) handler.Update();
  193. foreach (var handler in m_ComponentHandlers) handler.Update();
  194. foreach (var handler in m_SerieHandlers) handler.AfterUpdate();
  195. m_DebugInfo.Update();
  196. if (m_OnUpdate != null)
  197. m_OnUpdate();
  198. }
  199. public Painter GetPainter(int index)
  200. {
  201. if (index >= 0 && index < m_PainterList.Count)
  202. {
  203. return m_PainterList[index];
  204. }
  205. return null;
  206. }
  207. public void RefreshBasePainter()
  208. {
  209. m_Painter.Refresh();
  210. }
  211. public void RefreshTopPainter()
  212. {
  213. m_PainterTop.Refresh();
  214. }
  215. public void RefreshUpperPainter()
  216. {
  217. m_PainterUpper.Refresh();
  218. }
  219. public void RefreshPainter(int index)
  220. {
  221. var painter = GetPainter(index);
  222. RefreshPainter(painter);
  223. }
  224. public void RefreshPainter(Serie serie)
  225. {
  226. if (serie == null) return;
  227. RefreshPainter(GetPainterIndexBySerie(serie));
  228. }
  229. internal override void RefreshPainter(Painter painter)
  230. {
  231. base.RefreshPainter(painter);
  232. if (painter != null && painter.type == Painter.Type.Serie)
  233. {
  234. m_PainterUpper.Refresh();
  235. }
  236. }
  237. public void SetPainterActive(int index, bool flag)
  238. {
  239. var painter = GetPainter(index);
  240. if (painter == null) return;
  241. painter.SetActive(flag, m_DebugInfo.showAllChartObject);
  242. }
  243. protected virtual void CheckTheme(bool firstInit = false)
  244. {
  245. if (m_Theme.sharedTheme == null)
  246. {
  247. m_Theme.sharedTheme = XCThemeMgr.GetTheme(ThemeType.Default);
  248. }
  249. if (firstInit)
  250. {
  251. m_CheckTheme = m_Theme.themeType;
  252. }
  253. if (m_Theme.sharedTheme != null && m_CheckTheme != m_Theme.themeType)
  254. {
  255. m_CheckTheme = m_Theme.themeType;
  256. m_Theme.sharedTheme.CopyTheme(m_CheckTheme);
  257. #if UNITY_EDITOR
  258. UnityEditor.EditorUtility.SetDirty(this);
  259. #endif
  260. SetAllComponentDirty();
  261. OnThemeChanged();
  262. }
  263. }
  264. protected override void CheckComponent()
  265. {
  266. base.CheckComponent();
  267. if (m_Theme.anyDirty)
  268. {
  269. if (m_Theme.componentDirty)
  270. {
  271. SetAllComponentDirty();
  272. }
  273. if (m_Theme.vertsDirty) RefreshChart();
  274. m_Theme.ClearDirty();
  275. }
  276. foreach (var com in m_Components)
  277. CheckComponentDirty(com);
  278. }
  279. protected void CheckComponentDirty(MainComponent component)
  280. {
  281. if (component == null) return;
  282. if (component.anyDirty)
  283. {
  284. if (component.componentDirty)
  285. {
  286. if (component.refreshComponent != null)
  287. component.refreshComponent.Invoke();
  288. else
  289. component.handler.InitComponent();
  290. }
  291. if (component.vertsDirty)
  292. {
  293. if (component.painter != null)
  294. {
  295. RefreshPainter(component.painter);
  296. }
  297. }
  298. component.ClearDirty();
  299. }
  300. }
  301. protected override void SetAllComponentDirty()
  302. {
  303. base.SetAllComponentDirty();
  304. m_Theme.SetAllDirty();
  305. foreach (var com in m_Components) com.SetAllDirty();
  306. foreach (var handler in m_SerieHandlers) handler.InitComponent();
  307. m_RefreshChart = true;
  308. }
  309. protected override void OnDestroy()
  310. {
  311. base.OnDestroy();
  312. XChartsMgr.RemoveChart(chartName);
  313. for (int i = transform.childCount - 1; i >= 0; i--)
  314. {
  315. DestroyImmediate(transform.GetChild(i).gameObject);
  316. }
  317. }
  318. protected virtual void CheckPainter()
  319. {
  320. for (int i = 0; i < m_Series.Count; i++)
  321. {
  322. var serie = m_Series[i];
  323. serie.index = i;
  324. SetPainterActive(i, true);
  325. }
  326. if (transform.childCount - 3 != m_PainterTop.transform.GetSiblingIndex())
  327. {
  328. m_PainterTop.transform.SetSiblingIndex(transform.childCount - 3);
  329. }
  330. }
  331. protected override void InitPainter()
  332. {
  333. base.InitPainter();
  334. if (settings == null) return;
  335. m_Painter.material = settings.basePainterMaterial;
  336. m_PainterList.Clear();
  337. var sizeDelta = new Vector2(m_GraphWidth, m_GraphHeight);
  338. for (int i = 0; i < settings.maxPainter; i++)
  339. {
  340. var index = settings.reversePainter ? settings.maxPainter - 1 - i : i;
  341. var painter = ChartHelper.AddPainterObject("painter_" + index, transform, m_GraphMinAnchor,
  342. m_GraphMaxAnchor, m_GraphPivot, sizeDelta, chartHideFlags, 2 + index);
  343. painter.index = m_PainterList.Count;
  344. painter.type = Painter.Type.Serie;
  345. painter.onPopulateMesh = OnDrawPainterSerie;
  346. painter.SetActive(false, m_DebugInfo.showAllChartObject);
  347. painter.material = settings.seriePainterMaterial;
  348. painter.transform.SetSiblingIndex(index + 1);
  349. m_PainterList.Add(painter);
  350. }
  351. m_PainterUpper = ChartHelper.AddPainterObject("painter_u", transform, m_GraphMinAnchor,
  352. m_GraphMaxAnchor, m_GraphPivot, sizeDelta, chartHideFlags, 2 + settings.maxPainter);
  353. m_PainterUpper.type = Painter.Type.Top;
  354. m_PainterUpper.onPopulateMesh = OnDrawPainterUpper;
  355. m_PainterUpper.SetActive(true, m_DebugInfo.showAllChartObject);
  356. m_PainterUpper.material = settings.topPainterMaterial;
  357. m_PainterUpper.transform.SetSiblingIndex(settings.maxPainter + 1);
  358. m_PainterTop = ChartHelper.AddPainterObject("painter_t", transform, m_GraphMinAnchor,
  359. m_GraphMaxAnchor, m_GraphPivot, sizeDelta, chartHideFlags, 2 + settings.maxPainter);
  360. m_PainterTop.type = Painter.Type.Top;
  361. m_PainterTop.onPopulateMesh = OnDrawPainterTop;
  362. m_PainterTop.SetActive(true, m_DebugInfo.showAllChartObject);
  363. m_PainterTop.material = settings.topPainterMaterial;
  364. m_PainterTop.transform.SetSiblingIndex(settings.maxPainter + 1);
  365. }
  366. internal void InitComponentHandlers()
  367. {
  368. m_ComponentHandlers.Clear();
  369. m_Components.Sort();
  370. m_ComponentMaps.Clear();
  371. foreach (var component in m_Components)
  372. {
  373. var type = component.GetType();
  374. List<MainComponent> list;
  375. if (!m_ComponentMaps.TryGetValue(type, out list))
  376. {
  377. list = new List<MainComponent>();
  378. m_ComponentMaps[type] = list;
  379. }
  380. component.index = list.Count;
  381. list.Add(component);
  382. CreateComponentHandler(component);
  383. }
  384. }
  385. protected override void CheckRefreshChart()
  386. {
  387. if (m_Painter == null) return;
  388. if (m_RefreshChart)
  389. {
  390. CheckRefreshPainter();
  391. m_RefreshChart = false;
  392. }
  393. }
  394. protected override void CheckRefreshPainter()
  395. {
  396. if (m_Painter == null) return;
  397. m_Painter.CheckRefresh();
  398. foreach (var painter in m_PainterList) painter.CheckRefresh();
  399. if (m_PainterUpper != null) m_PainterUpper.CheckRefresh();
  400. if (m_PainterTop != null) m_PainterTop.CheckRefresh();
  401. }
  402. public void Internal_CheckAnimation()
  403. {
  404. if (!m_CheckAnimation)
  405. {
  406. m_CheckAnimation = true;
  407. AnimationFadeIn();
  408. }
  409. }
  410. protected override void OnSizeChanged()
  411. {
  412. base.OnSizeChanged();
  413. m_ChartWidth = m_GraphWidth;
  414. m_ChartHeight = m_GraphHeight;
  415. m_ChartX = m_GraphX;
  416. m_ChartY = m_GraphY;
  417. m_ChartPosition = m_GraphPosition;
  418. m_ChartMinAnchor = m_GraphMinAnchor;
  419. m_ChartMaxAnchor = m_GraphMaxAnchor;
  420. m_ChartPivot = m_GraphPivot;
  421. m_ChartSizeDelta = m_GraphSizeDelta;
  422. m_ChartRect = m_GraphRect;
  423. SetAllComponentDirty();
  424. OnCoordinateChanged();
  425. RefreshChart();
  426. }
  427. internal virtual void OnSerieDataUpdate(int serieIndex)
  428. {
  429. foreach (var handler in m_ComponentHandlers) handler.OnSerieDataUpdate(serieIndex);
  430. }
  431. internal virtual void OnCoordinateChanged()
  432. {
  433. foreach (var component in m_Components)
  434. {
  435. if (component is Axis)
  436. component.SetAllDirty();
  437. if (component is IUpdateRuntimeData)
  438. (component as IUpdateRuntimeData).UpdateRuntimeData(this);
  439. }
  440. }
  441. protected override void OnLocalPositionChanged()
  442. {
  443. Background background;
  444. if (TryGetChartComponent<Background>(out background))
  445. background.SetAllDirty();
  446. }
  447. protected virtual void OnThemeChanged() { }
  448. public virtual void OnDataZoomRangeChanged(DataZoom dataZoom)
  449. {
  450. foreach (var index in dataZoom.xAxisIndexs)
  451. {
  452. var axis = GetChartComponent<XAxis>(index);
  453. if (axis != null && axis.show) axis.SetAllDirty();
  454. }
  455. foreach (var index in dataZoom.yAxisIndexs)
  456. {
  457. var axis = GetChartComponent<YAxis>(index);
  458. if (axis != null && axis.show) axis.SetAllDirty();
  459. }
  460. }
  461. public override void OnPointerClick(PointerEventData eventData)
  462. {
  463. m_DebugInfo.clickChartCount++;
  464. base.OnPointerClick(eventData);
  465. foreach (var handler in m_SerieHandlers) handler.OnPointerClick(eventData);
  466. foreach (var handler in m_ComponentHandlers) handler.OnPointerClick(eventData);
  467. }
  468. public override void OnPointerDown(PointerEventData eventData)
  469. {
  470. base.OnPointerDown(eventData);
  471. foreach (var handler in m_SerieHandlers) handler.OnPointerDown(eventData);
  472. foreach (var handler in m_ComponentHandlers) handler.OnPointerDown(eventData);
  473. }
  474. public override void OnPointerUp(PointerEventData eventData)
  475. {
  476. base.OnPointerUp(eventData);
  477. foreach (var handler in m_SerieHandlers) handler.OnPointerUp(eventData);
  478. foreach (var handler in m_ComponentHandlers) handler.OnPointerUp(eventData);
  479. }
  480. public override void OnPointerEnter(PointerEventData eventData)
  481. {
  482. base.OnPointerEnter(eventData);
  483. foreach (var handler in m_SerieHandlers) handler.OnPointerEnter(eventData);
  484. foreach (var handler in m_ComponentHandlers) handler.OnPointerEnter(eventData);
  485. }
  486. public override void OnPointerExit(PointerEventData eventData)
  487. {
  488. base.OnPointerExit(eventData);
  489. foreach (var handler in m_SerieHandlers) handler.OnPointerExit(eventData);
  490. foreach (var handler in m_ComponentHandlers) handler.OnPointerExit(eventData);
  491. }
  492. public override void OnBeginDrag(PointerEventData eventData)
  493. {
  494. base.OnBeginDrag(eventData);
  495. foreach (var handler in m_SerieHandlers) handler.OnBeginDrag(eventData);
  496. foreach (var handler in m_ComponentHandlers) handler.OnBeginDrag(eventData);
  497. }
  498. public override void OnDrag(PointerEventData eventData)
  499. {
  500. base.OnDrag(eventData);
  501. foreach (var handler in m_SerieHandlers) handler.OnDrag(eventData);
  502. foreach (var handler in m_ComponentHandlers) handler.OnDrag(eventData);
  503. }
  504. public override void OnEndDrag(PointerEventData eventData)
  505. {
  506. base.OnEndDrag(eventData);
  507. foreach (var handler in m_SerieHandlers) handler.OnEndDrag(eventData);
  508. foreach (var handler in m_ComponentHandlers) handler.OnEndDrag(eventData);
  509. }
  510. public override void OnScroll(PointerEventData eventData)
  511. {
  512. base.OnScroll(eventData);
  513. foreach (var handler in m_SerieHandlers) handler.OnScroll(eventData);
  514. foreach (var handler in m_ComponentHandlers) handler.OnScroll(eventData);
  515. }
  516. public virtual void OnLegendButtonClick(int index, string legendName, bool show)
  517. {
  518. foreach (var handler in m_SerieHandlers)
  519. handler.OnLegendButtonClick(index, legendName, show);
  520. }
  521. public virtual void OnLegendButtonEnter(int index, string legendName)
  522. {
  523. foreach (var handler in m_SerieHandlers)
  524. handler.OnLegendButtonEnter(index, legendName);
  525. }
  526. public virtual void OnLegendButtonExit(int index, string legendName)
  527. {
  528. foreach (var handler in m_SerieHandlers)
  529. handler.OnLegendButtonExit(index, legendName);
  530. }
  531. protected override void OnDrawPainterBase(VertexHelper vh, Painter painter)
  532. {
  533. vh.Clear();
  534. DrawBackground(vh);
  535. DrawPainterBase(vh);
  536. foreach (var handler in m_ComponentHandlers) handler.DrawBase(vh);
  537. foreach (var handler in m_SerieHandlers) handler.DrawBase(vh);
  538. if (m_OnDrawBase != null)
  539. {
  540. m_OnDrawBase(vh);
  541. }
  542. m_BasePainterVertCount = vh.currentVertCount;
  543. }
  544. protected virtual void OnDrawPainterSerie(VertexHelper vh, Painter painter)
  545. {
  546. vh.Clear();
  547. var maxPainter = settings.maxPainter;
  548. var maxSeries = m_Series.Count;
  549. var rate = Mathf.CeilToInt(maxSeries * 1.0f / maxPainter);
  550. m_PainterUpper.Refresh();
  551. m_PainterTop.Refresh();
  552. m_DebugInfo.refreshCount++;
  553. for (int i = painter.index * rate; i < (painter.index + 1) * rate && i < maxSeries; i++)
  554. {
  555. var serie = m_Series[i];
  556. serie.context.colorIndex = GetLegendRealShowNameIndex(serie.legendName);
  557. serie.context.dataPoints.Clear();
  558. serie.context.dataIndexs.Clear();
  559. serie.context.dataIgnores.Clear();
  560. serie.animation.context.isAllItemAnimationEnd = true;
  561. if (serie.show && !serie.animation.HasFadeOut())
  562. {
  563. if (m_OnDrawSerieBefore != null)
  564. {
  565. m_OnDrawSerieBefore.Invoke(vh, serie);
  566. }
  567. DrawPainterSerie(vh, serie);
  568. if (i >= 0 && i < m_SerieHandlers.Count)
  569. {
  570. var handler = m_SerieHandlers[i];
  571. handler.DrawSerie(vh);
  572. handler.RefreshLabelNextFrame();
  573. }
  574. if (m_OnDrawSerieAfter != null)
  575. {
  576. m_OnDrawSerieAfter(vh, serie);
  577. }
  578. }
  579. serie.context.vertCount = vh.currentVertCount;
  580. }
  581. }
  582. protected virtual void OnDrawPainterUpper(VertexHelper vh, Painter painter)
  583. {
  584. vh.Clear();
  585. DrawPainterUpper(vh);
  586. foreach (var draw in m_ComponentHandlers) draw.DrawUpper(vh);
  587. if (m_OnDrawUpper != null)
  588. {
  589. m_OnDrawUpper(vh);
  590. }
  591. m_UpperPainterVertCount = vh.currentVertCount;
  592. }
  593. protected virtual void OnDrawPainterTop(VertexHelper vh, Painter painter)
  594. {
  595. vh.Clear();
  596. DrawPainterTop(vh);
  597. foreach (var draw in m_ComponentHandlers) draw.DrawTop(vh);
  598. if (m_OnDrawTop != null)
  599. {
  600. m_OnDrawTop(vh);
  601. }
  602. m_TopPainterVertCount = vh.currentVertCount;
  603. }
  604. protected virtual void DrawPainterSerie(VertexHelper vh, Serie serie) { }
  605. protected virtual void DrawPainterUpper(VertexHelper vh)
  606. {
  607. foreach (var handler in m_SerieHandlers)
  608. handler.DrawUpper(vh);
  609. }
  610. protected virtual void DrawPainterTop(VertexHelper vh)
  611. {
  612. foreach (var handler in m_SerieHandlers)
  613. handler.DrawTop(vh);
  614. }
  615. protected virtual void DrawBackground(VertexHelper vh)
  616. {
  617. var background = GetChartComponent<Background>();
  618. if (background != null && background.show)
  619. return;
  620. Vector3 p1 = new Vector3(chartX, chartY + chartHeight);
  621. Vector3 p2 = new Vector3(chartX + chartWidth, chartY + chartHeight);
  622. Vector3 p3 = new Vector3(chartX + chartWidth, chartY);
  623. Vector3 p4 = new Vector3(chartX, chartY);
  624. UGL.DrawQuadrilateral(vh, p1, p2, p3, p4, theme.backgroundColor);
  625. }
  626. protected int GetPainterIndexBySerie(Serie serie)
  627. {
  628. var maxPainter = settings.maxPainter;
  629. var maxSeries = m_Series.Count;
  630. if (maxPainter >= maxSeries) return serie.index;
  631. else
  632. {
  633. var rate = Mathf.CeilToInt(maxSeries * 1.0f / maxPainter);
  634. return serie.index / rate;
  635. }
  636. }
  637. private void InitListForFieldInfos()
  638. {
  639. if (m_TypeListForSerie.Count != 0) return;
  640. m_TypeListForComponent.Clear();
  641. m_TypeListForSerie.Clear();
  642. var fileds1 = GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
  643. var fileds2 = GetType().BaseType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
  644. var list = ListPool<FieldInfo>.Get();
  645. list.AddRange(fileds1);
  646. list.AddRange(fileds2);
  647. foreach (var field in list)
  648. {
  649. var attribute1 = field.GetAttribute<ListForSerie>(false);
  650. if (attribute1 != null)
  651. m_TypeListForSerie.Add(attribute1.type, field);
  652. var attribute2 = field.GetAttribute<ListForComponent>(false);
  653. if (attribute2 != null)
  654. m_TypeListForComponent.Add(attribute2.type, field);
  655. }
  656. ListPool<FieldInfo>.Release(list);
  657. }
  658. public void OnBeforeSerialize()
  659. {
  660. #if UNITY_EDITOR && UNITY_2019_3_OR_NEWER
  661. if (!UnityEditor.EditorUtility.IsDirty(this))
  662. return;
  663. UnityEditor.EditorUtility.ClearDirty(this);
  664. #endif
  665. InitListForFieldInfos();
  666. foreach (var kv in m_TypeListForSerie)
  667. {
  668. ReflectionUtil.InvokeListClear(this, kv.Value);
  669. }
  670. foreach (var kv in m_TypeListForComponent)
  671. {
  672. ReflectionUtil.InvokeListClear(this, kv.Value);
  673. }
  674. foreach (var component in m_Components)
  675. {
  676. FieldInfo field;
  677. if (m_TypeListForComponent.TryGetValue(component.GetType(), out field))
  678. ReflectionUtil.InvokeListAdd(this, field, component);
  679. else
  680. Debug.LogError("No ListForComponent:" + component.GetType());
  681. }
  682. foreach (var serie in m_Series)
  683. {
  684. FieldInfo field;
  685. serie.OnBeforeSerialize();
  686. if (m_TypeListForSerie.TryGetValue(serie.GetType(), out field))
  687. ReflectionUtil.InvokeListAdd(this, field, serie);
  688. else
  689. Debug.LogError("No ListForSerie:" + serie.GetType());
  690. }
  691. }
  692. public void OnAfterDeserialize()
  693. {
  694. InitListForFieldInfos();
  695. m_Components.Clear();
  696. m_Series.Clear();
  697. foreach (var kv in m_TypeListForComponent)
  698. {
  699. ReflectionUtil.InvokeListAddTo<MainComponent>(this, kv.Value, AddComponent);
  700. }
  701. foreach (var kv in m_TypeListForSerie)
  702. {
  703. ReflectionUtil.InvokeListAddTo<Serie>(this, kv.Value, AddSerieAfterDeserialize);
  704. }
  705. m_Series.Sort();
  706. m_Components.Sort();
  707. InitComponentHandlers();
  708. InitSerieHandlers();
  709. }
  710. }
  711. }