From 2a3d8dc5b2463d4d99ed1244de2881aa926a3eca Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Tue, 17 Mar 2020 09:13:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0`BarChart`=E5=8F=AF=E7=94=A8`?= =?UTF-8?q?ItemStyle`=E7=9A=84`backgroundColor`=E8=AE=BE=E7=BD=AE=E6=9F=B1?= =?UTF-8?q?=E6=9D=A1=E8=83=8C=E6=99=AF=E9=A2=9C=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/XCharts/CHANGELOG.md | 1 + Assets/XCharts/Runtime/Helper/SerieHelper.cs | 5 ++-- .../Internal/CoordinateChart_DrawBar.cs | 28 ++++++++++++++++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Assets/XCharts/CHANGELOG.md b/Assets/XCharts/CHANGELOG.md index 7fffd709..acbc2fcf 100644 --- a/Assets/XCharts/CHANGELOG.md +++ b/Assets/XCharts/CHANGELOG.md @@ -1,6 +1,7 @@ # 更新日志 +* (2020.03.17) 增加`BarChart`可用`ItemStyle`的`backgroundColor`设置数据项背景颜色 * (2020.03.17) 增加`SerieData`的`ItemStyle`和`Emphasis`可单独配置数据项样式的支持 * (2020.03.15) 重构`EmptyCricle`类型的`Symbol`边宽取自`ItemStyle`的`borderWidth` * (2020.03.15) 重构`SerieSymbol`,去掉`color`和`opacity`参数,用`ItemStyle`参数代替 diff --git a/Assets/XCharts/Runtime/Helper/SerieHelper.cs b/Assets/XCharts/Runtime/Helper/SerieHelper.cs index 88433420..b6dd673e 100644 --- a/Assets/XCharts/Runtime/Helper/SerieHelper.cs +++ b/Assets/XCharts/Runtime/Helper/SerieHelper.cs @@ -11,7 +11,7 @@ namespace XCharts { internal static class SerieHelper { - internal static Color GetItemBackgroundColor(Serie serie, SerieData serieData, ThemeInfo theme, int index, bool highlight) + internal static Color GetItemBackgroundColor(Serie serie, SerieData serieData, ThemeInfo theme, int index, bool highlight, bool useDefault = true) { var itemStyle = GetItemStyle(serie, serieData); var color = Color.clear; @@ -32,13 +32,14 @@ namespace XCharts color.a *= itemStyle.opacity; return color; } - else + else if (useDefault) { color = (Color)theme.GetColor(index); if (highlight) color *= color; color.a = 0.2f; return color; } + return Color.clear; } internal static Color GetItemColor(Serie serie, SerieData serieData, ThemeInfo theme, int index, bool highlight) diff --git a/Assets/XCharts/Runtime/Internal/CoordinateChart_DrawBar.cs b/Assets/XCharts/Runtime/Internal/CoordinateChart_DrawBar.cs index 6c5c7277..c20ed042 100644 --- a/Assets/XCharts/Runtime/Internal/CoordinateChart_DrawBar.cs +++ b/Assets/XCharts/Runtime/Internal/CoordinateChart_DrawBar.cs @@ -98,9 +98,9 @@ namespace XCharts p4 = ClampInCoordinate(p4); top = ClampInCoordinate(top); serie.dataPoints.Add(top); - if (serie.show) { + DrawBarBackground(vh, serie, serieData, colorIndex, highlight, pX, pY, space, barWidth, true); Color areaColor = SerieHelper.GetItemColor(serie, serieData, m_ThemeInfo, colorIndex, highlight); Color areaToColor = SerieHelper.GetItemToColor(serie, serieData, m_ThemeInfo, colorIndex, highlight); if (serie.barType == BarType.Zebra) @@ -231,6 +231,7 @@ namespace XCharts { Color areaColor = SerieHelper.GetItemColor(serie, serieData, m_ThemeInfo, colorIndex, highlight); Color areaToColor = SerieHelper.GetItemToColor(serie, serieData, m_ThemeInfo, colorIndex, highlight); + DrawBarBackground(vh, serie, serieData, colorIndex, highlight, pX, pY, space, barWidth, false); if (serie.barType == BarType.Zebra) { p1 = (p4 + p1) / 2; @@ -262,6 +263,31 @@ namespace XCharts } } + private void DrawBarBackground(VertexHelper vh, Serie serie, SerieData serieData, int colorIndex, bool highlight, + float pX, float pY, float space, float barWidth, bool isYAxis) + { + Color color = SerieHelper.GetItemBackgroundColor(serie, serieData, m_ThemeInfo, colorIndex, highlight, false); + if (color != Color.clear) + { + if (isYAxis) + { + Vector3 p1 = new Vector3(coordinateX, pY + space + barWidth); + Vector3 p2 = new Vector3(coordinateX + coordinateWidth, pY + space + barWidth); + Vector3 p3 = new Vector3(coordinateX + coordinateWidth, pY + space); + Vector3 p4 = new Vector3(coordinateX, pY + space); + CheckClipAndDrawPolygon(vh, ref p4, ref p1, ref p2, ref p3, color, color, serie.clip); + } + else + { + Vector3 p1 = new Vector3(pX + space, coordinateY); + Vector3 p2 = new Vector3(pX + space, coordinateY + coordinateHeight); + Vector3 p3 = new Vector3(pX + space + barWidth, coordinateY + coordinateHeight); + Vector3 p4 = new Vector3(pX + space + barWidth, coordinateY); + CheckClipAndDrawPolygon(vh, ref p4, ref p1, ref p2, ref p3, color, color, serie.clip); + } + } + } + private float GetBarGap() { float gap = 0.3f;