No Description
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.

TitleHandler.cs 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using UnityEngine;
  2. namespace XCharts.Runtime
  3. {
  4. [UnityEngine.Scripting.Preserve]
  5. internal sealed class TitleHander : MainComponentHandler<Title>
  6. {
  7. private static readonly string s_TitleObjectName = "title";
  8. private static readonly string s_SubTitleObjectName = "title_sub";
  9. private ChartLabel m_LabelObject;
  10. private ChartLabel m_SubLabelObject;
  11. public override void InitComponent()
  12. {
  13. var title = component;
  14. title.painter = null;
  15. title.refreshComponent = delegate()
  16. {
  17. title.OnChanged();
  18. var anchorMin = title.location.runtimeAnchorMin;
  19. var anchorMax = title.location.runtimeAnchorMax;
  20. var pivot = title.location.runtimePivot;
  21. var objName = ChartCached.GetComponentObjectName(title);
  22. var titleObject = ChartHelper.AddObject(objName, chart.transform, anchorMin, anchorMax,
  23. pivot, chart.chartSizeDelta);
  24. title.gameObject = titleObject;
  25. title.gameObject.transform.SetSiblingIndex(chart.m_PainterUpper.transform.GetSiblingIndex() + 1);
  26. anchorMin = title.location.runtimeAnchorMin;
  27. anchorMax = title.location.runtimeAnchorMax;
  28. pivot = title.location.runtimePivot;
  29. var fontSize = title.labelStyle.textStyle.GetFontSize(chart.theme.title);
  30. ChartHelper.UpdateRectTransform(titleObject, anchorMin, anchorMax, pivot, new Vector2(chart.chartWidth, chart.chartHeight));
  31. var titlePosition = chart.GetTitlePosition(title);
  32. var subTitlePosition = -new Vector3(0, fontSize + title.itemGap, 0);
  33. titleObject.transform.localPosition = titlePosition;
  34. titleObject.hideFlags = chart.chartHideFlags;
  35. ChartHelper.HideAllObject(titleObject);
  36. m_LabelObject = ChartHelper.AddChartLabel(s_TitleObjectName, titleObject.transform, title.labelStyle, chart.theme.title,
  37. GetTitleText(title), Color.clear, title.location.runtimeTextAlignment);
  38. m_LabelObject.SetActive(title.show && title.labelStyle.show);
  39. m_SubLabelObject = ChartHelper.AddChartLabel(s_SubTitleObjectName, titleObject.transform, title.subLabelStyle, chart.theme.subTitle,
  40. GetSubTitleText(title), Color.clear, title.location.runtimeTextAlignment);
  41. m_SubLabelObject.SetActive(title.show && title.subLabelStyle.show);
  42. m_SubLabelObject.transform.localPosition = subTitlePosition + title.subLabelStyle.offset;
  43. };
  44. title.refreshComponent();
  45. }
  46. public override void OnSerieDataUpdate(int serieIndex)
  47. {
  48. if (m_LabelObject != null && FormatterHelper.NeedFormat(component.text))
  49. m_LabelObject.SetText(GetTitleText(component));
  50. if (m_SubLabelObject != null && FormatterHelper.NeedFormat(component.subText))
  51. m_SubLabelObject.SetText(GetSubTitleText(component));
  52. }
  53. private string GetTitleText(Title title)
  54. {
  55. if (FormatterHelper.NeedFormat(title.text))
  56. {
  57. var content = title.text;
  58. FormatterHelper.ReplaceContent(ref content, 0, title.labelStyle.numericFormatter, null, chart);
  59. return content;
  60. }
  61. else
  62. {
  63. return title.text;
  64. }
  65. }
  66. private string GetSubTitleText(Title title)
  67. {
  68. if (FormatterHelper.NeedFormat(title.subText))
  69. {
  70. var content = title.subText;
  71. FormatterHelper.ReplaceContent(ref content, 0, title.subLabelStyle.numericFormatter, null, chart);
  72. return content;
  73. }
  74. else
  75. {
  76. return title.subText;
  77. }
  78. }
  79. }
  80. }