[feature] 增加AxisindicatorLabel,移除TooltipindicatorLabelStyle (#226)

This commit is contained in:
monitor1394
2022-10-30 10:28:17 +08:00
parent e0974fffbe
commit c9d554d184
15 changed files with 152 additions and 103 deletions

View File

@@ -103,6 +103,7 @@ namespace XCharts.Runtime
[SerializeField] protected AxisSplitArea m_SplitArea = AxisSplitArea.defaultSplitArea;
[SerializeField][Since("v3.2.0")] protected AxisMinorTick m_MinorTick = AxisMinorTick.defaultMinorTick;
[SerializeField][Since("v3.2.0")] protected AxisMinorSplitLine m_MinorSplitLine = AxisMinorSplitLine.defaultMinorSplitLine;
[SerializeField][Since("v3.4.0")] protected LabelStyle m_IndicatorLabel = new LabelStyle();
public AxisContext context = new AxisContext();
@@ -372,6 +373,15 @@ namespace XCharts.Runtime
set { if (value != null) { m_MinorSplitLine = value; SetVerticesDirty(); } }
}
/// <summary>
/// Style of axis tooltip indicator label.
/// |指示器文本的样式。Tooltip为Cross时使用。
/// </summary>
public LabelStyle indicatorLabel
{
get { return m_IndicatorLabel; }
set { if (value != null) { m_IndicatorLabel = value; SetComponentDirty(); } }
}
/// <summary>
/// Whether to add new data at the head or at the end of the list.
/// |添加新数据时是在列表的头部还是尾部加入。
/// </summary>
@@ -401,7 +411,8 @@ namespace XCharts.Runtime
{
return m_ComponentDirty ||
axisName.anyDirty ||
axisLabel.anyDirty;
axisLabel.anyDirty ||
indicatorLabel.anyDirty;
}
}
@@ -410,6 +421,7 @@ namespace XCharts.Runtime
base.ClearComponentDirty();
axisName.ClearComponentDirty();
axisLabel.ClearComponentDirty();
indicatorLabel.ClearComponentDirty();
}
public override void ClearVerticesDirty()
@@ -422,6 +434,7 @@ namespace XCharts.Runtime
splitArea.ClearVerticesDirty();
minorTick.ClearVerticesDirty();
minorSplitLine.ClearVerticesDirty();
indicatorLabel.ClearComponentDirty();
}
public override void SetComponentDirty()
@@ -455,6 +468,7 @@ namespace XCharts.Runtime
axis.splitArea = splitArea.Clone();
axis.minorTick = minorTick.Clone();
axis.minorSplitLine = minorSplitLine.Clone();
axis.indicatorLabel = indicatorLabel.Clone();
axis.icons = new List<Sprite>();
axis.data = new List<string>();
ChartHelper.CopyList(axis.data, data);
@@ -485,6 +499,7 @@ namespace XCharts.Runtime
splitArea.Copy(axis.splitArea);
minorTick.Copy(axis.minorTick);
minorSplitLine.Copy(axis.minorSplitLine);
indicatorLabel.Copy(axis.indicatorLabel);
ChartHelper.CopyList(data, axis.data);
ChartHelper.CopyList<Sprite>(icons, axis.icons);
}
@@ -719,8 +734,6 @@ namespace XCharts.Runtime
}
}
/// <summary>
/// 获得指定区域缩放的类目数据列表
/// </summary>

View File

@@ -56,6 +56,7 @@ namespace XCharts.Runtime
public double pointerValue;
public Vector3 pointerLabelPosition;
public double axisTooltipValue;
public TextAnchor aligment;
public List<string> runtimeData { get { return m_RuntimeData; } }
public List<double> labelValueList { get { return m_LabelValueList; } }
public List<ChartLabel> labelObjectList { get { return m_AxisLabelList; } }

View File

