TooltipHelper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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, -1,
  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. chart);
  47. foreach (var item in content.Split('|'))
  48. {
  49. param.columns.Add(item);
  50. }
  51. }
  52. }
  53. }
  54. public static bool IsIgnoreFormatter(string itemFormatter)
  55. {
  56. return "-".Equals(itemFormatter) ||"{i}".Equals(itemFormatter, StringComparison.CurrentCultureIgnoreCase);
  57. }
  58. public static void LimitInRect(Tooltip tooltip, Rect chartRect)
  59. {
  60. if (tooltip.view == null)
  61. return;
  62. var pos = tooltip.view.GetTargetPos();
  63. if (pos.x + tooltip.context.width > chartRect.x + chartRect.width)
  64. {
  65. pos.x = tooltip.context.pointer.x - tooltip.context.width - tooltip.offset.x;
  66. }
  67. else if (pos.x < chartRect.x)
  68. {
  69. pos.x = tooltip.context.pointer.x - tooltip.context.width + Mathf.Abs(tooltip.offset.x);
  70. }
  71. if (pos.y - tooltip.context.height < chartRect.y)
  72. {
  73. pos.y = chartRect.y + tooltip.context.height;
  74. }
  75. if (pos.y > chartRect.y + chartRect.height)
  76. pos.y = chartRect.y + chartRect.height;
  77. tooltip.UpdateContentPos(pos, chartRect.width / 2, chartRect.height / 2);
  78. }
  79. public static string GetItemNumericFormatter(Tooltip tooltip, Serie serie, SerieData serieData)
  80. {
  81. var itemStyle = SerieHelper.GetItemStyle(serie, serieData);
  82. if (!string.IsNullOrEmpty(itemStyle.numericFormatter)) return itemStyle.numericFormatter;
  83. else return tooltip.numericFormatter;
  84. }
  85. public static Color32 GetLineColor(Tooltip tooltip, Color32 defaultColor)
  86. {
  87. var lineStyle = tooltip.lineStyle;
  88. if (!ChartHelper.IsClearColor(lineStyle.color))
  89. {
  90. return lineStyle.GetColor();
  91. }
  92. else
  93. {
  94. var color = defaultColor;
  95. ChartHelper.SetColorOpacity(ref color, lineStyle.opacity);
  96. return color;
  97. }
  98. }
  99. }
  100. }