AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
AxisLabel.cs
1 /************************************************/
2 /* */
3 /* Copyright (c) 2018 - 2021 monitor1394 */
4 /* https://github.com/monitor1394 */
5 /* */
6 /************************************************/
7 
8 using System;
9 using UnityEngine;
10 using UnityEngine.UI;
11 
12 namespace XCharts
13 {
18  [Serializable]
19  public class AxisLabel : SubComponent
20  {
21  [SerializeField] private bool m_Show = true;
22  [SerializeField] private string m_Formatter;
23  [SerializeField] private int m_Interval = 0;
24  [SerializeField] private bool m_Inside = false;
25  [SerializeField] private float m_Margin;
26  [SerializeField] private string m_NumericFormatter = "";
27  [SerializeField] private bool m_ShowAsPositiveNumber = false;
28  [SerializeField] private bool m_OnZero = false;
29  [SerializeField] private float m_Width = 0f;
30  [SerializeField] private float m_Height = 0f;
31  [SerializeField] private bool m_ShowStartLabel = true;
32  [SerializeField] private bool m_ShowEndLabel = true;
33  [SerializeField] private TextLimit m_TextLimit = new TextLimit();
34  [SerializeField] private TextStyle m_TextStyle = new TextStyle();
35  private DelegateAxisLabelFormatter m_FormatterFunction;
36 
41  public bool show
42  {
43  get { return m_Show; }
44  set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetComponentDirty(); }
45  }
50  public int interval
51  {
52  get { return m_Interval; }
53  set { if (PropertyUtil.SetStruct(ref m_Interval, value)) SetComponentDirty(); }
54  }
59  public bool inside
60  {
61  get { return m_Inside; }
62  set { if (PropertyUtil.SetStruct(ref m_Inside, value)) SetComponentDirty(); }
63  }
68  public float margin
69  {
70  get { return m_Margin; }
71  set { if (PropertyUtil.SetStruct(ref m_Margin, value)) SetComponentDirty(); }
72  }
77  public string formatter
78  {
79  get { return m_Formatter; }
80  set { if (PropertyUtil.SetClass(ref m_Formatter, value)) SetComponentDirty(); }
81  }
82 
90  public string numericFormatter
91  {
92  get { return m_NumericFormatter; }
93  set { if (PropertyUtil.SetClass(ref m_NumericFormatter, value)) SetComponentDirty(); }
94  }
95 
100  public bool showAsPositiveNumber
101  {
102  get { return m_ShowAsPositiveNumber; }
103  set { if (PropertyUtil.SetStruct(ref m_ShowAsPositiveNumber, value)) SetComponentDirty(); }
104  }
105 
109  public bool onZero
110  {
111  get { return m_OnZero; }
112  set { if (PropertyUtil.SetStruct(ref m_OnZero, value)) SetComponentDirty(); }
113  }
117  public float width
118  {
119  get { return m_Width; }
120  set { if (PropertyUtil.SetStruct(ref m_Width, value)) SetComponentDirty(); }
121  }
125  public float height
126  {
127  get { return m_Height; }
128  set { if (PropertyUtil.SetStruct(ref m_Height, value)) SetComponentDirty(); }
129  }
134  public bool showStartLabel
135  {
136  get { return m_ShowStartLabel; }
137  set { if (PropertyUtil.SetStruct(ref m_ShowStartLabel, value)) SetComponentDirty(); }
138  }
143  public bool showEndLabel
144  {
145  get { return m_ShowEndLabel; }
146  set { if (PropertyUtil.SetStruct(ref m_ShowEndLabel, value)) SetComponentDirty(); }
147  }
151  public TextLimit textLimit
152  {
153  get { return m_TextLimit; }
154  set { if (value != null) { m_TextLimit = value; SetComponentDirty(); } }
155  }
156 
161  public TextStyle textStyle
162  {
163  get { return m_TextStyle; }
164  set { if (PropertyUtil.SetClass(ref m_TextStyle, value)) SetComponentDirty(); }
165  }
166 
167  public DelegateAxisLabelFormatter formatterFunction
168  {
169  set { m_FormatterFunction = value; }
170  }
171 
172  public override bool componentDirty { get { return m_ComponentDirty || m_TextLimit.componentDirty; } }
173  public override void ClearComponentDirty()
174  {
175  base.ClearComponentDirty();
176  textLimit.ClearComponentDirty();
177  }
178 
179  public static AxisLabel defaultAxisLabel
180  {
181  get
182  {
183  return new AxisLabel()
184  {
185  m_Show = true,
186  m_Interval = 0,
187  m_Inside = false,
188  m_Margin = 8,
189  m_TextStyle = new TextStyle(),
190  };
191  }
192  }
193 
194  public AxisLabel Clone()
195  {
196  var axisLabel = new AxisLabel();
197  axisLabel.show = show;
198  axisLabel.formatter = formatter;
199  axisLabel.interval = interval;
200  axisLabel.inside = inside;
201  axisLabel.margin = margin;
202  axisLabel.numericFormatter = numericFormatter;
203  axisLabel.width = width;
204  axisLabel.height = height;
205  axisLabel.showStartLabel = showStartLabel;
206  axisLabel.showEndLabel = showEndLabel;
207  axisLabel.textLimit = textLimit.Clone();
208  axisLabel.textStyle.Copy(textStyle);
209  return axisLabel;
210  }
211 
212  public void Copy(AxisLabel axisLabel)
213  {
214  show = axisLabel.show;
215  formatter = axisLabel.formatter;
216  interval = axisLabel.interval;
217  inside = axisLabel.inside;
218  margin = axisLabel.margin;
219  numericFormatter = axisLabel.numericFormatter;
220  width = axisLabel.width;
221  height = axisLabel.height;
222  showStartLabel = axisLabel.showStartLabel;
223  showEndLabel = axisLabel.showEndLabel;
224  textLimit.Copy(axisLabel.textLimit);
225  textStyle.Copy(axisLabel.textStyle);
226  }
227 
228  public void SetRelatedText(ChartText txt, float labelWidth)
229  {
230  m_TextLimit.SetRelatedText(txt, labelWidth);
231  }
232 
233  public string GetFormatterContent(int labelIndex, string category)
234  {
235  if (m_FormatterFunction != null)
236  {
237  return m_FormatterFunction(labelIndex, 0, category);
238  }
239  if (string.IsNullOrEmpty(category)) return category;
240  if (string.IsNullOrEmpty(m_Formatter))
241  {
242  return m_TextLimit.GetLimitContent(category);
243  }
244  else
245  {
246  var content = m_Formatter;
247  FormatterHelper.ReplaceAxisLabelContent(ref content, category);
248  return m_TextLimit.GetLimitContent(content);
249  }
250  }
251 
252  public string GetFormatterContent(int labelIndex, double value, double minValue, double maxValue, bool isLog = false)
253  {
254  if (showAsPositiveNumber && value < 0)
255  {
256  value = Math.Abs(value);
257  }
258  if (m_FormatterFunction != null)
259  {
260  return m_FormatterFunction(labelIndex, value, null);
261  }
262  if (string.IsNullOrEmpty(m_Formatter))
263  {
264  if (isLog)
265  {
266  return ChartCached.NumberToStr(value, numericFormatter);
267  }
268  if (minValue >= -1 && minValue <= 1 && maxValue >= -1 && maxValue <= 1)
269  {
270  int minAcc = ChartHelper.GetFloatAccuracy(minValue);
271  int maxAcc = ChartHelper.GetFloatAccuracy(maxValue);
272  int curAcc = ChartHelper.GetFloatAccuracy(value);
273  int acc = Mathf.Max(Mathf.Max(minAcc, maxAcc), curAcc);
274  return ChartCached.FloatToStr(value, numericFormatter, acc);
275  }
276  return ChartCached.NumberToStr(value, numericFormatter);
277  }
278  else
279  {
280  var content = m_Formatter;
281  FormatterHelper.ReplaceAxisLabelContent(ref content, numericFormatter, value);
282  return content;
283  }
284  }
285 
286  public string GetFormatterDateTime(int labelIndex, double value)
287  {
288  if (m_FormatterFunction != null)
289  {
290  return m_FormatterFunction(labelIndex, value, null);
291  }
292  var timestamp = (int)value;
293  var dateTime = DateTimeUtil.GetDateTime(timestamp);
294  var format = string.IsNullOrEmpty(numericFormatter) ? "yyyy/M/d" : numericFormatter;
295  if (!string.IsNullOrEmpty(m_Formatter))
296  {
297  var content = m_Formatter;
298  FormatterHelper.ReplaceAxisLabelContent(ref content, dateTime.ToString(format));
299  return m_TextLimit.GetLimitContent(content);
300  }
301  else
302  {
303  var content = dateTime.ToString(format);
304  return m_TextLimit.GetLimitContent(content);
305  }
306  }
307  }
308 }
XCharts.AxisLabel.margin
float margin
The margin between the axis label and the axis line. 刻度标签与轴线之间的距离。
Definition: AxisLabel.cs:69
XCharts.AxisLabel.showAsPositiveNumber
bool showAsPositiveNumber
Show negative number as positive number. 将负数数值显示为正数。一般和Serie的showAsPositiveNumber配合使用。
Definition: AxisLabel.cs:101
XCharts.AxisLabel
Settings related to axis label. 坐标轴刻度标签的相关设置。
Definition: AxisLabel.cs:19
XCharts.AxisLabel.inside
bool inside
Set this to true so the axis labels face the inside direction. 刻度标签是否朝内,默认朝外。
Definition: AxisLabel.cs:60
XCharts.TextStyle
Settings related to text. 文本的相关设置。
Definition: TextStyle.cs:21
XCharts.AxisLabel.height
float height
文本的高。为0时会自动匹配。
Definition: AxisLabel.cs:126
XCharts.AxisLabel.showStartLabel
bool showStartLabel
Whether to display the first label. 是否显示第一个文本。
Definition: AxisLabel.cs:135
XCharts.ChartComponent.componentDirty
virtual bool componentDirty
组件重新初始化标记。
Definition: ChartComponent.cs:25
XCharts.SubComponent
Definition: ChartComponent.cs:71
XCharts.AxisLabel.numericFormatter
string numericFormatter
Standard numeric format strings. 标准数字格式字符串。用于将数值格式化显示为字符串。 使用Axx的形式:A是格式说明符的单字符,支持C货币、D十进制、E指数、F定点数、G...
Definition: AxisLabel.cs:91
XCharts
Definition: RewardChart.cs:14
XCharts.AxisLabel.interval
int interval
The display interval of the axis label. 坐标轴刻度标签的显示间隔,在类目轴中有效。0表示显示所有标签,1表示隔一个隔显示一个标签,以此类推。
Definition: AxisLabel.cs:51
XCharts.TextLimit
Text character limitation and adaptation component. When the length of the text exceeds the set lengt...
Definition: TextLimit.cs:21
XCharts.AxisLabel.textLimit
TextLimit textLimit
文本限制。
Definition: AxisLabel.cs:152
XCharts.AxisLabel.showEndLabel
bool showEndLabel
Whether to display the last label. 是否显示最后一个文本。
Definition: AxisLabel.cs:144
XCharts.AxisLabel.textStyle
TextStyle textStyle
The text style of axis name. 文本样式。
Definition: AxisLabel.cs:162
XCharts.AxisLabel.show
bool show
Set this to false to prevent the axis label from appearing. 是否显示刻度标签。
Definition: AxisLabel.cs:42
XCharts.DelegateAxisLabelFormatter
delegate string DelegateAxisLabelFormatter(int labelIndex, double value, string category)
The delegate function for AxisLabel's formatter. | AxisLabel的formatter自定义委托函数。
XCharts.AxisLabel.formatter
string formatter
图例内容字符串模版格式器。支持用 换行。 模板变量为图例名称 {value}。
Definition: AxisLabel.cs:78
XCharts.AxisLabel.width
float width
文本的宽。为0时会自动匹配。
Definition: AxisLabel.cs:118
XCharts.AxisLabel.onZero
bool onZero
刻度标签显示在0刻度上。
Definition: AxisLabel.cs:110