설명 없음
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.

TooltipHelper.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using UnityEngine;
  3. namespace XCharts.Runtime
  4. {
  5. public static class TooltipHelper
  6. {
  7. internal static void ResetTooltipParamsByItemFormatter(Tooltip tooltip, BaseChart chart)
  8. {
  9. if (!string.IsNullOrEmpty(tooltip.titleFormatter))
  10. {
  11. if (IsIgnoreFormatter(tooltip.titleFormatter))
  12. {
  13. tooltip.context.data.title = string.Empty;
  14. }
  15. else
  16. {
  17. tooltip.context.data.title = tooltip.titleFormatter;
  18. FormatterHelper.ReplaceContent(ref tooltip.context.data.title, 0,
  19. tooltip.numericFormatter, null, chart);
  20. }
  21. }
  22. for (int i = tooltip.context.data.param.Count - 1; i >= 0; i--)
  23. {
  24. var param = tooltip.context.data.param[i];
  25. if (IsIgnoreFormatter(param.itemFormatter))
  26. {
  27. tooltip.context.data.param.RemoveAt(i);
  28. }
  29. }
  30. foreach (var param in tooltip.context.data.param)
  31. {
  32. if (!string.IsNullOrEmpty(param.itemFormatter))
  33. {
  34. param.columns.Clear();
  35. var content = param.itemFormatter;
  36. FormatterHelper.ReplaceSerieLabelContent(ref content,
  37. param.numericFormatter,
  38. param.dataCount,
  39. param.value,
  40. param.total,
  41. param.serieName,
  42. param.category,
  43. param.serieData.name,
  44. param.color,
  45. param.serieData);
  46. foreach (var item in content.Split('|'))
  47. {
  48. param.columns.Add(item);
  49. }
  50. }
  51. }
  52. }
  53. public static bool IsIgnoreFormatter(string itemFormatter)
  54. {
  55. return "-".Equals(itemFormatter) ||"{i}".Equals(itemFormatter, StringComparison.CurrentCultureIgnoreCase);
  56. }
  57. public static void LimitInRect(Tooltip tooltip, Rect chartRect)
  58. {
  59. if (tooltip.view == null)
  60. return;
  61. var pos = tooltip.view.GetTargetPos();
  62. if (pos.x + tooltip.context.width > chartRect.x + chartRect.width)
  63. {
  64. pos.x = tooltip.context.pointer.x - tooltip.context.width - tooltip.offset.x;
  65. }
  66. else if (pos.x < chartRect.x)
  67. {
  68. pos.x = tooltip.context.pointer.x - tooltip.context.width + Mathf.Abs(tooltip.offset.x);
  69. }
  70. if (pos.y - tooltip.context.height < chartRect.y)
  71. {
  72. pos.y = chartRect.y + tooltip.context.height;
  73. }
  74. if (pos.y > chartRect.y + chartRect.height)
  75. pos.y = chartRect.y + chartRect.height;
  76. tooltip.UpdateContentPos(pos, chartRect.width / 2, chartRect.height / 2);
  77. }
  78. public static string GetItemNumericFormatter(Tooltip tooltip, Serie serie, SerieData serieData)
  79. {
  80. var itemStyle = SerieHelper.GetItemStyle(serie, serieData);
  81. if (!string.IsNullOrEmpty(itemStyle.numericFormatter)) return itemStyle.numericFormatter;
  82. else return tooltip.numericFormatter;
  83. }
  84. public static Color32 GetLineColor(Tooltip tooltip, Color32 defaultColor)
  85. {
  86. var lineStyle = tooltip.lineStyle;
  87. if (!ChartHelper.IsClearColor(lineStyle.color))
  88. {
  89. return lineStyle.GetColor();
  90. }
  91. else
  92. {
  93. var color = defaultColor;
  94. ChartHelper.SetColorOpacity(ref color, lineStyle.opacity);
  95. return color;
  96. }
  97. }
  98. }
  99. }