[optimize][legend] support background and padding

This commit is contained in:
monitor1394
2022-06-24 22:15:49 +08:00
parent 8a6253621b
commit 7c07499e44
4 changed files with 56 additions and 0 deletions

View File

@@ -80,6 +80,8 @@ namespace XCharts.Runtime
[SerializeField] private List<string> m_Data = new List<string>(); [SerializeField] private List<string> m_Data = new List<string>();
[SerializeField] private List<Sprite> m_Icons = new List<Sprite>(); [SerializeField] private List<Sprite> m_Icons = new List<Sprite>();
[SerializeField] private List<Color> m_Colors = new List<Color>(); [SerializeField] private List<Color> m_Colors = new List<Color>();
[SerializeField] protected ImageStyle m_Background = new ImageStyle() { show = false };
[SerializeField] protected Padding m_Padding = new Padding();
public LegendContext context = new LegendContext(); public LegendContext context = new LegendContext();
@@ -213,6 +215,24 @@ namespace XCharts.Runtime
set { if (PropertyUtil.SetClass(ref m_LabelStyle, value)) SetComponentDirty(); } set { if (PropertyUtil.SetClass(ref m_LabelStyle, value)) SetComponentDirty(); }
} }
/// <summary> /// <summary>
/// the sytle of background.
/// |背景图样式。
/// </summary>
public ImageStyle background
{
get { return m_Background; }
set { if (PropertyUtil.SetClass(ref m_Background, value)) SetAllDirty(); }
}
/// <summary>
/// the paddinng of item and background.
/// |图例标记和背景的间距。
/// </summary>
public Padding padding
{
get { return m_Padding; }
set { if (PropertyUtil.SetClass(ref m_Padding, value)) SetAllDirty(); }
}
/// <summary>
/// Data array of legend. An array item is usually a name representing string. (If it is a pie chart, /// 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. /// 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. /// |If data is not specified, it will be auto collected from series.

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.UI;
namespace XCharts.Runtime namespace XCharts.Runtime
{ {
@@ -14,6 +15,7 @@ namespace XCharts.Runtime
/// 运行时图例的总高度 /// 运行时图例的总高度
/// </summary> /// </summary>
public float height { get; internal set; } public float height { get; internal set; }
public Vector2 center { get; internal set; }
/// <summary> /// <summary>
/// the button list of legend. /// the button list of legend.
/// |图例按钮列表。 /// |图例按钮列表。
@@ -27,5 +29,6 @@ namespace XCharts.Runtime
/// 单列高度 /// 单列高度
/// </summary> /// </summary>
internal float eachHeight { get; set; } internal float eachHeight { get; set; }
public Image background { get; set; }
} }
} }

View File

@@ -56,6 +56,9 @@ namespace XCharts.Runtime
legend.gameObject = legendObject; legend.gameObject = legendObject;
legendObject.hideFlags = chart.chartHideFlags; legendObject.hideFlags = chart.chartHideFlags;
SeriesHelper.UpdateSerieNameList(chart, ref chart.m_LegendRealShowName); 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<string> datas; List<string> datas;
if (legend.show && legend.data.Count > 0) if (legend.show && legend.data.Count > 0)
{ {

View File

@@ -52,6 +52,7 @@ namespace XCharts.Runtime
var iconObj = ChartHelper.AddObject("icon", btnObj.transform, anchorMin, anchorMax, pivot, iconSizeDelta); var iconObj = ChartHelper.AddObject("icon", btnObj.transform, anchorMin, anchorMax, pivot, iconSizeDelta);
var img = ChartHelper.GetOrAddComponent<Image>(btnObj); var img = ChartHelper.GetOrAddComponent<Image>(btnObj);
img.color = Color.clear; img.color = Color.clear;
img.raycastTarget = true;
ChartHelper.GetOrAddComponent<Button>(btnObj); ChartHelper.GetOrAddComponent<Button>(btnObj);
ChartHelper.GetOrAddComponent<Image>(iconObj); ChartHelper.GetOrAddComponent<Image>(iconObj);
@@ -69,9 +70,22 @@ namespace XCharts.Runtime
item.SetIconImage(legend.GetIcon(i)); item.SetIconImage(legend.GetIcon(i));
item.SetContentPosition(legend.labelStyle.offset); item.SetContentPosition(legend.labelStyle.offset);
item.SetContent(content); item.SetContent(content);
//item.SetBackground(legend.background);
return item; return item;
} }
public static void SetLegendBackground(Legend legend, ImageStyle style)
{
var background = legend.context.background;
if (background == null) return;
ChartHelper.SetActive(background, style.show);
if (!style.show) return;
var rect = background.gameObject.GetComponent<RectTransform>();
rect.localPosition = legend.context.center;
rect.sizeDelta = new Vector2(legend.context.width, legend.context.height);
ChartHelper.SetBackground(background, style);
}
public static void ResetItemPosition(Legend legend, Vector3 chartPos, float chartWidth, float chartHeight) public static void ResetItemPosition(Legend legend, Vector3 chartPos, float chartWidth, float chartHeight)
{ {
legend.location.UpdateRuntimeData(chartWidth, chartHeight); legend.location.UpdateRuntimeData(chartWidth, chartHeight);
@@ -122,8 +136,19 @@ namespace XCharts.Runtime
startY = chartPos.y + legendRuntimeHeight + legend.location.runtimeBottom; startY = chartPos.y + legendRuntimeHeight + legend.location.runtimeBottom;
break; break;
} }
if (!legend.padding.show)
{
legend.context.center = new Vector2(startX + legend.context.width / 2, startY - legend.context.height / 2);
}
else
{
legend.context.center = new Vector2(startX + legend.context.width / 2 - legend.padding.left,
startY - legend.context.height / 2 + legend.padding.top);
}
if (isVertical) SetVerticalItemPosition(legend, legendMaxHeight, startX, startY); if (isVertical) SetVerticalItemPosition(legend, legendMaxHeight, startX, startY);
else SetHorizonalItemPosition(legend, legendMaxWidth, startX, startY); else SetHorizonalItemPosition(legend, legendMaxWidth, startX, startY);
SetLegendBackground(legend, legend.background);
} }
private static void SetVerticalItemPosition(Legend legend, float legendMaxHeight, float startX, float startY) private static void SetVerticalItemPosition(Legend legend, float legendMaxHeight, float startX, float startY)
@@ -214,6 +239,11 @@ namespace XCharts.Runtime
legend.context.height = realHeight > 0 ? realHeight : height; legend.context.height = realHeight > 0 ? realHeight : height;
legend.context.width = realWidth + width; legend.context.width = realWidth + width;
} }
if (legend.padding.show)
{
legend.context.width += legend.padding.left + legend.padding.right;
legend.context.height += legend.padding.top + legend.padding.bottom;
}
} }
private static bool IsBeyondWidth(Legend legend, float maxWidth) private static bool IsBeyondWidth(Legend legend, float maxWidth)