mirror of
https://github.com/XCharts-Team/XCharts.git
synced 2026-05-20 23:40:10 +00:00
Tooltip增加crossLabel显示十字准星指示器
This commit is contained in:
@@ -244,10 +244,21 @@ namespace XCharts
|
||||
|
||||
private void InitTooltip()
|
||||
{
|
||||
GameObject obj = ChartHelper.AddTooltipObject("tooltip", transform, m_ThemeInfo.font);
|
||||
m_Tooltip.SetObj(obj);
|
||||
m_Tooltip.SetBackgroundColor(m_ThemeInfo.tooltipBackgroundColor);
|
||||
m_Tooltip.SetTextColor(m_ThemeInfo.tooltipTextColor);
|
||||
var tooltipObject = ChartHelper.AddObject("tooltip", transform, chartAnchorMin,
|
||||
chartAnchorMax, chartPivot, new Vector2(chartWidth, chartHeight));
|
||||
tooltipObject.transform.localPosition = Vector3.zero;
|
||||
DestroyImmediate(tooltipObject.GetComponent<Image>());
|
||||
ChartHelper.HideAllObject(tooltipObject.transform);
|
||||
GameObject content = ChartHelper.AddTooltipContent("content", tooltipObject.transform, m_ThemeInfo.font);
|
||||
GameObject labelX = ChartHelper.AddTooltipLabel("label_x", tooltipObject.transform, m_ThemeInfo.font, new Vector2(0.5f, 1));
|
||||
GameObject labelY = ChartHelper.AddTooltipLabel("label_y", tooltipObject.transform, m_ThemeInfo.font, new Vector2(1, 0.5f));
|
||||
m_Tooltip.SetObj(tooltipObject);
|
||||
m_Tooltip.SetContentObj(content);
|
||||
m_Tooltip.SetLabelObj(labelX, labelY);
|
||||
m_Tooltip.SetContentBackgroundColor(m_ThemeInfo.tooltipBackgroundColor);
|
||||
m_Tooltip.SetContentTextColor(m_ThemeInfo.tooltipTextColor);
|
||||
m_Tooltip.SetLabelBackgroundColor(m_ThemeInfo.tooltipLabelColor);
|
||||
m_Tooltip.SetLabelTextColor(m_ThemeInfo.tooltipTextColor);
|
||||
m_Tooltip.SetActive(false);
|
||||
}
|
||||
|
||||
@@ -302,7 +313,9 @@ namespace XCharts
|
||||
private void CheckTooltip()
|
||||
{
|
||||
if (!m_Tooltip.show || !m_Tooltip.isInited) return;
|
||||
m_Tooltip.SetLabelActive(m_Tooltip.crossLabel);
|
||||
m_Tooltip.dataIndex = 0;
|
||||
|
||||
Vector2 local;
|
||||
|
||||
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform,
|
||||
@@ -312,7 +325,7 @@ namespace XCharts
|
||||
if (local.x < 0 || local.x > chartWidth ||
|
||||
local.y < 0 || local.y > chartHeight)
|
||||
return;
|
||||
|
||||
m_Tooltip.pointerPos = local;
|
||||
CheckTootipArea(local);
|
||||
}
|
||||
|
||||
@@ -342,6 +355,7 @@ namespace XCharts
|
||||
}
|
||||
InitTitle();
|
||||
InitLegend();
|
||||
InitTooltip();
|
||||
}
|
||||
|
||||
protected virtual void OnTitleChanged()
|
||||
|
||||
@@ -129,9 +129,9 @@ namespace XCharts
|
||||
}
|
||||
if (m_Tooltip.dataIndex > 0)
|
||||
{
|
||||
m_Tooltip.UpdatePos(new Vector2(local.x + 18, local.y - 25));
|
||||
m_Tooltip.UpdateContentPos(new Vector2(local.x + 18, local.y - 25));
|
||||
RefreshTooltip();
|
||||
if (m_Tooltip.lastDataIndex != m_Tooltip.dataIndex)
|
||||
if (m_Tooltip.lastDataIndex != m_Tooltip.dataIndex || m_Tooltip.crossLabel)
|
||||
{
|
||||
RefreshChart();
|
||||
}
|
||||
@@ -152,8 +152,9 @@ namespace XCharts
|
||||
m_Tooltip.SetActive(true);
|
||||
if (m_Series.Count == 1)
|
||||
{
|
||||
string txt = tempAxis.GetData(index) + ": " + m_Series.GetData(0, index);
|
||||
m_Tooltip.UpdateTooltipText(txt);
|
||||
float value = m_Series.GetData(0, index);
|
||||
string txt = tempAxis.GetData(index) + ": " + value;
|
||||
m_Tooltip.UpdateContentText(txt);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -171,9 +172,33 @@ namespace XCharts
|
||||
}
|
||||
|
||||
}
|
||||
m_Tooltip.UpdateTooltipText(sb.ToString());
|
||||
m_Tooltip.UpdateContentText(sb.ToString());
|
||||
}
|
||||
var pos = m_Tooltip.GetPos();
|
||||
if(m_XAxis.type == Axis.AxisType.Value)
|
||||
{
|
||||
float hig = (maxValue - minValue) * (m_Tooltip.pointerPos.x - zeroX) / coordinateWid;
|
||||
m_Tooltip.UpdateLabelText(hig.ToString("f2"),tempAxis.GetData(index));
|
||||
float splitWidth = m_YAxis.GetSplitWidth(coordinateHig);
|
||||
float py = zeroY + (m_Tooltip.dataIndex - 1) * splitWidth
|
||||
+ (m_YAxis.boundaryGap ? splitWidth / 2 : 0);
|
||||
Vector2 xLabelPos = new Vector2(m_Tooltip.pointerPos.x,coordinateY- 4 * m_Coordinate.tickness);
|
||||
Vector2 yLabelPos = new Vector2(coordinateX - 6 * m_Coordinate.tickness,py);
|
||||
m_Tooltip.UpdateLabelPos(xLabelPos, yLabelPos);
|
||||
}
|
||||
else
|
||||
{
|
||||
float hig = (maxValue - minValue) * (m_Tooltip.pointerPos.y - zeroY) / coordinateHig;
|
||||
m_Tooltip.UpdateLabelText(tempAxis.GetData(index), hig.ToString("f2"));
|
||||
float splitWidth = m_XAxis.GetSplitWidth(coordinateWid);
|
||||
float px = zeroX + (m_Tooltip.dataIndex - 1) * splitWidth
|
||||
+ (m_XAxis.boundaryGap ? splitWidth / 2 : 0);
|
||||
Vector2 xLabelPos = new Vector2(px, coordinateY - 6 * m_Coordinate.tickness);
|
||||
Vector2 yLabelPos = new Vector2(coordinateX - 4 * m_Coordinate.tickness, m_Tooltip.pointerPos.y);
|
||||
m_Tooltip.UpdateLabelPos(xLabelPos, yLabelPos);
|
||||
}
|
||||
|
||||
|
||||
var pos = m_Tooltip.GetContentPos();
|
||||
if (pos.x + m_Tooltip.width > chartWidth)
|
||||
{
|
||||
pos.x = chartWidth - m_Tooltip.width;
|
||||
@@ -182,7 +207,7 @@ namespace XCharts
|
||||
{
|
||||
pos.y = m_Tooltip.height;
|
||||
}
|
||||
m_Tooltip.UpdatePos(pos);
|
||||
m_Tooltip.UpdateContentPos(pos);
|
||||
}
|
||||
|
||||
TextGenerationSettings GetTextSetting()
|
||||
@@ -293,12 +318,13 @@ namespace XCharts
|
||||
{
|
||||
if (m_XAxis.boundaryGap)
|
||||
{
|
||||
return new Vector3(coordinateX + (i + 1) * scaleWid, coordinateY - m_XAxis.axisTick.length - 5, 0);
|
||||
return new Vector3(coordinateX + (i + 1) * scaleWid,
|
||||
coordinateY - m_XAxis.axisTick.length - 12, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Vector3(coordinateX + (i + 1 - 0.5f) * scaleWid,
|
||||
coordinateY - m_XAxis.axisTick.length - 10, 0);
|
||||
coordinateY - m_XAxis.axisTick.length - 12, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -396,12 +422,14 @@ namespace XCharts
|
||||
maxValue = tempMaxValue;
|
||||
if (m_XAxis.type == Axis.AxisType.Value)
|
||||
{
|
||||
m_ZeroXOffset = minValue > 0 ? 0 : Mathf.Abs(minValue) * (coordinateWid / (Mathf.Abs(minValue) + Mathf.Abs(maxValue)));
|
||||
m_ZeroXOffset = minValue > 0 ? 0 :
|
||||
Mathf.Abs(minValue) * (coordinateWid / (Mathf.Abs(minValue) + Mathf.Abs(maxValue)));
|
||||
OnXMaxValueChanged();
|
||||
}
|
||||
else if (m_YAxis.type == Axis.AxisType.Value)
|
||||
{
|
||||
m_ZeroYOffset = minValue > 0 ? 0 : Mathf.Abs(minValue) * (coordinateHig / (Mathf.Abs(minValue) + Mathf.Abs(maxValue)));
|
||||
m_ZeroYOffset = minValue > 0 ? 0 :
|
||||
Mathf.Abs(minValue) * (coordinateHig / (Mathf.Abs(minValue) + Mathf.Abs(maxValue)));
|
||||
OnYMaxValueChanged();
|
||||
}
|
||||
RefreshChart();
|
||||
@@ -470,7 +498,7 @@ namespace XCharts
|
||||
if (m_YAxis.showSplitLine)
|
||||
{
|
||||
DrawSplitLine(vh, true, m_YAxis.splitLineType, new Vector3(coordinateX, pY),
|
||||
new Vector3(coordinateX + coordinateWid, pY));
|
||||
new Vector3(coordinateX + coordinateWid, pY), m_ThemeInfo.axisSplitLineColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -487,13 +515,13 @@ namespace XCharts
|
||||
if (m_XAxis.axisTick.show)
|
||||
{
|
||||
pY += zeroY - m_XAxis.axisTick.length - 2;
|
||||
ChartHelper.DrawLine(vh, new Vector3(pX, zeroY), new Vector3(pX, pY), m_Coordinate.tickness,
|
||||
m_ThemeInfo.axisLineColor);
|
||||
ChartHelper.DrawLine(vh, new Vector3(pX, zeroY), new Vector3(pX, pY),
|
||||
m_Coordinate.tickness, m_ThemeInfo.axisLineColor);
|
||||
}
|
||||
if (m_XAxis.showSplitLine)
|
||||
{
|
||||
DrawSplitLine(vh, false, m_XAxis.splitLineType, new Vector3(pX, coordinateY),
|
||||
new Vector3(pX, coordinateY + coordinateHig));
|
||||
new Vector3(pX, coordinateY + coordinateHig), m_ThemeInfo.axisSplitLineColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -534,8 +562,8 @@ namespace XCharts
|
||||
#endregion
|
||||
}
|
||||
|
||||
private void DrawSplitLine(VertexHelper vh, bool isYAxis, Axis.SplitLineType type, Vector3 startPos,
|
||||
Vector3 endPos)
|
||||
protected void DrawSplitLine(VertexHelper vh, bool isYAxis, Axis.SplitLineType type,
|
||||
Vector3 startPos, Vector3 endPos, Color color)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
@@ -552,22 +580,21 @@ namespace XCharts
|
||||
{
|
||||
var toX = startX + dashLen;
|
||||
ChartHelper.DrawLine(vh, new Vector3(startX, startY), new Vector3(toX, startY),
|
||||
m_Coordinate.tickness, m_ThemeInfo.axisSplitLineColor);
|
||||
m_Coordinate.tickness, color);
|
||||
startX += dashLen * 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
var toY = startY + dashLen;
|
||||
ChartHelper.DrawLine(vh, new Vector3(startX, startY), new Vector3(startX, toY),
|
||||
m_Coordinate.tickness, m_ThemeInfo.axisSplitLineColor);
|
||||
m_Coordinate.tickness, color);
|
||||
startY += dashLen * 2;
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
case Axis.SplitLineType.Solid:
|
||||
ChartHelper.DrawLine(vh, startPos, endPos, m_Coordinate.tickness,
|
||||
m_ThemeInfo.axisSplitLineColor);
|
||||
ChartHelper.DrawLine(vh, startPos, endPos, m_Coordinate.tickness, color);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,8 @@ namespace XCharts
|
||||
[SerializeField] private Color32 m_TooltipBackgroundColor;
|
||||
[SerializeField] private Color32 m_TooltipFlagAreaColor;
|
||||
[SerializeField] private Color32 m_TooltipTextColor;
|
||||
[SerializeField] private Color32 m_TooltipLabelColor;
|
||||
[SerializeField] private Color32 m_TooltipLineColor;
|
||||
[SerializeField] private Color32[] m_ColorPalette;
|
||||
|
||||
public Font font { get { return m_Font; } set { m_Font = value; } }
|
||||
@@ -40,6 +42,8 @@ namespace XCharts
|
||||
public Color32 tooltipBackgroundColor { get { return m_TooltipBackgroundColor; } set { m_TooltipBackgroundColor = value; } }
|
||||
public Color32 tooltipFlagAreaColor { get { return m_TooltipFlagAreaColor; } set { m_TooltipFlagAreaColor = value; } }
|
||||
public Color32 tooltipTextColor { get { return m_TooltipTextColor; } set { m_TooltipTextColor = value; } }
|
||||
public Color32 tooltipLabelColor { get { return m_TooltipLabelColor; } set { m_TooltipLabelColor = value; } }
|
||||
public Color32 tooltipLineColor { get { return m_TooltipLineColor; } set { m_TooltipLineColor = value; } }
|
||||
public Color32[] colorPalette { get { return m_ColorPalette; } set { m_ColorPalette = value; } }
|
||||
|
||||
public Color32 GetColor(int index)
|
||||
@@ -65,6 +69,8 @@ namespace XCharts
|
||||
m_AxisSplitLineColor = theme.m_AxisSplitLineColor;
|
||||
m_TooltipBackgroundColor = theme.m_TooltipBackgroundColor;
|
||||
m_TooltipTextColor = theme.m_TooltipTextColor;
|
||||
m_TooltipLabelColor = theme.m_TooltipLabelColor;
|
||||
m_TooltipLineColor = theme.m_TooltipLineColor;
|
||||
m_ColorPalette = new Color32[theme.m_ColorPalette.Length];
|
||||
for (int i = 0; i < theme.m_ColorPalette.Length; i++)
|
||||
{
|
||||
@@ -90,6 +96,8 @@ namespace XCharts
|
||||
m_TooltipBackgroundColor = GetColor("#515151B5"),
|
||||
m_TooltipTextColor = GetColor("#FFFFFFFF"),
|
||||
m_TooltipFlagAreaColor = GetColor("#51515120"),
|
||||
m_TooltipLabelColor = GetColor("#292929FF"),
|
||||
m_TooltipLineColor = GetColor("#29292964"),
|
||||
m_ColorPalette = new Color32[]
|
||||
{
|
||||
new Color32(194, 53, 49, 255),
|
||||
@@ -126,6 +134,8 @@ namespace XCharts
|
||||
m_TooltipBackgroundColor = GetColor("#515151B5"),
|
||||
m_TooltipTextColor = GetColor("#FFFFFFFF"),
|
||||
m_TooltipFlagAreaColor = GetColor("#51515120"),
|
||||
m_TooltipLabelColor = GetColor("#292929FF"),
|
||||
m_TooltipLineColor = GetColor("#29292964"),
|
||||
m_ColorPalette = new Color32[]
|
||||
{
|
||||
new Color32(55, 162, 218, 255),
|
||||
@@ -164,6 +174,8 @@ namespace XCharts
|
||||
m_TooltipBackgroundColor = GetColor("#515151B5"),
|
||||
m_TooltipTextColor = GetColor("#FFFFFFFF"),
|
||||
m_TooltipFlagAreaColor = GetColor("#51515120"),
|
||||
m_TooltipLabelColor = GetColor("#A7A7A7FF"),
|
||||
m_TooltipLineColor = GetColor("#eee"),
|
||||
m_ColorPalette = new Color32[]
|
||||
{
|
||||
new Color32(221, 107, 102, 255),
|
||||
|
||||
@@ -8,16 +8,27 @@ namespace XCharts
|
||||
public class Tooltip
|
||||
{
|
||||
[SerializeField] private bool m_Show;
|
||||
[SerializeField] private bool m_CrossLabel;
|
||||
|
||||
[NonSerialized] private GameObject m_GameObject;
|
||||
[NonSerialized] private Text m_Text;
|
||||
[NonSerialized] private RectTransform m_BackgroudRect;
|
||||
[NonSerialized] private GameObject m_Content;
|
||||
[NonSerialized] private GameObject m_LabelX;
|
||||
[NonSerialized] private GameObject m_LabelY;
|
||||
[NonSerialized] private Text m_ContentText;
|
||||
[NonSerialized] private Text m_LabelTextX;
|
||||
[NonSerialized] private Text m_LabelTextY;
|
||||
[NonSerialized] private RectTransform m_ContentRect;
|
||||
[NonSerialized] private RectTransform m_LabelRectX;
|
||||
[NonSerialized] private RectTransform m_LabelRectY;
|
||||
|
||||
public bool show { get { return m_Show; }set { m_Show = value; } }
|
||||
public bool crossLabel { get { return m_CrossLabel; } set { m_CrossLabel = value; } }
|
||||
|
||||
public int dataIndex { get; set; }
|
||||
public int lastDataIndex { get; set; }
|
||||
public float width { get { return m_BackgroudRect.sizeDelta.x; } }
|
||||
public float height { get { return m_BackgroudRect.sizeDelta.y; } }
|
||||
public Vector2 pointerPos { get; set; }
|
||||
public float width { get { return m_ContentRect.sizeDelta.x; } }
|
||||
public float height { get { return m_ContentRect.sizeDelta.y; } }
|
||||
public bool isInited { get { return m_GameObject != null; } }
|
||||
|
||||
public static Tooltip defaultTooltip
|
||||
@@ -26,7 +37,8 @@ namespace XCharts
|
||||
{
|
||||
var tooltip = new Tooltip
|
||||
{
|
||||
m_Show = true
|
||||
m_Show = true,
|
||||
m_CrossLabel = false
|
||||
};
|
||||
return tooltip;
|
||||
}
|
||||
@@ -35,29 +47,73 @@ namespace XCharts
|
||||
public void SetObj(GameObject obj)
|
||||
{
|
||||
m_GameObject = obj;
|
||||
m_BackgroudRect = m_GameObject.GetComponent<RectTransform>();
|
||||
m_Text = m_GameObject.GetComponentInChildren<Text>();
|
||||
}
|
||||
|
||||
public void SetBackgroundColor(Color color)
|
||||
public void SetContentObj(GameObject content)
|
||||
{
|
||||
m_GameObject.GetComponent<Image>().color = color;
|
||||
m_Content = content;
|
||||
m_ContentRect = m_Content.GetComponent<RectTransform>();
|
||||
m_ContentText = m_Content.GetComponentInChildren<Text>();
|
||||
}
|
||||
|
||||
public void SetTextColor(Color color)
|
||||
public void SetLabelObj(GameObject labelX,GameObject labelY)
|
||||
{
|
||||
if (m_Text)
|
||||
m_LabelX = labelX;
|
||||
m_LabelRectX = labelX.GetComponent<RectTransform>();
|
||||
m_LabelTextX = labelX.GetComponentInChildren<Text>();
|
||||
m_LabelY = labelY;
|
||||
m_LabelRectY = labelY.GetComponent<RectTransform>();
|
||||
m_LabelTextY = labelY.GetComponentInChildren<Text>();
|
||||
}
|
||||
|
||||
public void SetContentBackgroundColor(Color color)
|
||||
{
|
||||
m_Content.GetComponent<Image>().color = color;
|
||||
}
|
||||
|
||||
public void SetContentTextColor(Color color)
|
||||
{
|
||||
if (m_ContentText)
|
||||
{
|
||||
m_Text.color = color;
|
||||
m_ContentText.color = color;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateTooltipText(string txt)
|
||||
public void SetLabelBackgroundColor(Color color)
|
||||
{
|
||||
if (m_Text)
|
||||
m_LabelX.GetComponent<Image>().color = color;
|
||||
m_LabelY.GetComponent<Image>().color = color;
|
||||
}
|
||||
|
||||
public void SetLabelTextColor(Color color)
|
||||
{
|
||||
m_LabelTextX.color = color;
|
||||
m_LabelTextY.color = color;
|
||||
}
|
||||
|
||||
public void UpdateContentText(string txt)
|
||||
{
|
||||
if (m_ContentText)
|
||||
{
|
||||
m_Text.text = txt;
|
||||
m_BackgroudRect.sizeDelta = new Vector2(m_Text.preferredWidth + 8, m_Text.preferredHeight + 8);
|
||||
m_ContentText.text = txt;
|
||||
m_ContentRect.sizeDelta = new Vector2(m_ContentText.preferredWidth + 8,
|
||||
m_ContentText.preferredHeight + 8);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateLabelText(string labelX,string labelY)
|
||||
{
|
||||
if (m_LabelTextX)
|
||||
{
|
||||
m_LabelTextX.text = labelX;
|
||||
m_LabelRectX.sizeDelta = new Vector2(m_LabelTextX.preferredWidth + 8,
|
||||
m_LabelTextX.preferredHeight + 8);
|
||||
}
|
||||
if (m_LabelTextY)
|
||||
{
|
||||
m_LabelTextY.text = labelY;
|
||||
m_LabelRectY.sizeDelta = new Vector2(m_LabelTextY.preferredWidth + 8,
|
||||
m_LabelTextY.preferredHeight + 8);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,16 +123,34 @@ namespace XCharts
|
||||
m_GameObject.SetActive(flag);
|
||||
}
|
||||
|
||||
public void UpdatePos(Vector2 pos)
|
||||
public void SetLabelActive(bool flag)
|
||||
{
|
||||
if(m_GameObject)
|
||||
m_GameObject.transform.localPosition = pos;
|
||||
if (m_LabelX && m_LabelX.activeInHierarchy != flag) m_LabelX.SetActive(flag);
|
||||
if (m_LabelY && m_LabelY.activeInHierarchy != flag) m_LabelY.SetActive(flag);
|
||||
}
|
||||
|
||||
public Vector3 GetPos()
|
||||
public void UpdateContentPos(Vector2 pos)
|
||||
{
|
||||
if (m_GameObject)
|
||||
return m_GameObject.transform.localPosition;
|
||||
if(m_Content)
|
||||
m_Content.transform.localPosition = pos;
|
||||
}
|
||||
|
||||
public void UpdateLabelPos(Vector2 xLabelPos,Vector2 yLabelPos)
|
||||
{
|
||||
if (m_LabelX)
|
||||
{
|
||||
m_LabelX.transform.localPosition = xLabelPos;
|
||||
}
|
||||
if (m_LabelY)
|
||||
{
|
||||
m_LabelY.transform.localPosition = yLabelPos;
|
||||
}
|
||||
}
|
||||
|
||||
public Vector3 GetContentPos()
|
||||
{
|
||||
if (m_Content)
|
||||
return m_Content.transform.localPosition;
|
||||
else
|
||||
return Vector3.zero;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user