2022-10-30 10:28:17 +08:00
using System ;
2022-05-22 22:17:38 +08:00
using UnityEngine ;
2019-07-29 00:25:10 +08:00
2022-02-19 22:37:57 +08:00
namespace XCharts.Runtime
2019-07-29 00:25:10 +08:00
{
/// <summary>
/// Text label of chart, to explain some data information about graphic item like value, name and so on.
2023-11-11 23:32:24 +08:00
/// ||图形上的文本标签,可用于说明图形的一些数据信息,比如值,名称等。
2019-07-29 00:25:10 +08:00
/// </summary>
2019-08-01 23:49:30 +08:00
[System.Serializable]
2023-02-12 21:22:53 +08:00
public class LabelStyle : ChildComponent , ISerieComponent , ISerieDataComponent
2019-07-29 00:25:10 +08:00
{
/// <summary>
2019-08-01 23:49:30 +08:00
/// The position of label.
2023-11-11 23:32:24 +08:00
/// ||标签的位置。
2019-07-29 00:25:10 +08:00
/// </summary>
public enum Position
{
2022-04-26 08:24:45 +08:00
Default ,
2019-07-29 00:25:10 +08:00
/// <summary>
/// Outside of sectors of pie chart, which relates to corresponding sector through visual guide line.
2023-11-11 23:32:24 +08:00
/// ||饼图扇区外侧,通过视觉引导线连到相应的扇区。
2019-07-29 00:25:10 +08:00
/// </summary>
Outside ,
/// <summary>
2019-08-01 23:49:30 +08:00
/// Inside the sectors of pie chart.
2023-11-11 23:32:24 +08:00
/// ||饼图扇区内部。
2019-07-29 00:25:10 +08:00
/// </summary>
Inside ,
/// <summary>
2019-08-01 23:49:30 +08:00
/// In the center of pie chart.
2023-11-11 23:32:24 +08:00
/// ||在饼图中心位置。
2019-08-01 23:49:30 +08:00
/// </summary>
2019-07-29 00:25:10 +08:00
Center ,
2019-08-01 23:49:30 +08:00
/// <summary>
/// top of symbol.
2023-11-11 23:32:24 +08:00
/// ||图形标志的顶部。
2019-08-01 23:49:30 +08:00
/// </summary>
2019-07-29 00:25:10 +08:00
Top ,
2019-08-01 23:49:30 +08:00
/// <summary>
2021-05-29 22:07:09 +08:00
/// the bottom of symbol.
2023-11-11 23:32:24 +08:00
/// ||图形标志的底部。
2021-05-29 22:07:09 +08:00
/// </summary>
Bottom ,
/// <summary>
2019-08-01 23:49:30 +08:00
/// the left of symbol.
2023-11-11 23:32:24 +08:00
/// ||图形标志的左边。
2019-08-01 23:49:30 +08:00
/// </summary>
2021-05-29 22:07:09 +08:00
Left ,
2019-08-01 23:49:30 +08:00
/// <summary>
/// the right of symbol.
2023-11-11 23:32:24 +08:00
/// ||图形标志的右边。
2019-08-01 23:49:30 +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.
2023-11-11 23:32:24 +08:00
/// ||线的起始点。
2021-07-15 21:18:23 +08:00
/// </summary>
Start ,
/// <summary>
/// the middle of line.
2023-11-11 23:32:24 +08:00
/// ||线的中点。
2021-07-15 21:18:23 +08:00
/// </summary>
Middle ,
/// <summary>
/// the end of line.
2023-11-11 23:32:24 +08:00
/// ||线的结束点。
2021-07-15 21:18:23 +08:00
/// </summary>
End
2019-07-29 00:25:10 +08:00
}
2019-10-05 18:23:06 +08:00
2022-04-14 21:32:44 +08:00
[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 ;
2022-04-14 21:32:44 +08:00
[SerializeField] protected Vector3 m_Offset ;
2022-04-19 13:30:24 +08:00
[SerializeField] protected float m_Rotate ;
2023-03-12 22:56:36 +08:00
[SerializeField] [ Since ( "v3.6.0" ) ] protected bool m_AutoRotate = false ;
2022-04-14 21:32:44 +08:00
[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 ;
2025-03-18 07:41:58 +08:00
[SerializeField] [ Since ( "v3.15.0" ) ] protected float m_FixedX = 0 ;
[SerializeField] [ Since ( "v3.15.0" ) ] protected float m_FixedY = 0 ;
2022-04-26 08:24:45 +08:00
[SerializeField] protected IconStyle m_Icon = new IconStyle ( ) ;
[SerializeField] protected ImageStyle m_Background = new ImageStyle ( ) ;
[SerializeField] protected TextPadding m_TextPadding = new TextPadding ( ) ;
2022-04-14 21:32:44 +08:00
[SerializeField] protected TextStyle m_TextStyle = new TextStyle ( ) ;
2022-04-26 08:24:45 +08:00
protected LabelFormatterFunction m_FormatterFunction ;
2020-03-05 20:25:19 +08:00
2020-06-10 13:10:24 +08:00
public void Reset ( )
{
m_Show = false ;
2022-04-26 08:24:45 +08:00
m_Position = Position . Default ;
2020-06-10 13:10:24 +08:00
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 ;
2020-06-10 13:10:24 +08:00
m_NumericFormatter = "" ;
m_AutoOffset = false ;
}
2019-07-29 00:25:10 +08:00
/// <summary>
2019-08-01 23:49:30 +08:00
/// Whether the label is showed.
2023-11-11 23:32:24 +08:00
/// ||是否显示文本标签。
2019-07-29 00:25:10 +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 ) ) SetAllDirty ( ) ; }
2020-03-05 20:25:19 +08:00
}
2019-07-29 00:25:10 +08:00
/// <summary>
2019-08-01 23:49:30 +08:00
/// The position of label.
2023-11-11 23:32:24 +08:00
/// ||标签的位置。
2019-07-29 00:25:10 +08:00
/// </summary>
2020-03-05 20:25:19 +08:00
public Position position
{
get { return m_Position ; }
2022-07-02 16:43:52 +08:00
set { if ( PropertyUtil . SetStruct ( ref m_Position , value ) ) SetAllDirty ( ) ; }
2020-03-05 20:25:19 +08:00
}
2019-07-29 00:25:10 +08:00
/// <summary>
2023-11-14 13:18:31 +08:00
/// label content string template formatter. \n line wrapping is supported. Formatters for some components will not take effect. <br />
/// Template placeholder have the following, some of which apply only to fixed components: <br />
2023-11-14 13:23:32 +08:00
/// `{.}` : indicates the dot mark. <br />
/// `{a}` : indicates the series name. <br />
2024-01-23 22:56:03 +08:00
/// `{b}` : category value of x axis or data name. <br />
2023-11-14 13:23:32 +08:00
/// `{c}` : data value. <br />
/// `{d}` : percentage. <br />
/// `{e}` : indicates the data name. <br />
/// `{f}` : data sum. <br />
/// `{g}` : indicates the total number of data. <br />
/// `{h}` : hexadecimal color value. <br />
2024-01-23 22:56:03 +08:00
/// `{y}` : category value of y axis. <br />
2025-03-01 22:30:51 +08:00
/// `{value}` : the value of the axis or legend. <br />
/// `{index}` : the index of the axis. <br />
2023-11-14 13:18:31 +08:00
/// The following placeholder apply to `UITable` components: <br />
2023-11-14 13:23:32 +08:00
/// `{name}` : indicates the row name of the table. <br />
/// `{index}` : indicates the row number of the table. <br />
2023-11-14 13:18:31 +08:00
/// The following placeholder apply to `UIStatistc` components: <br />
2023-11-14 13:23:32 +08:00
/// `{title}` : title text. <br />
/// `{dd}` : day. <br />
/// `{hh}` : hours. <br />
/// `{mm}` : minutes. <br />
/// `{ss}` : second. <br />
/// `{fff}` : milliseconds. <br />
/// `{d}` : day. <br />
/// `{h}` : hours. <br />
/// `{m}` : minutes. <br />
/// `{s}` : second. <br />
/// `{f}` : milliseconds. <br />
2023-11-14 13:18:31 +08:00
/// Example :{b}:{c}<br />
/// ||标签内容字符串模版格式器。支持用 \n 换行。部分组件的格式器会不生效。<br/>
/// 模板通配符有以下这些,部分只适用于固定的组件:<br/>
/// `{.}`:圆点标记。<br/>
/// `{a}`:系列名。<br/>
2024-01-23 22:56:03 +08:00
/// `{b}`: X轴类目名或数据名。<br/>
2023-11-14 13:18:31 +08:00
/// `{c}`:数据值。<br/>
/// `{d}`:百分比。<br/>
/// `{e}`:数据名。<br/>
/// `{f}`:数据和。<br/>
/// `{g}`:数据总个数。<br/>
/// `{h}`:十六进制颜色值。<br/>
2024-01-23 22:56:03 +08:00
/// `{y}`: Y轴的类目名。<br/>
2023-11-14 13:18:31 +08:00
/// `{value}`:坐标轴或图例的值。<br/>
2025-03-01 22:30:51 +08:00
/// `{index}`:坐标轴编号。<br/>
2023-11-14 13:18:31 +08:00
/// 以下通配符适用UITable组件: <br/>
/// `{name}`: 表格的行名。<br/>
/// `{index}`:表格的行号。<br/>
/// 以下通配符适用UIStatistc组件: <br/>
/// `{title}`:标题文本。<br/>
/// `{dd}`:天。<br/>
/// `{hh}`:小时。<br/>
/// `{mm}`:分钟。<br/>
/// `{ss}`:秒。<br/>
/// `{fff}`:毫秒。<br/>
/// `{d}`:天。<br/>
/// `{h}`:小时。<br/>
/// `{m}`:分钟。<br/>
/// `{s}`:秒。<br/>
/// `{f}`:毫秒。<br/>
2019-09-20 12:38:45 +08:00
/// 示例:“{b}:{c}”
2022-05-22 22:17:38 +08:00
/// </summary>
2020-03-05 20:25:19 +08:00
public string formatter
{
get { return m_Formatter ; }
2022-07-02 16:43:52 +08:00
set { if ( PropertyUtil . SetClass ( ref m_Formatter , value ) ) SetComponentDirty ( ) ; }
2020-03-05 20:25:19 +08:00
}
2019-09-20 12:38:45 +08:00
/// <summary>
2024-09-11 08:12:14 +08:00
/// Standard number and date format string. Used to format a Double value or a DateTime date as a string.
/// numericFormatter is used as an argument to either `Double.ToString ()` or `DateTime.ToString()`. <br />
/// The number format uses the Axx format: A is a single-character format specifier that supports C currency,
/// D decimal, E exponent, F fixed-point number, G regular, N digit, P percentage, R round trip, and X hexadecimal.
/// xx is precision specification, from 0-99. E.g. F1, E2<br />
/// Date format: Starts with `date`, which is used to format DateTime. Common date formats are:
/// yyyy year, MM month, dd day, HH hour, mm minute, ss second, fff millisecond. For example: date:yyyy-MM-dd HH:mm:ss<br />
/// Time format: Starts with `time`, which is used to format TimeSpan. Common time formats are:
/// d day, HH hour, mm minute, ss second, fffffff fractional part.
/// Only the version of Unity2018 or later can support formatting, and the characters inside should be escaped.
/// For example: time:HH\:mm\:ss<br />
2023-11-16 08:20:26 +08:00
/// number format reference: https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings<br/>
/// date format reference: https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings<br/>
2024-09-11 08:12:14 +08:00
/// Note: The date and time formats are only supported by 'v3.12.0' or later.<br/>
2023-11-16 08:20:26 +08:00
/// ||标准数字和日期格式字符串。用于将Double数值或DateTime日期格式化显示为字符串。numericFormatter用来作为Double.ToString()或DateTime.ToString()的参数。<br/>
/// 数字格式使用Axx的形式: A是格式说明符的单字符, 支持C货币、D十进制、E指数、F定点数、G常规、N数字、P百分比、R往返、X十六进制的。xx是精度说明, 从0-99。如: F1, E2<br/>
2024-09-11 08:12:14 +08:00
/// 日期格式:以`date`开头, 用来格式化DateTime, 常见格式有: yyyy年, MM月, dd日, HH时, mm分, ss秒, fff毫秒。如: date:yyyy-MM-dd HH:mm:ss<br/>
/// 时间格式:以`time`开头, 用来格式化TimeSpan, 常见格式有: d日, HH时, mm分, ss秒, fffffff小数部分。
/// 需要Unity2018以上版本才支持格式化, 并且里面的字符要转义。如: time:d\.HH\:mm\:ss<br/>
2023-11-16 08:20:26 +08:00
/// 数值格式化参考: https://docs.microsoft.com/zh-cn/dotnet/standard/base-types/standard-numeric-format-strings <br/>
2024-09-11 08:12:14 +08:00
/// 日期格式化参考: https://learn.microsoft.com/zh-cn/dotnet/standard/base-types/standard-date-and-time-format-strings <br/>
/// 时间格式化参考: https://learn.microsoft.com/zh-cn/dotnet/standard/base-types/standard-timespan-format-strings <br/>
/// 注意: date和time格式需要`v3.12.0`以上版本才支持。
2023-11-14 13:18:31 +08:00
/// </summary>
public string numericFormatter
{
get { return m_NumericFormatter ; }
set { if ( PropertyUtil . SetClass ( ref m_NumericFormatter , value ) ) SetComponentDirty ( ) ; }
}
/// <summary>
2022-03-24 08:37:06 +08:00
/// offset to the host graphic element.
2023-11-11 23:32:24 +08:00
/// ||距离图形元素的偏移
2019-07-29 00:25:10 +08:00
/// </summary>
2020-03-05 20:25:19 +08:00
public Vector3 offset
{
get { return m_Offset ; }
2022-07-02 16:43:52 +08:00
set { if ( PropertyUtil . SetStruct ( ref m_Offset , value ) ) SetAllDirty ( ) ; }
2020-03-05 20:25:19 +08:00
}
2019-07-29 00:25:10 +08:00
/// <summary>
2022-04-19 13:30:24 +08:00
/// Rotation of label.
2023-11-11 23:32:24 +08:00
/// ||文本的旋转。
2022-04-19 13:30:24 +08:00
/// </summary>
public float rotate
{
get { return m_Rotate ; }
set { if ( PropertyUtil . SetStruct ( ref m_Rotate , value ) ) SetComponentDirty ( ) ; }
}
/// <summary>
2023-03-12 22:56:36 +08:00
/// auto rotate of label.
2023-11-11 23:32:24 +08:00
/// ||是否自动旋转。
2023-03-12 22:56:36 +08:00
/// </summary>
public bool autoRotate
{
get { return m_AutoRotate ; }
set { if ( PropertyUtil . SetStruct ( ref m_AutoRotate , value ) ) SetComponentDirty ( ) ; }
}
/// <summary>
2023-11-14 13:18:31 +08:00
/// the distance of label to axis line.
/// ||距离轴线的距离。
2019-11-30 21:24:04 +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 ; }
2022-07-02 16:43:52 +08:00
set { if ( PropertyUtil . SetStruct ( ref m_Distance , value ) ) SetAllDirty ( ) ; }
2020-03-05 20:25:19 +08:00
}
2019-07-29 00:25:10 +08:00
/// <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.
2023-11-11 23:32:24 +08:00
/// ||标签的宽度。一般不用指定,不指定时则自动是文字的宽度。
2019-08-20 09:40:19 +08:00
/// </summary>
2022-04-26 08:24:45 +08:00
public float width
2020-03-05 20:25:19 +08:00
{
2022-04-26 08:24:45 +08:00
get { return m_Width ; }
set { if ( PropertyUtil . SetStruct ( ref m_Width , value ) ) SetComponentDirty ( ) ; }
2020-03-05 20:25:19 +08:00
}
2019-08-20 09:40:19 +08:00
/// <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.
2023-11-11 23:32:24 +08:00
/// ||标签的高度。一般不用指定,不指定时则自动是文字的高度。
2019-08-20 09:40:19 +08:00
/// </summary>
2022-04-26 08:24:45 +08:00
public float height
2020-03-05 20:25:19 +08:00
{
2022-04-26 08:24:45 +08:00
get { return m_Height ; }
set { if ( PropertyUtil . SetStruct ( ref m_Height , value ) ) SetComponentDirty ( ) ; }
2020-03-05 20:25:19 +08:00
}
2019-08-20 09:40:19 +08:00
/// <summary>
2022-04-26 08:24:45 +08:00
/// the text padding of label.
2023-11-11 23:32:24 +08:00
/// ||文本的边距。
2019-08-20 09:40:19 +08:00
/// </summary>
2022-04-26 08:24:45 +08:00
public TextPadding textPadding
2020-03-05 20:25:19 +08:00
{
2022-04-26 08:24:45 +08:00
get { return m_TextPadding ; }
set { if ( PropertyUtil . SetClass ( ref m_TextPadding , value ) ) SetComponentDirty ( ) ; }
2020-03-05 20:25:19 +08:00
}
2019-08-01 23:49:30 +08:00
/// <summary>
2023-11-14 13:18:31 +08:00
/// Whether to automatically offset. When turned on, the Y offset will automatically determine the opening of the curve to determine whether to offset up or down.
/// ||是否开启自动偏移。当开启时, Y的偏移会自动判断曲线的开口来决定向上还是向下偏移。
2020-06-04 12:33:25 +08:00
/// </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>
2025-03-18 07:41:58 +08:00
/// the fixed x of label. When not 0, it will be fixed on the specified x value.
/// ||固定的X值。不为0时, 会固定在指定的X值上。
/// </summary>
public float fixedX
{
get { return m_FixedX ; }
set { if ( PropertyUtil . SetStruct ( ref m_FixedX , value ) ) SetComponentDirty ( ) ; }
}
/// <summary>
/// the fixed y of label. When not 0, it will be fixed on the specified y value.
/// ||固定的Y值。不为0时, 会固定在指定的Y值上。
/// </summary>
public float fixedY
{
get { return m_FixedY ; }
set { if ( PropertyUtil . SetStruct ( ref m_FixedY , value ) ) SetComponentDirty ( ) ; }
}
/// <summary>
2022-04-26 08:24:45 +08:00
/// the sytle of background.
2023-11-11 23:32:24 +08:00
/// ||背景图样式。
2022-04-26 08:24:45 +08:00
/// </summary>
public ImageStyle background
{
get { return m_Background ; }
set { if ( PropertyUtil . SetClass ( ref m_Background , value ) ) SetAllDirty ( ) ; }
}
/// <summary>
/// the sytle of icon.
2023-11-11 23:32:24 +08:00
/// ||图标样式。
2022-04-26 08:24:45 +08:00
/// </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.
2023-11-11 23:32:24 +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 ( ) ; }
2020-06-04 12:33:25 +08:00
}
2023-11-14 13:18:31 +08:00
/// <summary>
/// the formatter function of label, which supports string template and callback function.
/// ||标签的文本格式化函数,支持字符串模版和回调函数。
/// </summary>
2022-04-26 08:24:45 +08:00
public LabelFormatterFunction formatterFunction
2021-06-27 12:26:20 +08:00
{
get { return m_FormatterFunction ; }
set { m_FormatterFunction = value ; }
}
2023-11-14 13:18:31 +08:00
/// <summary>
/// whether the label is inside.
/// ||是否在内部。
/// </summary>
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 ;
2025-03-18 07:41:58 +08:00
label . m_FixedX = m_FixedX ;
label . m_FixedY = m_FixedY ;
2022-04-26 08:24:45 +08:00
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 ;
2025-03-18 07:41:58 +08:00
m_FixedX = label . m_FixedX ;
m_FixedY = label . m_FixedY ;
2022-04-26 08:24:45 +08:00
m_Icon . Copy ( label . m_Icon ) ;
m_Background . Copy ( label . m_Background ) ;
m_TextPadding = label . m_TextPadding ;
m_TextStyle . Copy ( label . m_TextStyle ) ;
}
2022-10-30 10:28:17 +08:00
2025-03-01 22:30:51 +08:00
public virtual string GetFormatterContent ( int labelIndex , int totalIndex , string category )
2022-10-30 10:28:17 +08:00
{
if ( string . IsNullOrEmpty ( category ) )
return GetFormatterFunctionContent ( labelIndex , category , category ) ;
if ( string . IsNullOrEmpty ( m_Formatter ) )
{
return GetFormatterFunctionContent ( labelIndex , category , category ) ;
}
else
{
var content = m_Formatter ;
2025-03-01 22:30:51 +08:00
FormatterHelper . ReplaceAxisLabelContent ( ref content , category , labelIndex , totalIndex ) ;
2022-10-30 10:28:17 +08:00
return GetFormatterFunctionContent ( labelIndex , category , category ) ;
}
}
2025-03-01 22:30:51 +08:00
public virtual string GetFormatterContent ( int labelIndex , int totalIndex , double value , double minValue , double maxValue , bool isLog = false )
2022-10-30 10:28:17 +08:00
{
2023-02-10 13:23:38 +08:00
var newNumericFormatter = numericFormatter ;
2024-09-11 08:12:14 +08:00
if ( value = = 0 & & ! DateTimeUtil . IsDateOrTimeRegex ( newNumericFormatter ) )
2023-02-10 13:23:38 +08:00
{
2023-05-04 23:33:45 +08:00
newNumericFormatter = "f0" ;
}
else if ( string . IsNullOrEmpty ( newNumericFormatter ) & & ! isLog )
{
2023-08-15 11:45:32 +08:00
if ( Math . Abs ( maxValue ) > = Math . Abs ( minValue ) )
{
2023-08-23 22:02:00 +08:00
newNumericFormatter = MathUtil . IsInteger ( maxValue ) ? "0.#" : "f" + MathUtil . GetPrecision ( maxValue ) ;
2023-08-15 11:45:32 +08:00
}
else
{
2023-08-23 22:02:00 +08:00
newNumericFormatter = MathUtil . IsInteger ( minValue ) ? "0.#" : "f" + MathUtil . GetPrecision ( minValue ) ;
2023-08-15 11:45:32 +08:00
}
2023-02-10 13:23:38 +08:00
}
2022-10-30 10:28:17 +08:00
if ( string . IsNullOrEmpty ( m_Formatter ) )
{
if ( isLog )
{
2023-02-10 13:23:38 +08:00
return GetFormatterFunctionContent ( labelIndex , value , ChartCached . NumberToStr ( value , newNumericFormatter ) ) ;
2022-10-30 10:28:17 +08:00
}
if ( minValue > = - 1 & & minValue < = 1 & & maxValue > = - 1 & & maxValue < = 1 )
{
2023-02-10 13:23:38 +08:00
int minAcc = MathUtil . GetPrecision ( minValue ) ;
int maxAcc = MathUtil . GetPrecision ( maxValue ) ;
int curAcc = MathUtil . GetPrecision ( value ) ;
2022-10-30 10:28:17 +08:00
int acc = Mathf . Max ( Mathf . Max ( minAcc , maxAcc ) , curAcc ) ;
2023-02-10 13:23:38 +08:00
return GetFormatterFunctionContent ( labelIndex , value , ChartCached . FloatToStr ( value , newNumericFormatter , acc ) ) ;
2022-10-30 10:28:17 +08:00
}
2023-02-10 13:23:38 +08:00
return GetFormatterFunctionContent ( labelIndex , value , ChartCached . NumberToStr ( value , newNumericFormatter ) ) ;
2022-10-30 10:28:17 +08:00
}
else
{
var content = m_Formatter ;
2025-03-01 22:30:51 +08:00
FormatterHelper . ReplaceAxisLabelContent ( ref content , newNumericFormatter , value , labelIndex , totalIndex ) ;
2022-10-30 10:28:17 +08:00
return GetFormatterFunctionContent ( labelIndex , value , content ) ;
}
}
2024-09-11 08:12:14 +08:00
private static bool isDateFormatter = false ;
private static string newFormatter = null ;
2025-10-31 08:39:29 +08:00
public string GetFormatterDateTime ( int labelIndex , int totalIndex , double value , double minValue , double maxValue , bool local )
2022-10-30 10:28:17 +08:00
{
2025-04-07 10:03:19 +08:00
var timestamp = value ;
2025-10-31 08:39:29 +08:00
var dateTime = DateTimeUtil . GetDateTime ( timestamp , local ) ;
2022-10-30 10:28:17 +08:00
var dateString = string . Empty ;
2022-10-30 10:37:10 +08:00
if ( string . IsNullOrEmpty ( numericFormatter ) | | numericFormatter . Equals ( "f2" ) )
2022-10-30 10:28:17 +08:00
{
dateString = DateTimeUtil . GetDateTimeFormatString ( dateTime , maxValue - minValue ) ;
}
else
{
2023-11-16 08:20:26 +08:00
try
{
2025-03-01 22:30:51 +08:00
if ( DateTimeUtil . IsDateOrTimeRegex ( numericFormatter , ref isDateFormatter , ref newFormatter ) )
2024-09-11 08:12:14 +08:00
{
2025-03-01 22:30:51 +08:00
if ( isDateFormatter )
2025-10-31 08:39:29 +08:00
dateString = ChartCached . NumberToDateStr ( timestamp , newFormatter , local ) ;
2024-09-11 08:12:14 +08:00
else
dateString = ChartCached . NumberToTimeStr ( timestamp , newFormatter ) ;
}
else
{
dateString = dateTime . ToString ( numericFormatter ) ;
}
2023-11-16 08:20:26 +08:00
}
catch
{
XLog . Warning ( "not support datetime formatter:" + numericFormatter ) ;
}
2022-10-30 10:28:17 +08:00
}
if ( ! string . IsNullOrEmpty ( m_Formatter ) )
{
var content = m_Formatter ;
2025-03-01 22:30:51 +08:00
FormatterHelper . ReplaceAxisLabelContent ( ref content , dateString , labelIndex , totalIndex ) ;
2022-10-30 10:28:17 +08:00
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 :
2023-06-12 08:05:10 +08:00
m_FormatterFunction ( labelIndex , value , null , currentContent ) ;
2022-10-30 10:28:17 +08:00
}
2022-04-26 08:24:45 +08:00
}
2022-05-22 22:17:38 +08:00
}