2019-10-14 07:45:56 +08:00
using UnityEngine ;
2022-02-19 22:37:57 +08:00
namespace XCharts.Runtime
2019-10-14 07:45:56 +08:00
{
/// <summary>
/// 图形样式。
/// </summary>
[System.Serializable]
2021-11-23 13:20:07 +08:00
public class ItemStyle : ChildComponent , ISerieDataComponent
2019-10-14 07:45:56 +08:00
{
2021-11-23 13:20:07 +08:00
[SerializeField] private bool m_Show = true ;
2020-08-23 14:31:26 +08:00
[SerializeField] private Color32 m_Color ;
2021-03-10 13:03:36 +08:00
[SerializeField] private Color32 m_Color0 ;
2020-08-23 14:31:26 +08:00
[SerializeField] private Color32 m_ToColor ;
[SerializeField] private Color32 m_ToColor2 ;
[SerializeField] private Color32 m_BackgroundColor ;
2020-03-08 10:47:48 +08:00
[SerializeField] private float m_BackgroundWidth ;
2020-08-23 14:31:26 +08:00
[SerializeField] private Color32 m_CenterColor ;
2020-03-08 10:47:48 +08:00
[SerializeField] private float m_CenterGap ;
2019-10-14 07:45:56 +08:00
[SerializeField] private float m_BorderWidth = 0 ;
2022-04-18 08:20:16 +08:00
[SerializeField] private float m_BorderGap = 0 ;
2020-08-23 14:31:26 +08:00
[SerializeField] private Color32 m_BorderColor ;
2021-03-10 13:03:36 +08:00
[SerializeField] private Color32 m_BorderColor0 ;
2021-04-26 07:17:26 +08:00
[SerializeField] private Color32 m_BorderToColor ;
2022-05-22 22:17:38 +08:00
[SerializeField] [ Range ( 0 , 1 ) ] private float m_Opacity = 1 ;
2021-12-19 20:53:55 +08:00
[SerializeField] private string m_ItemMarker ;
[SerializeField] private string m_ItemFormatter ;
2020-05-04 13:29:56 +08:00
[SerializeField] private string m_NumericFormatter = "" ;
2020-03-29 10:57:59 +08:00
[SerializeField] private float [ ] m_CornerRadius = new float [ ] { 0 , 0 , 0 , 0 } ;
2019-10-14 07:45:56 +08:00
2020-06-10 13:10:24 +08:00
public void Reset ( )
{
m_Show = false ;
m_Color = Color . clear ;
2021-03-10 13:03:36 +08:00
m_Color0 = Color . clear ;
2020-06-10 13:10:24 +08:00
m_ToColor = Color . clear ;
2020-07-30 09:31:44 +08:00
m_ToColor2 = Color . clear ;
2020-06-10 13:10:24 +08:00
m_BackgroundColor = Color . clear ;
m_BackgroundWidth = 0 ;
m_CenterColor = Color . clear ;
m_CenterGap = 0 ;
m_BorderWidth = 0 ;
2022-04-18 08:20:16 +08:00
m_BorderGap = 0 ;
2020-06-10 13:10:24 +08:00
m_BorderColor = Color . clear ;
2021-03-10 13:03:36 +08:00
m_BorderColor0 = Color . clear ;
2021-04-26 07:17:26 +08:00
m_BorderToColor = Color . clear ;
2020-06-10 13:10:24 +08:00
m_Opacity = 1 ;
2021-12-19 20:53:55 +08:00
m_ItemFormatter = null ;
m_ItemMarker = null ;
2020-06-10 13:10:24 +08:00
m_NumericFormatter = "" ;
2020-06-12 09:22:51 +08:00
if ( m_CornerRadius = = null )
{
m_CornerRadius = new float [ ] { 0 , 0 , 0 , 0 } ;
}
else
{
for ( int i = 0 ; i < m_CornerRadius . Length ; i + + )
m_CornerRadius [ i ] = 0 ;
}
2020-06-10 13:10:24 +08:00
}
2019-10-14 07:45:56 +08:00
/// <summary>
/// 是否启用。
/// </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 ) ) SetVerticesDirty ( ) ; }
2020-03-05 20:25:19 +08:00
}
2019-10-14 07:45:56 +08:00
/// <summary>
/// 数据项颜色。
/// </summary>
2020-08-23 14:31:26 +08:00
public Color32 color
2020-03-05 20:25:19 +08:00
{
get { return m_Color ; }
2021-01-11 08:54:28 +08:00
set { if ( PropertyUtil . SetColor ( ref m_Color , value ) ) SetVerticesDirty ( ) ; }
2020-03-05 20:25:19 +08:00
}
2019-10-14 07:45:56 +08:00
/// <summary>
2021-03-10 13:03:36 +08:00
/// 数据项颜色。
/// </summary>
public Color32 color0
{
get { return m_Color0 ; }
set { if ( PropertyUtil . SetColor ( ref m_Color0 , value ) ) SetVerticesDirty ( ) ; }
}
/// <summary>
2020-07-30 09:31:44 +08:00
/// Gradient color1.
2022-03-24 08:37:06 +08:00
/// |渐变色的颜色1。
2020-03-17 08:37:48 +08:00
/// </summary>
2020-08-23 14:31:26 +08:00
public Color32 toColor
2020-03-17 08:37:48 +08:00
{
get { return m_ToColor ; }
2021-01-11 08:54:28 +08:00
set { if ( PropertyUtil . SetColor ( ref m_ToColor , value ) ) SetVerticesDirty ( ) ; }
2020-03-17 08:37:48 +08:00
}
/// <summary>
2020-07-30 09:31:44 +08:00
/// Gradient color2.Only valid in line diagrams.
2022-03-24 08:37:06 +08:00
/// |渐变色的颜色2。只在折线图中有效。
2020-07-30 09:31:44 +08:00
/// </summary>
2020-08-23 14:31:26 +08:00
public Color32 toColor2
2020-07-30 09:31:44 +08:00
{
get { return m_ToColor2 ; }
2021-01-11 08:54:28 +08:00
set { if ( PropertyUtil . SetColor ( ref m_ToColor2 , value ) ) SetVerticesDirty ( ) ; }
2020-07-30 09:31:44 +08:00
}
/// <summary>
2020-03-08 10:47:48 +08:00
/// 数据项背景颜色。
/// </summary>
2020-08-23 14:31:26 +08:00
public Color32 backgroundColor
2020-03-08 10:47:48 +08:00
{
get { return m_BackgroundColor ; }
2021-01-11 08:54:28 +08:00
set { if ( PropertyUtil . SetColor ( ref m_BackgroundColor , value ) ) SetVerticesDirty ( ) ; }
2020-03-08 10:47:48 +08:00
}
/// <summary>
2022-05-05 13:10:04 +08:00
/// 数据项背景宽度。
/// </summary>
public float backgroundWidth
{
get { return m_BackgroundWidth ; }
set { if ( PropertyUtil . SetStruct ( ref m_BackgroundWidth , value ) ) SetVerticesDirty ( ) ; }
}
/// <summary>
2020-03-08 10:47:48 +08:00
/// 中心区域颜色。
/// </summary>
2020-08-23 14:31:26 +08:00
public Color32 centerColor
2020-03-08 10:47:48 +08:00
{
get { return m_CenterColor ; }
2021-01-11 08:54:28 +08:00
set { if ( PropertyUtil . SetColor ( ref m_CenterColor , value ) ) SetVerticesDirty ( ) ; }
2020-03-08 10:47:48 +08:00
}
/// <summary>
/// 中心区域间隙。
/// </summary>
public float centerGap
{
get { return m_CenterGap ; }
2021-01-11 08:54:28 +08:00
set { if ( PropertyUtil . SetStruct ( ref m_CenterGap , value ) ) SetVerticesDirty ( ) ; }
2020-03-08 10:47:48 +08:00
}
/// <summary>
2019-10-14 07:45:56 +08:00
/// 边框的颜色。
/// </summary>
2020-08-23 14:31:26 +08:00
public Color32 borderColor
2020-03-05 20:25:19 +08:00
{
get { return m_BorderColor ; }
2021-01-11 08:54:28 +08:00
set { if ( PropertyUtil . SetColor ( ref m_BorderColor , value ) ) SetVerticesDirty ( ) ; }
2020-03-05 20:25:19 +08:00
}
2019-10-14 07:45:56 +08:00
/// <summary>
2021-03-10 13:03:36 +08:00
/// 边框的颜色。
/// </summary>
public Color32 borderColor0
{
get { return m_BorderColor0 ; }
set { if ( PropertyUtil . SetColor ( ref m_BorderColor0 , value ) ) SetVerticesDirty ( ) ; }
}
/// <summary>
2021-04-26 07:17:26 +08:00
/// 边框的渐变色。
/// </summary>
public Color32 borderToColor
{
get { return m_BorderToColor ; }
set { if ( PropertyUtil . SetColor ( ref m_BorderToColor , value ) ) SetVerticesDirty ( ) ; }
}
/// <summary>
2019-10-14 07:45:56 +08:00
/// 边框宽。
/// </summary>
2020-03-05 20:25:19 +08:00
public float borderWidth
{
get { return m_BorderWidth ; }
2021-01-11 08:54:28 +08:00
set { if ( PropertyUtil . SetStruct ( ref m_BorderWidth , value ) ) SetVerticesDirty ( ) ; }
2020-03-05 20:25:19 +08:00
}
2019-10-14 07:45:56 +08:00
/// <summary>
2022-04-18 08:20:16 +08:00
/// 边框间隙。
/// </summary>
public float borderGap
{
get { return m_BorderGap ; }
set { if ( PropertyUtil . SetStruct ( ref m_BorderGap , value ) ) SetVerticesDirty ( ) ; }
}
/// <summary>
2019-10-14 07:45:56 +08:00
/// 透明度。支持从 0 到 1 的数字,为 0 时不绘制该图形。
/// </summary>
2020-03-05 20:25:19 +08:00
public float opacity
{
get { return m_Opacity ; }
2021-01-11 08:54:28 +08:00
set { if ( PropertyUtil . SetStruct ( ref m_Opacity , value ) ) SetVerticesDirty ( ) ; }
2020-03-05 20:25:19 +08:00
}
2020-03-09 22:10:48 +08:00
/// <summary>
2020-03-21 11:26:50 +08:00
/// 提示框单项的字符串模版格式器。具体配置参考`Tooltip`的`formatter`
/// </summary>
2021-12-19 20:53:55 +08:00
public string itemFormatter
2020-03-21 11:26:50 +08:00
{
2021-12-19 20:53:55 +08:00
get { return m_ItemFormatter ; }
set { if ( PropertyUtil . SetClass ( ref m_ItemFormatter , value ) ) SetVerticesDirty ( ) ; }
}
/// <summary>
/// 提示框单项的字符标志。用在Tooltip中。
/// </summary>
public string itemMarker
{
get { return m_ItemMarker ; }
set { if ( PropertyUtil . SetClass ( ref m_ItemMarker , value ) ) SetVerticesDirty ( ) ; }
2020-03-21 11:26:50 +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
/// </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 ( ) ; }
2020-05-04 13:29:56 +08:00
}
/// <summary>
2020-03-29 10:57:59 +08:00
/// The radius of rounded corner. Its unit is px. Use array to respectively specify the 4 corner radiuses((clockwise upper left, upper right, bottom right and bottom left)).
2022-03-24 08:37:06 +08:00
/// |圆角半径。用数组分别指定4个圆角半径( 顺时针左上, 右上, 右下, 左下) 。
2020-03-29 10:57:59 +08:00
/// </summary>
public float [ ] cornerRadius
{
get { return m_CornerRadius ; }
2021-01-11 08:54:28 +08:00
set { if ( PropertyUtil . SetClass ( ref m_CornerRadius , value , true ) ) SetVerticesDirty ( ) ; }
2020-03-29 10:57:59 +08:00
}
/// <summary>
2020-03-09 22:10:48 +08:00
/// 实际边框宽。边框不显示时为0。
/// </summary>
public float runtimeBorderWidth { get { return NeedShowBorder ( ) ? borderWidth : 0 ; } }
/// <summary>
/// 是否需要显示边框。
/// </summary>
public bool NeedShowBorder ( )
{
2020-05-13 09:54:40 +08:00
return borderWidth ! = 0 & & ! ChartHelper . IsClearColor ( borderColor ) ;
2020-03-09 22:10:48 +08:00
}
2020-08-23 14:31:26 +08:00
public Color32 GetColor ( )
2020-03-17 08:37:48 +08:00
{
2021-11-23 13:20:07 +08:00
if ( m_Opacity = = 1 | | m_Color . a = = 0 )
return m_Color ;
2020-03-17 08:37:48 +08:00
var color = m_Color ;
2022-05-22 22:17:38 +08:00
color . a = ( byte ) ( color . a * m_Opacity ) ;
2020-03-17 08:37:48 +08:00
return color ;
2021-03-10 13:03:36 +08:00
}
2022-03-04 22:17:32 +08:00
2022-04-13 13:26:46 +08:00
public Color32 GetToColor ( )
{
if ( m_Opacity = = 1 | | m_ToColor . a = = 0 )
return m_ToColor ;
var color = m_ToColor ;
2022-05-22 22:17:38 +08:00
color . a = ( byte ) ( color . a * m_Opacity ) ;
2022-04-13 13:26:46 +08:00
return color ;
}
2021-03-10 13:03:36 +08:00
public Color32 GetColor0 ( )
{
2021-11-23 13:20:07 +08:00
if ( m_Opacity = = 1 | | m_Color0 . a = = 0 )
return m_Color0 ;
2021-03-10 13:03:36 +08:00
var color = m_Color0 ;
2022-05-22 22:17:38 +08:00
color . a = ( byte ) ( color . a * m_Opacity ) ;
2021-03-10 13:03:36 +08:00
return color ;
}
2022-03-04 22:17:32 +08:00
2021-03-10 13:03:36 +08:00
public Color32 GetColor ( Color32 defaultColor )
{
var color = ChartHelper . IsClearColor ( m_Color ) ? defaultColor : m_Color ;
2021-11-23 13:20:07 +08:00
if ( m_Opacity = = 1 | | color . a = = 0 )
return color ;
2022-05-22 22:17:38 +08:00
color . a = ( byte ) ( color . a * m_Opacity ) ;
2021-03-10 13:03:36 +08:00
return color ;
}
2022-03-04 22:17:32 +08:00
2021-03-10 13:03:36 +08:00
public Color32 GetColor0 ( Color32 defaultColor )
{
var color = ChartHelper . IsClearColor ( m_Color0 ) ? defaultColor : m_Color0 ;
2021-11-23 13:20:07 +08:00
if ( m_Opacity = = 1 | | color . a = = 0 )
return color ;
2022-05-22 22:17:38 +08:00
color . a = ( byte ) ( color . a * m_Opacity ) ;
2021-03-10 13:03:36 +08:00
return color ;
}
2022-03-04 22:17:32 +08:00
2021-03-10 13:03:36 +08:00
public Color32 GetBorderColor ( Color32 defaultColor )
{
var color = ChartHelper . IsClearColor ( m_BorderColor ) ? defaultColor : m_BorderColor ;
2021-11-23 13:20:07 +08:00
if ( m_Opacity = = 1 | | color . a = = 0 )
return color ;
2022-05-22 22:17:38 +08:00
color . a = ( byte ) ( color . a * m_Opacity ) ;
2021-03-10 13:03:36 +08:00
return color ;
}
2022-03-04 22:17:32 +08:00
2021-03-10 13:03:36 +08:00
public Color32 GetBorderColor0 ( Color32 defaultColor )
{
var color = ChartHelper . IsClearColor ( m_BorderColor0 ) ? defaultColor : m_BorderColor0 ;
2021-11-23 13:20:07 +08:00
if ( m_Opacity = = 1 | | color . a = = 0 )
return color ;
2022-05-22 22:17:38 +08:00
color . a = ( byte ) ( color . a * m_Opacity ) ;
2021-03-10 13:03:36 +08:00
return color ;
2020-03-17 08:37:48 +08:00
}
2020-07-30 09:31:44 +08:00
public bool IsNeedGradient ( )
{
return ! ChartHelper . IsClearColor ( m_ToColor ) | | ! ChartHelper . IsClearColor ( m_ToColor2 ) ;
}
2020-08-23 14:31:26 +08:00
public Color32 GetGradientColor ( float value , Color32 defaultColor )
2020-07-30 09:31:44 +08:00
{
2021-11-23 13:20:07 +08:00
if ( ! IsNeedGradient ( ) )
return ChartConst . clearColor32 ;
2020-07-30 09:31:44 +08:00
value = Mathf . Clamp01 ( value ) ;
2020-08-23 14:31:26 +08:00
var startColor = ChartHelper . IsClearColor ( m_Color ) ? defaultColor : m_Color ;
2020-08-29 23:35:40 +08:00
Color32 color ;
2021-11-23 13:20:07 +08:00
2020-08-23 14:31:26 +08:00
if ( ! ChartHelper . IsClearColor ( m_ToColor2 ) )
2020-07-30 09:31:44 +08:00
{
2021-11-23 13:20:07 +08:00
if ( value < = 0.5f )
color = Color32 . Lerp ( startColor , m_ToColor , 2 * value ) ;
else
color = Color32 . Lerp ( m_ToColor , m_ToColor2 , 2 * ( value - 0.5f ) ) ;
2020-07-30 09:31:44 +08:00
}
else
{
2020-08-29 23:35:40 +08:00
color = Color32 . Lerp ( startColor , m_ToColor , value ) ;
2020-07-30 09:31:44 +08:00
}
2020-08-29 23:35:40 +08:00
if ( m_Opacity ! = 1 )
{
2022-05-22 22:17:38 +08:00
color . a = ( byte ) ( color . a * m_Opacity ) ;
2020-08-29 23:35:40 +08:00
}
return color ;
2020-07-30 09:31:44 +08:00
}
2021-11-23 13:20:07 +08:00
2022-04-18 08:20:16 +08:00
public bool IsNeedCorner ( )
2021-11-23 13:20:07 +08:00
{
2022-04-18 08:20:16 +08:00
if ( m_CornerRadius = = null ) return false ;
foreach ( var value in m_CornerRadius )
2021-11-23 13:20:07 +08:00
{
if ( value ! = 0 ) return true ;
}
return false ;
}
}
2022-05-22 22:17:38 +08:00
}