mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-17 05:50:09 +00:00
增加SerieData的自定义图标相关配置支持
This commit is contained in:
@@ -41,6 +41,7 @@ namespace XCharts
|
||||
SerializedProperty m_Animation = prop.FindPropertyRelative("m_Animation");
|
||||
SerializedProperty m_DataDimension = prop.FindPropertyRelative("m_ShowDataDimension");
|
||||
SerializedProperty m_ShowDataName = prop.FindPropertyRelative("m_ShowDataName");
|
||||
SerializedProperty m_ShowDataIcon = prop.FindPropertyRelative("m_ShowDataIcon");
|
||||
SerializedProperty m_Datas = prop.FindPropertyRelative("m_Data");
|
||||
|
||||
int index = InitToggle(prop);
|
||||
@@ -154,14 +155,18 @@ namespace XCharts
|
||||
{
|
||||
EditorGUI.indentLevel++;
|
||||
|
||||
float nameWid = 76;
|
||||
EditorGUI.PropertyField(new Rect(drawRect.x, drawRect.y, pos.width - nameWid - 2, pos.height), m_DataDimension);
|
||||
var nameRect = new Rect(pos.width - nameWid + 14, drawRect.y, nameWid, pos.height);
|
||||
var btnName = m_ShowDataName.boolValue ? "Hide Name" : "Show Name";
|
||||
if (GUI.Button(nameRect, new GUIContent(btnName)))
|
||||
float nameWid = 40;
|
||||
EditorGUI.PropertyField(new Rect(drawRect.x, drawRect.y, pos.width - 2 * nameWid - 2, pos.height), m_DataDimension);
|
||||
var nameRect = new Rect(pos.width - 2 * nameWid + 14, drawRect.y, nameWid, pos.height);
|
||||
if (GUI.Button(nameRect, new GUIContent("Name")))
|
||||
{
|
||||
m_ShowDataName.boolValue = !m_ShowDataName.boolValue;
|
||||
}
|
||||
var iconRect = new Rect(pos.width - nameWid + 14, drawRect.y, nameWid, pos.height);
|
||||
if (GUI.Button(iconRect, new GUIContent("Icon")))
|
||||
{
|
||||
m_ShowDataIcon.boolValue = !m_ShowDataIcon.boolValue;
|
||||
}
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
|
||||
var listSize = m_Datas.arraySize;
|
||||
@@ -172,6 +177,7 @@ namespace XCharts
|
||||
if (m_DataDimension.intValue < 1) m_DataDimension.intValue = 1;
|
||||
int dimension = m_DataDimension.intValue;
|
||||
bool showName = m_ShowDataName.boolValue;
|
||||
bool showIcon = m_ShowDataIcon.boolValue;
|
||||
bool showSelected = (serieType == SerieType.Pie);
|
||||
if (listSize != m_Datas.arraySize)
|
||||
{
|
||||
@@ -185,20 +191,20 @@ namespace XCharts
|
||||
int num = listSize > 10 ? 10 : listSize;
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
DrawDataElement(ref drawRect, dimension, m_Datas, showName, showSelected, i, pos.width);
|
||||
DrawDataElement(ref drawRect, dimension, m_Datas, showName, showIcon, showSelected, i, pos.width);
|
||||
}
|
||||
if (num >= 10)
|
||||
{
|
||||
EditorGUI.LabelField(drawRect, "...");
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
DrawDataElement(ref drawRect, dimension, m_Datas, showName, showSelected, listSize - 1, pos.width);
|
||||
DrawDataElement(ref drawRect, dimension, m_Datas, showName, showIcon, showSelected, listSize - 1, pos.width);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < m_Datas.arraySize; i++)
|
||||
{
|
||||
DrawDataElement(ref drawRect, dimension, m_Datas, showName, showSelected, i, pos.width);
|
||||
DrawDataElement(ref drawRect, dimension, m_Datas, showName, showIcon, showSelected, i, pos.width);
|
||||
}
|
||||
}
|
||||
EditorGUI.indentLevel--;
|
||||
@@ -208,7 +214,7 @@ namespace XCharts
|
||||
}
|
||||
|
||||
private void DrawDataElement(ref Rect drawRect, int dimension, SerializedProperty m_Datas, bool showName,
|
||||
bool showSelected, int index, float currentWidth)
|
||||
bool showIconDetail, bool showSelected, int index, float currentWidth)
|
||||
{
|
||||
var lastX = drawRect.x;
|
||||
var lastWid = drawRect.width;
|
||||
@@ -217,6 +223,7 @@ namespace XCharts
|
||||
var serieData = m_Datas.GetArrayElementAtIndex(index);
|
||||
var sereName = serieData.FindPropertyRelative("m_Name");
|
||||
var selected = serieData.FindPropertyRelative("m_Selected");
|
||||
|
||||
var data = serieData.FindPropertyRelative("m_Data");
|
||||
var fieldCount = dimension + (showName ? 1 : 0);
|
||||
|
||||
@@ -238,7 +245,6 @@ namespace XCharts
|
||||
{
|
||||
EditorGUI.PropertyField(drawRect, element);
|
||||
}
|
||||
|
||||
drawRect.y += EditorGUI.GetPropertyHeight(element) + EditorGUIUtility.standardVerticalSpacing;
|
||||
}
|
||||
else
|
||||
@@ -265,12 +271,45 @@ namespace XCharts
|
||||
drawRect.width = dataWid + 40;
|
||||
EditorGUI.PropertyField(drawRect, sereName, GUIContent.none);
|
||||
}
|
||||
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
drawRect.x = lastX;
|
||||
drawRect.width = lastWid;
|
||||
EditorGUIUtility.fieldWidth = lastFieldWid;
|
||||
EditorGUIUtility.labelWidth = lastLabelWid;
|
||||
}
|
||||
if (showIconDetail)
|
||||
{
|
||||
EditorGUI.indentLevel++;
|
||||
var m_ShowIcon = serieData.FindPropertyRelative("m_ShowIcon");
|
||||
var m_IconImage = serieData.FindPropertyRelative("m_IconImage");
|
||||
var m_IconColor = serieData.FindPropertyRelative("m_IconColor");
|
||||
var m_IconWidth = serieData.FindPropertyRelative("m_IconWidth");
|
||||
var m_IconHeight = serieData.FindPropertyRelative("m_IconHeight");
|
||||
var m_IconOffset = serieData.FindPropertyRelative("m_IconOffset");
|
||||
EditorGUI.PropertyField(drawRect, m_ShowIcon);
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
EditorGUI.PropertyField(drawRect, m_IconImage);
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
EditorGUI.PropertyField(drawRect, m_IconColor);
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
EditorGUI.PropertyField(drawRect, m_IconWidth);
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
EditorGUI.PropertyField(drawRect, m_IconHeight);
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
|
||||
EditorGUI.LabelField(drawRect, "Icon Offset");
|
||||
var startX = drawRect.x + EditorGUIUtility.labelWidth - EditorGUI.indentLevel * 15;
|
||||
var tempWidth = (drawRect.width - startX + 72) / 2;
|
||||
var centerXRect = new Rect(startX, drawRect.y, tempWidth, drawRect.height);
|
||||
var centerYRect = new Rect(centerXRect.x + tempWidth - 58, drawRect.y, tempWidth, drawRect.height);
|
||||
var x = EditorGUI.FloatField(centerXRect, m_IconOffset.vector2Value.x);
|
||||
var y = EditorGUI.FloatField(centerYRect, m_IconOffset.vector2Value.y);
|
||||
m_IconOffset.vector2Value = new Vector2(x, y);
|
||||
drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
|
||||
|
||||
EditorGUI.indentLevel--;
|
||||
}
|
||||
}
|
||||
|
||||
public override float GetPropertyHeight(SerializedProperty prop, GUIContent label)
|
||||
@@ -317,7 +356,13 @@ namespace XCharts
|
||||
SerializedProperty m_Data = prop.FindPropertyRelative("m_Data");
|
||||
int num = m_Data.arraySize + 2;
|
||||
if (num > 30) num = 14;
|
||||
if (prop.FindPropertyRelative("m_ShowDataIcon").boolValue)
|
||||
{
|
||||
num *= 5;
|
||||
num += 2;
|
||||
}
|
||||
height += num * EditorGUIUtility.singleLineHeight + (num - 1) * EditorGUIUtility.standardVerticalSpacing;
|
||||
|
||||
}
|
||||
if (m_ShowJsonDataArea)
|
||||
{
|
||||
|
||||
@@ -144,6 +144,8 @@ namespace XCharts
|
||||
[SerializeField] private LineArrow m_LineArrow = new LineArrow();
|
||||
[SerializeField] [Range(1, 10)] private int m_ShowDataDimension;
|
||||
[SerializeField] private bool m_ShowDataName;
|
||||
[SerializeField] private bool m_ShowDataIcon;
|
||||
|
||||
[SerializeField] private List<SerieData> m_Data = new List<SerieData>();
|
||||
|
||||
[NonSerialized] private int m_FilterStart;
|
||||
@@ -846,6 +848,91 @@ namespace XCharts
|
||||
else if (m_BarGap <= 1) return GetBarWidth(categoryWidth) * m_BarGap;
|
||||
else return m_BarGap;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置所有数据的图标是否显示
|
||||
/// </summary>
|
||||
/// <param name="flag"></param>
|
||||
public void SetDataIconActive(bool flag)
|
||||
{
|
||||
foreach (var data in m_Data)
|
||||
{
|
||||
data.showIcon = flag;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定index的数据图标是否显示
|
||||
/// </summary>
|
||||
/// <param name="dataIndex"></param>
|
||||
/// <param name="flag"></param>
|
||||
public void SetDataIconActive(int dataIndex, bool flag)
|
||||
{
|
||||
if (dataIndex >= 0 && dataIndex < m_Data.Count)
|
||||
{
|
||||
var data = m_Data[dataIndex];
|
||||
data.showIcon = flag;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 统一设置图标的尺寸
|
||||
/// </summary>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
public void SetDataIconSize(float width, float height)
|
||||
{
|
||||
foreach (var data in m_Data)
|
||||
{
|
||||
data.iconWidth = width;
|
||||
data.iconHeight = height;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定index的数据图标的图片
|
||||
/// </summary>
|
||||
/// <param name="dataIndex"></param>
|
||||
/// <param name="image"></param>
|
||||
public void SetDataIcon(int dataIndex, Sprite image)
|
||||
{
|
||||
if (dataIndex >= 0 && dataIndex < m_Data.Count)
|
||||
{
|
||||
var data = m_Data[dataIndex];
|
||||
data.iconImage = image;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定index的数据图标的尺寸
|
||||
/// </summary>
|
||||
/// <param name="dataIndex"></param>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
public void SetDataIconSize(int dataIndex, float width, float height)
|
||||
{
|
||||
if (dataIndex >= 0 && dataIndex < m_Data.Count)
|
||||
{
|
||||
var data = m_Data[dataIndex];
|
||||
data.iconWidth = width;
|
||||
data.iconHeight = height;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定index的数据图标的颜色
|
||||
/// </summary>
|
||||
/// <param name="dataIndex"></param>
|
||||
/// <param name="color"></param>
|
||||
public void SetDataIconColor(int dataIndex, Color color)
|
||||
{
|
||||
if (dataIndex >= 0 && dataIndex < m_Data.Count)
|
||||
{
|
||||
var data = m_Data[dataIndex];
|
||||
data.iconColor = color;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从json中导入数据
|
||||
/// </summary>
|
||||
|
||||
@@ -295,6 +295,9 @@ namespace XCharts
|
||||
serieData.InitLabel(labelObj, isAutoSize, serie.label.paddingLeftRight, serie.label.paddingTopBottom);
|
||||
serieData.SetLabelActive(false);
|
||||
serieData.SetLabelText(serieData.name);
|
||||
|
||||
var iconObj = ChartHelper.AddIcon("Icon", labelObj.transform, serieData.iconWidth, serieData.iconHeight);
|
||||
serieData.SetIconObj(iconObj);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1178,6 +1178,7 @@ namespace XCharts
|
||||
serieData.SetLabelActive(true);
|
||||
if (serieData.SetLabelText(content)) RefreshChart();
|
||||
serieData.SetLabelPosition(serieData.labelPosition);
|
||||
serieData.UpdateIcon();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -14,6 +14,13 @@ namespace XCharts
|
||||
{
|
||||
[SerializeField] private string m_Name;
|
||||
[SerializeField] private bool m_Selected;
|
||||
[SerializeField] private bool m_ShowIcon;
|
||||
[SerializeField] private Sprite m_IconImage;
|
||||
[SerializeField] private Color m_IconColor = Color.white;
|
||||
[SerializeField] private float m_IconWidth = 40;
|
||||
[SerializeField] private float m_IconHeight = 40;
|
||||
[SerializeField] private Vector2 m_IconOffset;
|
||||
|
||||
[SerializeField] private List<float> m_Data = new List<float>();
|
||||
|
||||
private bool m_Show = true;
|
||||
@@ -32,6 +39,33 @@ namespace XCharts
|
||||
/// 该数据项是否被选中。
|
||||
/// </summary>
|
||||
public bool selected { get { return m_Selected; } set { m_Selected = value; } }
|
||||
/// <summary>
|
||||
/// Whether the data icon is show.
|
||||
/// 是否显示图标。
|
||||
/// </summary>
|
||||
public bool showIcon { get { return m_ShowIcon; } set { m_ShowIcon = value; } }
|
||||
/// <summary>
|
||||
/// The image of icon.
|
||||
/// 图标的图片。
|
||||
/// </summary>
|
||||
public Sprite iconImage { get { return m_IconImage; } set { m_IconImage = value; } }
|
||||
/// <summary>
|
||||
/// 图标颜色。
|
||||
/// </summary>
|
||||
public Color iconColor { get { return m_IconColor; } set { m_IconColor = value; } }
|
||||
/// <summary>
|
||||
/// 图标宽。
|
||||
/// </summary>
|
||||
public float iconWidth { get { return m_IconWidth; } set { m_IconWidth = value; } }
|
||||
/// <summary>
|
||||
/// 图标高。
|
||||
/// </summary>
|
||||
public float iconHeight { get { return m_IconHeight; } set { m_IconHeight = value; } }
|
||||
/// <summary>
|
||||
/// 图标偏移。
|
||||
/// </summary>
|
||||
public Vector2 iconOffset { get { return m_IconOffset; } set { m_IconOffset = value; } }
|
||||
|
||||
/// <summary>
|
||||
/// An arbitrary dimension data list of data item.
|
||||
/// 可指定任意维数的数值列表。
|
||||
@@ -53,6 +87,9 @@ namespace XCharts
|
||||
/// </summary>
|
||||
public Text labelText { get; private set; }
|
||||
public RectTransform labelRect { get; private set; }
|
||||
/// <summary>
|
||||
/// 标志位置。
|
||||
/// </summary>
|
||||
public Vector3 labelPosition { get; set; }
|
||||
/// <summary>
|
||||
/// 是否可以显示Label
|
||||
@@ -68,6 +105,8 @@ namespace XCharts
|
||||
/// 最小值。
|
||||
/// </summary>
|
||||
public float min { get { return m_Data.Min(); } }
|
||||
public Image icon { get; private set; }
|
||||
public RectTransform iconRect { get; private set; }
|
||||
|
||||
public float GetData(int index)
|
||||
{
|
||||
@@ -130,5 +169,28 @@ namespace XCharts
|
||||
m_Label.transform.localPosition = position;
|
||||
}
|
||||
}
|
||||
|
||||
public void SetIconObj(GameObject iconObj)
|
||||
{
|
||||
icon = iconObj.GetComponent<Image>();
|
||||
iconRect = iconObj.GetComponent<RectTransform>();
|
||||
UpdateIcon();
|
||||
}
|
||||
|
||||
public void UpdateIcon()
|
||||
{
|
||||
if (m_ShowIcon)
|
||||
{
|
||||
icon.gameObject.SetActive(true);
|
||||
icon.sprite = m_IconImage;
|
||||
icon.color = m_IconColor;
|
||||
iconRect.sizeDelta = new Vector2(m_IconWidth, m_IconHeight);
|
||||
icon.transform.localPosition = m_IconOffset;
|
||||
}
|
||||
else
|
||||
{
|
||||
icon.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,6 +191,16 @@ namespace XCharts
|
||||
return tooltipObj;
|
||||
}
|
||||
|
||||
public static GameObject AddIcon(string name,Transform parent,float width,float height){
|
||||
var anchorMax = new Vector2(0.5f, 0.5f);
|
||||
var anchorMin = new Vector2(0.5f, 0.5f);
|
||||
var pivot = new Vector2(0.5f, 0.5f);
|
||||
var sizeDelta = new Vector2(width, height);
|
||||
GameObject iconObj = AddObject(name, parent, anchorMin, anchorMax, pivot, sizeDelta);
|
||||
GetOrAddComponent<Image>(iconObj);
|
||||
return iconObj;
|
||||
}
|
||||
|
||||
public static GameObject AddSerieLabel(string name, Transform parent, Font font, Color textColor, Color backgroundColor,
|
||||
int fontSize, FontStyle fontStyle, float rotate, float width, float height)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user