2019-10-22 04:09:04 +08:00
|
|
|
|
|
2019-07-13 16:38:38 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using UnityEngine;
|
2020-01-26 22:34:57 +08:00
|
|
|
|
using UnityEngine.UI;
|
2019-07-13 16:38:38 +08:00
|
|
|
|
|
2022-02-19 22:37:57 +08:00
|
|
|
|
namespace XCharts.Runtime
|
2019-07-13 16:38:38 +08:00
|
|
|
|
{
|
2019-08-01 23:49:30 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Settings related to axis label.
|
2022-03-24 08:37:06 +08:00
|
|
|
|
/// |坐标轴刻度标签的相关设置。
|
2019-08-01 23:49:30 +08:00
|
|
|
|
/// </summary>
|
2019-07-13 16:38:38 +08:00
|
|
|
|
[Serializable]
|
2021-11-23 13:20:07 +08:00
|
|
|
|
public class AxisLabel : ChildComponent
|
2019-07-13 16:38:38 +08:00
|
|
|
|
{
|
2019-08-01 23:49:30 +08:00
|
|
|
|
[SerializeField] private bool m_Show = true;
|
2019-09-23 19:09:56 +08:00
|
|
|
|
[SerializeField] private string m_Formatter;
|
2019-08-01 23:49:30 +08:00
|
|
|
|
[SerializeField] private int m_Interval = 0;
|
|
|
|
|
|
[SerializeField] private bool m_Inside = false;
|
2022-02-19 17:35:22 +08:00
|
|
|
|
[SerializeField] private float m_Distance;
|
2020-05-04 13:29:56 +08:00
|
|
|
|
[SerializeField] private string m_NumericFormatter = "";
|
2020-04-19 11:08:48 +08:00
|
|
|
|
[SerializeField] private bool m_ShowAsPositiveNumber = false;
|
2020-04-19 22:08:40 +08:00
|
|
|
|
[SerializeField] private bool m_OnZero = false;
|
2021-05-24 07:17:54 +08:00
|
|
|
|
[SerializeField] private float m_Width = 0f;
|
|
|
|
|
|
[SerializeField] private float m_Height = 0f;
|
2021-06-27 21:45:17 +08:00
|
|
|
|
[SerializeField] private bool m_ShowStartLabel = true;
|
|
|
|
|
|
[SerializeField] private bool m_ShowEndLabel = true;
|
2020-01-26 22:34:57 +08:00
|
|
|
|
[SerializeField] private TextLimit m_TextLimit = new TextLimit();
|
2021-01-11 08:54:28 +08:00
|
|
|
|
[SerializeField] private TextStyle m_TextStyle = new TextStyle();
|
2022-03-04 22:17:32 +08:00
|
|
|
|
private AxisLabelFormatterFunction m_FormatterFunction;
|
2019-07-13 16:38:38 +08:00
|
|
|
|
|
2019-08-01 23:49:30 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Set this to false to prevent the axis label from appearing.
|
2022-03-24 08:37:06 +08:00
|
|
|
|
/// |是否显示刻度标签。
|
2019-08-01 23:49:30 +08:00
|
|
|
|
/// </summary>
|
2020-03-05 20:25:19 +08:00
|
|
|
|
public bool show
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_Show; }
|
2021-01-11 08:54:28 +08:00
|
|
|
|
set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetComponentDirty(); }
|
2020-03-05 20:25:19 +08:00
|
|
|
|
}
|
2019-08-01 23:49:30 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The display interval of the axis label.
|
2022-03-24 08:37:06 +08:00
|
|
|
|
/// |坐标轴刻度标签的显示间隔,在类目轴中有效。0表示显示所有标签,1表示隔一个隔显示一个标签,以此类推。
|
2019-08-01 23:49:30 +08:00
|
|
|
|
/// </summary>
|
2020-03-05 20:25:19 +08:00
|
|
|
|
public int interval
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_Interval; }
|
2021-01-11 08:54:28 +08:00
|
|
|
|
set { if (PropertyUtil.SetStruct(ref m_Interval, value)) SetComponentDirty(); }
|
2020-03-05 20:25:19 +08:00
|
|
|
|
}
|
2019-08-01 23:49:30 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Set this to true so the axis labels face the inside direction.
|
2022-03-24 08:37:06 +08:00
|
|
|
|
/// |刻度标签是否朝内,默认朝外。
|
2019-08-01 23:49:30 +08:00
|
|
|
|
/// </summary>
|
2020-03-05 20:25:19 +08:00
|
|
|
|
public bool inside
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_Inside; }
|
2021-01-11 08:54:28 +08:00
|
|
|
|
set { if (PropertyUtil.SetStruct(ref m_Inside, value)) SetComponentDirty(); }
|
2020-03-05 20:25:19 +08:00
|
|
|
|
}
|
2019-08-01 23:49:30 +08:00
|
|
|
|
/// <summary>
|
2022-02-19 17:35:22 +08:00
|
|
|
|
/// The distance between the axis label and the axis line.
|
2022-03-24 08:37:06 +08:00
|
|
|
|
/// |刻度标签与轴线之间的距离。
|
2019-08-01 23:49:30 +08:00
|
|
|
|
/// </summary>
|
2022-02-19 17:35:22 +08:00
|
|
|
|
public float distance
|
2020-03-05 20:25:19 +08:00
|
|
|
|
{
|
2022-02-19 17:35:22 +08:00
|
|
|
|
get { return m_Distance; }
|
|
|
|
|
|
set { if (PropertyUtil.SetStruct(ref m_Distance, value)) SetComponentDirty(); }
|
2020-03-05 20:25:19 +08:00
|
|
|
|
}
|
2019-09-23 19:09:56 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 图例内容字符串模版格式器。支持用 \n 换行。
|
2020-05-04 13:29:56 +08:00
|
|
|
|
/// 模板变量为图例名称 {value}。
|
2019-09-23 19:09:56 +08:00
|
|
|
|
/// </summary>
|
2020-03-05 20:25:19 +08:00
|
|
|
|
public string formatter
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_Formatter; }
|
2021-01-11 08:54:28 +08:00
|
|
|
|
set { if (PropertyUtil.SetClass(ref m_Formatter, value)) SetComponentDirty(); }
|
2020-03-05 20:25:19 +08:00
|
|
|
|
}
|
2020-05-04 13:29:56 +08:00
|
|
|
|
|
2019-10-26 05:02:32 +08:00
|
|
|
|
/// <summary>
|
2020-05-04 13:29:56 +08:00
|
|
|
|
/// Standard numeric format strings.
|
2022-03-24 08:37:06 +08:00
|
|
|
|
/// |标准数字格式字符串。用于将数值格式化显示为字符串。
|
2020-05-04 13:29:56 +08:00
|
|
|
|
/// 使用Axx的形式:A是格式说明符的单字符,支持C货币、D十进制、E指数、F定点数、G常规、N数字、P百分比、R往返、X十六进制的。xx是精度说明,从0-99。
|
|
|
|
|
|
/// 参考:https://docs.microsoft.com/zh-cn/dotnet/standard/base-types/standard-numeric-format-strings
|
2019-10-26 05:02:32 +08:00
|
|
|
|
/// </summary>
|
2020-05-04 13:29:56 +08:00
|
|
|
|
/// <value></value>
|
|
|
|
|
|
public string numericFormatter
|
2020-03-05 20:25:19 +08:00
|
|
|
|
{
|
2020-05-04 13:29:56 +08:00
|
|
|
|
get { return m_NumericFormatter; }
|
2021-01-11 08:54:28 +08:00
|
|
|
|
set { if (PropertyUtil.SetClass(ref m_NumericFormatter, value)) SetComponentDirty(); }
|
2020-03-05 20:25:19 +08:00
|
|
|
|
}
|
2020-04-19 11:08:48 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Show negative number as positive number.
|
2022-03-24 08:37:06 +08:00
|
|
|
|
/// |将负数数值显示为正数。一般和`Serie`的`showAsPositiveNumber`配合使用。
|
2020-04-19 11:08:48 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool showAsPositiveNumber
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_ShowAsPositiveNumber; }
|
2021-01-11 08:54:28 +08:00
|
|
|
|
set { if (PropertyUtil.SetStruct(ref m_ShowAsPositiveNumber, value)) SetComponentDirty(); }
|
2020-04-19 11:08:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-19 22:08:40 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 刻度标签显示在0刻度上。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool onZero
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_OnZero; }
|
2021-01-11 08:54:28 +08:00
|
|
|
|
set { if (PropertyUtil.SetStruct(ref m_OnZero, value)) SetComponentDirty(); }
|
2020-04-19 22:08:40 +08:00
|
|
|
|
}
|
2021-05-24 07:17:54 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 文本的宽。为0时会自动匹配。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public float width
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_Width; }
|
|
|
|
|
|
set { if (PropertyUtil.SetStruct(ref m_Width, value)) SetComponentDirty(); }
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 文本的高。为0时会自动匹配。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public float height
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_Height; }
|
|
|
|
|
|
set { if (PropertyUtil.SetStruct(ref m_Height, value)) SetComponentDirty(); }
|
|
|
|
|
|
}
|
2021-06-27 21:45:17 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Whether to display the first label.
|
2022-03-24 08:37:06 +08:00
|
|
|
|
/// |是否显示第一个文本。
|
2021-06-27 21:45:17 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool showStartLabel
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_ShowStartLabel; }
|
|
|
|
|
|
set { if (PropertyUtil.SetStruct(ref m_ShowStartLabel, value)) SetComponentDirty(); }
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Whether to display the last label.
|
2022-03-24 08:37:06 +08:00
|
|
|
|
/// |是否显示最后一个文本。
|
2021-06-27 21:45:17 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool showEndLabel
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_ShowEndLabel; }
|
|
|
|
|
|
set { if (PropertyUtil.SetStruct(ref m_ShowEndLabel, value)) SetComponentDirty(); }
|
|
|
|
|
|
}
|
2020-01-26 22:34:57 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 文本限制。
|
|
|
|
|
|
/// </summary>
|
2020-03-05 20:25:19 +08:00
|
|
|
|
public TextLimit textLimit
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_TextLimit; }
|
|
|
|
|
|
set { if (value != null) { m_TextLimit = value; SetComponentDirty(); } }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-11 08:54:28 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The text style of axis name.
|
2022-03-24 08:37:06 +08:00
|
|
|
|
/// |文本样式。
|
2021-01-11 08:54:28 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public TextStyle textStyle
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_TextStyle; }
|
|
|
|
|
|
set { if (PropertyUtil.SetClass(ref m_TextStyle, value)) SetComponentDirty(); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-04 22:17:32 +08:00
|
|
|
|
public AxisLabelFormatterFunction formatterFunction
|
2021-06-27 12:26:20 +08:00
|
|
|
|
{
|
|
|
|
|
|
set { m_FormatterFunction = value; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-05 20:25:19 +08:00
|
|
|
|
public override bool componentDirty { get { return m_ComponentDirty || m_TextLimit.componentDirty; } }
|
2021-05-16 23:38:06 +08:00
|
|
|
|
public override void ClearComponentDirty()
|
2020-03-05 20:25:19 +08:00
|
|
|
|
{
|
|
|
|
|
|
base.ClearComponentDirty();
|
|
|
|
|
|
textLimit.ClearComponentDirty();
|
|
|
|
|
|
}
|
2019-07-13 16:38:38 +08:00
|
|
|
|
|
|
|
|
|
|
public static AxisLabel defaultAxisLabel
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
return new AxisLabel()
|
|
|
|
|
|
{
|
|
|
|
|
|
m_Show = true,
|
|
|
|
|
|
m_Interval = 0,
|
|
|
|
|
|
m_Inside = false,
|
2022-02-19 17:35:22 +08:00
|
|
|
|
m_Distance = 8,
|
2021-01-11 08:54:28 +08:00
|
|
|
|
m_TextStyle = new TextStyle(),
|
2019-07-13 16:38:38 +08:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-09-23 19:09:56 +08:00
|
|
|
|
|
2020-04-18 12:31:21 +08:00
|
|
|
|
public AxisLabel Clone()
|
|
|
|
|
|
{
|
2021-05-24 07:17:54 +08:00
|
|
|
|
var axisLabel = new AxisLabel();
|
|
|
|
|
|
axisLabel.show = show;
|
|
|
|
|
|
axisLabel.formatter = formatter;
|
|
|
|
|
|
axisLabel.interval = interval;
|
|
|
|
|
|
axisLabel.inside = inside;
|
2022-02-19 17:35:22 +08:00
|
|
|
|
axisLabel.distance = distance;
|
2021-05-24 07:17:54 +08:00
|
|
|
|
axisLabel.numericFormatter = numericFormatter;
|
|
|
|
|
|
axisLabel.width = width;
|
|
|
|
|
|
axisLabel.height = height;
|
2021-06-27 21:45:17 +08:00
|
|
|
|
axisLabel.showStartLabel = showStartLabel;
|
|
|
|
|
|
axisLabel.showEndLabel = showEndLabel;
|
2021-05-24 07:17:54 +08:00
|
|
|
|
axisLabel.textLimit = textLimit.Clone();
|
|
|
|
|
|
axisLabel.textStyle.Copy(textStyle);
|
|
|
|
|
|
return axisLabel;
|
2020-04-18 12:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-24 07:17:54 +08:00
|
|
|
|
public void Copy(AxisLabel axisLabel)
|
|
|
|
|
|
{
|
|
|
|
|
|
show = axisLabel.show;
|
|
|
|
|
|
formatter = axisLabel.formatter;
|
|
|
|
|
|
interval = axisLabel.interval;
|
|
|
|
|
|
inside = axisLabel.inside;
|
2022-02-19 17:35:22 +08:00
|
|
|
|
distance = axisLabel.distance;
|
2021-05-24 07:17:54 +08:00
|
|
|
|
numericFormatter = axisLabel.numericFormatter;
|
|
|
|
|
|
width = axisLabel.width;
|
|
|
|
|
|
height = axisLabel.height;
|
2021-06-27 21:45:17 +08:00
|
|
|
|
showStartLabel = axisLabel.showStartLabel;
|
|
|
|
|
|
showEndLabel = axisLabel.showEndLabel;
|
2021-05-24 07:17:54 +08:00
|
|
|
|
textLimit.Copy(axisLabel.textLimit);
|
|
|
|
|
|
textStyle.Copy(axisLabel.textStyle);
|
2020-04-18 12:31:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-11 08:54:28 +08:00
|
|
|
|
public void SetRelatedText(ChartText txt, float labelWidth)
|
2020-01-26 22:34:57 +08:00
|
|
|
|
{
|
2020-02-08 15:16:28 +08:00
|
|
|
|
m_TextLimit.SetRelatedText(txt, labelWidth);
|
2020-01-26 22:34:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-27 12:26:20 +08:00
|
|
|
|
public string GetFormatterContent(int labelIndex, string category)
|
2019-09-23 19:09:56 +08:00
|
|
|
|
{
|
2021-06-27 12:26:20 +08:00
|
|
|
|
if (m_FormatterFunction != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_FormatterFunction(labelIndex, 0, category);
|
|
|
|
|
|
}
|
2021-11-23 13:20:07 +08:00
|
|
|
|
if (string.IsNullOrEmpty(category))
|
|
|
|
|
|
return category;
|
|
|
|
|
|
|
2019-09-23 19:09:56 +08:00
|
|
|
|
if (string.IsNullOrEmpty(m_Formatter))
|
2020-01-26 22:34:57 +08:00
|
|
|
|
{
|
|
|
|
|
|
return m_TextLimit.GetLimitContent(category);
|
|
|
|
|
|
}
|
2019-09-23 19:09:56 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2021-03-25 12:55:52 +08:00
|
|
|
|
var content = m_Formatter;
|
|
|
|
|
|
FormatterHelper.ReplaceAxisLabelContent(ref content, category);
|
2020-01-26 22:34:57 +08:00
|
|
|
|
return m_TextLimit.GetLimitContent(content);
|
2019-09-23 19:09:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-08 07:19:31 +08:00
|
|
|
|
public string GetFormatterContent(int labelIndex, double value, double minValue, double maxValue, bool isLog = false)
|
2019-09-23 19:09:56 +08:00
|
|
|
|
{
|
2020-04-19 11:08:48 +08:00
|
|
|
|
if (showAsPositiveNumber && value < 0)
|
|
|
|
|
|
{
|
2021-07-08 07:19:31 +08:00
|
|
|
|
value = Math.Abs(value);
|
2020-04-19 11:08:48 +08:00
|
|
|
|
}
|
2021-06-27 12:26:20 +08:00
|
|
|
|
if (m_FormatterFunction != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_FormatterFunction(labelIndex, value, null);
|
|
|
|
|
|
}
|
2019-09-23 19:09:56 +08:00
|
|
|
|
if (string.IsNullOrEmpty(m_Formatter))
|
2019-10-26 05:02:32 +08:00
|
|
|
|
{
|
2020-01-15 19:41:21 +08:00
|
|
|
|
if (isLog)
|
|
|
|
|
|
{
|
2020-05-04 13:29:56 +08:00
|
|
|
|
return ChartCached.NumberToStr(value, numericFormatter);
|
2020-01-15 19:41:21 +08:00
|
|
|
|
}
|
2019-10-26 05:02:32 +08:00
|
|
|
|
if (minValue >= -1 && minValue <= 1 && maxValue >= -1 && maxValue <= 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
int minAcc = ChartHelper.GetFloatAccuracy(minValue);
|
|
|
|
|
|
int maxAcc = ChartHelper.GetFloatAccuracy(maxValue);
|
2019-12-06 10:08:48 +08:00
|
|
|
|
int curAcc = ChartHelper.GetFloatAccuracy(value);
|
|
|
|
|
|
int acc = Mathf.Max(Mathf.Max(minAcc, maxAcc), curAcc);
|
2020-05-04 13:29:56 +08:00
|
|
|
|
return ChartCached.FloatToStr(value, numericFormatter, acc);
|
2019-10-26 05:02:32 +08:00
|
|
|
|
}
|
2020-05-04 13:29:56 +08:00
|
|
|
|
return ChartCached.NumberToStr(value, numericFormatter);
|
2019-10-26 05:02:32 +08:00
|
|
|
|
}
|
2020-07-21 09:13:51 +08:00
|
|
|
|
else
|
2019-09-23 19:09:56 +08:00
|
|
|
|
{
|
|
|
|
|
|
var content = m_Formatter;
|
2020-07-21 09:13:51 +08:00
|
|
|
|
FormatterHelper.ReplaceAxisLabelContent(ref content, numericFormatter, value);
|
2019-09-23 19:09:56 +08:00
|
|
|
|
return content;
|
2019-12-06 10:08:48 +08:00
|
|
|
|
}
|
2019-09-23 19:09:56 +08:00
|
|
|
|
}
|
2021-03-25 12:55:52 +08:00
|
|
|
|
|
2021-11-23 13:20:07 +08:00
|
|
|
|
public string GetFormatterDateTime(int labelIndex, double value, double minValue, double maxValue)
|
2021-03-25 12:55:52 +08:00
|
|
|
|
{
|
2021-06-27 12:26:20 +08:00
|
|
|
|
if (m_FormatterFunction != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_FormatterFunction(labelIndex, value, null);
|
|
|
|
|
|
}
|
|
|
|
|
|
var timestamp = (int)value;
|
|
|
|
|
|
var dateTime = DateTimeUtil.GetDateTime(timestamp);
|
2021-11-23 13:20:07 +08:00
|
|
|
|
var dateString = string.Empty;
|
|
|
|
|
|
if (string.IsNullOrEmpty(numericFormatter))
|
|
|
|
|
|
{
|
|
|
|
|
|
dateString = DateTimeUtil.GetDateTimeFormatString(dateTime, maxValue - minValue);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
dateString = dateTime.ToString(numericFormatter);
|
|
|
|
|
|
}
|
2021-03-25 12:55:52 +08:00
|
|
|
|
if (!string.IsNullOrEmpty(m_Formatter))
|
|
|
|
|
|
{
|
|
|
|
|
|
var content = m_Formatter;
|
2021-11-23 13:20:07 +08:00
|
|
|
|
FormatterHelper.ReplaceAxisLabelContent(ref content, dateString);
|
2021-03-25 12:55:52 +08:00
|
|
|
|
return m_TextLimit.GetLimitContent(content);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2021-11-23 13:20:07 +08:00
|
|
|
|
return m_TextLimit.GetLimitContent(dateString);
|
2021-03-25 12:55:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-07-13 16:38:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|