123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- using System;
- using System.Data;
- using System.Collections.Generic;
- using TMPro;
- using XCharts.Runtime;
- using UnityEngine;
- using UnityEngine.UI;
-
- public class Moudle : MonoBehaviour
- {
- //==========Moudle===========================
- public static void 載入日期(int num,TMP_Dropdown 開始年_cb,TMP_Dropdown 開始月_cb,TMP_Dropdown 開始日_cb){
- Main.Global.目前開始月份[num] = 開始月_cb.options[開始月_cb.value].text;
- Main.Global.目前開始年度[num] = 開始年_cb.options[開始年_cb.value].text;
- if(Main.Global.上次開始月份[num] != Main.Global.目前開始月份[num] || Main.Global.上次開始年度[num] != Main.Global.目前開始年度[num]){
- Main.Global.上次開始月份[num] = Main.Global.目前開始月份[num]; Main.Global.上次開始年度[num] = Main.Global.目前開始年度[num];
- List<string> 日期Options = new(){ "" };
- int N_year,N_month;
- N_year = int.Parse(開始年_cb.options[開始年_cb.value].text);
- if(開始月_cb.value==0){
- N_month = 1;
- } else{
- N_month = 開始月_cb.value;
- }
- for(int i=1;i<= DateTime.DaysInMonth(N_year,N_month);i++){
- DateTime day2 = new (N_year ,N_month ,i);
- 日期Options.Add(i.ToString()+","+day2.DayOfWeek);
- }
- 開始日_cb.ClearOptions();
- 開始日_cb.AddOptions(日期Options);
- 開始日_cb.value=0;
- }
- }
- //===========================================
-
- public static void 載入表格(string table_name,DataTable table,Transform tableParent,GameObject rowPrefab,GameObject ceneter,TMP_FontAsset myFont,Sprite UIsprite,GameObject LinePrefab,bool overflowMode){
- 清除控件(ceneter);
- Image centerImage = ceneter.GetComponent<Image>();
- if (centerImage == null)
- {
- centerImage = ceneter.AddComponent<Image>();
- centerImage.type = Image.Type.Sliced;
- }
- centerImage.sprite = UIsprite;
- centerImage.fillCenter = false;
- centerImage.color = Main.Global.系統主題 == 1 ? new Color(0f, 0f, 0f, 1f) : new Color(1f, 1f, 1f, 1f);
- //==========動態生成表格==================================
- GameObject headerRow = Instantiate(rowPrefab, tableParent);
- // 設定表頭行的位置
- headerRow.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
- headerRow.AddComponent<Image>().type=Image.Type.Sliced;
- headerRow.GetComponent<Image>().sprite=UIsprite;
- headerRow.GetComponent<Image>().fillCenter=false;
- headerRow.GetComponent<Image>().color = Main.Global.系統主題 == 1 ? new Color(0f, 0f, 0f, 1f) : new Color(1f, 1f, 1f, 1f);
- float width = headerRow.GetComponent<RectTransform>().rect.width / table.Columns.Count ;
- //headerRow.AddComponent<Image>().color=new Color(0f,0.67f,0.87f,1f);
- // 取得所有欄位名稱,並建立表頭
- for (int i = 0; i < table.Columns.Count; i++)
- {
- GameObject headerText = new GameObject(table_name+"_Header_"+i.ToString());
- headerText.transform.SetParent(headerRow.transform, false);
- headerText.AddComponent<TextMeshProUGUI>().text = table.Columns[i].ColumnName;
- headerText.GetComponent<TextMeshProUGUI>().tag = "Header";
- }
- GridLayoutGroup gridLayout = headerRow.GetComponent<GridLayoutGroup>();
-
- gridLayout.cellSize =new Vector2(width, 70);
- for (int i = 0; i < table.Rows.Count; i++){
- // 建立資料列
- GameObject row = Instantiate(rowPrefab, tableParent);
- // 設定資料列位置
- row.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, -70 * (i + 1));
- // 建立資料列中的所有欄位
- for (int j = 0; j < table.Columns.Count; j++)
- {
- // 取得欄位值
- string value = table.Rows[i][j].ToString();
- if(string.Equals(value, "true", StringComparison.OrdinalIgnoreCase) || string.Equals(value, "false", StringComparison.OrdinalIgnoreCase)){
- Toggle cellText = new GameObject(table_name+"_Cells_"+i.ToString()+"_"+j.ToString()).AddComponent<Toggle>();
- cellText.transform.SetParent(row.transform, false);
- cellText.tag = "Toggle";
- Image Background_img = new GameObject("bg"+i.ToString()).AddComponent<Image>();
- Background_img.transform.SetParent(cellText.transform, false);
- Background_img.sprite = Main.Global.sprite_box;
- Background_img.rectTransform.sizeDelta = new Vector2(100f,70f);
- Image Check_img = new GameObject("Check"+i.ToString()).AddComponent<Image>();
- Check_img.transform.SetParent(cellText.transform, false);
- Check_img.rectTransform.sizeDelta = new Vector2(100f,70f);
- Check_img.sprite = Main.Global.sprite_check;
- cellText.graphic = Check_img;
- cellText.targetGraphic=Background_img;
- if(string.Equals(value, "true", StringComparison.OrdinalIgnoreCase)){
- cellText.isOn=true;
- }
- }else{
- GameObject cellText = new GameObject(table_name+"_Cells_"+i.ToString()+"_"+j.ToString());
- cellText.transform.SetParent(row.transform, false);
- double number;
- if (double.TryParse(value, out number) && table_name=="區間"){
- string formattedValue = String.Format("{0:###,##0.##}", number);
- cellText.AddComponent<TextMeshProUGUI>().text = formattedValue;
-
- }else{
- cellText.AddComponent<TextMeshProUGUI>().text = value;
- }
- //cellText.AddComponent<TextMeshProUGUI>().text = value;
- cellText.GetComponent<TextMeshProUGUI>().tag="Cells";
- if(overflowMode){cellText.GetComponent<TextMeshProUGUI>().overflowMode=TextOverflowModes.Ellipsis;}
- cellText.AddComponent<Button>();
- cellText.GetComponent<Button>().onClick.AddListener(() => { Main.按鈕事件2(cellText); });
- }
- // 建立欄位文字
- }
- gridLayout = row.GetComponent<GridLayoutGroup>();
- gridLayout.cellSize =new Vector2(width, 70);
- }
- TextMeshProUGUI[] texts = ceneter.GetComponentsInChildren<TextMeshProUGUI>();
- foreach (TextMeshProUGUI text in texts){
- if(text.tag.Contains("Header")){
- text.font = myFont;
- text.color = Main.Global.系統主題 == 1 ? new Color(0, 0, 0, 1) : text.color;
- text.alignment = TextAlignmentOptions.Center;
- text.fontStyle = FontStyles.Bold;
- text.fontSize=22;
- if(!text.name.Contains("年度")){
- text.enableAutoSizing = true;
- text.autoSizeTextContainer = true;
- }
- text.margin = new Vector4(10, 0, 10, 0);
- }else if(text.tag.Contains("Cells")){
- text.font = myFont;
- text.color = Main.Global.系統主題 == 1 ? new Color(0, 0, 0, 1) : text.color;
- text.alignment = TextAlignmentOptions.Center;
- text.fontStyle = FontStyles.Bold;
- text.fontSize=18;
- if(!text.name.Contains("年度")){
- text.enableAutoSizing = true;
- text.autoSizeTextContainer = true;
- }
- text.margin = new Vector4(10, 0, 10, 0);
- }else{
-
- }
- }
- ceneter.GetComponent<RectTransform>().sizeDelta = new Vector2(ceneter.GetComponent<RectTransform>().sizeDelta.x,(table.Rows.Count+1)*70f);
- //========建立分隔線=============
- for(int i=0;i<=table.Columns.Count-1;i++){
- GameObject Separation = Instantiate(LinePrefab, tableParent);
- Separation.GetComponent<RectTransform>().sizeDelta = new Vector2(3f,ceneter.GetComponent<RectTransform>().sizeDelta.y-5f);
- Separation.GetComponent<RectTransform>().anchoredPosition = new Vector2(width* (i + 1)-5f,0);
- Separation.GetComponent<Image>().color = Main.Global.系統主題 == 1 ? new Color(0f, 0f, 0f, 1f) : new Color(1f, 1f, 1f, 1f);
- }
- }
-
- public static void 清除控件(GameObject content){
- List<GameObject> childList = new List<GameObject>();
- foreach (Transform child in content.transform) {
- childList.Add(child.gameObject);
- }
- foreach (GameObject child in childList) {
- if (child.name.Contains("(Clone)")) {
- Destroy(child);
- }
- }
- }
- public static void LineChart_表格設定(LineChart chart,Color color,Color color2,bool title_show,bool tooltip_show,bool legend_show){
- var title = chart.GetOrAddChartComponent<Title>();
- title.text = "";
- title.show = title_show;
- var tooltip = chart.GetOrAddChartComponent<Tooltip>();
- tooltip.show = tooltip_show;
- tooltip.numericFormatter ="N0";
- Legend legend = chart.GetOrAddChartComponent<Legend>();
- legend.show = legend_show;
- legend.labelStyle.textStyle.color=color;
- var xAxis = chart.GetOrAddChartComponent<XAxis>();
- xAxis.axisLine.lineStyle.color = color;
- xAxis.axisTick.lineStyle.color = color;
- xAxis.axisLabel.textStyle.color = color;
- xAxis.show = false;
- xAxis.show = true;
- var yAxis = chart.GetOrAddChartComponent<YAxis>();
- yAxis.axisLine.lineStyle.color = color;
- yAxis.axisTick.lineStyle.color = color;
- yAxis.axisLabel.textStyle.color = color;
- yAxis.show = false;
- yAxis.show = true;
- //===========設定Chart Serie==============
- chart.GetSerie<Line>(0).itemStyle.color=color2;
- foreach (var series in chart.series)
- {
- series.symbol.type = SymbolType.Circle;
- }
- }
- public static void BarChart_表格設定(BarChart chart,Color color,Color color2,bool title_show,bool tooltip_show,bool legend_show){
- var title = chart.GetOrAddChartComponent<Title>();
- title.text = "";
- title.show = title_show;
- var tooltip = chart.GetOrAddChartComponent<Tooltip>();
- tooltip.show = tooltip_show;
- tooltip.numericFormatter ="N0";
- Legend legend = chart.GetOrAddChartComponent<Legend>();
- legend.show = legend_show;
- legend.labelStyle.textStyle.color=color;
- var xAxis = chart.GetOrAddChartComponent<XAxis>();
- xAxis.axisLine.lineStyle.color = color;
- xAxis.axisTick.lineStyle.color = color;
- xAxis.axisLabel.textStyle.color = color;
- xAxis.show = false;
- xAxis.show = true;
- var yAxis = chart.GetOrAddChartComponent<YAxis>();
- yAxis.axisLine.lineStyle.color = color;
- yAxis.axisTick.lineStyle.color = color;
- yAxis.axisLabel.textStyle.color = color;
- yAxis.show = false;
- yAxis.show = true;
- var ser = chart.GetSerie<Bar>();
- ser.itemStyle.color=color2;
- }
- }
|