From 7c07499e447f716b43ac9b45fbf59a100bf223f8 Mon Sep 17 00:00:00 2001 From: monitor1394 Date: Fri, 24 Jun 2022 22:15:49 +0800 Subject: [PATCH] [optimize][legend] support background and padding --- Runtime/Component/Legend/Legend.cs | 20 +++++++++++++++ Runtime/Component/Legend/LegendContext.cs | 3 +++ Runtime/Component/Legend/LegendHandler.cs | 3 +++ Runtime/Component/Legend/LegendHelper.cs | 30 +++++++++++++++++++++++ 4 files changed, 56 insertions(+) diff --git a/Runtime/Component/Legend/Legend.cs b/Runtime/Component/Legend/Legend.cs index 4c59156f..c905508e 100644 --- a/Runtime/Component/Legend/Legend.cs +++ b/Runtime/Component/Legend/Legend.cs @@ -80,6 +80,8 @@ namespace XCharts.Runtime [SerializeField] private List m_Data = new List(); [SerializeField] private List m_Icons = new List(); [SerializeField] private List m_Colors = new List(); + [SerializeField] protected ImageStyle m_Background = new ImageStyle() { show = false }; + [SerializeField] protected Padding m_Padding = new Padding(); public LegendContext context = new LegendContext(); @@ -213,6 +215,24 @@ namespace XCharts.Runtime set { if (PropertyUtil.SetClass(ref m_LabelStyle, value)) SetComponentDirty(); } } /// + /// the sytle of background. + /// |背景图样式。 + /// + public ImageStyle background + { + get { return m_Background; } + set { if (PropertyUtil.SetClass(ref m_Background, value)) SetAllDirty(); } + } + /// + /// the paddinng of item and background. + /// |图例标记和背景的间距。 + /// + public Padding padding + { + get { return m_Padding; } + set { if (PropertyUtil.SetClass(ref m_Padding, value)) SetAllDirty(); } + } + /// /// Data array of legend. An array item is usually a name representing string. (If it is a pie chart, /// it could also be the name of a single data in the pie chart) of a series. /// |If data is not specified, it will be auto collected from series. diff --git a/Runtime/Component/Legend/LegendContext.cs b/Runtime/Component/Legend/LegendContext.cs index 9bead405..12cc5688 100644 --- a/Runtime/Component/Legend/LegendContext.cs +++ b/Runtime/Component/Legend/LegendContext.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using UnityEngine; +using UnityEngine.UI; namespace XCharts.Runtime { @@ -14,6 +15,7 @@ namespace XCharts.Runtime /// 运行时图例的总高度 /// public float height { get; internal set; } + public Vector2 center { get; internal set; } /// /// the button list of legend. /// |图例按钮列表。 @@ -27,5 +29,6 @@ namespace XCharts.Runtime /// 单列高度 /// internal float eachHeight { get; set; } + public Image background { get; set; } } } \ No newline at end of file diff --git a/Runtime/Component/Legend/LegendHandler.cs b/Runtime/Component/Legend/LegendHandler.cs index 75b71615..724d5d06 100644 --- a/Runtime/Component/Legend/LegendHandler.cs +++ b/Runtime/Component/Legend/LegendHandler.cs @@ -56,6 +56,9 @@ namespace XCharts.Runtime legend.gameObject = legendObject; legendObject.hideFlags = chart.chartHideFlags; SeriesHelper.UpdateSerieNameList(chart, ref chart.m_LegendRealShowName); + legend.context.background = ChartHelper.AddIcon("background", legendObject.transform, 0, 0); + legend.context.background.transform.SetSiblingIndex(0); + ChartHelper.SetBackground(legend.context.background, legend.background); List datas; if (legend.show && legend.data.Count > 0) { diff --git a/Runtime/Component/Legend/LegendHelper.cs b/Runtime/Component/Legend/LegendHelper.cs index 422eb2ab..f51f9cfa 100644 --- a/Runtime/Component/Legend/LegendHelper.cs +++ b/Runtime/Component/Legend/LegendHelper.cs @@ -52,6 +52,7 @@ namespace XCharts.Runtime var iconObj = ChartHelper.AddObject("icon", btnObj.transform, anchorMin, anchorMax, pivot, iconSizeDelta); var img = ChartHelper.GetOrAddComponent(btnObj); img.color = Color.clear; + img.raycastTarget = true; ChartHelper.GetOrAddComponent