diff --git a/CHANGELOG-EN.md b/CHANGELOG-EN.md index 105d2a1a..40034611 100644 --- a/CHANGELOG-EN.md +++ b/CHANGELOG-EN.md @@ -1,6 +1,7 @@ # 更新日志 +* (2020.09.08) Optimize the color of `Legend` to automatically match the custom color of `ItemStyle` * (2020.09.05) Optimize `LineChart` to display `XAxis1` without using `XAxis1`. * (2020.08.29) Added `toColor` and `toColor2` of `LineStyle` to set the horizontal gradient of `LineChart`. Cancel `ItemStyle` to set the horizontal gradient of `LineChart`. * (2020.08.29) Added the `onPointerClickPie` of `PieChart`, a callback function of click pie area. diff --git a/CHANGELOG.md b/CHANGELOG.md index a44e0b92..c98135c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 更新日志 +* (2020.09.08) 优化`Legend`的颜色可自动匹配`ItemStyle`的自定义颜色#89 * (2020.09.05) 优化`LineChart`在不使用`XAxis1`时也能显示`XAxis1` * (2020.08.29) 增加`LineStyle`的`toColor`和`toColor2`设置`LineChart`的水平渐变,取消通过`ItemStyle`设置`LineChart`的水平渐变 * (2020.08.29) 增加`PieChart`的`onPointerClickPie`点击扇形图扇区回调 diff --git a/Runtime/API/BaseChart_API.cs b/Runtime/API/BaseChart_API.cs index dc7d6c79..b8bd16dc 100644 --- a/Runtime/API/BaseChart_API.cs +++ b/Runtime/API/BaseChart_API.cs @@ -464,7 +464,7 @@ namespace XCharts var legendIndex = m_LegendRealShowName.IndexOf(legendName); if (legendIndex >= 0) { - var iconColor = LegendHelper.GetIconColor(legend, legendIndex, m_ThemeInfo, active); + var iconColor = LegendHelper.GetIconColor(legend, legendIndex, m_ThemeInfo, m_Series, legendName, active); var contentColor = LegendHelper.GetContentColor(legend, m_ThemeInfo, active); m_Legend.UpdateButtonColor(legendName, iconColor); m_Legend.UpdateContentColor(legendName, contentColor); diff --git a/Runtime/Internal/BaseChart.cs b/Runtime/Internal/BaseChart.cs index 647e7a40..45aa5f31 100644 --- a/Runtime/Internal/BaseChart.cs +++ b/Runtime/Internal/BaseChart.cs @@ -364,7 +364,7 @@ namespace XCharts string legendName = m_Legend.GetFormatterContent(datas[i]); var readIndex = m_LegendRealShowName.IndexOf(datas[i]); var active = IsActiveByLegend(datas[i]); - var bgColor = LegendHelper.GetIconColor(m_Legend, readIndex, themeInfo, active); + var bgColor = LegendHelper.GetIconColor(m_Legend, readIndex, themeInfo, m_Series, datas[i], active); var item = LegendHelper.AddLegendItem(m_Legend, i, datas[i], legendObject.transform, m_ThemeInfo, legendName, bgColor, active); m_Legend.SetButton(legendName, item, totalLegend); diff --git a/Runtime/Internal/Helper/LegendHelper.cs b/Runtime/Internal/Helper/LegendHelper.cs index 1d76617c..446fe12d 100644 --- a/Runtime/Internal/Helper/LegendHelper.cs +++ b/Runtime/Internal/Helper/LegendHelper.cs @@ -18,12 +18,14 @@ namespace XCharts else return (Color)themeInfo.legendUnableColor; } - public static Color GetIconColor(Legend legend, int readIndex, ThemeInfo themeInfo, bool active) + public static Color GetIconColor(Legend legend, int readIndex, ThemeInfo themeInfo, Series series, string legendName, bool active) { if (active) { if (legend.itemAutoColor || legend.GetIcon(readIndex) == null) - return (Color)themeInfo.GetColor(readIndex); + { + return SeriesHelper.GetNameColor(series, readIndex, legendName, themeInfo); + } else return Color.white; } diff --git a/Runtime/Internal/Helper/SeriesHelper.cs b/Runtime/Internal/Helper/SeriesHelper.cs index a0e09703..a5dff96a 100644 --- a/Runtime/Internal/Helper/SeriesHelper.cs +++ b/Runtime/Internal/Helper/SeriesHelper.cs @@ -119,6 +119,42 @@ namespace XCharts } } + internal static Color GetNameColor(Series series, int index, string name, ThemeInfo theme) + { + Serie destSerie = null; + SerieData destSerieData = null; + + for (int n = 0; n < series.list.Count; n++) + { + var serie = series.GetSerie(n); + if (serie.type == SerieType.Pie || serie.type == SerieType.Radar || serie.type == SerieType.Ring) + { + bool found = false; + for (int i = 0; i < serie.data.Count; i++) + { + if (name.Equals(serie.data[i].name)) + { + destSerie = serie; + destSerieData = serie.data[i]; + found = true; + break; + } + } + if (found) break; + } + else + { + if (name.Equals(serie.name)) + { + destSerie = serie; + destSerieData = null; + break; + } + } + } + return SerieHelper.GetItemColor(destSerie, destSerieData, theme, index, false); + } + /// /// 同堆叠的serie是否有渐变色的。 ///