2021-12-19 20:53:55 +08:00
|
|
|
using System;
|
2020-03-21 11:26:50 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
2022-02-19 22:37:57 +08:00
|
|
|
namespace XCharts.Runtime
|
2020-03-21 11:26:50 +08:00
|
|
|
{
|
2021-04-27 12:47:46 +08:00
|
|
|
public static class TooltipHelper
|
2020-03-21 11:26:50 +08:00
|
|
|
{
|
2021-12-19 20:53:55 +08:00
|
|
|
internal static void ResetTooltipParamsByItemFormatter(Tooltip tooltip, BaseChart chart)
|
2020-03-21 11:26:50 +08:00
|
|
|
{
|
2021-12-19 20:53:55 +08:00
|
|
|
if (!string.IsNullOrEmpty(tooltip.titleFormatter))
|
2020-03-21 11:26:50 +08:00
|
|
|
{
|
2022-06-29 13:33:38 +08:00
|
|
|
if (IsIgnoreFormatter(tooltip.titleFormatter))
|
2021-03-10 13:03:36 +08:00
|
|
|
{
|
2021-12-19 20:53:55 +08:00
|
|
|
tooltip.context.data.title = string.Empty;
|
2021-03-10 13:03:36 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-12-19 20:53:55 +08:00
|
|
|
tooltip.context.data.title = tooltip.titleFormatter;
|
|
|
|
|
FormatterHelper.ReplaceContent(ref tooltip.context.data.title, 0,
|
|
|
|
|
tooltip.numericFormatter, null, chart);
|
2021-03-10 13:03:36 +08:00
|
|
|
}
|
2020-03-21 11:26:50 +08:00
|
|
|
}
|
2022-01-13 21:45:59 +08:00
|
|
|
for (int i = tooltip.context.data.param.Count - 1; i >= 0; i--)
|
|
|
|
|
{
|
|
|
|
|
var param = tooltip.context.data.param[i];
|
2022-06-29 13:33:38 +08:00
|
|
|
if (IsIgnoreFormatter(param.itemFormatter))
|
2022-01-13 21:45:59 +08:00
|
|
|
{
|
|
|
|
|
tooltip.context.data.param.RemoveAt(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-19 20:53:55 +08:00
|
|
|
foreach (var param in tooltip.context.data.param)
|
2020-07-01 09:38:00 +08:00
|
|
|
{
|
2021-12-19 20:53:55 +08:00
|
|
|
if (!string.IsNullOrEmpty(param.itemFormatter))
|
2020-07-02 09:44:25 +08:00
|
|
|
{
|
2022-01-13 21:45:59 +08:00
|
|
|
param.columns.Clear();
|
2021-12-19 20:53:55 +08:00
|
|
|
var content = param.itemFormatter;
|
|
|
|
|
FormatterHelper.ReplaceSerieLabelContent(ref content,
|
|
|
|
|
param.numericFormatter,
|
2022-05-22 22:17:38 +08:00
|
|
|
param.dataCount,
|
2021-12-19 20:53:55 +08:00
|
|
|
param.value,
|
|
|
|
|
param.total,
|
|
|
|
|
param.serieName,
|
|
|
|
|
param.category,
|
|
|
|
|
param.serieData.name,
|
2022-06-29 13:33:38 +08:00
|
|
|
param.color,
|
|
|
|
|
param.serieData);
|
2021-12-19 20:53:55 +08:00
|
|
|
foreach (var item in content.Split('|'))
|
2020-07-01 09:38:00 +08:00
|
|
|
{
|
2021-12-19 20:53:55 +08:00
|
|
|
param.columns.Add(item);
|
2020-07-01 09:38:00 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-29 13:33:38 +08:00
|
|
|
public static bool IsIgnoreFormatter(string itemFormatter)
|
2022-01-13 21:45:59 +08:00
|
|
|
{
|
2022-06-29 13:33:38 +08:00
|
|
|
return "-".Equals(itemFormatter) ||"{i}".Equals(itemFormatter, StringComparison.CurrentCultureIgnoreCase);
|
2022-01-13 21:45:59 +08:00
|
|
|
}
|
|
|
|
|
|
2021-12-19 20:53:55 +08:00
|
|
|
public static void LimitInRect(Tooltip tooltip, Rect chartRect)
|
2020-03-21 11:26:50 +08:00
|
|
|
{
|
2021-12-19 20:53:55 +08:00
|
|
|
if (tooltip.view == null)
|
|
|
|
|
return;
|
2020-03-21 11:26:50 +08:00
|
|
|
|
2021-12-19 20:53:55 +08:00
|
|
|
var pos = tooltip.view.GetTargetPos();
|
|
|
|
|
if (pos.x + tooltip.context.width > chartRect.x + chartRect.width)
|
2020-06-14 18:16:23 +08:00
|
|
|
{
|
2022-06-20 13:41:28 +08:00
|
|
|
pos.x = tooltip.context.pointer.x - tooltip.context.width - tooltip.offset.x;
|
|
|
|
|
}
|
|
|
|
|
else if (pos.x < chartRect.x)
|
|
|
|
|
{
|
|
|
|
|
pos.x = tooltip.context.pointer.x - tooltip.context.width + Mathf.Abs(tooltip.offset.x);
|
2020-06-14 18:16:23 +08:00
|
|
|
}
|
2021-12-19 20:53:55 +08:00
|
|
|
if (pos.y - tooltip.context.height < chartRect.y)
|
2020-06-14 18:16:23 +08:00
|
|
|
{
|
2021-12-19 20:53:55 +08:00
|
|
|
pos.y = chartRect.y + tooltip.context.height;
|
2020-06-14 18:16:23 +08:00
|
|
|
}
|
2022-06-20 13:41:28 +08:00
|
|
|
if (pos.y > chartRect.y + chartRect.height)
|
|
|
|
|
pos.y = chartRect.y + chartRect.height;
|
2022-08-26 07:50:48 +08:00
|
|
|
tooltip.UpdateContentPos(pos, chartRect.width / 2, chartRect.height / 2);
|
2020-03-21 11:26:50 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-29 22:07:09 +08:00
|
|
|
public static string GetItemNumericFormatter(Tooltip tooltip, Serie serie, SerieData serieData)
|
2020-05-04 13:29:56 +08:00
|
|
|
{
|
|
|
|
|
var itemStyle = SerieHelper.GetItemStyle(serie, serieData);
|
|
|
|
|
if (!string.IsNullOrEmpty(itemStyle.numericFormatter)) return itemStyle.numericFormatter;
|
|
|
|
|
else return tooltip.numericFormatter;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-27 16:54:09 +08:00
|
|
|
public static Color32 GetLineColor(Tooltip tooltip, Color32 defaultColor)
|
2020-03-21 11:26:50 +08:00
|
|
|
{
|
|
|
|
|
var lineStyle = tooltip.lineStyle;
|
2020-05-13 09:54:40 +08:00
|
|
|
if (!ChartHelper.IsClearColor(lineStyle.color))
|
2020-03-21 11:26:50 +08:00
|
|
|
{
|
2020-08-23 14:31:26 +08:00
|
|
|
return lineStyle.GetColor();
|
2020-03-21 11:26:50 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-11-27 16:54:09 +08:00
|
|
|
var color = defaultColor;
|
2020-08-23 14:31:26 +08:00
|
|
|
ChartHelper.SetColorOpacity(ref color, lineStyle.opacity);
|
2020-03-21 11:26:50 +08:00
|
|
|
return color;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|