AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
Radar.cs
1 /************************************************/
2 /* */
3 /* Copyright (c) 2018 - 2021 monitor1394 */
4 /* https://github.com/monitor1394 */
5 /* */
6 /************************************************/
7 
8 using UnityEngine;
9 using System.Collections.Generic;
10 using System;
11 using System.Text.RegularExpressions;
12 using UnityEngine.UI;
13 
14 namespace XCharts
15 {
20  [System.Serializable]
21  public class Radar : MainComponent
22  {
27  public enum Shape
28  {
29  Polygon,
30  Circle
31  }
36  public enum PositionType
37  {
42  Vertice,
47  Between,
48  }
53  [System.Serializable]
54  public class Indicator
55  {
56  [SerializeField] private string m_Name;
57  [SerializeField] private double m_Max;
58  [SerializeField] private double m_Min;
59  [SerializeField] private double[] m_Range = new double[2] { 0, 0 };
60  [SerializeField] private TextStyle m_TextStyle = new TextStyle();
61 
66  public string name { get { return FormatterHelper.TrimAndReplaceLine(m_Name); } set { m_Name = value; } }
71  public double max { get { return m_Max; } set { m_Max = value; } }
76  public double min { get { return m_Min; } set { m_Min = value; } }
81  public TextStyle textStyle { get { return m_TextStyle; } set { m_TextStyle = value; } }
86  public Text text { get; set; }
91  public double[] range
92  {
93  get { return m_Range; }
94  set { if (value != null && value.Length == 2) { m_Range = value; } }
95  }
96 
97  public bool IsInRange(double value)
98  {
99  if (m_Range == null || m_Range.Length < 2) return true;
100  if (m_Range[0] != 0 || m_Range[1] != 0)
101  return value >= m_Range[0] && value <= m_Range[1];
102  else
103  return true;
104  }
105  }
106  [SerializeField] private bool m_Show;
107  [SerializeField] private Shape m_Shape;
108  [SerializeField] private float m_Radius = 100;
109  [SerializeField] private int m_SplitNumber = 5;
110  [SerializeField] private float[] m_Center = new float[2] { 0.5f, 0.5f };
111  [SerializeField] private AxisLine m_AxisLine = AxisLine.defaultAxisLine;
112  [SerializeField] private AxisSplitLine m_SplitLine = AxisSplitLine.defaultSplitLine;
113  [SerializeField] private AxisSplitArea m_SplitArea = AxisSplitArea.defaultSplitArea;
114  [SerializeField] private bool m_Indicator = true;
115  [SerializeField] private PositionType m_PositionType = PositionType.Vertice;
116  [SerializeField] private float m_IndicatorGap = 10;
117  [SerializeField] private int m_CeilRate = 0;
118  [SerializeField] private bool m_IsAxisTooltip;
119  [SerializeField] private Color32 m_OutRangeColor = Color.red;
120  [SerializeField] private bool m_ConnectCenter = false;
121  [SerializeField] private bool m_LineGradient = true;
122  [SerializeField] private List<Indicator> m_IndicatorList = new List<Indicator>();
128  public bool show { get { return m_Show; } set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetComponentDirty(); } }
134  public Shape shape
135  {
136  get { return m_Shape; }
137  set { if (PropertyUtil.SetStruct(ref m_Shape, value)) SetAllDirty(); }
138  }
143  public float radius
144  {
145  get { return m_Radius; }
146  set { if (PropertyUtil.SetStruct(ref m_Radius, value)) SetAllDirty(); }
147  }
152  public int splitNumber
153  {
154  get { return m_SplitNumber; }
155  set { if (PropertyUtil.SetStruct(ref m_SplitNumber, value)) SetAllDirty(); }
156  }
162  public float[] center
163  {
164  get { return m_Center; }
165  set { if (value != null) { m_Center = value; SetAllDirty(); } }
166  }
171  public AxisLine axisLine
172  {
173  get { return m_AxisLine; }
174  set { if (PropertyUtil.SetClass(ref m_AxisLine, value, true)) SetAllDirty(); }
175  }
180  public AxisSplitLine splitLine
181  {
182  get { return m_SplitLine; }
183  set { if (PropertyUtil.SetClass(ref m_SplitLine, value, true)) SetAllDirty(); }
184  }
189  public AxisSplitArea splitArea
190  {
191  get { return m_SplitArea; }
192  set { if (PropertyUtil.SetClass(ref m_SplitArea, value, true)) SetAllDirty(); }
193  }
198  public bool indicator
199  {
200  get { return m_Indicator; }
201  set { if (PropertyUtil.SetStruct(ref m_Indicator, value)) SetComponentDirty(); }
202  }
207  public float indicatorGap
208  {
209  get { return m_IndicatorGap; }
210  set { if (PropertyUtil.SetStruct(ref m_IndicatorGap, value)) SetComponentDirty(); }
211  }
216  public int ceilRate
217  {
218  get { return m_CeilRate; }
219  set { if (PropertyUtil.SetStruct(ref m_CeilRate, value < 0 ? 0 : value)) SetAllDirty(); }
220  }
224  public bool isAxisTooltip
225  {
226  get { return m_IsAxisTooltip; }
227  set { if (PropertyUtil.SetStruct(ref m_IsAxisTooltip, value)) SetAllDirty(); }
228  }
234  {
235  get { return m_PositionType; }
236  set { if (PropertyUtil.SetStruct(ref m_PositionType, value)) SetAllDirty(); }
237  }
242  public Color32 outRangeColor
243  {
244  get { return m_OutRangeColor; }
245  set { if (PropertyUtil.SetStruct(ref m_OutRangeColor, value)) SetAllDirty(); }
246  }
251  public bool connectCenter
252  {
253  get { return m_ConnectCenter; }
254  set { if (PropertyUtil.SetStruct(ref m_ConnectCenter, value)) SetAllDirty(); }
255  }
260  public bool lineGradient
261  {
262  get { return m_LineGradient; }
263  set { if (PropertyUtil.SetStruct(ref m_LineGradient, value)) SetAllDirty(); }
264  }
269  public List<Indicator> indicatorList { get { return m_IndicatorList; } }
270 
271  public int index { get; internal set; }
276  public Vector3 runtimeCenterPos { get; internal set; }
281  public float runtimeRadius { get; internal set; }
282  public float runtimeDataRadius { get; internal set; }
287  public Dictionary<int, List<Vector3>> runtimeDataPosList = new Dictionary<int, List<Vector3>>();
288 
289  public static Radar defaultRadar
290  {
291  get
292  {
293  var radar = new Radar
294  {
295  m_Show = true,
296  m_Shape = Shape.Polygon,
297  m_Radius = 0.35f,
298  m_SplitNumber = 5,
299  m_Indicator = true,
300  m_IndicatorList = new List<Indicator>(5){
301  new Indicator(){name="indicator1",max = 0},
302  new Indicator(){name="indicator2",max = 0},
303  new Indicator(){name="indicator3",max = 0},
304  new Indicator(){name="indicator4",max = 0},
305  new Indicator(){name="indicator5",max = 0},
306  }
307  };
308  radar.center[0] = 0.5f;
309  radar.center[1] = 0.4f;
310  radar.splitLine.show = true;
311  radar.splitArea.show = true;
312  return radar;
313  }
314  }
315 
316  private bool IsEqualsIndicatorList(List<Indicator> indicators1, List<Indicator> indicators2)
317  {
318  if (indicators1.Count != indicators2.Count) return false;
319  for (int i = 0; i < indicators1.Count; i++)
320  {
321  var indicator1 = indicators1[i];
322  var indicator2 = indicators2[i];
323  if (!indicator1.Equals(indicator2)) return false;
324  }
325  return true;
326  }
327 
328  public bool IsInIndicatorRange(int index, double value)
329  {
330  var indicator = GetIndicator(index);
331  return indicator == null ? true : indicator.IsInRange(value);
332  }
333 
334  public double GetIndicatorMin(int index)
335  {
336  if (index >= 0 && index < m_IndicatorList.Count)
337  {
338  return m_IndicatorList[index].min;
339  }
340  return 0;
341  }
342  public double GetIndicatorMax(int index)
343  {
344  if (index >= 0 && index < m_IndicatorList.Count)
345  {
346  return m_IndicatorList[index].max;
347  }
348  return 0;
349  }
350 
351  internal void UpdateRadarCenter(Vector3 chartPosition, float chartWidth, float chartHeight)
352  {
353  if (center.Length < 2) return;
354  var centerX = center[0] <= 1 ? chartWidth * center[0] : center[0];
355  var centerY = center[1] <= 1 ? chartHeight * center[1] : center[1];
356  runtimeCenterPos = chartPosition + new Vector3(centerX, centerY);
357  if (radius <= 0)
358  {
359  runtimeRadius = 0;
360  }
361  else if (radius <= 1)
362  {
363  runtimeRadius = Mathf.Min(chartWidth, chartHeight) * radius;
364  }
365  else
366  {
368  }
369  if (shape == Radar.Shape.Polygon && positionType == PositionType.Between)
370  {
371  var angle = Mathf.PI / indicatorList.Count;
372  runtimeDataRadius = runtimeRadius * Mathf.Cos(angle);
373  }
374  else
375  {
376  runtimeDataRadius = runtimeRadius;
377  }
378  }
379 
380  public Vector3 GetIndicatorPosition(int index)
381  {
382  int indicatorNum = indicatorList.Count;
383  var angle = 0f;
384  switch (positionType)
385  {
386  case PositionType.Vertice:
387  angle = 2 * Mathf.PI / indicatorNum * index;
388  break;
389  case PositionType.Between:
390  angle = 2 * Mathf.PI / indicatorNum * (index + 0.5f);
391  break;
392  }
393  var x = runtimeCenterPos.x + (runtimeRadius + indicatorGap) * Mathf.Sin(angle);
394  var y = runtimeCenterPos.y + (runtimeRadius + indicatorGap) * Mathf.Cos(angle);
395  return new Vector3(x, y);
396  }
397 
398  public void AddIndicator(Radar.Indicator indicator)
399  {
401  SetAllDirty();
402  }
403 
404  public Radar.Indicator AddIndicator(string name, float min, float max)
405  {
406  var indicator = new Radar.Indicator();
407  indicator.name = name;
408  indicator.min = min;
409  indicator.max = max;
411  SetAllDirty();
412  return indicator;
413  }
414 
415  public bool UpdateIndicator(int indicatorIndex, string name, float min, float max)
416  {
417  var indicator = GetIndicator(indicatorIndex);
418  if (indicator == null) return false;
419  indicator.name = name;
420  indicator.min = min;
421  indicator.max = max;
422  SetAllDirty();
423  return true;
424  }
425 
426  public Radar.Indicator GetIndicator(int indicatorIndex)
427  {
428  if (indicatorIndex < 0 || indicatorIndex > indicatorList.Count - 1) return null;
429  return indicatorList[indicatorIndex];
430  }
431  }
432 }
XCharts.Radar.Indicator.min
double min
The minimum value of indicator, with default value of 0. 指示器的最小值,默认为 0 无限制。
Definition: Radar.cs:76
XCharts.Radar.shape
Shape shape
Radar render type, in which 'Polygon' and 'Circle' are supported. 雷达图绘制类型,支持 'Polygon' 和 'Circle'。
Definition: Radar.cs:135
XCharts.Radar.splitArea
AxisSplitArea splitArea
Split area of axis in grid area. 分割区域。
Definition: Radar.cs:190
XCharts.Radar.splitNumber
int splitNumber
Segments of indicator axis. 指示器轴的分割段数。
Definition: Radar.cs:153
XCharts.Radar.runtimeCenterPos
Vector3 runtimeCenterPos
the center position of radar in container. 雷达图在容器中的具体中心点。
Definition: Radar.cs:276
XCharts.SerieSymbolType.Circle
@ Circle
圆形。
XCharts.Radar.ceilRate
int? ceilRate
The ratio of maximum and minimum values rounded upward. The default is 0, which is automatically calc...
Definition: Radar.cs:217
XCharts.MainComponent
Definition: ChartComponent.cs:67
XCharts.SerieType.Radar
@ Radar
雷达图。雷达图主要用于表现多变量的数据,例如球员的各个属性分析。依赖 radar 组件。
XCharts.TextStyle
Settings related to text. 文本的相关设置。
Definition: TextStyle.cs:21
XCharts.Radar.radius
float radius
the radius of radar. 雷达图的半径。
Definition: Radar.cs:144
XCharts.Radar.positionType
PositionType positionType
The position type of indicator. 显示位置类型。
Definition: Radar.cs:234
XCharts.Radar.Indicator.range
double[] range
Normal range. When the value is outside this range, the display color is automatically changed....
Definition: Radar.cs:92
XCharts.Radar.Indicator
Indicator of radar chart, which is used to assign multiple variables(dimensions) in radar chart....
Definition: Radar.cs:54
XCharts.Radar.Shape
Shape
Radar render type, in which 'Polygon' and 'Circle' are supported. 雷达图绘制类型,支持 'Polygon' 和 'Circle'。
Definition: Radar.cs:27
XCharts
Definition: RewardChart.cs:14
XCharts.Radar.indicatorGap
float indicatorGap
The gap of indicator and radar. 指示器和雷达的间距。
Definition: Radar.cs:208
XCharts.Radar.isAxisTooltip
bool isAxisTooltip
是否Tooltip显示轴线上的所有数据。
Definition: Radar.cs:225
XCharts.Radar
Radar coordinate conponnet for radar charts. 雷达图坐标系组件,只适用于雷达图。
Definition: Radar.cs:21
XCharts.Radar.splitLine
AxisSplitLine splitLine
split line. 分割线。
Definition: Radar.cs:181
XCharts.Radar.Indicator.textStyle
TextStyle textStyle
the style of text. 文本样式。
Definition: Radar.cs:81
XCharts.Radar.indicator
bool indicator
Whether to show indicator. 是否显示指示器。
Definition: Radar.cs:199
XCharts.Radar.indicatorList
List< Indicator > indicatorList
the indicator list. 指示器列表。
Definition: Radar.cs:269
XCharts.Radar.runtimeDataPosList
Dictionary< int, List< Vector3 > > runtimeDataPosList
the data position list of radar. 雷达图的所有数据坐标点列表。
Definition: Radar.cs:287
XCharts.Radar.runtimeRadius
float runtimeRadius
the true radius of radar. 雷达图的运行时实际半径。
Definition: Radar.cs:281
XCharts.Radar.Indicator.name
string name
The name of indicator. 指示器名称。
Definition: Radar.cs:66
XCharts.AxisSplitArea
Split area of axis in grid area, not shown by default. 坐标轴在 grid 区域中的分隔区域,默认不显示。
Definition: AxisSplitArea.cs:19
XCharts.Radar.show
bool show
[default:true] Set this to false to prevent the radar from showing. 是否显示雷达坐标系组件。
Definition: Radar.cs:128
XCharts.Radar.lineGradient
bool lineGradient
Whether need gradient for data line. 数值线段是否需要渐变。
Definition: Radar.cs:261
XCharts.Radar.axisLine
AxisLine axisLine
axis line. 轴线。
Definition: Radar.cs:172
XCharts.AxisLine
Settings related to axis line. 坐标轴轴线。
Definition: AxisLine.cs:17
XCharts.Radar.Indicator.text
Text text
the text conponent of indicator. 指示器的文本组件。
Definition: Radar.cs:86
XCharts.Radar.Indicator.max
double max
The maximum value of indicator, with default value of 0, but we recommend to set it manually....
Definition: Radar.cs:71
XCharts.Radar.PositionType
PositionType
The position type of radar. 显示位置。
Definition: Radar.cs:36
XCharts.Radar.center
float[] center
the center of radar chart. 雷达图的中心点。数组的第一项是横坐标,第二项是纵坐标。 当值为0-1之间时表示百分比,设置成百分比时第一项是相对于容器宽度,第二项是相对于容器高度。
Definition: Radar.cs:163
XCharts.Radar.outRangeColor
Color32 outRangeColor
The color displayed when data out of range. 数值超出范围时显示的颜色。
Definition: Radar.cs:243
XCharts.AxisSplitLine
Split line of axis in grid area. 坐标轴在 grid 区域中的分隔线。
Definition: AxisSplitLine.cs:18
XCharts.Radar.connectCenter
bool connectCenter
Whether serie data connect to radar center with line. 数值是否连线到中心点。
Definition: Radar.cs:252