优化Tooltip的对齐方式

This commit is contained in:
monitor1394
2025-02-18 23:30:56 +08:00
parent 214b84c71d
commit 9ddc543ed7
8 changed files with 87 additions and 39 deletions

View File

@@ -79,6 +79,7 @@ slug: /changelog
## master
* (2025.02.18) 优化`Tooltip`的对齐方式
* (2025.02.09) 修复`SaveAsImage`保存图片时不支持透明度的问题 (#337)
* (2025.02.05) 增加`GraphChart`可单独设置`data`样式的支持
* (2025.02.05) 重构`SerieSymbol``sizeFunction`参数

View File

@@ -154,7 +154,7 @@ namespace XCharts.Runtime
[SerializeField]
private List<LabelStyle> m_ContentLabelStyles = new List<LabelStyle>()
{
new LabelStyle() { textPadding = new TextPadding(0, 5, 0, 0), textStyle = new TextStyle() { alignment = TextAnchor.MiddleLeft } },
new LabelStyle() { textPadding = new TextPadding(0, 5, 0, 0), textStyle = new TextStyle() { alignment = TextAnchor.MiddleCenter } },
new LabelStyle() { textPadding = new TextPadding(0, 20, 0, 0), textStyle = new TextStyle() { alignment = TextAnchor.MiddleLeft } },
new LabelStyle() { textPadding = new TextPadding(0, 0, 0, 0), textStyle = new TextStyle() { alignment = TextAnchor.MiddleRight } }
};

View File

@@ -89,7 +89,7 @@ namespace XCharts.Runtime
item.gameObject.SetActive(true);
for (int j = 0; j < param.columns.Count; j++)
{
var column = GetItemColumn(item, j);
var column = GetItemColumn(item, j, j == 0 && IsSecondaryMark(param, param.columns[j]));
column.SetActive(true);
column.SetText(param.columns[j]);
@@ -117,6 +117,11 @@ namespace XCharts.Runtime
tooltip.gameObject.transform.SetAsLastSibling();
}
private static bool IsSecondaryMark(SerieParams sp, string mark)
{
return sp.isSecondaryMark && mark == sp.marker;
}
private void ResetSize()
{
var maxHig = 0f;
@@ -158,8 +163,10 @@ namespace XCharts.Runtime
var xPos = 0f;
for (int j = 0; j < m_ColumnMaxWidth.Count; j++)
{
if(j >= item.columns.Count) break;
var deltaX = j == m_ColumnMaxWidth.Count - 1 ? maxWid - xPos : m_ColumnMaxWidth[j];
item.columns[j].text.SetSizeDelta(new Vector2(deltaX, tooltip.itemHeight));
item.columns[j].SetSize(deltaX, tooltip.itemHeight);
item.columns[j].SetRectPosition(new Vector3(xPos, 0));
xPos += m_ColumnMaxWidth[j];
}
@@ -192,19 +199,24 @@ namespace XCharts.Runtime
}
}
private ChartLabel GetItemColumn(TooltipViewItem item, int i)
private ChartLabel GetItemColumn(TooltipViewItem item, int i, bool isSecondaryMark = false)
{
if (i < 0) i = 0;
ChartLabel column;
if (i < item.columns.Count)
{
return item.columns[i];
column = item.columns[i];
}
else
{
var column = CreateViewItemColumn(i, item.gameObject.transform, tooltip, theme);
column = CreateViewItemColumn(i, item.gameObject.transform, tooltip, theme);
item.columns.Add(column);
return column;
}
if (isSecondaryMark)
{
column.text.text.fontSize = (int)(tooltip.GetContentLabelStyle(i).textStyle.fontSize * 0.6f);
}
return column;
}
public static TooltipView CreateView(Tooltip tooltip, ThemeStyle theme, Transform parent)
@@ -266,8 +278,9 @@ namespace XCharts.Runtime
private static ChartLabel CreateViewItemColumn(int i, Transform parent, Tooltip tooltip, ComponentTheme theme)
{
var labelStyle = tooltip.GetContentLabelStyle(i);
labelStyle.textStyle.autoAlign = false;
var label = ChartHelper.AddChartLabel("column" + i, parent, labelStyle, theme,
"", Color.clear, TextAnchor.MiddleLeft);
"", Color.clear, TextAnchor.MiddleLeft, true);
return label;
}
}

View File

@@ -377,7 +377,7 @@ namespace XCharts.Runtime
serie.index = i;
SetPainterActive(i, true);
}
if (transform.childCount - 3 != m_PainterTop.transform.GetSiblingIndex())
if (m_PainterTop != null && transform.childCount - 3 != m_PainterTop.transform.GetSiblingIndex())
{
m_PainterTop.transform.SetSiblingIndex(transform.childCount - 3);
}

View File

@@ -167,7 +167,7 @@ namespace XCharts.Runtime
{
if (!s_ColorDotStr.ContainsKey(color))
{
s_ColorDotStr[color] = "<color=#" + ColorToStr(color) + ">● </color>";
s_ColorDotStr[color] = "<color=#" + ColorToStr(color) + ">●</color>";
}
return s_ColorDotStr[color];
}

View File

