暫無描述
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.

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