修复Legendformatter设置为固定值时显示不正常的问题

This commit is contained in:
monitor1394
2022-12-12 07:29:30 +08:00
parent d4a3886530
commit a3a25fe7c6
3 changed files with 43 additions and 5 deletions

View File

@@ -65,6 +65,7 @@ slug: /changelog
## master
* (2022.12.12) 修复`Legend``formatter`设置为固定值时显示不正常的问题
* (2022.12.08) 增加`AreaStyle``toTop`参数可设置折线图渐变色是到顶部还是到实际位置
* (2022.12.07) 增加`Formatter`的文本通配符`{h}`支持设置当前颜色值

View File

@@ -55,6 +55,7 @@ namespace XCharts.Runtime
chart.chartMaxAnchor, chart.chartPivot, chart.chartSizeDelta);
legend.gameObject = legendObject;
legendObject.hideFlags = chart.chartHideFlags;
//ChartHelper.DestoryGameObjectByMatch(legendObject.transform, "_");
SeriesHelper.UpdateSerieNameList(chart, ref chart.m_LegendRealShowName);
legend.context.background = ChartHelper.AddIcon("background", legendObject.transform, 0, 0);
legend.context.background.transform.SetSiblingIndex(0);
@@ -85,13 +86,14 @@ namespace XCharts.Runtime
for (int i = 0; i < datas.Count; i++)
{
if (!SeriesHelper.IsLegalLegendName(datas[i])) continue;
string legendName = GetFormatterContent(legend, i, datas[i]);
string legendName = datas[i];
var legendContent = GetFormatterContent(legend, i, datas[i]);
var readIndex = chart.m_LegendRealShowName.IndexOf(datas[i]);
var active = chart.IsActiveByLegend(datas[i]);
var bgColor = LegendHelper.GetIconColor(chart, legend, readIndex, datas[i], active);
bgColor.a = legend.itemOpacity;
var item = LegendHelper.AddLegendItem(chart, legend, i, datas[i], legendObject.transform, chart.theme,
legendName, bgColor, active, readIndex);
legendContent, bgColor, active, readIndex);
legend.SetButton(legendName, item, totalLegend);
ChartHelper.ClearEventListener(item.button.gameObject);
ChartHelper.AddEventListener(item.button.gameObject, EventTriggerType.PointerDown, (data) =>

View File

@@ -123,14 +123,14 @@ namespace XCharts.Runtime
GameObject.DestroyImmediate(go.gameObject, true);
}
}
public static void DestoryGameObjectByMatch(Transform parent, string match)
public static void DestoryGameObjectByMatch(Transform parent, string containString)
{
if (parent == null) return;
var childCount = parent.childCount;
for (int i = childCount - 1; i >= 0; i--)
{
var go = parent.GetChild(i);
if (go != null && go.name.StartsWith(match))
if (go != null && go.name.Contains(containString))
{
GameObject.DestroyImmediate(go.gameObject, true);
}
@@ -281,7 +281,7 @@ namespace XCharts.Runtime
return chartText;
}
internal static Painter AddPainterObject(string name, Transform parent, Vector2 anchorMin, Vector2 anchorMax,
public static Painter AddPainterObject(string name, Transform parent, Vector2 anchorMin, Vector2 anchorMax,
Vector2 pivot, Vector2 sizeDelta, HideFlags hideFlags, int siblingIndex)
{
var painterObj = ChartHelper.AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
@@ -381,6 +381,41 @@ namespace XCharts.Runtime
return label;
}
public static ChartLabel AddChartLabel2(string name, Transform parent, LabelStyle labelStyle,
ComponentTheme theme, string content, Color autoColor, TextAnchor autoAlignment = TextAnchor.MiddleCenter)
{
Vector2 anchorMin, anchorMax, pivot;
var sizeDelta = new Vector2(labelStyle.width, labelStyle.height);
var textStyle = labelStyle.textStyle;
var alignment = textStyle.GetAlignment(autoAlignment);
UpdateAnchorAndPivotByTextAlignment(alignment, out anchorMin, out anchorMax, out pivot);
var vector0_5 = new Vector2(0.5f, 0.5f);
var labelObj = AddObject(name, parent, vector0_5, vector0_5, vector0_5, sizeDelta);
var label = GetOrAddComponent<ChartLabel>(labelObj);
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);
label.SetSize(labelStyle.width, labelStyle.height);
label.SetTextPadding(labelStyle.textPadding);
label.SetText(content);
label.UpdateIcon(labelStyle.icon);
if (labelStyle.background.show)
{
label.color = (!labelStyle.background.autoColor || autoColor == Color.clear) ?
labelStyle.background.color : autoColor;
label.sprite = labelStyle.background.sprite;
label.type = labelStyle.background.type;
}
else
{
label.color = Color.clear;
label.sprite = null;
}
label.transform.localEulerAngles = new Vector3(0, 0, labelStyle.rotate);
label.transform.localPosition = labelStyle.offset;
return label;
}
private static void UpdateAnchorAndPivotByTextAlignment(TextAnchor alignment, out Vector2 anchorMin, out Vector2 anchorMax,
out Vector2 pivot)
{