2019-10-22 04:09:04 +08:00
|
|
|
|
/******************************************/
|
|
|
|
|
|
/* */
|
|
|
|
|
|
/* Copyright (c) 2018 monitor1394 */
|
|
|
|
|
|
/* https://github.com/monitor1394 */
|
|
|
|
|
|
/* */
|
|
|
|
|
|
/******************************************/
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
namespace XCharts
|
|
|
|
|
|
{
|
2019-08-01 23:49:30 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Settings related to axis label.
|
|
|
|
|
|
/// 坐标轴刻度标签的相关设置。
|
|
|
|
|
|
/// </summary>
|
2019-07-13 16:38:38 +08:00
|
|
|
|
[Serializable]
|
2019-10-14 18:13:08 +08:00
|
|
|
|
public class AxisLabel : SubComponent
|
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;
|
2019-07-13 16:38:38 +08:00
|
|
|
|
[SerializeField] private float m_Rotate;
|
|
|
|
|
|
[SerializeField] private float m_Margin;
|
|
|
|
|
|
[SerializeField] private Color m_Color;
|
|
|
|
|
|
[SerializeField] private int m_FontSize;
|
|
|
|
|
|
[SerializeField] private FontStyle m_FontStyle;
|
2019-10-26 05:02:32 +08:00
|
|
|
|
[SerializeField] private bool m_ForceENotation = false;
|
2020-01-26 22:34:57 +08:00
|
|
|
|
[SerializeField] private TextLimit m_TextLimit = new TextLimit();
|
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.
|
|
|
|
|
|
/// 是否显示刻度标签。
|
|
|
|
|
|
/// </summary>
|
2020-03-05 20:25:19 +08:00
|
|
|
|
public bool show
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_Show; }
|
|
|
|
|
|
set { if (PropertyUtility.SetStruct(ref m_Show, value)) SetComponentDirty(); }
|
|
|
|
|
|
}
|
2019-08-01 23:49:30 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The display interval of the axis label.
|
|
|
|
|
|
/// 坐标轴刻度标签的显示间隔,在类目轴中有效。0表示显示所有标签,1表示隔一个隔显示一个标签,以此类推。
|
|
|
|
|
|
/// </summary>
|
2020-03-05 20:25:19 +08:00
|
|
|
|
public int interval
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_Interval; }
|
|
|
|
|
|
set { if (PropertyUtility.SetStruct(ref m_Interval, value)) SetComponentDirty(); }
|
|
|
|
|
|
}
|
2019-08-01 23:49:30 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Set this to true so the axis labels face the inside direction.
|
|
|
|
|
|
/// 刻度标签是否朝内,默认朝外。
|
|
|
|
|
|
/// </summary>
|
2020-03-05 20:25:19 +08:00
|
|
|
|
public bool inside
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_Inside; }
|
|
|
|
|
|
set { if (PropertyUtility.SetStruct(ref m_Inside, value)) SetComponentDirty(); }
|
|
|
|
|
|
}
|
2019-08-01 23:49:30 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Rotation degree of axis label, which is especially useful when there is no enough space for category axis.
|
|
|
|
|
|
/// 刻度标签旋转的角度,在类目轴的类目标签显示不下的时候可以通过旋转防止标签之间重叠。
|
|
|
|
|
|
/// </summary>
|
2020-03-05 20:25:19 +08:00
|
|
|
|
public float rotate
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_Rotate; }
|
|
|
|
|
|
set { if (PropertyUtility.SetStruct(ref m_Rotate, value)) SetComponentDirty(); }
|
|
|
|
|
|
}
|
2019-08-01 23:49:30 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The margin between the axis label and the axis line.
|
|
|
|
|
|
/// 刻度标签与轴线之间的距离。
|
|
|
|
|
|
/// </summary>
|
2020-03-05 20:25:19 +08:00
|
|
|
|
public float margin
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_Margin; }
|
|
|
|
|
|
set { if (PropertyUtility.SetStruct(ref m_Margin, value)) SetComponentDirty(); }
|
|
|
|
|
|
}
|
2019-08-01 23:49:30 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// the color of axis label text.
|
|
|
|
|
|
/// 刻度标签文字的颜色,默认取Theme的axisTextColor。
|
|
|
|
|
|
/// </summary>
|
2020-03-05 20:25:19 +08:00
|
|
|
|
public Color color
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_Color; }
|
|
|
|
|
|
set { if (PropertyUtility.SetColor(ref m_Color, value)) SetComponentDirty(); }
|
|
|
|
|
|
}
|
2019-08-01 23:49:30 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// font size.
|
|
|
|
|
|
/// 文字的字体大小。
|
|
|
|
|
|
/// </summary>
|
2020-03-05 20:25:19 +08:00
|
|
|
|
public int fontSize
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_FontSize; }
|
|
|
|
|
|
set { if (PropertyUtility.SetStruct(ref m_FontSize, value)) SetComponentDirty(); }
|
|
|
|
|
|
}
|
2019-08-01 23:49:30 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// font style.
|
|
|
|
|
|
/// 文字字体的风格。
|
|
|
|
|
|
/// </summary>
|
2020-03-05 20:25:19 +08:00
|
|
|
|
public FontStyle fontStyle
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_FontStyle; }
|
|
|
|
|
|
set { if (PropertyUtility.SetStruct(ref m_FontStyle, value)) SetComponentDirty(); }
|
|
|
|
|
|
}
|
2019-09-23 19:09:56 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 图例内容字符串模版格式器。支持用 \n 换行。
|
2020-01-15 19:49:09 +08:00
|
|
|
|
/// 模板变量为图例名称 {value},支持{value:f0},{value:f1},{value:f2}
|
2019-09-23 19:09:56 +08:00
|
|
|
|
/// </summary>
|
2020-03-05 20:25:19 +08:00
|
|
|
|
public string formatter
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_Formatter; }
|
|
|
|
|
|
set { if (PropertyUtility.SetClass(ref m_Formatter, value)) SetComponentDirty(); }
|
|
|
|
|
|
}
|
2019-10-26 05:02:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 是否强制使用科学计数法格式化显示数值。默认为false,当小数精度大于3时才采用科学计数法。
|
|
|
|
|
|
/// </summary>
|
2020-03-05 20:25:19 +08:00
|
|
|
|
public bool forceENotation
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return m_ForceENotation; }
|
|
|
|
|
|
set { if (PropertyUtility.SetStruct(ref m_ForceENotation, 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(); } }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool componentDirty { get { return m_ComponentDirty || m_TextLimit.componentDirty; } }
|
|
|
|
|
|
internal override void ClearComponentDirty()
|
|
|
|
|
|
{
|
|
|
|
|
|
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,
|
|
|
|
|
|
m_Rotate = 0,
|
|
|
|
|
|
m_Margin = 8,
|
|
|
|
|
|
m_Color = Color.clear,
|
|
|
|
|
|
m_FontSize = 18,
|
|
|
|
|
|
m_FontStyle = FontStyle.Normal
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-09-23 19:09:56 +08:00
|
|
|
|
|
2020-01-26 22:34:57 +08:00
|
|
|
|
public void SetRelatedText(Text txt, float labelWidth)
|
|
|
|
|
|
{
|
2020-02-08 15:16:28 +08:00
|
|
|
|
m_TextLimit.SetRelatedText(txt, labelWidth);
|
2020-01-26 22:34:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-09-23 19:09:56 +08:00
|
|
|
|
public string GetFormatterContent(string category)
|
|
|
|
|
|
{
|
2020-01-26 22:34:57 +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
|
|
|
|
|
|
{
|
|
|
|
|
|
var content = m_Formatter.Replace("{value}", category);
|
|
|
|
|
|
content = content.Replace("\\n", "\n");
|
|
|
|
|
|
content = content.Replace("<br/>", "\n");
|
2020-01-26 22:34:57 +08:00
|
|
|
|
return m_TextLimit.GetLimitContent(content);
|
2019-09-23 19:09:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-15 19:41:21 +08:00
|
|
|
|
public string GetFormatterContent(float value, float minValue, float maxValue, bool isLog = false)
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (value - (int)value == 0)
|
|
|
|
|
|
return ChartCached.IntToStr((int)value);
|
|
|
|
|
|
else
|
|
|
|
|
|
return ChartCached.FloatToStr(value);
|
|
|
|
|
|
}
|
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);
|
2019-10-26 05:02:32 +08:00
|
|
|
|
return ChartCached.FloatToStr(value, acc, m_ForceENotation);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (value - (int)value == 0)
|
2019-09-23 19:09:56 +08:00
|
|
|
|
return ChartCached.IntToStr((int)value);
|
|
|
|
|
|
else
|
|
|
|
|
|
return ChartCached.FloatToStr(value, 1);
|
2019-10-26 05:02:32 +08:00
|
|
|
|
}
|
2019-11-11 18:21:30 -08:00
|
|
|
|
else if (m_Formatter.Contains("{value"))
|
2019-09-23 19:09:56 +08:00
|
|
|
|
{
|
|
|
|
|
|
var content = m_Formatter;
|
2020-01-15 19:49:09 +08:00
|
|
|
|
if (content.Contains("{value:f0}"))
|
|
|
|
|
|
content = m_Formatter.Replace("{value:f0}", ChartCached.IntToStr((int)value));
|
2019-09-23 19:09:56 +08:00
|
|
|
|
if (content.Contains("{value:f2}"))
|
|
|
|
|
|
content = m_Formatter.Replace("{value:f2}", ChartCached.FloatToStr(value, 2));
|
|
|
|
|
|
else if (content.Contains("{value:f1}"))
|
|
|
|
|
|
content = m_Formatter.Replace("{value:f1}", ChartCached.FloatToStr(value, 1));
|
|
|
|
|
|
else if (content.Contains("{value}"))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (value - (int)value == 0)
|
|
|
|
|
|
content = m_Formatter.Replace("{value}", ChartCached.IntToStr((int)value));
|
|
|
|
|
|
else
|
|
|
|
|
|
content = m_Formatter.Replace("{value}", ChartCached.FloatToStr(value, 1));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
content = content.Replace("\\n", "\n");
|
|
|
|
|
|
content = content.Replace("<br/>", "\n");
|
|
|
|
|
|
return content;
|
2019-12-06 10:08:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2019-11-11 18:21:30 -08:00
|
|
|
|
return value.ToString(m_Formatter);
|
2019-09-23 19:09:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-07-13 16:38:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|