No Description
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.

LegendHelper.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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.GetOrAddComponent<Image>(btnObj);
  50. img.color = Color.clear;
  51. img.raycastTarget = true;
  52. ChartHelper.GetOrAddComponent<Button>(btnObj);
  53. ChartHelper.GetOrAddComponent<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. foreach (var kv in legend.context.buttonList)
  150. {
  151. var item = kv.Value;
  152. if (currHeight + item.height > legendMaxHeight)
  153. {
  154. currHeight = 0;
  155. offsetX += legend.context.eachWidthDict[row];
  156. row++;
  157. }
  158. item.SetPosition(new Vector3(startX + offsetX, startY - currHeight));
  159. currHeight += item.height + legend.itemGap;
  160. }
  161. }
  162. private static void SetHorizonalItemPosition(Legend legend, float legendMaxWidth, float startX, float startY)
  163. {
  164. var currWidth = 0f;
  165. var offsetY = 0f;
  166. foreach (var kv in legend.context.buttonList)
  167. {
  168. var item = kv.Value;
  169. if (currWidth + item.width > legendMaxWidth)
  170. {
  171. currWidth = 0;
  172. offsetY += legend.context.eachHeight;
  173. }
  174. item.SetPosition(new Vector3(startX + currWidth, startY - offsetY));
  175. currWidth += item.width + legend.itemGap;
  176. }
  177. }
  178. private static void UpdateLegendWidthAndHeight(Legend legend, float maxWidth, float maxHeight)
  179. {
  180. var width = 0f;
  181. var height = 0f;
  182. var realHeight = 0f;
  183. var realWidth = 0f;
  184. legend.context.eachWidthDict.Clear();
  185. legend.context.eachHeight = 0;
  186. if (legend.orient == Orient.Horizonal)
  187. {
  188. foreach (var kv in legend.context.buttonList)
  189. {
  190. if (width + kv.Value.width > maxWidth)
  191. {
  192. realWidth = width - legend.itemGap;
  193. realHeight += height + legend.itemGap;
  194. if (legend.context.eachHeight < height + legend.itemGap)
  195. {
  196. legend.context.eachHeight = height + legend.itemGap;
  197. }
  198. height = 0;
  199. width = 0;
  200. }
  201. width += kv.Value.width + legend.itemGap;
  202. if (kv.Value.height > height)
  203. height = kv.Value.height;
  204. }
  205. width -= legend.itemGap;
  206. legend.context.height = realHeight + height;
  207. legend.context.width = realWidth > 0 ? realWidth : width;
  208. }
  209. else
  210. {
  211. var row = 0;
  212. foreach (var kv in legend.context.buttonList)
  213. {
  214. if (height + kv.Value.height > maxHeight)
  215. {
  216. realHeight = height - legend.itemGap;
  217. realWidth += width + legend.itemGap;
  218. legend.context.eachWidthDict[row] = width + legend.itemGap;
  219. row++;
  220. height = 0;
  221. width = 0;
  222. }
  223. height += kv.Value.height + legend.itemGap;
  224. if (kv.Value.width > width)
  225. width = kv.Value.width;
  226. }
  227. height -= legend.itemGap;
  228. legend.context.height = realHeight > 0 ? realHeight : height;
  229. legend.context.width = realWidth + width;
  230. }
  231. if (legend.padding.show)
  232. {
  233. legend.context.width += legend.padding.left + legend.padding.right;
  234. legend.context.height += legend.padding.top + legend.padding.bottom;
  235. }
  236. }
  237. private static bool IsBeyondWidth(Legend legend, float maxWidth)
  238. {
  239. var totalWidth = 0f;
  240. foreach (var kv in legend.context.buttonList)
  241. {
  242. var item = kv.Value;
  243. totalWidth += item.width + legend.itemGap;
  244. if (totalWidth > maxWidth) return true;
  245. }
  246. return false;
  247. }
  248. public static bool CheckDataShow(Serie serie, string legendName, bool show)
  249. {
  250. bool needShow = false;
  251. if (legendName.Equals(serie.serieName))
  252. {
  253. serie.show = show;
  254. serie.highlight = false;
  255. if (serie.show) needShow = true;
  256. }
  257. else
  258. {
  259. foreach (var data in serie.data)
  260. {
  261. if (legendName.Equals(data.name))
  262. {
  263. data.show = show;
  264. data.context.highlight = false;
  265. if (data.show) needShow = true;
  266. }
  267. }
  268. }
  269. return needShow;
  270. }
  271. public static bool CheckDataHighlighted(Serie serie, string legendName, bool heighlight)
  272. {
  273. bool show = false;
  274. if (legendName.Equals(serie.serieName))
  275. {
  276. serie.highlight = heighlight;
  277. }
  278. else
  279. {
  280. foreach (var data in serie.data)
  281. {
  282. if (legendName.Equals(data.name))
  283. {
  284. data.context.highlight = heighlight;
  285. if (data.context.highlight) show = true;
  286. }
  287. }
  288. }
  289. return show;
  290. }
  291. }
  292. }