Nessuna descrizione
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.

Moudle.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Data;
  3. using System.Collections.Generic;
  4. using TMPro;
  5. using XCharts.Runtime;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. public class Moudle : MonoBehaviour
  9. {
  10. //==========Moudle===========================
  11. public static void 載入日期(int num,TMP_Dropdown 開始年_cb,TMP_Dropdown 開始月_cb,TMP_Dropdown 開始日_cb){
  12. Main.Global.目前開始月份[num] = 開始月_cb.options[開始月_cb.value].text;
  13. Main.Global.目前開始年度[num] = 開始年_cb.options[開始年_cb.value].text;
  14. if(Main.Global.上次開始月份[num] != Main.Global.目前開始月份[num] || Main.Global.上次開始年度[num] != Main.Global.目前開始年度[num]){
  15. Main.Global.上次開始月份[num] = Main.Global.目前開始月份[num]; Main.Global.上次開始年度[num] = Main.Global.目前開始年度[num];
  16. List<string> 日期Options = new(){ "" };
  17. int N_year,N_month;
  18. N_year = int.Parse(開始年_cb.options[開始年_cb.value].text);
  19. if(開始月_cb.value==0){
  20. N_month = 1;
  21. } else{
  22. N_month = 開始月_cb.value;
  23. }
  24. for(int i=1;i<= DateTime.DaysInMonth(N_year,N_month);i++){
  25. DateTime day2 = new (N_year ,N_month ,i);
  26. 日期Options.Add(i.ToString()+","+day2.DayOfWeek);
  27. }
  28. 開始日_cb.ClearOptions();
  29. 開始日_cb.AddOptions(日期Options);
  30. 開始日_cb.value=0;
  31. }
  32. }
  33. //===========================================
  34. public static void 載入表格(string table_name,DataTable table,Transform tableParent,GameObject rowPrefab,GameObject ceneter,TMP_FontAsset myFont,Sprite UIsprite,GameObject LinePrefab,bool overflowMode){
  35. 清除控件(ceneter);
  36. Image centerImage = ceneter.GetComponent<Image>();
  37. if (centerImage == null)
  38. {
  39. centerImage = ceneter.AddComponent<Image>();
  40. centerImage.type = Image.Type.Sliced;
  41. }
  42. centerImage.sprite = UIsprite;
  43. centerImage.fillCenter = false;
  44. centerImage.color = Main.Global.系統主題 == 1 ? new Color(0f, 0f, 0f, 1f) : new Color(1f, 1f, 1f, 1f);
  45. //==========動態生成表格==================================
  46. GameObject headerRow = Instantiate(rowPrefab, tableParent);
  47. // 設定表頭行的位置
  48. headerRow.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
  49. headerRow.AddComponent<Image>().type=Image.Type.Sliced;
  50. headerRow.GetComponent<Image>().sprite=UIsprite;
  51. headerRow.GetComponent<Image>().fillCenter=false;
  52. headerRow.GetComponent<Image>().color = Main.Global.系統主題 == 1 ? new Color(0f, 0f, 0f, 1f) : new Color(1f, 1f, 1f, 1f);
  53. float width = headerRow.GetComponent<RectTransform>().rect.width / table.Columns.Count ;
  54. //headerRow.AddComponent<Image>().color=new Color(0f,0.67f,0.87f,1f);
  55. // 取得所有欄位名稱,並建立表頭
  56. for (int i = 0; i < table.Columns.Count; i++)
  57. {
  58. GameObject headerText = new GameObject(table_name+"_Header_"+i.ToString());
  59. headerText.transform.SetParent(headerRow.transform, false);
  60. headerText.AddComponent<TextMeshProUGUI>().text = table.Columns[i].ColumnName;
  61. headerText.GetComponent<TextMeshProUGUI>().tag = "Header";
  62. }
  63. GridLayoutGroup gridLayout = headerRow.GetComponent<GridLayoutGroup>();
  64. gridLayout.cellSize =new Vector2(width, 70);
  65. for (int i = 0; i < table.Rows.Count; i++){
  66. // 建立資料列
  67. GameObject row = Instantiate(rowPrefab, tableParent);
  68. // 設定資料列位置
  69. row.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, -70 * (i + 1));
  70. // 建立資料列中的所有欄位
  71. for (int j = 0; j < table.Columns.Count; j++)
  72. {
  73. // 取得欄位值
  74. string value = table.Rows[i][j].ToString();
  75. if(string.Equals(value, "true", StringComparison.OrdinalIgnoreCase) || string.Equals(value, "false", StringComparison.OrdinalIgnoreCase)){
  76. Toggle cellText = new GameObject(table_name+"_Cells_"+i.ToString()+"_"+j.ToString()).AddComponent<Toggle>();
  77. cellText.transform.SetParent(row.transform, false);
  78. cellText.tag = "Toggle";
  79. Image Background_img = new GameObject("bg"+i.ToString()).AddComponent<Image>();
  80. Background_img.transform.SetParent(cellText.transform, false);
  81. Background_img.sprite = Main.Global.sprite_box;
  82. Background_img.rectTransform.sizeDelta = new Vector2(100f,70f);
  83. Image Check_img = new GameObject("Check"+i.ToString()).AddComponent<Image>();
  84. Check_img.transform.SetParent(cellText.transform, false);
  85. Check_img.rectTransform.sizeDelta = new Vector2(100f,70f);
  86. Check_img.sprite = Main.Global.sprite_check;
  87. cellText.graphic = Check_img;
  88. cellText.targetGraphic=Background_img;
  89. if(string.Equals(value, "true", StringComparison.OrdinalIgnoreCase)){
  90. cellText.isOn=true;
  91. }
  92. }else{
  93. GameObject cellText = new GameObject(table_name+"_Cells_"+i.ToString()+"_"+j.ToString());
  94. cellText.transform.SetParent(row.transform, false);
  95. double number;
  96. if (double.TryParse(value, out number) && table_name=="區間"){
  97. string formattedValue = String.Format("{0:###,##0.##}", number);
  98. cellText.AddComponent<TextMeshProUGUI>().text = formattedValue;
  99. }else{
  100. cellText.AddComponent<TextMeshProUGUI>().text = value;
  101. }
  102. //cellText.AddComponent<TextMeshProUGUI>().text = value;
  103. cellText.GetComponent<TextMeshProUGUI>().tag="Cells";
  104. if(overflowMode){cellText.GetComponent<TextMeshProUGUI>().overflowMode=TextOverflowModes.Ellipsis;}
  105. cellText.AddComponent<Button>();
  106. cellText.GetComponent<Button>().onClick.AddListener(() => { Main.按鈕事件2(cellText); });
  107. }
  108. // 建立欄位文字
  109. }
  110. gridLayout = row.GetComponent<GridLayoutGroup>();
  111. gridLayout.cellSize =new Vector2(width, 70);
  112. }
  113. TextMeshProUGUI[] texts = ceneter.GetComponentsInChildren<TextMeshProUGUI>();
  114. foreach (TextMeshProUGUI text in texts){
  115. if(text.tag.Contains("Header")){
  116. text.font = myFont;
  117. text.color = Main.Global.系統主題 == 1 ? new Color(0, 0, 0, 1) : text.color;
  118. text.alignment = TextAlignmentOptions.Center;
  119. text.fontStyle = FontStyles.Bold;
  120. text.fontSize=22;
  121. if(!text.name.Contains("年度")){
  122. text.enableAutoSizing = true;
  123. text.autoSizeTextContainer = true;
  124. }
  125. text.margin = new Vector4(10, 0, 10, 0);
  126. }else if(text.tag.Contains("Cells")){
  127. text.font = myFont;
  128. text.color = Main.Global.系統主題 == 1 ? new Color(0, 0, 0, 1) : text.color;
  129. text.alignment = TextAlignmentOptions.Center;
  130. text.fontStyle = FontStyles.Bold;
  131. text.fontSize=18;
  132. if(!text.name.Contains("年度")){
  133. text.enableAutoSizing = true;
  134. text.autoSizeTextContainer = true;
  135. }
  136. text.margin = new Vector4(10, 0, 10, 0);
  137. }else{
  138. }
  139. }
  140. ceneter.GetComponent<RectTransform>().sizeDelta = new Vector2(ceneter.GetComponent<RectTransform>().sizeDelta.x,(table.Rows.Count+1)*70f);
  141. //========建立分隔線=============
  142. for(int i=0;i<=table.Columns.Count-1;i++){
  143. GameObject Separation = Instantiate(LinePrefab, tableParent);
  144. Separation.GetComponent<RectTransform>().sizeDelta = new Vector2(3f,ceneter.GetComponent<RectTransform>().sizeDelta.y-5f);
  145. Separation.GetComponent<RectTransform>().anchoredPosition = new Vector2(width* (i + 1)-5f,0);
  146. Separation.GetComponent<Image>().color = Main.Global.系統主題 == 1 ? new Color(0f, 0f, 0f, 1f) : new Color(1f, 1f, 1f, 1f);
  147. }
  148. }
  149. public static void 清除控件(GameObject content){
  150. List<GameObject> childList = new List<GameObject>();
  151. foreach (Transform child in content.transform) {
  152. childList.Add(child.gameObject);
  153. }
  154. foreach (GameObject child in childList) {
  155. if (child.name.Contains("(Clone)")) {
  156. Destroy(child);
  157. }
  158. }
  159. }
  160. public static void LineChart_表格設定(LineChart chart,Color color,Color color2,bool title_show,bool tooltip_show,bool legend_show){
  161. var title = chart.GetOrAddChartComponent<Title>();
  162. title.text = "";
  163. title.show = title_show;
  164. var tooltip = chart.GetOrAddChartComponent<Tooltip>();
  165. tooltip.show = tooltip_show;
  166. tooltip.numericFormatter ="N0";
  167. Legend legend = chart.GetOrAddChartComponent<Legend>();
  168. legend.show = legend_show;
  169. legend.labelStyle.textStyle.color=color;
  170. var xAxis = chart.GetOrAddChartComponent<XAxis>();
  171. xAxis.axisLine.lineStyle.color = color;
  172. xAxis.axisTick.lineStyle.color = color;
  173. xAxis.axisLabel.textStyle.color = color;
  174. xAxis.show = false;
  175. xAxis.show = true;
  176. var yAxis = chart.GetOrAddChartComponent<YAxis>();
  177. yAxis.axisLine.lineStyle.color = color;
  178. yAxis.axisTick.lineStyle.color = color;
  179. yAxis.axisLabel.textStyle.color = color;
  180. yAxis.show = false;
  181. yAxis.show = true;
  182. //===========設定Chart Serie==============
  183. chart.GetSerie<Line>(0).itemStyle.color=color2;
  184. foreach (var series in chart.series)
  185. {
  186. series.symbol.type = SymbolType.Circle;
  187. }
  188. }
  189. public static void BarChart_表格設定(BarChart chart,Color color,Color color2,bool title_show,bool tooltip_show,bool legend_show){
  190. var title = chart.GetOrAddChartComponent<Title>();
  191. title.text = "";
  192. title.show = title_show;
  193. var tooltip = chart.GetOrAddChartComponent<Tooltip>();
  194. tooltip.show = tooltip_show;
  195. tooltip.numericFormatter ="N0";
  196. Legend legend = chart.GetOrAddChartComponent<Legend>();
  197. legend.show = legend_show;
  198. legend.labelStyle.textStyle.color=color;
  199. var xAxis = chart.GetOrAddChartComponent<XAxis>();
  200. xAxis.axisLine.lineStyle.color = color;
  201. xAxis.axisTick.lineStyle.color = color;
  202. xAxis.axisLabel.textStyle.color = color;
  203. xAxis.show = false;
  204. xAxis.show = true;
  205. var yAxis = chart.GetOrAddChartComponent<YAxis>();
  206. yAxis.axisLine.lineStyle.color = color;
  207. yAxis.axisTick.lineStyle.color = color;
  208. yAxis.axisLabel.textStyle.color = color;
  209. yAxis.show = false;
  210. yAxis.show = true;
  211. var ser = chart.GetSerie<Bar>();
  212. ser.itemStyle.color=color2;
  213. }
  214. }