@@ -373,6 +373,7 @@ namespace XCharts
TextAnchor.MiddleRight);
if (axis.IsCategory() && axis.boundaryGap)
splitNumber -= 1;
axis.context.aligment = defaultAlignment;
for (int i = 0; i < splitNumber; i++)
{
var labelWidth = AxisHelper.GetScaleWidth(axis, axisLength, i + 1, dataZoom);

View File

@@ -143,7 +143,7 @@ namespace XCharts.Runtime
m_TextLimit.SetRelatedText(txt, labelWidth);
}
public string GetFormatterContent(int labelIndex, string category)
public override string GetFormatterContent(int labelIndex, string category)
{
if (string.IsNullOrEmpty(category))
return GetFormatterFunctionContent(labelIndex, category, category);
@@ -160,71 +160,13 @@ namespace XCharts.Runtime
}
}
public string GetFormatterContent(int labelIndex, double value, double minValue, double maxValue, bool isLog = false)
public override string GetFormatterContent(int labelIndex, double value, double minValue, double maxValue, bool isLog = false)
{
if (showAsPositiveNumber && value < 0)
{
value = Math.Abs(value);
}
if (string.IsNullOrEmpty(m_Formatter))
{
if (isLog)
{
return GetFormatterFunctionContent(labelIndex, value, ChartCached.NumberToStr(value, numericFormatter));
}
if (minValue >= -1 && minValue <= 1 && maxValue >= -1 && maxValue <= 1)
{
int minAcc = ChartHelper.GetFloatAccuracy(minValue);
int maxAcc = ChartHelper.GetFloatAccuracy(maxValue);
int curAcc = ChartHelper.GetFloatAccuracy(value);
int acc = Mathf.Max(Mathf.Max(minAcc, maxAcc), curAcc);
return GetFormatterFunctionContent(labelIndex, value, ChartCached.FloatToStr(value, numericFormatter, acc));
}
return GetFormatterFunctionContent(labelIndex, value, ChartCached.NumberToStr(value, numericFormatter));
}
else
{
var content = m_Formatter;
FormatterHelper.ReplaceAxisLabelContent(ref content, numericFormatter, value);
return GetFormatterFunctionContent(labelIndex, value, content);
}
}
public string GetFormatterDateTime(int labelIndex, double value, double minValue, double maxValue)
{
var timestamp = (int) value;
var dateTime = DateTimeUtil.GetDateTime(timestamp);
var dateString = string.Empty;
if (string.IsNullOrEmpty(numericFormatter))
{
dateString = DateTimeUtil.GetDateTimeFormatString(dateTime, maxValue - minValue);
}
else
{
dateString = dateTime.ToString(numericFormatter);
}
if (!string.IsNullOrEmpty(m_Formatter))
{
var content = m_Formatter;
FormatterHelper.ReplaceAxisLabelContent(ref content, dateString);
return GetFormatterFunctionContent(labelIndex, value, m_TextLimit.GetLimitContent(content));
}
else
{
return GetFormatterFunctionContent(labelIndex, value, m_TextLimit.GetLimitContent(dateString));
}
}
private string GetFormatterFunctionContent(int labelIndex, string category, string currentContent)
{
return m_FormatterFunction == null ? currentContent :
m_FormatterFunction(labelIndex, labelIndex, category, currentContent);
}
private string GetFormatterFunctionContent(int labelIndex, double value, string currentContent)
{
return m_FormatterFunction == null ? currentContent :
m_FormatterFunction(labelIndex, labelIndex, null, currentContent);
return base.GetFormatterContent(labelIndex, value, minValue, maxValue, isLog);
}
}
}

View File

