No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseChart.cs 30KB

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