@@ -468,16 +468,21 @@ namespace XCharts.Runtime
}
public static ChartLabel AddChartLabel(string name, Transform parent, LabelStyle labelStyle,
ComponentTheme theme, string content, Color autoColor, TextAnchor autoAlignment = TextAnchor.MiddleCenter)
ComponentTheme theme, string content, Color autoColor, TextAnchor autoAlignment = TextAnchor.MiddleCenter,
bool isObjectAnchor = false)
{
Vector2 anchorMin, anchorMax, pivot;
var sizeDelta = new Vector2(labelStyle.width, labelStyle.height);
var textStyle = labelStyle.textStyle;
var alignment = textStyle.GetAlignment(autoAlignment);
var alignment = isObjectAnchor ? autoAlignment : textStyle.GetAlignment(autoAlignment);
UpdateAnchorAndPivotByTextAlignment(alignment, out anchorMin, out anchorMax, out pivot);
var labelObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
//ChartHelper.RemoveComponent<Text>(labelObj);
var label = EnsureComponent<ChartLabel>(labelObj);
if(isObjectAnchor)
{
UpdateAnchorAndPivotByTextAlignment(textStyle.GetAlignment(autoAlignment), out anchorMin, out anchorMax, out pivot);
}
label.text = AddTextObject("Text", label.gameObject.transform, anchorMin, anchorMax, pivot,
sizeDelta, textStyle, theme, autoColor, autoAlignment, label.text);
label.icon = ChartHelper.AddIcon("Icon", label.gameObject.transform, labelStyle.icon);

View File

@@ -33,37 +33,19 @@ namespace XCharts.Runtime
var newMarker = SerieHelper.GetItemMarker(serie, serieData, marker);
var newItemFormatter = SerieHelper.GetItemFormatter(serie, serieData, itemFormatter);
var newNumericFormatter = SerieHelper.GetNumericFormatter(serie, serieData, numericFormatter);
var isEmptyItemFormatter = string.IsNullOrEmpty(newItemFormatter);
var param = serie.context.param;
param.serieName = serie.serieName;
param.serieIndex = serie.index;
param.category = category;
param.dimension = 1;
param.serieData = serieData;
param.dataCount = serie.dataCount;
param.value = 0;
param.total = 0;
param.color = color;
param.marker = newMarker;
param.itemFormatter = newItemFormatter;
param.numericFormatter = newNumericFormatter;
param.columns.Clear();
param.columns.Add(param.marker);
param.columns.Add(serie.serieName);
param.columns.Add(string.Empty);
paramList.Add(param);
for (int i = 1; i < 5; i++)
if (isEmptyItemFormatter)
{
param = new SerieParams();
var param = serie.context.param;
param.serieName = serie.serieName;
param.serieIndex = serie.index;
param.dimension = i;
param.category = category;
param.dimension = 1;
param.serieData = serieData;
param.dataCount = serie.dataCount;
param.value = serieData.GetData(i);
param.total = SerieHelper.GetMaxData(serie, i);
param.value = 0;
param.total = 0;
param.color = color;
param.marker = newMarker;
param.itemFormatter = newItemFormatter;
@@ -71,10 +53,56 @@ namespace XCharts.Runtime
param.columns.Clear();
param.columns.Add(param.marker);
param.columns.Add(XCSettings.lang.GetCandlestickDimensionName(i - 1));
param.columns.Add(ChartCached.NumberToStr(param.value, param.numericFormatter));
param.columns.Add(serie.serieName);
param.columns.Add(string.Empty);
paramList.Add(param);
for (int i = 1; i < 5; i++)
{
param = new SerieParams();
param.serieName = serie.serieName;
param.serieIndex = serie.index;
param.dimension = i;
param.serieData = serieData;
param.dataCount = serie.dataCount;
param.value = serieData.GetData(i);
param.total = SerieHelper.GetMaxData(serie, i);
param.color = color;
param.marker = newMarker;
param.itemFormatter = newItemFormatter;
param.numericFormatter = newNumericFormatter;
param.isSecondaryMark = true;
param.columns.Clear();
param.columns.Add(param.marker);
param.columns.Add(XCSettings.lang.GetCandlestickDimensionName(i - 1));
param.columns.Add(ChartCached.NumberToStr(param.value, param.numericFormatter));
paramList.Add(param);
}
}
else
{
newItemFormatter = newItemFormatter.Replace("\\n", "\n");
var temp = newItemFormatter.Split('\n');
foreach (var str in temp)
{
var param = new SerieParams();
param.serieName = serie.serieName;
param.serieIndex = serie.index;
param.category = category;
param.serieData = serieData;
param.dataCount = serie.dataCount;
param.value = 0;
param.total = 0;
param.color = color;
param.marker = newMarker;
param.itemFormatter = str;
param.numericFormatter = newNumericFormatter;
param.isSecondaryMark = false;
param.columns.Clear();
paramList.Add(param);
}
}
}

View File

@@ -10,6 +10,7 @@ namespace XCharts.Runtime
public int serieIndex;
public string serieName;
public string marker = "●";
public bool isSecondaryMark;
public string category;
public int dimension;
public SerieData serieData;
@@ -20,6 +21,6 @@ namespace XCharts.Runtime
public string itemFormatter;
public string numericFormatter;
public bool ignore;
public List<string> columns = new List<string>();
public List<string> columns = new List<string>();
}
}