@@ -1,3 +1,4 @@
using System;
using UnityEngine;
namespace XCharts.Runtime
@@ -315,5 +316,85 @@ namespace XCharts.Runtime
m_TextPadding = label.m_TextPadding;
m_TextStyle.Copy(label.m_TextStyle);
}
public virtual string GetFormatterContent(int labelIndex, string category)
{
if (string.IsNullOrEmpty(category))
return GetFormatterFunctionContent(labelIndex, category, category);
if (string.IsNullOrEmpty(m_Formatter))
{
return GetFormatterFunctionContent(labelIndex, category, category);
}
else
{
var content = m_Formatter;
FormatterHelper.ReplaceAxisLabelContent(ref content, category);
return GetFormatterFunctionContent(labelIndex, category, category);
}
}
public virtual string GetFormatterContent(int labelIndex, double value, double minValue, double maxValue, bool isLog = false)
{
if (string.IsNullOrEmpty(m_Formatter))
{
if (isLog)
{
return GetFormatterFunctionContent(labelIndex, value, ChartCached.NumberToStr(value, numericFormatter));
}
if (minValue >= -1 && minValue <= 1 && maxValue >= -1 && maxValue <= 1)
{
int minAcc = ChartHelper.GetFloatAccuracy(minValue);
int maxAcc = ChartHelper.GetFloatAccuracy(maxValue);
int curAcc = ChartHelper.GetFloatAccuracy(value);
int acc = Mathf.Max(Mathf.Max(minAcc, maxAcc), curAcc);
return GetFormatterFunctionContent(labelIndex, value, ChartCached.FloatToStr(value, numericFormatter, acc));
}
return GetFormatterFunctionContent(labelIndex, value, ChartCached.NumberToStr(value, numericFormatter));
}
else
{
var content = m_Formatter;
FormatterHelper.ReplaceAxisLabelContent(ref content, numericFormatter, value);
return GetFormatterFunctionContent(labelIndex, value, content);
}
}
public string GetFormatterDateTime(int labelIndex, double value, double minValue, double maxValue)
{
var timestamp = (int) value;
var dateTime = DateTimeUtil.GetDateTime(timestamp);
var dateString = string.Empty;
if (string.IsNullOrEmpty(numericFormatter))
{
dateString = DateTimeUtil.GetDateTimeFormatString(dateTime, maxValue - minValue);
}
else
{
dateString = dateTime.ToString(numericFormatter);
}
if (!string.IsNullOrEmpty(m_Formatter))
{
var content = m_Formatter;
FormatterHelper.ReplaceAxisLabelContent(ref content, dateString);
return GetFormatterFunctionContent(labelIndex, value, content);
}
else
{
return GetFormatterFunctionContent(labelIndex, value, dateString);
}
}
protected string GetFormatterFunctionContent(int labelIndex, string category, string currentContent)
{
return m_FormatterFunction == null ? currentContent :
m_FormatterFunction(labelIndex, labelIndex, category, currentContent);
}
protected string GetFormatterFunctionContent(int labelIndex, double value, string currentContent)
{
return m_FormatterFunction == null ? currentContent :
m_FormatterFunction(labelIndex, labelIndex, null, currentContent);
}
}
}

View File

@@ -120,7 +120,6 @@ namespace XCharts.Runtime
[SerializeField] private float m_ItemHeight = 25f;
[SerializeField] private Color32 m_BorderColor = new Color32(230, 230, 230, 255);
[SerializeField] private LineStyle m_LineStyle = new LineStyle(LineStyle.Type.None);
[SerializeField] private LabelStyle m_IndicatorLabelStyle = new LabelStyle();
[SerializeField]
private LabelStyle m_TitleLabelStyle = new LabelStyle()
{
@@ -378,15 +377,6 @@ namespace XCharts.Runtime
set { if (PropertyUtil.SetStruct(ref m_ItemHeight, value)) SetComponentDirty(); }
}
/// <summary>
/// the label style of tooltip axis indicator label.
/// |提示框的坐标轴指示器文本的样式。
/// </summary>
public LabelStyle indicatorLabelStyle
{
get { return m_IndicatorLabelStyle; }
set { if (value != null) { m_IndicatorLabelStyle = value; SetComponentDirty(); } }
}
/// <summary>
/// the textstyle of title.
/// |标题的文本样式。
/// </summary>
@@ -420,14 +410,13 @@ namespace XCharts.Runtime
/// </summary>
public override bool componentDirty
{
get { return m_ComponentDirty || lineStyle.componentDirty || indicatorLabelStyle.componentDirty; }
get { return m_ComponentDirty || lineStyle.componentDirty; }
}
public override void ClearComponentDirty()
{
base.ClearComponentDirty();
lineStyle.ClearComponentDirty();
indicatorLabelStyle.ClearComponentDirty();
}
/// <summary>
/// 当前提示框所指示的Serie索引目前只对散点图有效

View File

