mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-22 17:00:08 +00:00
增加Axis的onLabelClick回调事件
This commit is contained in:
@@ -80,6 +80,7 @@ slug: /changelog
|
|||||||
|
|
||||||
## master
|
## master
|
||||||
|
|
||||||
|
* (2025.03.27) 增加`Axis`的`onLabelClick`回调事件
|
||||||
* (2025.03.26) 增加`Animation`的`Exchange`排序交换动画
|
* (2025.03.26) 增加`Animation`的`Exchange`排序交换动画
|
||||||
* (2025.03.22) 增加`Comment`的`layer`设置层级
|
* (2025.03.22) 增加`Comment`的`layer`设置层级
|
||||||
* (2025.03.21) 优化`Comment`的坐标刷新
|
* (2025.03.21) 优化`Comment`的坐标刷新
|
||||||
|
|||||||
@@ -115,6 +115,13 @@ namespace XCharts.Runtime
|
|||||||
|
|
||||||
public AxisContext context = new AxisContext();
|
public AxisContext context = new AxisContext();
|
||||||
|
|
||||||
|
private Action<Vector3, int, string> m_OnLabelClick;
|
||||||
|
/// <summary>
|
||||||
|
/// Callback function when click on the label. Parameters: clickPos, labelIndex, labelName.
|
||||||
|
/// ||点击文本标签回调函数。参数:clickPos, labelIndex, labelName。
|
||||||
|
/// </summary>
|
||||||
|
[Since("v3.15.0")]
|
||||||
|
public Action<Vector3, int, string> onLabelClick { internal get { return m_OnLabelClick; } set { m_OnLabelClick = value; } }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Whether to show axis.
|
/// Whether to show axis.
|
||||||
/// ||是否显示坐标轴。
|
/// ||是否显示坐标轴。
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.EventSystems;
|
||||||
using UnityEngine.UI;
|
using UnityEngine.UI;
|
||||||
using XCharts.Runtime;
|
using XCharts.Runtime;
|
||||||
using XUGL;
|
using XUGL;
|
||||||
@@ -32,6 +33,33 @@ namespace XCharts
|
|||||||
|
|
||||||
protected virtual Orient orient { get; set; }
|
protected virtual Orient orient { get; set; }
|
||||||
|
|
||||||
|
public override void OnPointerClick(PointerEventData eventData)
|
||||||
|
{
|
||||||
|
if (component.onLabelClick == null) return;
|
||||||
|
var labelObjects = component.context.labelObjectList;
|
||||||
|
for (int i = 0; i < labelObjects.Count; i++)
|
||||||
|
{
|
||||||
|
var label = labelObjects[i];
|
||||||
|
if (label == null) continue;
|
||||||
|
if (label.InRect(chart.pointerPos))
|
||||||
|
{
|
||||||
|
component.onLabelClick.Invoke(chart.pointerPos, i, label.text.text.text);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// public override void DrawTop(VertexHelper vh)
|
||||||
|
// {
|
||||||
|
// var color = Color.red;
|
||||||
|
// color.a = 0.5f;
|
||||||
|
// foreach (var label in component.context.labelObjectList)
|
||||||
|
// {
|
||||||
|
// if (label == null) continue;
|
||||||
|
// UGL.DrawRectangle(vh, label.rect, color);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
protected virtual void UpdatePointerValue(Axis axis)
|
protected virtual void UpdatePointerValue(Axis axis)
|
||||||
{
|
{
|
||||||
var grid = chart.GetChartComponent<GridCoord>(axis.gridIndex);
|
var grid = chart.GetChartComponent<GridCoord>(axis.gridIndex);
|
||||||
|
|||||||
@@ -53,6 +53,11 @@ namespace XCharts.Runtime
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool InRect(Vector2 local)
|
||||||
|
{
|
||||||
|
return rect.Contains(local);
|
||||||
|
}
|
||||||
|
|
||||||
protected override void Awake()
|
protected override void Awake()
|
||||||
{
|
{
|
||||||
raycastTarget = false;
|
raycastTarget = false;
|
||||||
@@ -183,6 +188,7 @@ namespace XCharts.Runtime
|
|||||||
public void SetPosition(Vector3 position)
|
public void SetPosition(Vector3 position)
|
||||||
{
|
{
|
||||||
transform.localPosition = position;
|
transform.localPosition = position;
|
||||||
|
UpdateRect();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetRectPosition(Vector3 position)
|
public void SetRectPosition(Vector3 position)
|
||||||
@@ -264,6 +270,45 @@ namespace XCharts.Runtime
|
|||||||
m_Width = sizeDelta.x + m_PaddingLeft + m_PaddingRight;
|
m_Width = sizeDelta.x + m_PaddingLeft + m_PaddingRight;
|
||||||
m_Height = sizeDelta.y + m_PaddingTop + m_PaddingBottom;
|
m_Height = sizeDelta.y + m_PaddingTop + m_PaddingBottom;
|
||||||
objectRect.sizeDelta = new Vector2(m_Width, m_Height);
|
objectRect.sizeDelta = new Vector2(m_Width, m_Height);
|
||||||
|
UpdateRect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateRect()
|
||||||
|
{
|
||||||
|
if (m_TextRect == null) return;
|
||||||
|
switch (text.alignment)
|
||||||
|
{
|
||||||
|
case TextAnchor.LowerLeft:
|
||||||
|
rect = new Rect(transform.localPosition.x, transform.localPosition.y, m_Width, m_Height);
|
||||||
|
break;
|
||||||
|
case TextAnchor.UpperLeft:
|
||||||
|
rect = new Rect(transform.localPosition.x, transform.localPosition.y - m_Height, m_Width, m_Height);
|
||||||
|
break;
|
||||||
|
case TextAnchor.MiddleLeft:
|
||||||
|
rect = new Rect(transform.localPosition.x, transform.localPosition.y - m_Height / 2, m_Width, m_Height);
|
||||||
|
break;
|
||||||
|
case TextAnchor.LowerRight:
|
||||||
|
rect = new Rect(transform.localPosition.x - m_Width, transform.localPosition.y, m_Width, m_Height);
|
||||||
|
break;
|
||||||
|
case TextAnchor.UpperRight:
|
||||||
|
rect = new Rect(transform.localPosition.x - m_Width, transform.localPosition.y - m_Height, m_Width, m_Height);
|
||||||
|
break;
|
||||||
|
case TextAnchor.MiddleRight:
|
||||||
|
rect = new Rect(transform.localPosition.x - m_Width, transform.localPosition.y - m_Height / 2, m_Width, m_Height);
|
||||||
|
break;
|
||||||
|
case TextAnchor.LowerCenter:
|
||||||
|
rect = new Rect(transform.localPosition.x - m_Width / 2, transform.localPosition.y, m_Width, m_Height);
|
||||||
|
break;
|
||||||
|
case TextAnchor.UpperCenter:
|
||||||
|
rect = new Rect(transform.localPosition.x - m_Width / 2, transform.localPosition.y - m_Height, m_Width, m_Height);
|
||||||
|
break;
|
||||||
|
case TextAnchor.MiddleCenter:
|
||||||
|
rect = new Rect(transform.localPosition.x - m_Width / 2, transform.localPosition.y - m_Height / 2, m_Width, m_Height);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
rect = new Rect(transform.localPosition.x - m_Width / 2, transform.localPosition.y - m_Height / 2, m_Width, m_Height);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user