Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

LegendHelper.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace XCharts.Runtime
  5. {
  6. public static class LegendHelper
  7. {
  8. public static Color GetContentColor(BaseChart chart, int legendIndex, string legendName, Legend legend, ThemeStyle theme, bool active)
  9. {
  10. var textStyle = legend.labelStyle.textStyle;
  11. if (active)
  12. {
  13. if (legend.labelStyle.textStyle.autoColor)
  14. return SeriesHelper.GetNameColor(chart, legendIndex, legendName);
  15. else
  16. return !ChartHelper.IsClearColor(textStyle.color) ? textStyle.color : theme.legend.textColor;
  17. }
  18. else return theme.legend.unableColor;
  19. }
  20. public static Color GetIconColor(BaseChart chart, Legend legend, int readIndex, string legendName, bool active)
  21. {
  22. if (active)
  23. {
  24. if (legend.itemAutoColor)
  25. {
  26. return SeriesHelper.GetNameColor(chart, readIndex, legendName);
  27. }
  28. else
  29. return legend.GetColor(readIndex);
  30. }
  31. else return chart.theme.legend.unableColor;
  32. }
  33. public static LegendItem AddLegendItem(BaseChart chart, Legend legend, int i, string legendName, Transform parent,
  34. ThemeStyle theme, string content, Color itemColor, bool active, int legendIndex)
  35. {
  36. var objName = i + "_" + legendName;
  37. var anchorMin = new Vector2(0, 0.5f);
  38. var anchorMax = new Vector2(0, 0.5f);
  39. var pivot = new Vector2(0, 0.5f);
  40. var sizeDelta = new Vector2(100, 30);
  41. var iconSizeDelta = new Vector2(legend.itemWidth, legend.itemHeight);
  42. var textStyle = legend.labelStyle.textStyle;
  43. var contentColor = GetContentColor(chart, legendIndex, legendName, legend, theme, active);
  44. var objAnchorMin = new Vector2(0, 1);
  45. var objAnchorMax = new Vector2(0, 1);
  46. var objPivot = new Vector2(0, 1);
  47. var btnObj = ChartHelper.AddObject(objName, parent, objAnchorMin, objAnchorMax, objPivot, sizeDelta);
  48. var iconObj = ChartHelper.AddObject("icon", btnObj.transform, anchorMin, anchorMax, pivot, iconSizeDelta);
  49. var img = ChartHelper.EnsureComponent<Image>(btnObj);
  50. img.color = Color.clear;
  51. img.raycastTarget = true;
  52. ChartHelper.EnsureComponent<Button>(btnObj);
  53. ChartHelper.EnsureComponent<Image>(iconObj);
  54. var label = ChartHelper.AddChartLabel("content", btnObj.transform, legend.labelStyle, theme.legend,
  55. content, contentColor, TextAnchor.MiddleLeft);
  56. label.SetActive(true);
  57. var item = new LegendItem();
  58. item.index = i;
  59. item.name = objName;
  60. item.legendName = legendName;
  61. item.SetObject(btnObj);
  62. item.SetIconSize(legend.itemWidth, legend.itemHeight);
  63. item.SetIconColor(itemColor);
  64. item.SetIconImage(legend.GetIcon(i));
  65. item.SetContentPosition(legend.labelStyle.offset);
  66. item.SetContent(content);
  67. //item.SetBackground(legend.background);
  68. return item;
  69. }
  70. public static void SetLegendBackground(Legend legend, ImageStyle style)
  71. {
  72. var background = legend.context.background;
  73. if (background == null) return;
  74. ChartHelper.SetActive(background, style.show);
  75. if (!style.show) return;
  76. var rect = background.gameObject.GetComponent<RectTransform>();
  77. rect.localPosition = legend.context.center;
  78. rect.sizeDelta = new Vector2(legend.context.width, legend.context.height);
  79. ChartHelper.SetBackground(background, style);
  80. }
  81. public static void ResetItemPosition(Legend legend, Vector3 chartPos, float chartWidth, float chartHeight)
  82. {
  83. legend.location.UpdateRuntimeData(chartWidth, chartHeight);
  84. var startX = 0f;
  85. var startY = 0f;
  86. var legendMaxWidth = chartWidth - legend.location.runtimeLeft - legend.location.runtimeRight;
  87. var legendMaxHeight = chartHeight - legend.location.runtimeTop - legend.location.runtimeBottom;
  88. UpdateLegendWidthAndHeight(legend, legendMaxWidth, legendMaxHeight);
  89. var legendRuntimeWidth = legend.context.width;
  90. var legendRuntimeHeight = legend.context.height;
  91. var isVertical = legend.orient == Orient.Vertical;
  92. switch (legend.location.align)
  93. {
  94. case Location.Align.TopCenter:
  95. startX = chartPos.x + chartWidth / 2 - legendRuntimeWidth / 2;
  96. startY = chartPos.y + chartHeight - legend.location.runtimeTop;
  97. break;
  98. case Location.Align.TopLeft:
  99. startX = chartPos.x + legend.location.runtimeLeft;
  100. startY = chartPos.y + chartHeight - legend.location.runtimeTop;
  101. break;
  102. case Location.Align.TopRight:
  103. startX = chartPos.x + chartWidth - legendRuntimeWidth - legend.location.runtimeRight;
  104. startY = chartPos.y + chartHeight - legend.location.runtimeTop;
  105. break;
  106. case Location.Align.Center:
  107. startX = chartPos.x + chartWidth / 2 - legendRuntimeWidth / 2;
  108. startY = chartPos.y + chartHeight / 2 + legendRuntimeHeight / 2;
  109. break;
  110. case Location.Align.CenterLeft:
  111. startX = chartPos.x + legend.location.runtimeLeft;
  112. startY = chartPos.y + chartHeight / 2 + legendRuntimeHeight / 2;
  113. break;
  114. case Location.Align.CenterRight:
  115. startX = chartPos.x + chartWidth - legendRuntimeWidth - legend.location.runtimeRight;
  116. startY = chartPos.y + chartHeight / 2 + legendRuntimeHeight / 2;
  117. break;
  118. case Location.Align.BottomCenter:
  119. startX = chartPos.x + chartWidth / 2 - legendRuntimeWidth / 2;
  120. startY = chartPos.y + legendRuntimeHeight + legend.location.runtimeBottom;
  121. break;
  122. case Location.Align.BottomLeft:
  123. startX = chartPos.x + legend.location.runtimeLeft;
  124. startY = chartPos.y + legendRuntimeHeight + legend.location.runtimeBottom;
  125. break;
  126. case Location.Align.BottomRight:
  127. startX = chartPos.x + chartWidth - legendRuntimeWidth - legend.location.runtimeRight;
  128. startY = chartPos.y + legendRuntimeHeight + legend.location.runtimeBottom;
  129. break;
  130. }
  131. if (!legend.padding.show)
  132. {
  133. legend.context.center = new Vector2(startX + legend.context.width / 2, startY - legend.context.height / 2);
  134. }
  135. else
  136. {
  137. legend.context.center = new Vector2(startX + legend.context.width / 2 - legend.padding.left,
  138. startY - legend.context.height / 2 + legend.padding.top);
  139. }
  140. if (isVertical) SetVerticalItemPosition(legend, legendMaxHeight, startX, startY);
  141. else SetHorizonalItemPosition(legend, legendMaxWidth, startX, startY);
  142. SetLegendBackground(legend, legend.background);
  143. }
  144. private static void SetVerticalItemPosition(Legend legend, float legendMaxHeight, float startX, float startY)
  145. {
  146. var currHeight = 0f;
  147. var offsetX = 0f;
  148. var row = 0;
  149. var index = 0;
  150. foreach (var kv in legend.context.buttonList)
  151. {
  152. var item = kv.Value;
  153. if (currHeight + item.height > legendMaxHeight)
  154. {
  155. currHeight = 0;
  156. offsetX += legend.context.eachWidthDict[row];
  157. row++;
  158. }
  159. item.SetPosition(legend.GetPosition(index++, new Vector3(startX + offsetX, startY - currHeight)));
  160. currHeight += item.height + legend.itemGap;
  161. }
  162. }
  163. private static void SetHorizonalItemPosition(Legend legend, float legendMaxWidth, float startX, float startY)
  164. {
  165. var currWidth = 0f;
  166. var offsetY = 0f;
  167. var index = 0;
  168. foreach (var kv in legend.context.buttonList)
  169. {
  170. var item = kv.Value;
  171. if (currWidth + item.width > legendMaxWidth)
  172. {
  173. currWidth = 0;
  174. offsetY += legend.context.eachHeight;
  175. }
  176. item.SetPosition(legend.GetPosition(index++, new Vector3(startX + currWidth, startY - offsetY)));
  177. currWidth += item.width + legend.itemGap;
  178. }
  179. }
  180. private static void UpdateLegendWidthAndHeight(Legend legend, float maxWidth, float maxHeight)
  181. {
  182. var width = 0f;
  183. var height = 0f;
  184. var realHeight = 0f;
  185. var realWidth = 0f;
  186. legend.context.eachWidthDict.Clear();
  187. legend.context.eachHeight = 0;
  188. if (legend.orient == Orient.Horizonal)
  189. {
  190. foreach (var kv in legend.context.buttonList)
  191. {
  192. if (width + kv.Value.width > maxWidth)
  193. {
  194. realWidth = width - legend.itemGap;
  195. realHeight += height + legend.itemGap;
  196. if (legend.context.eachHeight < height + legend.itemGap)
  197. {
  198. legend.context.eachHeight = height + legend.itemGap;
  199. }
  200. height = 0;
  201. width = 0;
  202. }
  203. width += kv.Value.width + legend.itemGap;
  204. if (kv.Value.height > height)
  205. height = kv.Value.height;
  206. }
  207. width -= legend.itemGap;
  208. legend.context.height = realHeight + height;
  209. legend.context.width = realWidth > 0 ? realWidth : width;
  210. }
  211. else
  212. {
  213. var row = 0;
  214. foreach (var kv in legend.context.buttonList)
  215. {
  216. if (height + kv.Value.height > maxHeight)
  217. {
  218. realHeight = height - legend.itemGap;
  219. realWidth += width + legend.itemGap;
  220. legend.context.eachWidthDict[row] = width + legend.itemGap;
  221. row++;
  222. height = 0;
  223. width = 0;
  224. }
  225. height += kv.Value.height + legend.itemGap;
  226. if (kv.Value.width > width)
  227. width = kv.Value.width;
  228. }
  229. height -= legend.itemGap;
  230. legend.context.height = realHeight > 0 ? realHeight : height;
  231. legend.context.width = realWidth + width;
  232. }
  233. if (legend.padding.show)
  234. {
  235. legend.context.width += legend.padding.left + legend.padding.right;
  236. legend.context.height += legend.padding.top + legend.padding.bottom;
  237. }
  238. }
  239. private static bool IsBeyondWidth(Legend legend, float maxWidth)
  240. {
  241. var totalWidth = 0f;
  242. foreach (var kv in legend.context.buttonList)
  243. {
  244. var item = kv.Value;
  245. totalWidth += item.width + legend.itemGap;
  246. if (totalWidth > maxWidth) return true;
  247. }
  248. return false;
  249. }
  250. public static bool CheckDataShow(Serie serie, string legendName, bool show)
  251. {
  252. bool needShow = false;
  253. if (legendName.Equals(serie.serieName))
  254. {
  255. serie.show = show;
  256. serie.highlight = false;
  257. if (serie.show) needShow = true;
  258. }
  259. else
  260. {
  261. foreach (var data in serie.data)
  262. {
  263. if (legendName.Equals(data.name))
  264. {
  265. data.show = show;
  266. data.context.highlight = false;
  267. if (data.show) needShow = true;
  268. }
  269. }
  270. }
  271. return needShow;
  272. }
  273. public static bool CheckDataHighlighted(Serie serie, string legendName, bool heighlight)
  274. {
  275. bool show = false;
  276. if (legendName.Equals(serie.serieName))
  277. {
  278. serie.highlight = heighlight;
  279. }
  280. else
  281. {
  282. foreach (var data in serie.data)
  283. {
  284. if (legendName.Equals(data.name))
  285. {
  286. data.context.highlight = heighlight;
  287. if (data.context.highlight) show = true;
  288. }
  289. }
  290. }
  291. return show;
  292. }
  293. }
  294. }