BaseChart.API.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. /// <summary>
  72. /// The callback function of chart init.
  73. /// ||图表的初始化完成回调。
  74. /// </summary>
  75. public Action onInit { set { m_OnInit = value; } }
  76. /// <summary>
  77. /// The callback function of chart update.
  78. /// ||图表的Update回调。
  79. /// </summary>
  80. public Action onUpdate { set { m_OnUpdate = value; } }
  81. /// <summary>
  82. /// 自定义绘制回调。在绘制Serie前调用。
  83. /// </summary>
  84. public Action<VertexHelper> onDraw { set { m_OnDrawBase = value; } }
  85. /// <summary>
  86. /// 自定义Serie绘制回调。在每个Serie绘制完前调用。
  87. /// </summary>
  88. public Action<VertexHelper, Serie> onDrawBeforeSerie { set { m_OnDrawSerieBefore = value; } }
  89. /// <summary>
  90. /// 自定义Serie绘制回调。在每个Serie绘制完后调用。
  91. /// </summary>
  92. public Action<VertexHelper, Serie> onDrawAfterSerie { set { m_OnDrawSerieAfter = value; } }
  93. /// <summary>
  94. /// 自定义Upper层绘制回调。在绘制Tooltip前调用。
  95. /// </summary>
  96. public Action<VertexHelper> onDrawUpper { set { m_OnDrawUpper = value; } }
  97. /// <summary>
  98. /// 自定义Top层绘制回调。在绘制Tooltip前调用。
  99. /// </summary>
  100. public Action<VertexHelper> onDrawTop { set { m_OnDrawTop = value; } }
  101. /// <summary>
  102. /// 自定义仪表盘指针绘制委托。
  103. /// </summary>
  104. public CustomDrawGaugePointerFunction customDrawGaugePointerFunction { set { m_CustomDrawGaugePointerFunction = value; } get { return m_CustomDrawGaugePointerFunction; } }
  105. /// <summary>
  106. /// the callback function of pointer click serie.
  107. /// ||鼠标点击Serie回调。
  108. /// </summary>
  109. [Since("v3.6.0")]
  110. public Action<SerieEventData> onSerieClick { set { m_OnSerieClick = value; m_ForceOpenRaycastTarget = true; } get { return m_OnSerieClick; } }
  111. /// <summary>
  112. /// the callback function of pointer down serie.
  113. /// ||鼠标按下Serie回调。
  114. /// </summary>
  115. [Since("v3.6.0")]
  116. public Action<SerieEventData> onSerieDown { set { m_OnSerieDown = value; m_ForceOpenRaycastTarget = true; } get { return m_OnSerieDown; } }
  117. /// <summary>
  118. /// the callback function of pointer enter serie.
  119. /// ||鼠标进入Serie回调。
  120. /// </summary>
  121. [Since("v3.6.0")]
  122. public Action<SerieEventData> onSerieEnter { set { m_OnSerieEnter = value; m_ForceOpenRaycastTarget = true; } get { return m_OnSerieEnter; } }
  123. /// <summary>
  124. /// the callback function of pointer exit serie.
  125. /// ||鼠标离开Serie回调。
  126. /// </summary>
  127. [Since("v3.6.0")]
  128. public Action<SerieEventData> onSerieExit { set { m_OnSerieExit = value; m_ForceOpenRaycastTarget = true; } get { return m_OnSerieExit; } }
  129. /// <summary>
  130. /// the callback function of pointer click pie area.
  131. /// ||点击饼图区域回调。参数:PointerEventData,SerieIndex,SerieDataIndex
  132. /// </summary>
  133. [Obsolete("Use \"onSerieClick\" instead", true)]
  134. public Action<PointerEventData, int, int> onPointerClickPie { get; set; }
  135. /// <summary>
  136. /// the callback function of pointer enter pie area.
  137. /// ||鼠标进入和离开饼图区域回调,SerieDataIndex为-1时表示离开。参数:PointerEventData,SerieIndex,SerieDataIndex
  138. /// </summary>
  139. [Since("v3.3.0")]
  140. [Obsolete("Use \"onSerieEnter\" instead", true)]
  141. public Action<int, int> onPointerEnterPie { set { m_OnPointerEnterPie = value; m_ForceOpenRaycastTarget = true; } get { return m_OnPointerEnterPie; } }
  142. /// <summary>
  143. /// the callback function of click bar.
  144. /// ||点击柱形图柱条回调。参数:eventData, dataIndex
  145. /// </summary>
  146. [Obsolete("Use \"onSerieClick\" instead", true)]
  147. public Action<PointerEventData, int> onPointerClickBar { get; set; }
  148. /// <summary>
  149. /// 坐标轴变更数据索引时回调。参数:axis, dataIndex/dataValue
  150. /// </summary>
  151. public Action<Axis, double> onAxisPointerValueChanged { set { m_OnAxisPointerValueChanged = value; } get { return m_OnAxisPointerValueChanged; } }
  152. /// <summary>
  153. /// the callback function of click legend.
  154. /// ||点击图例按钮回调。参数:legendIndex, legendName, show
  155. /// </summary>
  156. public Action<Legend, int, string, bool> onLegendClick { set { m_OnLegendClick = value; } internal get { return m_OnLegendClick; } }
  157. /// <summary>
  158. /// the callback function of enter legend.
  159. /// ||鼠标进入图例回调。参数:legendIndex, legendName
  160. /// </summary>
  161. public Action<Legend, int, string> onLegendEnter { set { m_OnLegendEnter = value; } internal get { return m_OnLegendEnter; } }
  162. /// <summary>
  163. /// the callback function of exit legend.
  164. /// ||鼠标退出图例回调。参数:legendIndex, legendName
  165. /// </summary>
  166. public Action<Legend, int, string> onLegendExit { set { m_OnLegendExit = value; } internal get { return m_OnLegendExit; } }
  167. public void Init(bool defaultChart = true)
  168. {
  169. if (defaultChart)
  170. {
  171. OnInit();
  172. DefaultChart();
  173. }
  174. else
  175. {
  176. OnBeforeSerialize();
  177. }
  178. }
  179. /// <summary>
  180. /// Redraw chart in next frame.
  181. /// ||在下一帧刷新整个图表。
  182. /// </summary>
  183. public void RefreshChart()
  184. {
  185. m_RefreshChart = true;
  186. if (m_Painter) m_Painter.Refresh();
  187. foreach (var painter in m_PainterList) painter.Refresh();
  188. if (m_PainterUpper) m_PainterUpper.Refresh();
  189. if (m_PainterTop) m_PainterTop.Refresh();
  190. }
  191. public override void RefreshGraph()
  192. {
  193. RefreshChart();
  194. }
  195. /// <summary>
  196. /// Redraw chart serie in next frame.
  197. /// ||在下一帧刷新图表的指定serie。
  198. /// </summary>
  199. public void RefreshChart(int serieIndex)
  200. {
  201. RefreshPainter(GetSerie(serieIndex));
  202. }
  203. /// <summary>
  204. /// Redraw chart serie in next frame.
  205. /// ||在下一帧刷新图表的指定serie。
  206. /// </summary>
  207. public void RefreshChart(Serie serie)
  208. {
  209. if (serie == null) return;
  210. // serie.ResetInteract();
  211. RefreshPainter(serie);
  212. }
  213. /// <summary>
  214. /// Clear all components and series data. Note: serie only empties the data and does not remove serie.
  215. /// ||清空所有组件和Serie的数据。注意:Serie只是清空数据,不会移除Serie。
  216. /// </summary>
  217. public virtual void ClearData()
  218. {
  219. ClearSerieData();
  220. ClearSerieLinks();
  221. ClearComponentData();
  222. }
  223. /// <summary>
  224. /// Clear the data of all series.
  225. /// ||清空所有serie的数据。
  226. /// </summary>
  227. [Since("v3.4.0")]
  228. public virtual void ClearSerieData()
  229. {
  230. foreach (var serie in m_Series)
  231. serie.ClearData();
  232. m_CheckAnimation = false;
  233. RefreshChart();
  234. }
  235. /// <summary>
  236. /// Clear the link data of all series.
  237. /// ||清空所有serie的link数据。
  238. /// </summary>
  239. [Since("v3.10.0")]
  240. public virtual void ClearSerieLinks()
  241. {
  242. foreach (var serie in m_Series)
  243. serie.ClearLinks();
  244. m_CheckAnimation = false;
  245. RefreshChart();
  246. }
  247. /// <summary>
  248. /// Clear the data of all components.
  249. /// ||清空所有组件的数据。
  250. /// </summary>
  251. [Since("v3.4.0")]
  252. public virtual void ClearComponentData()
  253. {
  254. foreach (var component in m_Components)
  255. component.ClearData();
  256. m_CheckAnimation = false;
  257. RefreshChart();
  258. }
  259. /// <summary>
  260. /// Empty all component data and remove all series. Use the chart again and again to tell the truth.
  261. /// Note: The component only clears the data part, and the parameters are retained and not reset.
  262. /// ||清空所有组件数据,并移除所有Serie。一般在图表重新初始化时使用。
  263. /// 注意:组件只清空数据部分,参数会保留不会被重置。
  264. /// </summary>
  265. public virtual void RemoveData()
  266. {
  267. foreach (var component in m_Components)
  268. component.ClearData();
  269. m_Series.Clear();
  270. m_SerieHandlers.Clear();
  271. m_CheckAnimation = false;
  272. RefreshChart();
  273. }
  274. /// <summary>
  275. /// Remove all of them Serie. This interface is used when Serie needs to be removed only, and RemoveData() is generally used in other cases.
  276. /// ||移除所有的Serie。当确认只需要移除Serie时使用该接口,其他情况下一般用RemoveData()。
  277. /// </summary>
  278. [Since("v3.2.0")]
  279. public virtual void RemoveAllSerie()
  280. {
  281. m_Series.Clear();
  282. m_SerieHandlers.Clear();
  283. m_CheckAnimation = false;
  284. RefreshChart();
  285. }
  286. /// <summary>
  287. /// Remove legend and serie by name.
  288. /// ||清除指定系列名称的数据。
  289. /// </summary>
  290. /// <param name="serieName">the name of serie</param>
  291. public virtual void RemoveData(string serieName)
  292. {
  293. RemoveSerie(serieName);
  294. foreach (var component in m_Components)
  295. {
  296. if (component is Legend)
  297. {
  298. var legend = component as Legend;
  299. legend.RemoveData(serieName);
  300. }
  301. }
  302. RefreshChart();
  303. }
  304. public virtual void UpdateLegendColor(string legendName, bool active)
  305. {
  306. var legendIndex = m_LegendRealShowName.IndexOf(legendName);
  307. if (legendIndex >= 0)
  308. {
  309. foreach (var component in m_Components)
  310. {
  311. if (component is Legend)
  312. {
  313. var legend = component as Legend;
  314. var iconColor = LegendHelper.GetIconColor(this, legend, legendIndex, legendName, active);
  315. var contentColor = LegendHelper.GetContentColor(this, legendIndex, legendName, legend, m_Theme, active);
  316. legend.UpdateButtonColor(legendName, iconColor);
  317. legend.UpdateContentColor(legendName, contentColor);
  318. }
  319. }
  320. }
  321. }
  322. /// <summary>
  323. /// Whether serie is activated.
  324. /// ||获得指定图例名字的系列是否显示。
  325. /// </summary>
  326. /// <param name="legendName"></param>
  327. /// <returns></returns>
  328. public virtual bool IsActiveByLegend(string legendName)
  329. {
  330. foreach (var serie in m_Series)
  331. {
  332. if (serie.show && legendName.Equals(serie.serieName))
  333. {
  334. return true;
  335. }
  336. else
  337. {
  338. foreach (var serieData in serie.data)
  339. {
  340. if (serieData.show && legendName.Equals(serieData.name))
  341. {
  342. return true;
  343. }
  344. }
  345. }
  346. }
  347. return false;
  348. }
  349. /// <summary>
  350. /// Update chart theme.
  351. /// ||切换内置主题。
  352. /// </summary>
  353. /// <param name="theme">theme</param>
  354. public bool UpdateTheme(ThemeType theme)
  355. {
  356. if (theme == ThemeType.Custom)
  357. {
  358. Debug.LogError("UpdateTheme: not support switch to Custom theme.");
  359. return false;
  360. }
  361. if (m_Theme.sharedTheme == null)
  362. m_Theme.sharedTheme = XCThemeMgr.GetTheme(ThemeType.Default);
  363. m_Theme.sharedTheme.CopyTheme(theme);
  364. return true;
  365. }
  366. /// <summary>
  367. /// Update chart theme info.
  368. /// ||切换图表主题。
  369. /// </summary>
  370. /// <param name="theme">theme</param>
  371. public void UpdateTheme(Theme theme)
  372. {
  373. m_Theme.sharedTheme = theme;
  374. SetAllComponentDirty();
  375. #if UNITY_EDITOR
  376. UnityEditor.EditorUtility.SetDirty(this);
  377. #endif
  378. }
  379. /// <summary>
  380. /// Whether enable serie animations.
  381. /// ||是否启用Serie动画。
  382. /// </summary>
  383. /// <param name="flag"></param>
  384. public void AnimationEnable(bool flag)
  385. {
  386. foreach (var serie in m_Series) serie.AnimationEnable(flag);
  387. }
  388. /// <summary>
  389. /// Start all serie fadein animations.
  390. /// ||开始所有Serie的渐入动画。
  391. /// </summary>
  392. /// <param name="reset">reset animation</param>
  393. public void AnimationFadeIn(bool reset = true)
  394. {
  395. if (reset) AnimationReset();
  396. foreach (var serie in m_Series) serie.AnimationFadeIn();
  397. }
  398. /// <summary>
  399. /// Start all serie fadeout animations.
  400. /// ||开始所有Serie的渐出动画。
  401. /// </summary>
  402. public void AnimationFadeOut()
  403. {
  404. foreach (var serie in m_Series) serie.AnimationFadeOut();
  405. }
  406. /// <summary>
  407. /// Pause all animations.
  408. /// ||暂停所有Serie的动画。
  409. /// </summary>
  410. public void AnimationPause()
  411. {
  412. foreach (var serie in m_Series) serie.AnimationPause();
  413. }
  414. /// <summary>
  415. /// Resume all animations.
  416. /// ||继续所有Serie的动画。
  417. /// </summary>
  418. public void AnimationResume()
  419. {
  420. foreach (var serie in m_Series) serie.AnimationResume();
  421. }
  422. /// <summary>
  423. /// Reset all animations.
  424. /// ||重置所有Serie的动画。
  425. /// </summary>
  426. public void AnimationReset()
  427. {
  428. foreach (var serie in m_Series) serie.AnimationReset();
  429. }
  430. /// <summary>
  431. /// 点击图例按钮
  432. /// </summary>
  433. /// <param name="legendIndex">图例按钮索引</param>
  434. /// <param name="legendName">图例按钮名称</param>
  435. /// <param name="show">显示还是隐藏</param>
  436. public void ClickLegendButton(int legendIndex, string legendName, bool show)
  437. {
  438. OnLegendButtonClick(legendIndex, legendName, show);
  439. RefreshChart();
  440. }
  441. /// <summary>
  442. /// 坐标是否在图表范围内
  443. /// </summary>
  444. /// <param name="local"></param>
  445. /// <returns></returns>
  446. public bool IsInChart(Vector2 local)
  447. {
  448. return IsInChart(local.x, local.y);
  449. }
  450. public bool IsInChart(float x, float y)
  451. {
  452. if (x < m_ChartX || x > m_ChartX + m_ChartWidth ||
  453. y < m_ChartY || y > m_ChartY + m_ChartHeight)
  454. {
  455. return false;
  456. }
  457. return true;
  458. }
  459. public void ClampInChart(ref Vector3 pos)
  460. {
  461. if (!IsInChart(pos.x, pos.y))
  462. {
  463. if (pos.x < m_ChartX) pos.x = m_ChartX;
  464. if (pos.x > m_ChartX + m_ChartWidth) pos.x = m_ChartX + m_ChartWidth;
  465. if (pos.y < m_ChartY) pos.y = m_ChartY;
  466. if (pos.y > m_ChartY + m_ChartHeight) pos.y = m_ChartY + m_ChartHeight;
  467. }
  468. }
  469. public Vector3 ClampInGrid(GridCoord grid, Vector3 pos)
  470. {
  471. if (grid.Contains(pos)) return pos;
  472. else
  473. {
  474. // var pos = new Vector3(pos.x, pos.y);
  475. if (pos.x < grid.context.x) pos.x = grid.context.x;
  476. if (pos.x > grid.context.x + grid.context.width) pos.x = grid.context.x + grid.context.width;
  477. if (pos.y < grid.context.y) pos.y = grid.context.y;
  478. if (pos.y > grid.context.y + grid.context.height) pos.y = grid.context.y + grid.context.height;
  479. return pos;
  480. }
  481. }
  482. /// <summary>
  483. /// 转换X轴和Y轴的配置
  484. /// </summary>
  485. /// <param name="index">坐标轴索引,0或1</param>
  486. public void ConvertXYAxis(int index)
  487. {
  488. List<MainComponent> m_XAxes;
  489. List<MainComponent> m_YAxes;
  490. m_ComponentMaps.TryGetValue(typeof(XAxis), out m_XAxes);
  491. m_ComponentMaps.TryGetValue(typeof(YAxis), out m_YAxes);
  492. if (index >= 0 && index <= 1)
  493. {
  494. var xAxis = m_XAxes[index] as XAxis;
  495. var yAxis = m_YAxes[index] as YAxis;
  496. var tempX = xAxis.Clone();
  497. xAxis.Copy(yAxis);
  498. yAxis.Copy(tempX);
  499. xAxis.context.offset = 0;
  500. yAxis.context.offset = 0;
  501. xAxis.context.minValue = 0;
  502. xAxis.context.maxValue = 0;
  503. yAxis.context.minValue = 0;
  504. yAxis.context.maxValue = 0;
  505. RefreshChart();
  506. }
  507. }
  508. /// <summary>
  509. /// 在下一帧刷新DataZoom
  510. /// </summary>
  511. public void RefreshDataZoom()
  512. {
  513. foreach (var handler in m_ComponentHandlers)
  514. {
  515. if (handler is DataZoomHandler)
  516. {
  517. (handler as DataZoomHandler).RefreshDataZoomLabel();
  518. }
  519. }
  520. }
  521. /// <summary>
  522. /// 设置可缓存的最大数据量。当数据量超过该值时,会自动删除第一个值再加入最新值。
  523. /// </summary>
  524. public void SetMaxCache(int maxCache)
  525. {
  526. foreach (var serie in m_Series)
  527. serie.maxCache = maxCache;
  528. foreach (var component in m_Components)
  529. {
  530. if (component is Axis)
  531. {
  532. (component as Axis).maxCache = maxCache;
  533. }
  534. }
  535. }
  536. public Vector3 GetTitlePosition(Title title)
  537. {
  538. return chartPosition + title.location.GetPosition(chartWidth, chartHeight);
  539. }
  540. public int GetLegendRealShowNameIndex(string name)
  541. {
  542. return m_LegendRealShowName.IndexOf(name);
  543. }
  544. public Color32 GetLegendRealShowNameColor(string name)
  545. {
  546. var index = GetLegendRealShowNameIndex(name);
  547. return theme.GetColor(index);
  548. }
  549. /// <summary>
  550. /// 设置Base Painter的材质球
  551. /// </summary>
  552. /// <param name="material"></param>
  553. public void SetBasePainterMaterial(Material material)
  554. {
  555. settings.basePainterMaterial = material;
  556. if (m_Painter != null)
  557. {
  558. m_Painter.material = material;
  559. }
  560. }
  561. /// <summary>
  562. /// 设置Serie Painter的材质球
  563. /// </summary>
  564. /// <param name="material"></param>
  565. public void SetSeriePainterMaterial(Material material)
  566. {
  567. settings.basePainterMaterial = material;
  568. if (m_PainterList != null)
  569. {
  570. foreach (var painter in m_PainterList)
  571. painter.material = material;
  572. }
  573. }
  574. /// <summary>
  575. /// 设置Upper Painter的材质球
  576. /// </summary>
  577. /// <param name="material"></param>
  578. public void SetUpperPainterMaterial(Material material)
  579. {
  580. settings.upperPainterMaterial = material;
  581. if (m_PainterUpper != null)
  582. {
  583. m_PainterUpper.material = material;
  584. }
  585. }
  586. /// <summary>
  587. /// 设置Top Painter的材质球
  588. /// </summary>
  589. /// <param name="material"></param>
  590. public void SetTopPainterMaterial(Material material)
  591. {
  592. settings.topPainterMaterial = material;
  593. if (m_PainterTop != null)
  594. {
  595. m_PainterTop.material = material;
  596. }
  597. }
  598. public Color32 GetChartBackgroundColor()
  599. {
  600. var background = GetChartComponent<Background>();
  601. return theme.GetBackgroundColor(background);
  602. }
  603. /// <summary>
  604. /// 获得Serie的标识颜色。
  605. /// </summary>
  606. /// <param name="serie"></param>
  607. /// <param name="serieData"></param>
  608. /// <returns></returns>
  609. [Since("v3.4.0")]
  610. public Color32 GetMarkColor(Serie serie, SerieData serieData)
  611. {
  612. var itemStyle = SerieHelper.GetItemStyle(serie, serieData);
  613. if (ChartHelper.IsClearColor(itemStyle.markColor))
  614. {
  615. return GetItemColor(serie, serieData);
  616. }
  617. else
  618. {
  619. return itemStyle.markColor;
  620. }
  621. }
  622. public Color32 GetItemColor(Serie serie, SerieData serieData)
  623. {
  624. Color32 color, toColor;
  625. SerieHelper.GetItemColor(out color, out toColor, serie, serieData, m_Theme);
  626. return color;
  627. }
  628. public Color32 GetItemColor(Serie serie, SerieData serieData, int colorIndex)
  629. {
  630. Color32 color, toColor;
  631. SerieHelper.GetItemColor(out color, out toColor, serie, serieData, m_Theme, colorIndex);
  632. return color;
  633. }
  634. public Color32 GetItemColor(Serie serie)
  635. {
  636. Color32 color, toColor;
  637. SerieHelper.GetItemColor(out color, out toColor, serie, null, m_Theme);
  638. return color;
  639. }
  640. /// <summary>
  641. /// trigger tooltip by data index.
  642. /// ||尝试触发指定数据项的Tooltip.
  643. /// </summary>
  644. /// <param name="dataIndex">数据项索引</param>
  645. /// <param name="serieIndex">Serie索引,默认为第0个Serie</param>
  646. /// <returns></returns>
  647. [Since("v3.7.0")]
  648. public bool TriggerTooltip(int dataIndex, int serieIndex = 0)
  649. {
  650. var serie = GetSerie(serieIndex);
  651. if (serie == null) return false;
  652. var dataPoints = serie.context.dataPoints;
  653. var dataPoint = Vector3.zero;
  654. if (dataPoints.Count == 0)
  655. {
  656. if (serie.dataCount == 0) return false;
  657. dataIndex = dataIndex % serie.dataCount;
  658. var serieData = serie.GetSerieData(dataIndex);
  659. if (serieData == null) return false;
  660. dataPoint = serie.GetSerieData(dataIndex).context.position;
  661. }
  662. else
  663. {
  664. dataIndex = dataIndex % dataPoints.Count;
  665. dataPoint = dataPoints[dataIndex];
  666. }
  667. return TriggerTooltip(dataPoint);
  668. }
  669. /// <summary>
  670. /// trigger tooltip by chart local position.
  671. /// ||在指定的位置尝试触发Tooltip.
  672. /// </summary>
  673. /// <param name="localPosition"></param>
  674. /// <returns></returns>
  675. [Since("v3.7.0")]
  676. public bool TriggerTooltip(Vector3 localPosition)
  677. {
  678. var screenPoint = LocalPointToScreenPoint(localPosition);
  679. var eventData = new PointerEventData(EventSystem.current);
  680. eventData.position = screenPoint;
  681. OnPointerEnter(eventData);
  682. return true;
  683. }
  684. /// <summary>
  685. /// cancel tooltip.
  686. /// ||取消Tooltip.
  687. /// </summary>
  688. [Since("v3.7.0")]
  689. public void CancelTooltip()
  690. {
  691. m_PointerEventData = null;
  692. var tooltip = GetChartComponent<Tooltip>();
  693. if (tooltip != null)
  694. {
  695. tooltip.SetActive(false);
  696. }
  697. }
  698. /// <summary>
  699. /// reset chart status. When some parameters are set, due to the animation effect, the chart status may not be correct.
  700. /// ||重置图表状态。当设置某些参数后,由于动画影响,可能导致图表状态不正确,此时可以调用该接口重置图表状态。
  701. /// </summary>
  702. [Since("v3.10.0")]
  703. public void ResetChartStatus()
  704. {
  705. foreach (var handler in m_SerieHandlers) handler.ForceUpdateSerieContext();
  706. }
  707. }
  708. }