Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

BaseChart.API.cs 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. namespace XCharts.Runtime
  7. {
  8. /// <summary>
  9. /// The base class of all charts.
  10. /// |所有Chart的基类。
  11. /// </summary>
  12. public partial class BaseChart
  13. {
  14. /// <summary>
  15. /// The name of chart.
  16. /// |</summary>
  17. public string chartName
  18. {
  19. get { return m_ChartName; }
  20. set
  21. {
  22. if (!string.IsNullOrEmpty(value) && XChartsMgr.ContainsChart(value))
  23. {
  24. Debug.LogError("chartName repeated:" + value);
  25. }
  26. else
  27. {
  28. m_ChartName = value;
  29. }
  30. }
  31. }
  32. /// <summary>
  33. /// The theme.
  34. /// |</summary>
  35. public ThemeStyle theme { get { return m_Theme; } set { m_Theme = value; } }
  36. /// <summary>
  37. /// Global parameter setting component.
  38. /// |全局设置组件。
  39. /// </summary>
  40. public Settings settings { get { return m_Settings; } }
  41. /// <summary>
  42. /// The x of chart.
  43. /// |图表的X
  44. /// </summary>
  45. public float chartX { get { return m_ChartX; } }
  46. /// <summary>
  47. /// The y of chart.
  48. /// |图表的Y
  49. /// </summary>
  50. public float chartY { get { return m_ChartY; } }
  51. /// <summary>
  52. /// The width of chart.
  53. /// |图表的宽
  54. /// </summary>
  55. public float chartWidth { get { return m_ChartWidth; } }
  56. /// <summary>
  57. /// The height of chart.
  58. /// |图表的高
  59. /// </summary>
  60. public float chartHeight { get { return m_ChartHeight; } }
  61. public Vector2 chartMinAnchor { get { return m_ChartMinAnchor; } }
  62. public Vector2 chartMaxAnchor { get { return m_ChartMaxAnchor; } }
  63. public Vector2 chartPivot { get { return m_ChartPivot; } }
  64. public Vector2 chartSizeDelta { get { return m_ChartSizeDelta; } }
  65. /// <summary>
  66. /// The position of chart.
  67. /// |图表的左下角起始坐标。
  68. /// </summary>
  69. public Vector3 chartPosition { get { return m_ChartPosition; } }
  70. public Rect chartRect { get { return m_ChartRect; } }
  71. public Action onInit { set { m_OnInit = value; } }
  72. public Action onUpdate { set { m_OnUpdate = value; } }
  73. /// <summary>
  74. /// 自定义绘制回调。在绘制Serie前调用。
  75. /// </summary>
  76. public Action<VertexHelper> onDraw { set { m_OnDrawBase = value; } }
  77. /// <summary>
  78. /// 自定义Serie绘制回调。在每个Serie绘制完前调用。
  79. /// </summary>
  80. public Action<VertexHelper, Serie> onDrawBeforeSerie { set { m_OnDrawSerieBefore = value; } }
  81. /// <summary>
  82. /// 自定义Serie绘制回调。在每个Serie绘制完后调用。
  83. /// </summary>
  84. public Action<VertexHelper, Serie> onDrawAfterSerie { set { m_OnDrawSerieAfter = value; } }
  85. /// <summary>
  86. /// 自定义Upper层绘制回调。在绘制Tooltip前调用。
  87. /// </summary>
  88. public Action<VertexHelper> onDrawUpper { set { m_OnDrawUpper = value; } }
  89. /// <summary>
  90. /// 自定义Top层绘制回调。在绘制Tooltip前调用。
  91. /// </summary>
  92. public Action<VertexHelper> onDrawTop { set { m_OnDrawTop = value; } }
  93. /// <summary>
  94. /// 自定义仪表盘指针绘制委托。
  95. /// </summary>
  96. public CustomDrawGaugePointerFunction customDrawGaugePointerFunction { set { m_CustomDrawGaugePointerFunction = value; } get { return m_CustomDrawGaugePointerFunction; } }
  97. /// <summary>
  98. /// the callback function of pointer click pie area.
  99. /// |点击饼图区域回调。参数:PointerEventData,SerieIndex,SerieDataIndex
  100. /// </summary>
  101. public Action<PointerEventData, int, int> onPointerClickPie { set { m_OnPointerClickPie = value; m_ForceOpenRaycastTarget = true; } get { return m_OnPointerClickPie; } }
  102. /// <summary>
  103. /// the callback function of pointer enter pie area.
  104. /// |鼠标进入和离开饼图区域回调,SerieDataIndex为-1时表示离开。参数:PointerEventData,SerieIndex,SerieDataIndex
  105. /// </summary>
  106. [Since("v3.3.0")]
  107. public Action<int, int> onPointerEnterPie { set { m_OnPointerEnterPie = value; m_ForceOpenRaycastTarget = true; } get { return m_OnPointerEnterPie; } }
  108. /// <summary>
  109. /// the callback function of click bar.
  110. /// |点击柱形图柱条回调。参数:eventData, dataIndex
  111. /// </summary>
  112. public Action<PointerEventData, int> onPointerClickBar { set { m_OnPointerClickBar = value; m_ForceOpenRaycastTarget = true; } get { return m_OnPointerClickBar; } }
  113. /// <summary>
  114. /// 坐标轴变更数据索引时回调。参数:axis, dataIndex/dataValue
  115. /// </summary>
  116. public Action<Axis, double> onAxisPointerValueChanged { set { m_OnAxisPointerValueChanged = value; } get { return m_OnAxisPointerValueChanged; } }
  117. /// <summary>
  118. /// the callback function of click legend.
  119. /// |点击图例按钮回调。参数:legendIndex, legendName, show
  120. /// </summary>
  121. public Action<Legend, int, string, bool> onLegendClick { set { m_OnLegendClick = value; } internal get { return m_OnLegendClick; } }
  122. /// <summary>
  123. /// the callback function of enter legend.
  124. /// |鼠标进入图例回调。参数:legendIndex, legendName
  125. /// </summary>
  126. public Action<Legend, int, string> onLegendEnter { set { m_OnLegendEnter = value; } internal get { return m_OnLegendEnter; } }
  127. /// <summary>
  128. /// the callback function of exit legend.
  129. /// |鼠标退出图例回调。参数:legendIndex, legendName
  130. /// </summary>
  131. public Action<Legend, int, string> onLegendExit { set { m_OnLegendExit = value; } internal get { return m_OnLegendExit; } }
  132. public void Init(bool defaultChart = true)
  133. {
  134. if (defaultChart)
  135. {
  136. OnInit();
  137. DefaultChart();
  138. }
  139. else
  140. {
  141. OnBeforeSerialize();
  142. }
  143. }
  144. /// <summary>
  145. /// Redraw chart in next frame.
  146. /// |在下一帧刷新整个图表。
  147. /// </summary>
  148. public void RefreshChart()
  149. {
  150. foreach (var serie in m_Series)
  151. serie.ResetInteract();
  152. m_RefreshChart = true;
  153. if (m_Painter) m_Painter.Refresh();
  154. foreach (var painter in m_PainterList) painter.Refresh();
  155. if (m_PainterUpper) m_PainterUpper.Refresh();
  156. if (m_PainterTop) m_PainterTop.Refresh();
  157. }
  158. public override void RefreshGraph()
  159. {
  160. RefreshChart();
  161. }
  162. /// <summary>
  163. /// Redraw chart serie in next frame.
  164. /// |在下一帧刷新图表的指定serie。
  165. /// </summary>
  166. public void RefreshChart(int serieIndex)
  167. {
  168. RefreshPainter(GetSerie(serieIndex));
  169. }
  170. /// <summary>
  171. /// Redraw chart serie in next frame.
  172. /// |在下一帧刷新图表的指定serie。
  173. /// </summary>
  174. public void RefreshChart(Serie serie)
  175. {
  176. if (serie == null) return;
  177. serie.ResetInteract();
  178. RefreshPainter(serie);
  179. }
  180. /// <summary>
  181. /// Clear all components and series data. Note: serie only empties the data and does not remove serie.
  182. /// |清空所有组件和Serie的数据。注意:Serie只是清空数据,不会移除Serie。
  183. /// </summary>
  184. public virtual void ClearData()
  185. {
  186. ClearSerieData();
  187. ClearComponentData();
  188. }
  189. [Since("v3.4.0")]
  190. /// <summary>
  191. /// Clear the data of all series.
  192. /// |清空所有serie的数据。
  193. /// </summary>
  194. public virtual void ClearSerieData()
  195. {
  196. foreach (var serie in m_Series)
  197. serie.ClearData();
  198. m_CheckAnimation = false;
  199. RefreshChart();
  200. }
  201. [Since("v3.4.0")]
  202. /// <summary>
  203. /// Clear the data of all components.
  204. /// |清空所有组件的数据。
  205. /// </summary>
  206. public virtual void ClearComponentData()
  207. {
  208. foreach (var component in m_Components)
  209. component.ClearData();
  210. m_CheckAnimation = false;
  211. RefreshChart();
  212. }
  213. /// <summary>
  214. /// Empty all component data and remove all series. Use the chart again and again to tell the truth.
  215. /// Note: The component only clears the data part, and the parameters are retained and not reset.
  216. /// |清空所有组件数据,并移除所有Serie。一般在图表重新初始化时使用。
  217. /// 注意:组件只清空数据部分,参数会保留不会被重置。
  218. /// </summary>
  219. public virtual void RemoveData()
  220. {
  221. foreach (var component in m_Components)
  222. component.ClearData();
  223. m_Series.Clear();
  224. m_SerieHandlers.Clear();
  225. m_CheckAnimation = false;
  226. RefreshChart();
  227. }
  228. /// <summary>
  229. /// Remove all of them Serie. This interface is used when Serie needs to be removed only, and RemoveData() is generally used in other cases.
  230. /// |移除所有的Serie。当确认只需要移除Serie时使用该接口,其他情况下一般用RemoveData()。
  231. /// </summary>
  232. [Since("v3.2.0")]
  233. public virtual void RemoveAllSerie()
  234. {
  235. m_Series.Clear();
  236. m_SerieHandlers.Clear();
  237. m_CheckAnimation = false;
  238. RefreshChart();
  239. }
  240. /// <summary>
  241. /// Remove legend and serie by name.
  242. /// |清除指定系列名称的数据。
  243. /// </summary>
  244. /// <param name="serieName">the name of serie</param>
  245. public virtual void RemoveData(string serieName)
  246. {
  247. RemoveSerie(serieName);
  248. foreach (var component in m_Components)
  249. {
  250. if (component is Legend)
  251. {
  252. var legend = component as Legend;
  253. legend.RemoveData(serieName);
  254. }
  255. }
  256. RefreshChart();
  257. }
  258. public virtual void UpdateLegendColor(string legendName, bool active)
  259. {
  260. var legendIndex = m_LegendRealShowName.IndexOf(legendName);
  261. if (legendIndex >= 0)
  262. {
  263. foreach (var component in m_Components)
  264. {
  265. if (component is Legend)
  266. {
  267. var legend = component as Legend;
  268. var iconColor = LegendHelper.GetIconColor(this, legend, legendIndex, legendName, active);
  269. var contentColor = LegendHelper.GetContentColor(this, legendIndex, legendName, legend, m_Theme, active);
  270. legend.UpdateButtonColor(legendName, iconColor);
  271. legend.UpdateContentColor(legendName, contentColor);
  272. }
  273. }
  274. }
  275. }
  276. /// <summary>
  277. /// Whether serie is activated.
  278. /// |获得指定图例名字的系列是否显示。
  279. /// </summary>
  280. /// <param name="legendName"></param>
  281. /// <returns></returns>
  282. public virtual bool IsActiveByLegend(string legendName)
  283. {
  284. foreach (var serie in m_Series)
  285. {
  286. if (serie.show && legendName.Equals(serie.serieName))
  287. {
  288. return true;
  289. }
  290. else
  291. {
  292. foreach (var serieData in serie.data)
  293. {
  294. if (serieData.show && legendName.Equals(serieData.name))
  295. {
  296. return true;
  297. }
  298. }
  299. }
  300. }
  301. return false;
  302. }
  303. /// <summary>
  304. /// Update chart theme.
  305. /// |切换内置主题。
  306. /// </summary>
  307. /// <param name="theme">theme</param>
  308. public bool UpdateTheme(ThemeType theme)
  309. {
  310. if (theme == ThemeType.Custom)
  311. {
  312. Debug.LogError("UpdateTheme: not support switch to Custom theme.");
  313. return false;
  314. }
  315. if (m_Theme.sharedTheme == null)
  316. m_Theme.sharedTheme = XCThemeMgr.GetTheme(ThemeType.Default);
  317. m_Theme.sharedTheme.CopyTheme(theme);
  318. return true;
  319. }
  320. /// <summary>
  321. /// Update chart theme info.
  322. /// |切换图表主题。
  323. /// </summary>
  324. /// <param name="theme">theme</param>
  325. public void UpdateTheme(Theme theme)
  326. {
  327. m_Theme.sharedTheme = theme;
  328. SetAllComponentDirty();
  329. #if UNITY_EDITOR
  330. UnityEditor.EditorUtility.SetDirty(this);
  331. #endif
  332. }
  333. /// <summary>
  334. /// Whether series animation enabel.
  335. /// |启用或关闭起始动画。
  336. /// </summary>
  337. /// <param name="flag"></param>
  338. public void AnimationEnable(bool flag)
  339. {
  340. foreach (var serie in m_Series) serie.AnimationEnable(flag);
  341. }
  342. /// <summary>
  343. /// fadeIn animation.
  344. /// |开始渐入动画。
  345. /// </summary>
  346. public void AnimationFadeIn(bool reset = true)
  347. {
  348. if (reset)
  349. AnimationReset();
  350. foreach (var serie in m_Series) serie.AnimationFadeIn();
  351. }
  352. /// <summary>
  353. /// fadeIn animation.
  354. /// |开始渐出动画。
  355. /// </summary>
  356. public void AnimationFadeOut()
  357. {
  358. foreach (var serie in m_Series) serie.AnimationFadeOut();
  359. }
  360. /// <summary>
  361. /// Pause animation.
  362. /// |暂停动画。
  363. /// </summary>
  364. public void AnimationPause()
  365. {
  366. foreach (var serie in m_Series) serie.AnimationPause();
  367. }
  368. /// <summary>
  369. /// Stop play animation.
  370. /// |继续动画。
  371. /// </summary>
  372. public void AnimationResume()
  373. {
  374. foreach (var serie in m_Series) serie.AnimationResume();
  375. }
  376. /// <summary>
  377. /// Reset animation.
  378. /// |重置动画。
  379. /// </summary>
  380. public void AnimationReset()
  381. {
  382. foreach (var serie in m_Series) serie.AnimationReset();
  383. }
  384. /// <summary>
  385. /// 点击图例按钮
  386. /// </summary>
  387. /// <param name="legendIndex">图例按钮索引</param>
  388. /// <param name="legendName">图例按钮名称</param>
  389. /// <param name="show">显示还是隐藏</param>
  390. public void ClickLegendButton(int legendIndex, string legendName, bool show)
  391. {
  392. OnLegendButtonClick(legendIndex, legendName, show);
  393. RefreshChart();
  394. }
  395. /// <summary>
  396. /// 坐标是否在图表范围内
  397. /// </summary>
  398. /// <param name="local"></param>
  399. /// <returns></returns>
  400. public bool IsInChart(Vector2 local)
  401. {
  402. return IsInChart(local.x, local.y);
  403. }
  404. public bool IsInChart(float x, float y)
  405. {
  406. if (x < m_ChartX || x > m_ChartX + m_ChartWidth ||
  407. y < m_ChartY || y > m_ChartY + m_ChartHeight)
  408. {
  409. return false;
  410. }
  411. return true;
  412. }
  413. public void ClampInChart(ref Vector3 pos)
  414. {
  415. if (!IsInChart(pos.x, pos.y))
  416. {
  417. if (pos.x < m_ChartX) pos.x = m_ChartX;
  418. if (pos.x > m_ChartX + m_ChartWidth) pos.x = m_ChartX + m_ChartWidth;
  419. if (pos.y < m_ChartY) pos.y = m_ChartY;
  420. if (pos.y > m_ChartY + m_ChartHeight) pos.y = m_ChartY + m_ChartHeight;
  421. }
  422. }
  423. public Vector3 ClampInGrid(GridCoord grid, Vector3 pos)
  424. {
  425. if (grid.Contains(pos)) return pos;
  426. else
  427. {
  428. // var pos = new Vector3(pos.x, pos.y);
  429. if (pos.x < grid.context.x) pos.x = grid.context.x;
  430. if (pos.x > grid.context.x + grid.context.width) pos.x = grid.context.x + grid.context.width;
  431. if (pos.y < grid.context.y) pos.y = grid.context.y;
  432. if (pos.y > grid.context.y + grid.context.height) pos.y = grid.context.y + grid.context.height;
  433. return pos;
  434. }
  435. }
  436. /// <summary>
  437. /// 转换X轴和Y轴的配置
  438. /// </summary>
  439. /// <param name="index">坐标轴索引,0或1</param>
  440. public void ConvertXYAxis(int index)
  441. {
  442. List<MainComponent> m_XAxes;
  443. List<MainComponent> m_YAxes;
  444. m_ComponentMaps.TryGetValue(typeof(XAxis), out m_XAxes);
  445. m_ComponentMaps.TryGetValue(typeof(YAxis), out m_YAxes);
  446. if (index >= 0 && index <= 1)
  447. {
  448. var xAxis = m_XAxes[index] as XAxis;
  449. var yAxis = m_YAxes[index] as YAxis;
  450. var tempX = xAxis.Clone();
  451. xAxis.Copy(yAxis);
  452. yAxis.Copy(tempX);
  453. xAxis.context.offset = 0;
  454. yAxis.context.offset = 0;
  455. xAxis.context.minValue = 0;
  456. xAxis.context.maxValue = 0;
  457. yAxis.context.minValue = 0;
  458. yAxis.context.maxValue = 0;
  459. RefreshChart();
  460. }
  461. }
  462. /// <summary>
  463. /// 在下一帧刷新DataZoom
  464. /// </summary>
  465. public void RefreshDataZoom()
  466. {
  467. foreach (var handler in m_ComponentHandlers)
  468. {
  469. if (handler is DataZoomHandler)
  470. {
  471. (handler as DataZoomHandler).RefreshDataZoomLabel();
  472. }
  473. }
  474. }
  475. /// <summary>
  476. /// 设置可缓存的最大数据量。当数据量超过该值时,会自动删除第一个值再加入最新值。
  477. /// </summary>
  478. public void SetMaxCache(int maxCache)
  479. {
  480. foreach (var serie in m_Series)
  481. serie.maxCache = maxCache;
  482. foreach (var component in m_Components)
  483. {
  484. if (component is Axis)
  485. {
  486. (component as Axis).maxCache = maxCache;
  487. }
  488. }
  489. }
  490. public Vector3 GetTitlePosition(Title title)
  491. {
  492. return chartPosition + title.location.GetPosition(chartWidth, chartHeight);
  493. }
  494. public int GetLegendRealShowNameIndex(string name)
  495. {
  496. return m_LegendRealShowName.IndexOf(name);
  497. }
  498. public Color32 GetLegendRealShowNameColor(string name)
  499. {
  500. var index = GetLegendRealShowNameIndex(name);
  501. return theme.GetColor(index);
  502. }
  503. /// <summary>
  504. /// 设置Base Painter的材质球
  505. /// </summary>
  506. /// <param name="material"></param>
  507. public void SetBasePainterMaterial(Material material)
  508. {
  509. settings.basePainterMaterial = material;
  510. if (m_Painter != null)
  511. {
  512. m_Painter.material = material;
  513. }
  514. }
  515. /// <summary>
  516. /// 设置Serie Painter的材质球
  517. /// </summary>
  518. /// <param name="material"></param>
  519. public void SetSeriePainterMaterial(Material material)
  520. {
  521. settings.basePainterMaterial = material;
  522. if (m_PainterList != null)
  523. {
  524. foreach (var painter in m_PainterList)
  525. painter.material = material;
  526. }
  527. }
  528. /// <summary>
  529. /// 设置Upper Painter的材质球
  530. /// </summary>
  531. /// <param name="material"></param>
  532. public void SetUpperPainterMaterial(Material material)
  533. {
  534. settings.upperPainterMaterial = material;
  535. if (m_PainterUpper != null)
  536. {
  537. m_PainterUpper.material = material;
  538. }
  539. }
  540. /// <summary>
  541. /// 设置Top Painter的材质球
  542. /// </summary>
  543. /// <param name="material"></param>
  544. public void SetTopPainterMaterial(Material material)
  545. {
  546. settings.topPainterMaterial = material;
  547. if (m_PainterTop != null)
  548. {
  549. m_PainterTop.material = material;
  550. }
  551. }
  552. public Color32 GetChartBackgroundColor()
  553. {
  554. var background = GetChartComponent<Background>();
  555. return theme.GetBackgroundColor(background);
  556. }
  557. [Since("v3.4.0")]
  558. /// <summary>
  559. /// 获得Serie的标识颜色。
  560. /// </summary>
  561. /// <param name="serie"></param>
  562. /// <param name="serieData"></param>
  563. /// <returns></returns>
  564. public Color32 GetMarkColor(Serie serie, SerieData serieData)
  565. {
  566. var itemStyle = SerieHelper.GetItemStyle(serie, serieData);
  567. if (ChartHelper.IsClearColor(itemStyle.markColor))
  568. {
  569. return GetItemColor(serie, serieData);
  570. }
  571. else
  572. {
  573. return itemStyle.markColor;
  574. }
  575. }
  576. public Color32 GetItemColor(Serie serie, SerieData serieData)
  577. {
  578. Color32 color, toColor;
  579. SerieHelper.GetItemColor(out color, out toColor, serie, serieData, m_Theme);
  580. return color;
  581. }
  582. public Color32 GetItemColor(Serie serie, SerieData serieData, int colorIndex)
  583. {
  584. Color32 color, toColor;
  585. SerieHelper.GetItemColor(out color, out toColor, serie, serieData, m_Theme, colorIndex);
  586. return color;
  587. }
  588. public Color32 GetItemColor(Serie serie)
  589. {
  590. Color32 color, toColor;
  591. SerieHelper.GetItemColor(out color, out toColor, serie, null, m_Theme);
  592. return color;
  593. }
  594. }
  595. }