BaseChart.Component.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace XCharts.Runtime
  5. {
  6. public partial class BaseChart
  7. {
  8. public bool TryAddChartComponent<T>() where T : MainComponent
  9. {
  10. return TryAddChartComponent(typeof(T));
  11. }
  12. public bool TryAddChartComponent(Type type)
  13. {
  14. if (CanAddChartComponent(type))
  15. {
  16. AddChartComponent(type);
  17. return true;
  18. }
  19. else
  20. {
  21. return false;
  22. }
  23. }
  24. public bool TryAddChartComponent<T>(out T component) where T : MainComponent
  25. {
  26. var type = typeof(T);
  27. if (CanAddChartComponent(type))
  28. {
  29. component = AddChartComponent(type) as T;
  30. return true;
  31. }
  32. else
  33. {
  34. component = null;
  35. return false;
  36. }
  37. }
  38. public T AddChartComponent<T>() where T : MainComponent
  39. {
  40. return (T)AddChartComponent(typeof(T));
  41. }
  42. public T AddChartComponentWhenNoExist<T>() where T : MainComponent
  43. {
  44. if (HasChartComponent<T>()) return null;
  45. return AddChartComponent<T>();
  46. }
  47. public MainComponent AddChartComponent(Type type)
  48. {
  49. if (!CanAddChartComponent(type))
  50. {
  51. Debug.LogError("XCharts ERROR: CanAddChartComponent:" + type.Name);
  52. return null;
  53. }
  54. CheckAddRequireChartComponent(type);
  55. var component = Activator.CreateInstance(type) as MainComponent;
  56. if (component == null)
  57. {
  58. Debug.LogError("XCharts ERROR: CanAddChartComponent:" + type.Name);
  59. return null;
  60. }
  61. component.SetDefaultValue();
  62. if (component is IUpdateRuntimeData)
  63. (component as IUpdateRuntimeData).UpdateRuntimeData(this);
  64. AddComponent(component);
  65. m_Components.Sort();
  66. CreateComponentHandler(component);
  67. #if UNITY_EDITOR && UNITY_2019_1_OR_NEWER
  68. UnityEditor.EditorUtility.SetDirty(this);
  69. OnBeforeSerialize();
  70. #endif
  71. return component;
  72. }
  73. private void AddComponent(MainComponent component)
  74. {
  75. var type = component.GetType();
  76. m_Components.Add(component);
  77. List<MainComponent> list;
  78. if (!m_ComponentMaps.TryGetValue(type, out list))
  79. {
  80. list = new List<MainComponent>();
  81. m_ComponentMaps[type] = list;
  82. }
  83. component.index = list.Count;
  84. list.Add(component);
  85. m_Components.Sort((a, b) => { return a.GetType().Name.CompareTo(b.GetType().Name); });
  86. }
  87. private void CheckAddRequireChartComponent(Type type)
  88. {
  89. if (Attribute.IsDefined(type, typeof(RequireChartComponentAttribute)))
  90. {
  91. foreach (var obj in type.GetCustomAttributes(typeof(RequireChartComponentAttribute), false))
  92. {
  93. var attribute = obj as RequireChartComponentAttribute;
  94. if (attribute.type0 != null && !HasChartComponent(attribute.type0))
  95. AddChartComponent(attribute.type0);
  96. if (attribute.type1 != null && !HasChartComponent(attribute.type1))
  97. AddChartComponent(attribute.type1);
  98. if (attribute.type2 != null && !HasChartComponent(attribute.type2))
  99. AddChartComponent(attribute.type2);
  100. }
  101. }
  102. }
  103. private void CreateComponentHandler(MainComponent component)
  104. {
  105. if (!component.GetType().IsDefined(typeof(ComponentHandlerAttribute), false))
  106. {
  107. Debug.LogError("MainComponent no Handler:" + component.GetType());
  108. return;
  109. }
  110. var attrubte = component.GetType().GetAttribute<ComponentHandlerAttribute>();
  111. if (attrubte.handler == null)
  112. return;
  113. var handler = (MainComponentHandler)Activator.CreateInstance(attrubte.handler);
  114. handler.attribute = attrubte;
  115. handler.chart = this;
  116. handler.order = attrubte.order;
  117. handler.SetComponent(component);
  118. component.handler = handler;
  119. m_ComponentHandlers.Add(handler);
  120. m_ComponentHandlers.Sort((a, b) => { return a.order.CompareTo(b.order); });
  121. }
  122. public bool RemoveChartComponent<T>(int index = 0)
  123. where T : MainComponent
  124. {
  125. return RemoveChartComponent(typeof(T), index);
  126. }
  127. public int RemoveChartComponents<T>()
  128. where T : MainComponent
  129. {
  130. return RemoveChartComponents(typeof(T));
  131. }
  132. public void RemoveAllChartComponent()
  133. {
  134. m_Components.Clear();
  135. InitComponentHandlers();
  136. }
  137. public bool RemoveChartComponent(Type type, int index = 0)
  138. {
  139. MainComponent toRemove = null;
  140. for (int i = 0; i < m_Components.Count; i++)
  141. {
  142. if (m_Components[i].GetType() == type && m_Components[i].index == index)
  143. {
  144. toRemove = m_Components[i];
  145. break;
  146. }
  147. }
  148. return RemoveChartComponent(toRemove);
  149. }
  150. public int RemoveChartComponents(Type type)
  151. {
  152. int count = 0;
  153. for (int i = m_Components.Count - 1; i > 0; i--)
  154. {
  155. if (m_Components[i].GetType() == type)
  156. {
  157. RemoveChartComponent(m_Components[i]);
  158. count++;
  159. }
  160. }
  161. return count;
  162. }
  163. public bool RemoveChartComponent(MainComponent component)
  164. {
  165. if (component == null) return false;
  166. if (m_Components.Remove(component))
  167. {
  168. if (component.gameObject != null)
  169. ChartHelper.SetActive(component.gameObject, false);
  170. #if UNITY_EDITOR && UNITY_2019_1_OR_NEWER
  171. UnityEditor.EditorUtility.SetDirty(this);
  172. OnBeforeSerialize();
  173. #endif
  174. InitComponentHandlers();
  175. RefreshChart();
  176. return true;
  177. }
  178. return false;
  179. }
  180. public bool CanAddChartComponent(Type type)
  181. {
  182. if (!type.IsSubclassOf(typeof(MainComponent))) return false;
  183. if (!m_TypeListForComponent.ContainsKey(type)) return false;
  184. if (CanMultipleComponent(type)) return !HasChartComponent(type);
  185. else return true;
  186. }
  187. public bool HasChartComponent<T>()
  188. where T : MainComponent
  189. {
  190. return HasChartComponent(typeof(T));
  191. }
  192. public bool HasChartComponent(Type type)
  193. {
  194. foreach (var component in m_Components)
  195. {
  196. if (component == null) continue;
  197. if (component.GetType() == type)
  198. return true;
  199. }
  200. return false;
  201. }
  202. public bool CanMultipleComponent(Type type)
  203. {
  204. return Attribute.IsDefined(type, typeof(DisallowMultipleComponent));
  205. }
  206. public int GetChartComponentNum<T>() where T : MainComponent
  207. {
  208. return GetChartComponentNum(typeof(T));
  209. }
  210. private static List<MainComponent> list;
  211. public int GetChartComponentNum(Type type)
  212. {
  213. if (m_ComponentMaps.TryGetValue(type, out list))
  214. return list.Count;
  215. else
  216. return 0;
  217. }
  218. public T GetChartComponent<T>(int index = 0) where T : MainComponent
  219. {
  220. foreach (var component in m_Components)
  221. {
  222. if (component is T && component.index == index)
  223. return component as T;
  224. }
  225. return null;
  226. }
  227. public List<MainComponent> GetChartComponents<T>() where T : MainComponent
  228. {
  229. var type = typeof(T);
  230. if (m_ComponentMaps.ContainsKey(type))
  231. return m_ComponentMaps[type];
  232. else
  233. return null;
  234. }
  235. [Obsolete("'GetOrAddChartComponent' is obsolete, Use 'EnsureChartComponent' instead.")]
  236. public T GetOrAddChartComponent<T>() where T : MainComponent
  237. {
  238. var component = GetChartComponent<T>();
  239. if (component == null)
  240. return AddChartComponent<T>();
  241. else
  242. return component;
  243. }
  244. /// <summary>
  245. /// Ensure the chart has the component, if not, add it.
  246. /// Note: it may fail to add.
  247. /// ||确保图表有该组件,如果没有则添加。注意:有可能添加不成功。
  248. /// </summary>
  249. /// <typeparam name="T"></typeparam>
  250. /// <returns>component, or null if add failed.</returns>
  251. [Since("v3.6.0")]
  252. public T EnsureChartComponent<T>() where T : MainComponent
  253. {
  254. var component = GetChartComponent<T>();
  255. if (component == null)
  256. return AddChartComponent<T>();
  257. else
  258. return component;
  259. }
  260. public bool TryGetChartComponent<T>(out T component, int index = 0)
  261. where T : MainComponent
  262. {
  263. component = null;
  264. foreach (var com in m_Components)
  265. {
  266. if (com is T && com.index == index)
  267. {
  268. component = (T)com;
  269. return true;
  270. }
  271. }
  272. return false;
  273. }
  274. public GridCoord GetGrid(Vector2 local)
  275. {
  276. List<MainComponent> list;
  277. if (m_ComponentMaps.TryGetValue(typeof(GridCoord), out list))
  278. {
  279. foreach (var component in list)
  280. {
  281. var grid = component as GridCoord;
  282. if (grid.Contains(local)) return grid;
  283. }
  284. }
  285. return null;
  286. }
  287. public GridCoord GetGridOfDataZoom(DataZoom dataZoom)
  288. {
  289. GridCoord grid = null;
  290. if (dataZoom.xAxisIndexs != null && dataZoom.xAxisIndexs.Count > 0)
  291. {
  292. var xAxis = GetChartComponent<XAxis>(dataZoom.xAxisIndexs[0]);
  293. grid = GetChartComponent<GridCoord>(xAxis.gridIndex);
  294. }
  295. else if (dataZoom.yAxisIndexs != null && dataZoom.yAxisIndexs.Count > 0)
  296. {
  297. var yAxis = GetChartComponent<YAxis>(dataZoom.yAxisIndexs[0]);
  298. grid = GetChartComponent<GridCoord>(yAxis.gridIndex);
  299. }
  300. if (grid == null) return GetChartComponent<GridCoord>();
  301. else return grid;
  302. }
  303. public DataZoom GetDataZoomOfAxis(Axis axis)
  304. {
  305. foreach (var component in m_Components)
  306. {
  307. if (component is DataZoom)
  308. {
  309. var dataZoom = component as DataZoom;
  310. if (!dataZoom.enable) continue;
  311. if (dataZoom.IsContainsAxis(axis)) return dataZoom;
  312. }
  313. }
  314. return null;
  315. }
  316. public VisualMap GetVisualMapOfSerie(Serie serie)
  317. {
  318. foreach (var component in m_Components)
  319. {
  320. if (component is VisualMap)
  321. {
  322. var visualMap = component as VisualMap;
  323. if (visualMap.serieIndex == serie.index) return visualMap;
  324. }
  325. }
  326. return null;
  327. }
  328. public void GetDataZoomOfSerie(Serie serie, out DataZoom xDataZoom, out DataZoom yDataZoom)
  329. {
  330. xDataZoom = null;
  331. yDataZoom = null;
  332. if (serie == null) return;
  333. foreach (var component in m_Components)
  334. {
  335. if (component is DataZoom)
  336. {
  337. var dataZoom = component as DataZoom;
  338. if (!dataZoom.enable) continue;
  339. if (dataZoom.IsContainsXAxis(serie.xAxisIndex))
  340. {
  341. xDataZoom = dataZoom;
  342. }
  343. if (dataZoom.IsContainsYAxis(serie.yAxisIndex))
  344. {
  345. yDataZoom = dataZoom;
  346. }
  347. }
  348. }
  349. }
  350. public DataZoom GetXDataZoomOfSerie(Serie serie)
  351. {
  352. if (serie == null) return null;
  353. foreach (var component in m_Components)
  354. {
  355. if (component is DataZoom)
  356. {
  357. var dataZoom = component as DataZoom;
  358. if (!dataZoom.enable) continue;
  359. if (dataZoom.IsContainsXAxis(serie.xAxisIndex))
  360. return dataZoom;
  361. }
  362. }
  363. return null;
  364. }
  365. /// <summary>
  366. /// reutrn true when all the show axis is `Value` type.
  367. /// ||纯数值坐标轴(数值轴或对数轴)。
  368. /// </summary>
  369. public bool IsAllAxisValue()
  370. {
  371. foreach (var component in m_Components)
  372. {
  373. if (component is Axis)
  374. {
  375. var axis = component as Axis;
  376. if (axis.show && !axis.IsValue() && !axis.IsLog() && !axis.IsTime()) return false;
  377. }
  378. }
  379. return true;
  380. }
  381. /// <summary>
  382. /// 纯类目轴。
  383. /// </summary>
  384. public bool IsAllAxisCategory()
  385. {
  386. foreach (var component in m_Components)
  387. {
  388. if (component is Axis)
  389. {
  390. var axis = component as Axis;
  391. if (axis.show && !axis.IsCategory()) return false;
  392. }
  393. }
  394. return true;
  395. }
  396. public bool IsInAnyGrid(Vector2 local)
  397. {
  398. List<MainComponent> list;
  399. if (m_ComponentMaps.TryGetValue(typeof(GridCoord), out list))
  400. {
  401. foreach (var grid in list)
  402. {
  403. if ((grid as GridCoord).Contains(local)) return true;
  404. }
  405. }
  406. return false;
  407. }
  408. internal string GetTooltipCategory(Serie serie)
  409. {
  410. var xAxis = GetChartComponent<XAxis>(serie.xAxisIndex);
  411. var yAxis = GetChartComponent<YAxis>(serie.yAxisIndex);
  412. if (yAxis.IsCategory())
  413. {
  414. return yAxis.GetData(serie.context.pointerItemDataIndex);
  415. }
  416. else if (xAxis.IsCategory())
  417. {
  418. return xAxis.GetData(serie.context.pointerItemDataIndex);
  419. }
  420. return null;
  421. }
  422. internal bool GetSerieGridCoordAxis(Serie serie, out Axis axis, out Axis relativedAxis)
  423. {
  424. var yAxis = GetChartComponent<YAxis>(serie.yAxisIndex);
  425. var xAxis = GetChartComponent<XAxis>(serie.xAxisIndex);
  426. if (xAxis == null || yAxis == null)
  427. {
  428. axis = null;
  429. relativedAxis = null;
  430. return false;
  431. }
  432. var isY = yAxis.IsCategory() && !xAxis.IsCategory();
  433. if (isY)
  434. {
  435. axis = yAxis;
  436. relativedAxis = GetChartComponent<XAxis>(serie.xAxisIndex);
  437. }
  438. else
  439. {
  440. axis = GetChartComponent<XAxis>(serie.xAxisIndex);
  441. relativedAxis = yAxis;
  442. }
  443. return isY;
  444. }
  445. }
  446. }