From a92e01d806132a396e453cabded4bd190d2fbf9c Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Sat, 14 Aug 2021 15:05:37 +0800 Subject: [PATCH] Added `Legend`'s `textAutoColor` to set the text color match with `Serie` color (#163) --- CHANGELOG-EN.md | 1 + CHANGELOG.md | 1 + Editor/PropertyDrawers/LegendDrawer.cs | 1 + Runtime/API/BaseChart_API.cs | 2 +- Runtime/Component/Main/Legend.cs | 11 +++++++++++ Runtime/Helper/LegendHelper.cs | 12 ++++++++---- Runtime/Internal/BaseChart.cs | 2 +- 7 files changed, 24 insertions(+), 6 deletions(-) diff --git a/CHANGELOG-EN.md b/CHANGELOG-EN.md index 7bb280c4..8a84cc37 100644 --- a/CHANGELOG-EN.md +++ b/CHANGELOG-EN.md @@ -39,6 +39,7 @@ ## master +* (2021.08.14) Added `Legend`'s `textAutoColor` to set the text color match with `Serie` color (#163) * (2021.08.12) Optimize `BarChart` setting `Corner` when the positive and negative columns are fillet symmetric * (2021.08.03) Fixed y axis not displaying when all data is 0 * (2021.07.29) Fixed ignored data will also participate in calculations when `ignore` is enabled (#161) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7eec2568..b9f3d398 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,7 @@ ## master +* (2021.08.14) 增加`Legend`的`textAutoColor`设置文本颜色和`Serie`一致 * (2021.08.12) 优化`BarChart`设置`Corner`时正负柱条圆角对称 * (2021.08.03) 优化`Serie`的数据全为0时Y轴不显示的问题 * (2021.07.29) 修复`Serie`开启`ignore`时被忽略的数据还会参与计算的问题 (#161) diff --git a/Editor/PropertyDrawers/LegendDrawer.cs b/Editor/PropertyDrawers/LegendDrawer.cs index a84378f3..51c462a8 100644 --- a/Editor/PropertyDrawers/LegendDrawer.cs +++ b/Editor/PropertyDrawers/LegendDrawer.cs @@ -25,6 +25,7 @@ namespace XCharts PropertyField(prop, "m_ItemHeight"); PropertyField(prop, "m_ItemGap"); PropertyField(prop, "m_ItemAutoColor"); + PropertyField(prop, "m_TextAutoColor"); PropertyField(prop, "m_SelectedMode"); PropertyField(prop, "m_Orient"); PropertyField(prop, "m_Location"); diff --git a/Runtime/API/BaseChart_API.cs b/Runtime/API/BaseChart_API.cs index 8303b505..507abc8d 100644 --- a/Runtime/API/BaseChart_API.cs +++ b/Runtime/API/BaseChart_API.cs @@ -543,7 +543,7 @@ namespace XCharts foreach (var legend in m_Legends) { var iconColor = LegendHelper.GetIconColor(this, legendIndex, legendName, active); - var contentColor = LegendHelper.GetContentColor(legend, m_Theme, active); + var contentColor = LegendHelper.GetContentColor(legendIndex, legend, m_Theme, active); legend.UpdateButtonColor(legendName, iconColor); legend.UpdateContentColor(legendName, contentColor); } diff --git a/Runtime/Component/Main/Legend.cs b/Runtime/Component/Main/Legend.cs index 387a0505..ab4e7927 100644 --- a/Runtime/Component/Main/Legend.cs +++ b/Runtime/Component/Main/Legend.cs @@ -78,6 +78,7 @@ namespace XCharts [SerializeField] private float m_ItemHeight = 12.0f; [SerializeField] private float m_ItemGap = 10f; [SerializeField] private bool m_ItemAutoColor = true; + [SerializeField] private bool m_TextAutoColor = false; [SerializeField] private string m_Formatter; [SerializeField] private TextStyle m_TextStyle = new TextStyle(); [SerializeField] private List m_Data = new List(); @@ -176,6 +177,16 @@ namespace XCharts set { if (PropertyUtil.SetStruct(ref m_ItemAutoColor, value)) SetComponentDirty(); } } /// + /// Whether the legend text matches the color automatically. + /// 图例标记的文本是否自动匹配颜色。 + /// [default:false] + /// + public bool textAutoColor + { + get { return m_TextAutoColor; } + set { if (PropertyUtil.SetStruct(ref m_TextAutoColor, value)) SetComponentDirty(); } + } + /// /// Legend content string template formatter. Support for wrapping lines with \n. Template:{name}. /// 图例内容字符串模版格式器。支持用 \n 换行。 /// 模板变量为图例名称 {name}。 diff --git a/Runtime/Helper/LegendHelper.cs b/Runtime/Helper/LegendHelper.cs index 86bb2723..d7c8604c 100644 --- a/Runtime/Helper/LegendHelper.cs +++ b/Runtime/Helper/LegendHelper.cs @@ -11,10 +11,14 @@ namespace XCharts { public static class LegendHelper { - public static Color GetContentColor(Legend legend, ChartTheme theme, bool active) + public static Color GetContentColor(int legendIndex, Legend legend, ChartTheme theme, bool active) { var textStyle = legend.textStyle; - if (active) return !ChartHelper.IsClearColor(textStyle.color) ? textStyle.color : theme.legend.textColor; + if (active) + { + if (legend.textAutoColor) return theme.GetColor(legendIndex); + else return !ChartHelper.IsClearColor(textStyle.color) ? textStyle.color : theme.legend.textColor; + } else return theme.legend.unableColor; } @@ -34,7 +38,7 @@ namespace XCharts } public static LegendItem AddLegendItem(Legend legend, int i, string legendName, Transform parent, - ChartTheme theme, string content, Color itemColor, bool active) + ChartTheme theme, string content, Color itemColor, bool active, int legendIndex) { var objName = i + "_" + legendName; var anchorMin = new Vector2(0, 0.5f); @@ -43,7 +47,7 @@ namespace XCharts var sizeDelta = new Vector2(100, 30); var iconSizeDelta = new Vector2(legend.itemWidth, legend.itemHeight); var textStyle = legend.textStyle; - var contentColor = GetContentColor(legend, theme, active); + var contentColor = GetContentColor(legendIndex, legend, theme, active); var objAnchorMin = new Vector2(0, 1); var objAnchorMax = new Vector2(0, 1); diff --git a/Runtime/Internal/BaseChart.cs b/Runtime/Internal/BaseChart.cs index 1681f07a..62ce23d0 100644 --- a/Runtime/Internal/BaseChart.cs +++ b/Runtime/Internal/BaseChart.cs @@ -450,7 +450,7 @@ namespace XCharts var active = IsActiveByLegend(datas[i]); var bgColor = LegendHelper.GetIconColor(this, readIndex, datas[i], active); var item = LegendHelper.AddLegendItem(legend, i, datas[i], legendObject.transform, m_Theme, - legendName, bgColor, active); + legendName, bgColor, active,readIndex); legend.SetButton(legendName, item, totalLegend); ChartHelper.ClearEventListener(item.button.gameObject); ChartHelper.AddEventListener(item.button.gameObject, EventTriggerType.PointerDown, (data) =>