설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RadarCoord.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace XCharts.Runtime
  5. {
  6. /// <summary>
  7. /// Radar coordinate conponnet for radar charts.
  8. /// 雷达图坐标系组件,只适用于雷达图。
  9. /// </summary>
  10. [System.Serializable]
  11. [ComponentHandler(typeof(RadarCoordHandler), true)]
  12. [CoordOptions(typeof(RadarCoord))]
  13. public class RadarCoord : CoordSystem, ISerieContainer
  14. {
  15. /// <summary>
  16. /// Radar render type, in which 'Polygon' and 'Circle' are supported.
  17. /// |雷达图绘制类型,支持 'Polygon' 和 'Circle'。
  18. /// </summary>
  19. public enum Shape
  20. {
  21. Polygon,
  22. Circle
  23. }
  24. /// <summary>
  25. /// The position type of radar.
  26. /// |显示位置。
  27. /// </summary>
  28. public enum PositionType
  29. {
  30. /// <summary>
  31. /// Display at the vertex.
  32. /// |显示在顶点处。
  33. /// </summary>
  34. Vertice,
  35. /// <summary>
  36. /// Display at the middle of line.
  37. /// |显示在两者之间。
  38. /// </summary>
  39. Between,
  40. }
  41. /// <summary>
  42. /// Indicator of radar chart, which is used to assign multiple variables(dimensions) in radar chart.
  43. /// |雷达图的指示器,用来指定雷达图中的多个变量(维度)。
  44. /// </summary>
  45. [System.Serializable]
  46. public class Indicator
  47. {
  48. [SerializeField] private string m_Name;
  49. [SerializeField] private double m_Max;
  50. [SerializeField] private double m_Min;
  51. [SerializeField] private double[] m_Range = new double[2] { 0, 0 };
  52. /// <summary>
  53. /// The name of indicator.
  54. /// |指示器名称。
  55. /// </summary>
  56. public string name { get { return m_Name; } set { m_Name = value; } }
  57. /// <summary>
  58. /// The maximum value of indicator, with default value of 0, but we recommend to set it manually.
  59. /// |指示器的最大值,默认为 0 无限制。
  60. /// </summary>
  61. public double max { get { return m_Max; } set { m_Max = value; } }
  62. /// <summary>
  63. /// The minimum value of indicator, with default value of 0.
  64. /// |指示器的最小值,默认为 0 无限制。
  65. /// </summary>
  66. public double min { get { return m_Min; } set { m_Min = value; } }
  67. /// <summary>
  68. /// the text conponent of indicator.
  69. /// |指示器的文本组件。
  70. /// </summary>
  71. public Text text { get; set; }
  72. /// <summary>
  73. /// Normal range. When the value is outside this range, the display color is automatically changed.
  74. /// |正常值范围。当数值不在这个范围时,会自动变更显示颜色。
  75. /// </summary>
  76. public double[] range
  77. {
  78. get { return m_Range; }
  79. set { if (value != null && value.Length == 2) { m_Range = value; } }
  80. }
  81. public bool IsInRange(double value)
  82. {
  83. if (m_Range == null || m_Range.Length < 2) return true;
  84. if (m_Range[0] != 0 || m_Range[1] != 0)
  85. return value >= m_Range[0] && value <= m_Range[1];
  86. else
  87. return true;
  88. }
  89. }
  90. [SerializeField] private bool m_Show;
  91. [SerializeField] private Shape m_Shape;
  92. [SerializeField] private float m_Radius = 100;
  93. [SerializeField] private int m_SplitNumber = 5;
  94. [SerializeField] private float[] m_Center = new float[2] { 0.5f, 0.5f };
  95. [SerializeField] private AxisLine m_AxisLine = AxisLine.defaultAxisLine;
  96. [SerializeField] private AxisName m_AxisName = AxisName.defaultAxisName;
  97. [SerializeField] private AxisSplitLine m_SplitLine = AxisSplitLine.defaultSplitLine;
  98. [SerializeField] private AxisSplitArea m_SplitArea = AxisSplitArea.defaultSplitArea;
  99. [SerializeField] private bool m_Indicator = true;
  100. [SerializeField] private PositionType m_PositionType = PositionType.Vertice;
  101. [SerializeField] private float m_IndicatorGap = 10;
  102. [SerializeField] private double m_CeilRate = 0;
  103. [SerializeField] private bool m_IsAxisTooltip;
  104. [SerializeField] private Color32 m_OutRangeColor = Color.red;
  105. [SerializeField] private bool m_ConnectCenter = false;
  106. [SerializeField] private bool m_LineGradient = true;
  107. [SerializeField][Since("v3.4.0")] private float m_StartAngle;
  108. [SerializeField] private List<Indicator> m_IndicatorList = new List<Indicator>();
  109. public RadarCoordContext context = new RadarCoordContext();
  110. /// <summary>
  111. /// [default:true]
  112. /// Set this to false to prevent the radar from showing.
  113. /// |是否显示雷达坐标系组件。
  114. /// </summary>
  115. public bool show { get { return m_Show; } set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetComponentDirty(); } }
  116. /// <summary>
  117. /// Radar render type, in which 'Polygon' and 'Circle' are supported.
  118. /// |雷达图绘制类型,支持 'Polygon' 和 'Circle'。
  119. /// </summary>
  120. /// <value></value>
  121. public Shape shape
  122. {
  123. get { return m_Shape; }
  124. set { if (PropertyUtil.SetStruct(ref m_Shape, value)) SetAllDirty(); }
  125. }
  126. /// <summary>
  127. /// the radius of radar.
  128. /// |雷达图的半径。
  129. /// </summary>
  130. public float radius
  131. {
  132. get { return m_Radius; }
  133. set { if (PropertyUtil.SetStruct(ref m_Radius, value)) SetAllDirty(); }
  134. }
  135. /// <summary>
  136. /// Segments of indicator axis.
  137. /// |指示器轴的分割段数。
  138. /// </summary>
  139. public int splitNumber
  140. {
  141. get { return m_SplitNumber; }
  142. set { if (PropertyUtil.SetStruct(ref m_SplitNumber, value)) SetAllDirty(); }
  143. }
  144. /// <summary>
  145. /// the center of radar chart.
  146. /// |雷达图的中心点。数组的第一项是横坐标,第二项是纵坐标。
  147. /// 当值为0-1之间时表示百分比,设置成百分比时第一项是相对于容器宽度,第二项是相对于容器高度。
  148. /// </summary>
  149. public float[] center
  150. {
  151. get { return m_Center; }
  152. set { if (value != null) { m_Center = value; SetAllDirty(); } }
  153. }
  154. /// <summary>
  155. /// axis line.
  156. /// |轴线。
  157. /// </summary>
  158. public AxisLine axisLine
  159. {
  160. get { return m_AxisLine; }
  161. set { if (PropertyUtil.SetClass(ref m_AxisLine, value, true)) SetAllDirty(); }
  162. }
  163. /// <summary>
  164. /// Name options for radar indicators.
  165. /// |雷达图每个指示器名称的配置项。
  166. /// </summary>
  167. public AxisName axisName
  168. {
  169. get { return m_AxisName; }
  170. set { if (PropertyUtil.SetClass(ref m_AxisName, value, true)) SetAllDirty(); }
  171. }
  172. /// <summary>
  173. /// split line.
  174. /// |分割线。
  175. /// </summary>
  176. public AxisSplitLine splitLine
  177. {
  178. get { return m_SplitLine; }
  179. set { if (PropertyUtil.SetClass(ref m_SplitLine, value, true)) SetAllDirty(); }
  180. }
  181. /// <summary>
  182. /// Split area of axis in grid area.
  183. /// |分割区域。
  184. /// </summary>
  185. public AxisSplitArea splitArea
  186. {
  187. get { return m_SplitArea; }
  188. set { if (PropertyUtil.SetClass(ref m_SplitArea, value, true)) SetAllDirty(); }
  189. }
  190. /// <summary>
  191. /// Whether to show indicator.
  192. /// |是否显示指示器。
  193. /// </summary>
  194. public bool indicator
  195. {
  196. get { return m_Indicator; }
  197. set { if (PropertyUtil.SetStruct(ref m_Indicator, value)) SetComponentDirty(); }
  198. }
  199. /// <summary>
  200. /// The gap of indicator and radar.
  201. /// |指示器和雷达的间距。
  202. /// </summary>
  203. public float indicatorGap
  204. {
  205. get { return m_IndicatorGap; }
  206. set { if (PropertyUtil.SetStruct(ref m_IndicatorGap, value)) SetComponentDirty(); }
  207. }
  208. /// <summary>
  209. /// The ratio of maximum and minimum values rounded upward. The default is 0, which is automatically calculated.
  210. /// |最大最小值向上取整的倍率。默认为0时自动计算。
  211. /// </summary>
  212. public double ceilRate
  213. {
  214. get { return m_CeilRate; }
  215. set { if (PropertyUtil.SetStruct(ref m_CeilRate, value < 0 ? 0 : value)) SetAllDirty(); }
  216. }
  217. /// <summary>
  218. /// 是否Tooltip显示轴线上的所有数据。
  219. /// </summary>
  220. public bool isAxisTooltip
  221. {
  222. get { return m_IsAxisTooltip; }
  223. set { if (PropertyUtil.SetStruct(ref m_IsAxisTooltip, value)) SetAllDirty(); }
  224. }
  225. /// <summary>
  226. /// The position type of indicator.
  227. /// |显示位置类型。
  228. /// </summary>
  229. public PositionType positionType
  230. {
  231. get { return m_PositionType; }
  232. set { if (PropertyUtil.SetStruct(ref m_PositionType, value)) SetAllDirty(); }
  233. }
  234. /// <summary>
  235. /// The color displayed when data out of range.
  236. /// |数值超出范围时显示的颜色。
  237. /// </summary>
  238. public Color32 outRangeColor
  239. {
  240. get { return m_OutRangeColor; }
  241. set { if (PropertyUtil.SetStruct(ref m_OutRangeColor, value)) SetAllDirty(); }
  242. }
  243. /// <summary>
  244. /// Whether serie data connect to radar center with line.
  245. /// |数值是否连线到中心点。
  246. /// </summary>
  247. public bool connectCenter
  248. {
  249. get { return m_ConnectCenter; }
  250. set { if (PropertyUtil.SetStruct(ref m_ConnectCenter, value)) SetAllDirty(); }
  251. }
  252. /// <summary>
  253. /// Whether need gradient for data line.
  254. /// |数值线段是否需要渐变。
  255. /// </summary>
  256. public bool lineGradient
  257. {
  258. get { return m_LineGradient; }
  259. set { if (PropertyUtil.SetStruct(ref m_LineGradient, value)) SetAllDirty(); }
  260. }
  261. /// <summary>
  262. /// 起始角度。和时钟一样,12点钟位置是0度,顺时针到360度。
  263. /// </summary>
  264. public float startAngle
  265. {
  266. get { return m_StartAngle; }
  267. set { if (PropertyUtil.SetStruct(ref m_StartAngle, value)) SetVerticesDirty(); }
  268. }
  269. /// <summary>
  270. /// the indicator list.
  271. /// |指示器列表。
  272. /// </summary>
  273. public List<Indicator> indicatorList { get { return m_IndicatorList; } }
  274. public bool IsPointerEnter()
  275. {
  276. return context.isPointerEnter;
  277. }
  278. public override void SetDefaultValue()
  279. {
  280. m_Show = true;
  281. m_Shape = Shape.Polygon;
  282. m_Radius = 0.35f;
  283. m_SplitNumber = 5;
  284. m_Indicator = true;
  285. m_IndicatorList = new List<Indicator>(5)
  286. {
  287. new Indicator() { name = "indicator1", max = 0 },
  288. new Indicator() { name = "indicator2", max = 0 },
  289. new Indicator() { name = "indicator3", max = 0 },
  290. new Indicator() { name = "indicator4", max = 0 },
  291. new Indicator() { name = "indicator5", max = 0 },
  292. };
  293. center[0] = 0.5f;
  294. center[1] = 0.4f;
  295. splitLine.show = true;
  296. splitArea.show = true;
  297. axisName.show = true;
  298. axisName.name = null;
  299. }
  300. private bool IsEqualsIndicatorList(List<Indicator> indicators1, List<Indicator> indicators2)
  301. {
  302. if (indicators1.Count != indicators2.Count) return false;
  303. for (int i = 0; i < indicators1.Count; i++)
  304. {
  305. var indicator1 = indicators1[i];
  306. var indicator2 = indicators2[i];
  307. if (!indicator1.Equals(indicator2)) return false;
  308. }
  309. return true;
  310. }
  311. public bool IsInIndicatorRange(int index, double value)
  312. {
  313. var indicator = GetIndicator(index);
  314. return indicator == null ? true : indicator.IsInRange(value);
  315. }
  316. public double GetIndicatorMin(int index)
  317. {
  318. if (index >= 0 && index < m_IndicatorList.Count)
  319. {
  320. return m_IndicatorList[index].min;
  321. }
  322. return 0;
  323. }
  324. public double GetIndicatorMax(int index)
  325. {
  326. if (index >= 0 && index < m_IndicatorList.Count)
  327. {
  328. return m_IndicatorList[index].max;
  329. }
  330. return 0;
  331. }
  332. internal void UpdateRadarCenter(Vector3 chartPosition, float chartWidth, float chartHeight)
  333. {
  334. if (center.Length < 2) return;
  335. var centerX = center[0] <= 1 ? chartWidth * center[0] : center[0];
  336. var centerY = center[1] <= 1 ? chartHeight * center[1] : center[1];
  337. context.center = chartPosition + new Vector3(centerX, centerY);
  338. if (radius <= 0)
  339. {
  340. context.radius = 0;
  341. }
  342. else if (radius <= 1)
  343. {
  344. context.radius = Mathf.Min(chartWidth, chartHeight) * radius;
  345. }
  346. else
  347. {
  348. context.radius = radius;
  349. }
  350. if (shape == RadarCoord.Shape.Polygon && positionType == PositionType.Between)
  351. {
  352. var angle = Mathf.PI / indicatorList.Count;
  353. context.dataRadius = context.radius * Mathf.Cos(angle);
  354. }
  355. else
  356. {
  357. context.dataRadius = context.radius;
  358. }
  359. }
  360. public Vector3 GetIndicatorPosition(int index)
  361. {
  362. int indicatorNum = indicatorList.Count;
  363. var angle = 0f;
  364. switch (positionType)
  365. {
  366. case PositionType.Vertice:
  367. angle = 2 * Mathf.PI / indicatorNum * index;
  368. break;
  369. case PositionType.Between:
  370. angle = 2 * Mathf.PI / indicatorNum * (index + 0.5f);
  371. break;
  372. }
  373. var x = context.center.x + (context.radius + indicatorGap) * Mathf.Sin(angle);
  374. var y = context.center.y + (context.radius + indicatorGap) * Mathf.Cos(angle);
  375. return new Vector3(x, y);
  376. }
  377. public void AddIndicator(RadarCoord.Indicator indicator)
  378. {
  379. indicatorList.Add(indicator);
  380. SetAllDirty();
  381. }
  382. public RadarCoord.Indicator AddIndicator(string name, double min, double max)
  383. {
  384. var indicator = new RadarCoord.Indicator();
  385. indicator.name = name;
  386. indicator.min = min;
  387. indicator.max = max;
  388. indicatorList.Add(indicator);
  389. SetAllDirty();
  390. return indicator;
  391. }
  392. [Since("v3.3.0")]
  393. public void AddIndicatorList(List<string> nameList, double min = 0, double max = 0)
  394. {
  395. foreach (var name in nameList)
  396. AddIndicator(name, min, max);
  397. }
  398. public bool UpdateIndicator(int indicatorIndex, string name, double min, double max)
  399. {
  400. var indicator = GetIndicator(indicatorIndex);
  401. if (indicator == null) return false;
  402. indicator.name = name;
  403. indicator.min = min;
  404. indicator.max = max;
  405. SetAllDirty();
  406. return true;
  407. }
  408. public RadarCoord.Indicator GetIndicator(int indicatorIndex)
  409. {
  410. if (indicatorIndex < 0 || indicatorIndex > indicatorList.Count - 1) return null;
  411. return indicatorList[indicatorIndex];
  412. }
  413. public override void ClearData()
  414. {
  415. indicatorList.Clear();
  416. }
  417. public string GetFormatterIndicatorContent(int indicatorIndex)
  418. {
  419. var indicator = GetIndicator(indicatorIndex);
  420. if (indicator == null)
  421. return string.Empty;
  422. else
  423. return GetFormatterIndicatorContent(indicator.name);
  424. }
  425. public string GetFormatterIndicatorContent(string indicatorName)
  426. {
  427. if (string.IsNullOrEmpty(indicatorName))
  428. return indicatorName;
  429. if (string.IsNullOrEmpty(m_AxisName.labelStyle.formatter))
  430. {
  431. return indicatorName;
  432. }
  433. else
  434. {
  435. var content = m_AxisName.labelStyle.formatter;
  436. FormatterHelper.ReplaceAxisLabelContent(ref content, indicatorName);
  437. return content;
  438. }
  439. }
  440. }
  441. }