@@ -9,7 +9,7 @@ namespace XCharts.Runtime
[UnityEngine.Scripting.Preserve]
internal sealed class TooltipHandler : MainComponentHandler<Tooltip>
{
private List<ChartLabel> m_IndicatorLabels = new List<ChartLabel>();
private Dictionary<string, ChartLabel> m_IndicatorLabels = new Dictionary<string, ChartLabel>();
private GameObject m_LabelRoot;
private ISerieContainer m_PointerContainer;
@@ -52,28 +52,35 @@ namespace XCharts.Runtime
chart.chartMaxAnchor, chart.chartPivot, chart.chartSizeDelta);
ChartHelper.HideAllObject(m_LabelRoot);
m_IndicatorLabels.Clear();
for (int i = 0; i < 2; i++)
foreach (var com in chart.components)
{
var labelName = "label_" + i;
var item = ChartHelper.AddTooltipIndicatorLabel(component, labelName, m_LabelRoot.transform,
chart.theme, TextAnchor.MiddleCenter);
item.SetActive(false);
m_IndicatorLabels.Add(item);
if (com is Axis)
{
var axis = com as Axis;
var labelName = ChartCached.GetComponentObjectName(axis);
var item = ChartHelper.AddTooltipIndicatorLabel(component, labelName, m_LabelRoot.transform,
chart.theme, axis.context.aligment, axis.indicatorLabel);
item.SetActive(false);
m_IndicatorLabels[labelName] = item;
}
}
};
tooltip.refreshComponent();
}
private ChartLabel GetIndicatorLabel(int index)
private ChartLabel GetIndicatorLabel(Axis axis)
{
if (m_LabelRoot == null) return null;
if (index < m_IndicatorLabels.Count) return m_IndicatorLabels[index];
var key = ChartCached.GetComponentObjectName(axis);
if (m_IndicatorLabels.ContainsKey(key))
{
return m_IndicatorLabels[key];
}
else
{
var labelName = "label_" + index;
var item = ChartHelper.AddTooltipIndicatorLabel(component, labelName, m_LabelRoot.transform,
chart.theme, TextAnchor.MiddleCenter);
m_IndicatorLabels.Add(item);
var item = ChartHelper.AddTooltipIndicatorLabel(component, key, m_LabelRoot.transform,
chart.theme, TextAnchor.MiddleCenter, axis.indicatorLabel);
m_IndicatorLabels[key] = item;
return item;
}
}
@@ -138,7 +145,6 @@ namespace XCharts.Runtime
{
if (tooltip.type == Tooltip.Type.Corss)
{
var labelCount = 0;
if (m_PointerContainer is GridCoord)
{
var grid = m_PointerContainer as GridCoord;
@@ -150,7 +156,7 @@ namespace XCharts.Runtime
var axis = component as Axis;
if (axis.gridIndex == grid.index)
{
var label = GetIndicatorLabel(labelCount++);
var label = GetIndicatorLabel(axis);
SetTooltipIndicatorLabel(tooltip, axis, label);
}
}
@@ -167,7 +173,7 @@ namespace XCharts.Runtime
var axis = component as Axis;
if (axis.polarIndex == polar.index)
{
var label = GetIndicatorLabel(labelCount++);
var label = GetIndicatorLabel(axis);
SetTooltipIndicatorLabel(tooltip, axis, label);
}
}
@@ -183,16 +189,27 @@ namespace XCharts.Runtime
if (double.IsNaN(axis.context.pointerValue)) return;
label.SetActive(true);
label.SetTextActive(true);
label.SetPosition(axis.context.pointerLabelPosition);
label.SetPosition(axis.context.pointerLabelPosition + axis.indicatorLabel.offset);
if (axis.IsCategory())
label.SetText(axis.GetData((int) axis.context.pointerValue));
{
var index = (int) axis.context.pointerValue;
var category = axis.GetData(index);
label.SetText(axis.indicatorLabel.GetFormatterContent(index, category));
}
else if (axis.IsTime())
{
label.SetText(axis.indicatorLabel.GetFormatterDateTime(0, axis.context.pointerValue, axis.context.minValue, axis.context.maxValue));
}
else
label.SetText(axis.context.pointerValue.ToString("f2"));
{
label.SetText(axis.indicatorLabel.GetFormatterContent(0, axis.context.pointerValue, axis.context.minValue, axis.context.maxValue, axis.IsLog()));
}
var textColor = axis.axisLabel.textStyle.GetColor(chart.theme.axis.textColor);
if (ChartHelper.IsClearColor(tooltip.indicatorLabelStyle.background.color))
if (ChartHelper.IsClearColor(axis.indicatorLabel.background.color))
label.color = textColor;
else
label.color = tooltip.indicatorLabelStyle.background.color;
label.color = axis.indicatorLabel.background.color;
label.SetTextColor(Color.white);
}