Files
XCharts/Runtime/Component/Label/LabelStyle.cs

400 lines
15 KiB
C#
Raw Normal View History

using System;
2022-05-22 22:17:38 +08:00
using UnityEngine;
2022-02-19 22:37:57 +08:00
namespace XCharts.Runtime
{
/// <summary>
/// Text label of chart, to explain some data information about graphic item like value, name and so on.
2022-03-24 08:37:06 +08:00
/// |图形上的文本标签,可用于说明图形的一些数据信息,比如值,名称等。
/// </summary>
[System.Serializable]
2021-12-28 08:18:24 +08:00
public class LabelStyle : ChildComponent, ISerieExtraComponent, ISerieDataComponent
{
/// <summary>
/// The position of label.
2022-03-24 08:37:06 +08:00
/// |标签的位置。
/// </summary>
public enum Position
{
2022-04-26 08:24:45 +08:00
Default,
/// <summary>
/// Outside of sectors of pie chart, which relates to corresponding sector through visual guide line.
2022-03-24 21:48:53 +08:00
/// |饼图扇区外侧,通过视觉引导线连到相应的扇区。
/// </summary>
Outside,
/// <summary>
/// Inside the sectors of pie chart.
2022-03-24 21:48:53 +08:00
/// |饼图扇区内部。
/// </summary>
Inside,
/// <summary>
/// In the center of pie chart.
2022-03-24 21:48:53 +08:00
/// |在饼图中心位置。
/// </summary>
Center,
/// <summary>
/// top of symbol.
2022-03-24 21:48:53 +08:00
/// |图形标志的顶部。
/// </summary>
Top,
/// <summary>
2021-05-29 22:07:09 +08:00
/// the bottom of symbol.
2022-03-24 21:48:53 +08:00
/// |图形标志的底部。
2021-05-29 22:07:09 +08:00
/// </summary>
Bottom,
/// <summary>
/// the left of symbol.
2022-03-24 21:48:53 +08:00
/// |图形标志的左边。
/// </summary>
2021-05-29 22:07:09 +08:00
Left,
/// <summary>
/// the right of symbol.
2022-03-24 21:48:53 +08:00
/// |图形标志的右边。
/// </summary>
2021-05-29 22:07:09 +08:00
Right,
2021-07-15 21:18:23 +08:00
/// <summary>
/// the start of line.
2022-03-24 21:48:53 +08:00
/// |线的起始点。
2021-07-15 21:18:23 +08:00
/// </summary>
Start,
/// <summary>
/// the middle of line.
2022-03-24 21:48:53 +08:00
/// |线的中点。
2021-07-15 21:18:23 +08:00
/// </summary>
Middle,
/// <summary>
/// the end of line.
2022-03-24 21:48:53 +08:00
/// |线的结束点。
2021-07-15 21:18:23 +08:00
/// </summary>
End
}
[SerializeField] protected bool m_Show = true;
2022-04-26 08:24:45 +08:00
[SerializeField] Position m_Position = Position.Default;
[SerializeField] protected bool m_AutoOffset = false;
[SerializeField] protected Vector3 m_Offset;
2022-04-19 13:30:24 +08:00
[SerializeField] protected float m_Rotate;
[SerializeField] protected float m_Distance;
[SerializeField] protected string m_Formatter;
[SerializeField] protected string m_NumericFormatter = "";
2022-04-26 08:24:45 +08:00
[SerializeField] protected float m_Width = 0;
[SerializeField] protected float m_Height = 0;
[SerializeField] protected IconStyle m_Icon = new IconStyle();
[SerializeField] protected ImageStyle m_Background = new ImageStyle();
[SerializeField] protected TextPadding m_TextPadding = new TextPadding();
[SerializeField] protected TextStyle m_TextStyle = new TextStyle();
2022-04-26 08:24:45 +08:00
protected LabelFormatterFunction m_FormatterFunction;
public void Reset()
{
m_Show = false;
2022-04-26 08:24:45 +08:00
m_Position = Position.Default;
m_Offset = Vector3.zero;
2022-02-19 17:35:22 +08:00
m_Distance = 0;
2022-04-19 13:30:24 +08:00
m_Rotate = 0;
2022-04-26 08:24:45 +08:00
m_Width = 0;
m_Height = 0;
m_NumericFormatter = "";
m_AutoOffset = false;
}
/// <summary>
/// Whether the label is showed.
2022-03-24 08:37:06 +08:00
/// |是否显示文本标签。
/// </summary>
public bool show
{
get { return m_Show; }
2021-01-11 08:54:28 +08:00
set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetAllDirty(); }
}
/// <summary>
/// The position of label.
2022-03-24 08:37:06 +08:00
/// |标签的位置。
/// </summary>
public Position position
{
get { return m_Position; }
2022-07-02 16:43:52 +08:00
set { if (PropertyUtil.SetStruct(ref m_Position, value)) SetAllDirty(); }
}
/// <summary>
2022-05-22 22:17:38 +08:00
/// formatter of label.
/// |标签内容字符串模版格式器。支持用 \n 换行。
/// 模板变量有:
2022-05-22 22:17:38 +08:00
/// {.}:圆点标记。
/// {a}:系列名。
/// {a}:系列名。
/// {b}:类目值或数据名。
/// {c}:数据值。
/// {d}:百分比。
/// {e}:数据名。
/// {f}:数据和。
/// 示例:“{b}:{c}”
2022-05-22 22:17:38 +08:00
/// </summary>
public string formatter
{
get { return m_Formatter; }
2022-07-02 16:43:52 +08:00
set { if (PropertyUtil.SetClass(ref m_Formatter, value)) SetComponentDirty(); }
}
/// <summary>
2022-03-24 08:37:06 +08:00
/// offset to the host graphic element.
/// |距离图形元素的偏移
/// </summary>
public Vector3 offset
{
get { return m_Offset; }
2022-07-02 16:43:52 +08:00
set { if (PropertyUtil.SetStruct(ref m_Offset, value)) SetAllDirty(); }
}
/// <summary>
2022-04-19 13:30:24 +08:00
/// Rotation of label.
/// |文本的旋转。
/// </summary>
public float rotate
{
get { return m_Rotate; }
set { if (PropertyUtil.SetStruct(ref m_Rotate, value)) SetComponentDirty(); }
}
/// <summary>
2019-11-30 21:24:04 +08:00
/// 距离轴线的距离。
/// </summary>
2022-02-19 17:35:22 +08:00
public float distance
{
2022-02-19 17:35:22 +08:00
get { return m_Distance; }
2022-07-02 16:43:52 +08:00
set { if (PropertyUtil.SetStruct(ref m_Distance, value)) SetAllDirty(); }
}
/// <summary>
2022-04-26 08:24:45 +08:00
/// the width of label. If set as default value 0, it means than the label width auto set as the text width.
/// |标签的宽度。一般不用指定,不指定时则自动是文字的宽度。
/// </summary>
/// <value></value>
2022-04-26 08:24:45 +08:00
public float width
{
2022-04-26 08:24:45 +08:00
get { return m_Width; }
set { if (PropertyUtil.SetStruct(ref m_Width, value)) SetComponentDirty(); }
}
/// <summary>
2022-04-26 08:24:45 +08:00
/// the height of label. If set as default value 0, it means than the label height auto set as the text height.
/// |标签的高度。一般不用指定,不指定时则自动是文字的高度。
/// </summary>
/// <value></value>
2022-04-26 08:24:45 +08:00
public float height
{
2022-04-26 08:24:45 +08:00
get { return m_Height; }
set { if (PropertyUtil.SetStruct(ref m_Height, value)) SetComponentDirty(); }
}
/// <summary>
2022-04-26 08:24:45 +08:00
/// the text padding of label.
/// |文本的边距。
/// </summary>
2022-04-26 08:24:45 +08:00
public TextPadding textPadding
{
2022-04-26 08:24:45 +08:00
get { return m_TextPadding; }
set { if (PropertyUtil.SetClass(ref m_TextPadding, value)) SetComponentDirty(); }
}
/// <summary>
/// Standard numeric format strings.
2022-03-24 08:37:06 +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
/// </summary>
/// <value></value>
public string numericFormatter
{
get { return m_NumericFormatter; }
2021-01-11 08:54:28 +08:00
set { if (PropertyUtil.SetClass(ref m_NumericFormatter, value)) SetComponentDirty(); }
}
/// <summary>
/// 是否开启自动偏移。当开启时Y的偏移会自动判断曲线的开口来决定向上还是向下偏移。
/// </summary>
public bool autoOffset
{
get { return m_AutoOffset; }
2021-01-11 08:54:28 +08:00
set { if (PropertyUtil.SetStruct(ref m_AutoOffset, value)) SetAllDirty(); }
}
2022-04-26 08:24:45 +08:00
/// <summary>
/// the sytle of background.
/// |背景图样式。
/// </summary>
public ImageStyle background
{
get { return m_Background; }
set { if (PropertyUtil.SetClass(ref m_Background, value)) SetAllDirty(); }
}
/// <summary>
/// the sytle of icon.
/// |图标样式。
/// </summary>
public IconStyle icon
{
get { return m_Icon; }
set { if (PropertyUtil.SetClass(ref m_Icon, value)) SetAllDirty(); }
}
2021-01-11 08:54:28 +08:00
/// <summary>
/// the sytle of text.
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)) SetAllDirty(); }
}
2022-04-26 08:24:45 +08:00
public LabelFormatterFunction formatterFunction
{
get { return m_FormatterFunction; }
set { m_FormatterFunction = value; }
}
2021-05-29 22:07:09 +08:00
public bool IsInside()
{
2022-05-04 08:45:19 +08:00
return m_Position == Position.Inside || m_Position == Position.Center;
}
public bool IsDefaultPosition(Position position)
{
return m_Position == Position.Default || m_Position == position;
2021-05-29 22:07:09 +08:00
}
2022-04-26 08:24:45 +08:00
public bool IsAutoSize()
{
return width == 0 && height == 0;
}
2022-02-19 17:35:22 +08:00
public Vector3 GetOffset(float radius)
{
var x = ChartHelper.GetActualValue(m_Offset.x, radius);
var y = ChartHelper.GetActualValue(m_Offset.y, radius);
var z = ChartHelper.GetActualValue(m_Offset.z, radius);
return new Vector3(x, y, z);
}
2021-05-29 22:07:09 +08:00
public Color GetColor(Color defaultColor)
{
if (ChartHelper.IsClearColor(textStyle.color))
{
return IsInside() ? Color.black : defaultColor;
}
else
{
return textStyle.color;
}
}
2022-04-26 08:24:45 +08:00
public virtual LabelStyle Clone()
2021-05-29 22:07:09 +08:00
{
2022-04-26 08:24:45 +08:00
var label = new LabelStyle();
label.m_Show = m_Show;
label.m_Position = m_Position;
label.m_Offset = m_Offset;
label.m_Rotate = m_Rotate;
label.m_Distance = m_Distance;
label.m_Formatter = m_Formatter;
label.m_Width = m_Width;
label.m_Height = m_Height;
label.m_NumericFormatter = m_NumericFormatter;
label.m_AutoOffset = m_AutoOffset;
label.m_Icon.Copy(m_Icon);
label.m_Background.Copy(m_Background);
label.m_TextPadding = m_TextPadding;
label.m_TextStyle.Copy(m_TextStyle);
return label;
2021-05-29 22:07:09 +08:00
}
2021-12-28 08:18:24 +08:00
2022-04-26 08:24:45 +08:00
public virtual void Copy(LabelStyle label)
{
m_Show = label.m_Show;
m_Position = label.m_Position;
m_Offset = label.m_Offset;
m_Rotate = label.m_Rotate;
m_Distance = label.m_Distance;
m_Formatter = label.m_Formatter;
m_Width = label.m_Width;
m_Height = label.m_Height;
m_NumericFormatter = label.m_NumericFormatter;
m_AutoOffset = label.m_AutoOffset;
m_Icon.Copy(label.m_Icon);
m_Background.Copy(label.m_Background);
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) || numericFormatter.Equals("f2"))
{
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);
}
2022-04-26 08:24:45 +08:00
}
2022-05-22 22:17:38 +08:00
}