Ei kuvausta
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.Component.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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(chartX, chartY, chartWidth, chartHeight);
  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. #endif
  70. return component;
  71. }
  72. private void AddComponent(MainComponent component)
  73. {
  74. var type = component.GetType();
  75. m_Components.Add(component);
  76. List<MainComponent> list;
  77. if (!m_ComponentMaps.TryGetValue(type, out list))
  78. {
  79. list = new List<MainComponent>();
  80. m_ComponentMaps[type] = list;
  81. }
  82. component.index = list.Count;
  83. list.Add(component);
  84. }
  85. private void CheckAddRequireChartComponent(Type type)
  86. {
  87. if (Attribute.IsDefined(type, typeof(RequireChartComponentAttribute)))
  88. {
  89. foreach (var obj in type.GetCustomAttributes(typeof(RequireChartComponentAttribute), false))
  90. {
  91. var attribute = obj as RequireChartComponentAttribute;
  92. if (attribute.type0 != null && !HasChartComponent(attribute.type0))
  93. AddChartComponent(attribute.type0);
  94. if (attribute.type1 != null && !HasChartComponent(attribute.type1))
  95. AddChartComponent(attribute.type1);
  96. if (attribute.type2 != null && !HasChartComponent(attribute.type2))
  97. AddChartComponent(attribute.type2);
  98. }
  99. }
  100. }
  101. private void CreateComponentHandler(MainComponent component)
  102. {
  103. if (!component.GetType().IsDefined(typeof(ComponentHandlerAttribute), false))
  104. {
  105. Debug.LogError("MainComponent no Handler:" + component.GetType());
  106. return;
  107. }
  108. var attrubte = component.GetType().GetAttribute<ComponentHandlerAttribute>();
  109. if (attrubte.handler == null)
  110. return;
  111. var handler = (MainComponentHandler) Activator.CreateInstance(attrubte.handler);
  112. handler.attribute = attrubte;
  113. handler.chart = this;
  114. handler.SetComponent(component);
  115. component.handler = handler;
  116. m_ComponentHandlers.Add(handler);
  117. }
  118. public bool RemoveChartComponent<T>(int index = 0)
  119. where T : MainComponent
  120. {
  121. return RemoveChartComponent(typeof(T), index);
  122. }
  123. public int RemoveChartComponents<T>()
  124. where T : MainComponent
  125. {
  126. return RemoveChartComponents(typeof(T));
  127. }
  128. public void RemoveAllChartComponent()
  129. {
  130. m_Components.Clear();
  131. InitComponentHandlers();
  132. }
  133. public bool RemoveChartComponent(Type type, int index = 0)
  134. {
  135. MainComponent toRemove = null;
  136. for (int i = 0; i < m_Components.Count; i++)
  137. {
  138. if (m_Components[i].GetType() == type && m_Components[i].index == index)
  139. {
  140. toRemove = m_Components[i];
  141. break;
  142. }
  143. }
  144. return RemoveChartComponent(toRemove);
  145. }
  146. public int RemoveChartComponents(Type type)
  147. {
  148. int count = 0;
  149. for (int i = m_Components.Count - 1; i > 0; i--)
  150. {
  151. if (m_Components[i].GetType() == type)
  152. {
  153. RemoveChartComponent(m_Components[i]);
  154. count++;
  155. }
  156. }
  157. return count;
  158. }
  159. public bool RemoveChartComponent(MainComponent component)
  160. {
  161. if (component == null) return false;
  162. if (m_Components.Remove(component))
  163. {
  164. if (component.gameObject != null)
  165. ChartHelper.SetActive(component.gameObject, false);
  166. InitComponentHandlers();
  167. RefreshChart();
  168. return true;
  169. }
  170. return false;
  171. }
  172. public bool CanAddChartComponent(Type type)
  173. {
  174. if (!type.IsSubclassOf(typeof(MainComponent))) return false;
  175. if (!m_TypeListForComponent.ContainsKey(type)) return false;
  176. if (CanMultipleComponent(type)) return !HasChartComponent(type);
  177. else return true;
  178. }
  179. public bool HasChartComponent<T>()
  180. where T : MainComponent
  181. {
  182. return HasChartComponent(typeof(T));
  183. }
  184. public bool HasChartComponent(Type type)
  185. {
  186. foreach (var component in m_Components)
  187. {
  188. if (component == null) continue;
  189. if (component.GetType() == type)
  190. return true;
  191. }
  192. return false;
  193. }
  194. public bool CanMultipleComponent(Type type)
  195. {
  196. return Attribute.IsDefined(type, typeof(DisallowMultipleComponent));
  197. }
  198. public int GetChartComponentNum<T>() where T : MainComponent
  199. {
  200. return GetChartComponentNum(typeof(T));
  201. }
  202. public int GetChartComponentNum(Type type)
  203. {
  204. List<MainComponent> list;
  205. if (m_ComponentMaps.TryGetValue(type, out list))
  206. return list.Count;
  207. else
  208. return 0;
  209. }
  210. public T GetChartComponent<T>(int index = 0) where T : MainComponent
  211. {
  212. foreach (var component in m_Components)
  213. {
  214. if (component is T && component.index == index)
  215. return component as T;
  216. }
  217. return null;
  218. }
  219. public List<MainComponent> GetChartComponents<T>() where T : MainComponent
  220. {
  221. return m_ComponentMaps[typeof(T)];
  222. }
  223. public T GetOrAddChartComponent<T>() where T : MainComponent
  224. {
  225. var component = GetChartComponent<T>();
  226. if (component == null)
  227. return AddChartComponent<T>();
  228. else
  229. return component;
  230. }
  231. public T EnsureChartComponent<T>() where T : MainComponent
  232. {
  233. var component = GetChartComponent<T>();
  234. if (component == null)
  235. return AddChartComponent<T>();
  236. else
  237. return component;
  238. }
  239. public bool TryGetChartComponent<T>(out T component, int index = 0)
  240. where T : MainComponent
  241. {
  242. component = null;
  243. foreach (var com in m_Components)
  244. {
  245. if (com is T && com.index == index)
  246. {
  247. component = (T) com;
  248. return true;
  249. }
  250. }
  251. return false;
  252. }
  253. public GridCoord GetGrid(Vector2 local)
  254. {
  255. List<MainComponent> list;
  256. if (m_ComponentMaps.TryGetValue(typeof(GridCoord), out list))
  257. {
  258. foreach (var component in list)
  259. {
  260. var grid = component as GridCoord;
  261. if (grid.Contains(local)) return grid;
  262. }
  263. }
  264. return null;
  265. }
  266. public GridCoord GetGridOfDataZoom(DataZoom dataZoom)
  267. {
  268. GridCoord grid = null;
  269. if (dataZoom.xAxisIndexs != null && dataZoom.xAxisIndexs.Count > 0)
  270. {
  271. var xAxis = GetChartComponent<XAxis>(dataZoom.xAxisIndexs[0]);
  272. grid = GetChartComponent<GridCoord>(xAxis.gridIndex);
  273. }
  274. else if (dataZoom.yAxisIndexs != null && dataZoom.yAxisIndexs.Count > 0)
  275. {
  276. var yAxis = GetChartComponent<YAxis>(dataZoom.yAxisIndexs[0]);
  277. grid = GetChartComponent<GridCoord>(yAxis.gridIndex);
  278. }
  279. if (grid == null) return GetChartComponent<GridCoord>();
  280. else return grid;
  281. }
  282. public DataZoom GetDataZoomOfAxis(Axis axis)
  283. {
  284. foreach (var component in m_Components)
  285. {
  286. if (component is DataZoom)
  287. {
  288. var dataZoom = component as DataZoom;
  289. if (!dataZoom.enable) continue;
  290. if (dataZoom.IsContainsAxis(axis)) return dataZoom;
  291. }
  292. }
  293. return null;
  294. }
  295. public VisualMap GetVisualMapOfSerie(Serie serie)
  296. {
  297. foreach (var component in m_Components)
  298. {
  299. if (component is VisualMap)
  300. {
  301. var visualMap = component as VisualMap;
  302. if (visualMap.serieIndex == serie.index) return visualMap;
  303. }
  304. }
  305. return null;
  306. }
  307. public void GetDataZoomOfSerie(Serie serie, out DataZoom xDataZoom, out DataZoom yDataZoom)
  308. {
  309. xDataZoom = null;
  310. yDataZoom = null;
  311. if (serie == null) return;
  312. foreach (var component in m_Components)
  313. {
  314. if (component is DataZoom)
  315. {
  316. var dataZoom = component as DataZoom;
  317. if (!dataZoom.enable) continue;
  318. if (dataZoom.IsContainsXAxis(serie.xAxisIndex))
  319. {
  320. xDataZoom = dataZoom;
  321. }
  322. if (dataZoom.IsContainsYAxis(serie.yAxisIndex))
  323. {
  324. yDataZoom = dataZoom;
  325. }
  326. }
  327. }
  328. }
  329. public DataZoom GetXDataZoomOfSerie(Serie serie)
  330. {
  331. if (serie == null) return null;
  332. foreach (var component in m_Components)
  333. {
  334. if (component is DataZoom)
  335. {
  336. var dataZoom = component as DataZoom;
  337. if (!dataZoom.enable) continue;
  338. if (dataZoom.IsContainsXAxis(serie.xAxisIndex))
  339. return dataZoom;
  340. }
  341. }
  342. return null;
  343. }
  344. /// <summary>
  345. /// reutrn true when all the show axis is `Value` type.
  346. /// |纯数值坐标轴(数值轴或对数轴)。
  347. /// </summary>
  348. public bool IsAllAxisValue()
  349. {
  350. foreach (var component in m_Components)
  351. {
  352. if (component is Axis)
  353. {
  354. var axis = component as Axis;
  355. if (axis.show && !axis.IsValue() && !axis.IsLog() && !axis.IsTime()) return false;
  356. }
  357. }
  358. return true;
  359. }
  360. /// <summary>
  361. /// 纯类目轴。
  362. /// </summary>
  363. public bool IsAllAxisCategory()
  364. {
  365. foreach (var component in m_Components)
  366. {
  367. if (component is Axis)
  368. {
  369. var axis = component as Axis;
  370. if (axis.show && !axis.IsCategory()) return false;
  371. }
  372. }
  373. return true;
  374. }
  375. public bool IsInAnyGrid(Vector2 local)
  376. {
  377. List<MainComponent> list;
  378. if (m_ComponentMaps.TryGetValue(typeof(GridCoord), out list))
  379. {
  380. foreach (var grid in list)
  381. {
  382. if ((grid as GridCoord).Contains(local)) return true;
  383. }
  384. }
  385. return false;
  386. }
  387. internal string GetTooltipCategory(int dataIndex, DataZoom dataZoom = null)
  388. {
  389. var xAxis = GetChartComponent<XAxis>();
  390. var yAxis = GetChartComponent<YAxis>();
  391. if (yAxis.IsCategory())
  392. {
  393. return yAxis.GetData((int) yAxis.context.pointerValue, dataZoom);
  394. }
  395. else if (xAxis.IsCategory())
  396. {
  397. return xAxis.GetData((int) xAxis.context.pointerValue, dataZoom);
  398. }
  399. return null;
  400. }
  401. internal string GetTooltipCategory(int dataIndex, Serie serie, DataZoom dataZoom = null)
  402. {
  403. var xAxis = GetChartComponent<XAxis>(serie.xAxisIndex);
  404. var yAxis = GetChartComponent<YAxis>(serie.yAxisIndex);
  405. if (yAxis.IsCategory())
  406. {
  407. return yAxis.GetData((int) yAxis.context.pointerValue, dataZoom);
  408. }
  409. else if (xAxis.IsCategory())
  410. {
  411. return xAxis.GetData((int) xAxis.context.pointerValue, dataZoom);
  412. }
  413. return null;
  414. }
  415. internal bool GetSerieGridCoordAxis(Serie serie, out Axis axis, out Axis relativedAxis)
  416. {
  417. var yAxis = GetChartComponent<YAxis>(serie.yAxisIndex);
  418. if (yAxis == null)
  419. {
  420. axis = null;
  421. relativedAxis = null;
  422. return false;
  423. }
  424. var isY = yAxis.IsCategory();
  425. if (isY)
  426. {
  427. axis = yAxis;
  428. relativedAxis = GetChartComponent<XAxis>(serie.xAxisIndex);
  429. }
  430. else
  431. {
  432. axis = GetChartComponent<XAxis>(serie.xAxisIndex);
  433. relativedAxis = yAxis;
  434. }
  435. return isY;
  436. }
  437. }
  438. }