Brak opisu
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.

物料使用.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. using UnityEngine.Networking;
  7. using UnityEngine.EventSystems;
  8. using System.Text.RegularExpressions;
  9. using System.Data.SqlClient;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. using TMPro;
  13. using System;
  14. using System.IO;
  15. using ZXing;
  16. using ZXing.QrCode;
  17. using System.Net;
  18. using System.Data;
  19. using SimpleJSON;
  20. using System.Linq;
  21. using System.ComponentModel;
  22. using System.Runtime.InteropServices;
  23. using System.Globalization;
  24. using XCharts.Runtime;
  25. public class 物料使用 : MonoBehaviour
  26. {
  27. [SerializeField] GameObject bar;
  28. [SerializeField] RectTransform panelRectTransform1;
  29. [SerializeField] GameObject bar1;
  30. [SerializeField] RectTransform panelRectTransform;
  31. [SerializeField] TextMeshProUGUI startDateText;
  32. [SerializeField] TextMeshProUGUI endDateText;
  33. [SerializeField] GameObject p1;
  34. [SerializeField] GameObject p2;
  35. private bool isP1Active = true;
  36. private JSONNode 計算1;
  37. private JSONNode 計算2;
  38. public GameObject buttonPrefab;
  39. public Transform buttonParent;
  40. public GameObject buttonceneter;
  41. private List<GameObject> copy = new List<GameObject>();
  42. public GameObject buttonPrefab1;
  43. public Transform buttonParent1;
  44. public GameObject buttonceneter1;
  45. private List<GameObject> copy1 = new List<GameObject>();
  46. public CanvasScaler canvasScaler;
  47. [SerializeField] GameObject 讀取面板;
  48. [SerializeField] TextMeshProUGUI 讀取;
  49. [SerializeField] GameObject 彈跳面板;
  50. [SerializeField] TextMeshProUGUI 彈跳文字;
  51. private string loadingText = "";
  52. private int dotCount = 0;
  53. // Start is called before the first frame update
  54. void Start()
  55. {
  56. Screen.autorotateToPortrait = false;
  57. Screen.orientation = ScreenOrientation.Portrait;
  58. float rate = 1000.0f / 1600.0f;
  59. if (canvasScaler == null)
  60. {
  61. canvasScaler = GetComponent<CanvasScaler>();
  62. }
  63. if (Main.Global.rate>rate)
  64. {
  65. canvasScaler.matchWidthOrHeight = 1f;
  66. }
  67. else
  68. {
  69. canvasScaler.matchWidthOrHeight = 0f;
  70. }
  71. StartCoroutine(AnimateText());
  72. //讀取面板.SetActive(true);
  73. //Main.Global.阿帕契路徑="http://106.1.48.106:8080/wp-content/themes/event-star/GF_main.php?";
  74. //Main.Global.預設伺服器路徑="GF01";
  75. DateTime today = DateTime.Today;
  76. DateTime startDate = new DateTime(today.Year, today.Month, 1);
  77. DateTime endDate = startDate.AddMonths(1).AddDays(-1);
  78. startDateText.text = startDate.ToString("yyyy/MM/dd");
  79. endDateText.text = endDate.ToString("yyyy/MM/dd");
  80. p1.SetActive(true);
  81. p2.SetActive(false);
  82. StartCoroutine(物料使用情況());
  83. }
  84. public void 變換()
  85. {
  86. isP1Active = !isP1Active;
  87. p1.SetActive(isP1Active);
  88. p2.SetActive(!isP1Active);
  89. }
  90. IEnumerator 物料使用情況()
  91. {
  92. 讀取面板.SetActive(true);
  93. if (copy1 != null)
  94. {
  95. DeletetextCopy1();
  96. }
  97. if (copy != null)
  98. {
  99. DeletetextCopy();
  100. }
  101. string strcon = "物料使用情況";
  102. string se = Main.Global.預設伺服器路徑;
  103. string pa1 = startDateText.text;
  104. string pa2 = endDateText.text;
  105. string strUrl = string.Format(Main.Global.阿帕契路徑+"comm={0}&se={1}&PA1={2}&PA2={3}", strcon, se,pa1,pa2);
  106. UnityWebRequest request = UnityWebRequest.Get(strUrl);
  107. yield return request.SendWebRequest();
  108. if (request.result == UnityWebRequest.Result.ConnectionError)
  109. {
  110. Debug.Log(request.error);
  111. yield break;
  112. }
  113. JSONNode json = JSON.Parse(request.downloadHandler.text);
  114. Debug.Log(json);
  115. int sum = 2;
  116. int sum1 = 2;
  117. 降冪(json, 1);
  118. Debug.Log(json);
  119. for (int i = 0; i < json.Count; i++)
  120. {
  121. GameObject buttonObj = Instantiate(buttonPrefab1) as GameObject;
  122. buttonObj.GetComponent<RectTransform>().anchoredPosition = new Vector2(2, -sum1);
  123. buttonObj.transform.SetParent(buttonParent1, false);
  124. TextMeshProUGUI[] text = buttonObj.GetComponentsInChildren<TextMeshProUGUI>();
  125. text[0].text =json[i][0].ToString().Trim('"').Replace("\\\\", "\\");
  126. text[1].text =json[i][1].ToString().Trim('"').Replace("\\\\", "\\");
  127. sum1+=75;
  128. copy1.Add(buttonObj);
  129. }
  130. int a = json.Count;
  131. panelRectTransform.anchoredPosition = new Vector2(0, 0);
  132. panelRectTransform1.anchoredPosition = new Vector2(0, 0);
  133. if (a<6)
  134. {
  135. panelRectTransform.sizeDelta= new Vector2(1000, 740);
  136. panelRectTransform1.sizeDelta= new Vector2(1000, 740);
  137. a=1000;
  138. }
  139. else
  140. {
  141. panelRectTransform.sizeDelta= new Vector2(a*167, 740);
  142. panelRectTransform1.sizeDelta= new Vector2(a*167, 740);
  143. a*=167;
  144. }
  145. BarChart barchart = bar1.GetComponent<BarChart>();
  146. var gc = barchart.EnsureChartComponent<GridCoord>();
  147. gc.left =100;
  148. gc.right =20;
  149. gc.top =120;
  150. gc.bottom =80;
  151. barchart.SetSize(a, 740);
  152. var title = barchart.EnsureChartComponent<Title>();
  153. title.text = "";
  154. List<string> xAxisValue = new List<string>();
  155. List<double> yAxisValue = new List<double>();
  156. for (int e = 0; e<json.Count; e++)
  157. {
  158. xAxisValue.Add(json[e][0].ToString().Trim('"').Replace("\\\\", "\\"));
  159. double h1 = (double.Parse(json[e][1]));
  160. yAxisValue.Add(h1);
  161. }
  162. barchart.RemoveData();
  163. barchart.AddSerie<Bar>("使用數量");
  164. foreach (int item in yAxisValue)
  165. {
  166. barchart.AddData(0, item);
  167. }
  168. foreach (var xValue in xAxisValue)
  169. {
  170. barchart.AddXAxisData(xValue);
  171. }
  172. Legend legend = barchart.EnsureChartComponent<Legend>();
  173. legend.show = true;
  174. legend.location =new Location() { align = Location.Align.TopLeft, top = 70, left =100 };
  175. Tooltip tooltip = barchart.EnsureChartComponent<Tooltip>();
  176. tooltip.show = true;
  177. tooltip.fixedY=-100;
  178. tooltip.numericFormatter="0,0";
  179. var yAxis = barchart.GetOrAddChartComponent<YAxis>();
  180. yAxis.axisLabel.numericFormatter="0,0";
  181. 降冪(json, 2);
  182. Debug.Log(json);
  183. for (int i = 0; i < json.Count; i++)
  184. {
  185. GameObject buttonObj = Instantiate(buttonPrefab) as GameObject;
  186. buttonObj.GetComponent<RectTransform>().anchoredPosition = new Vector2(2, -sum);
  187. buttonObj.transform.SetParent(buttonParent, false);
  188. TextMeshProUGUI[] text = buttonObj.GetComponentsInChildren<TextMeshProUGUI>();
  189. text[0].text =json[i][0].ToString().Trim('"').Replace("\\\\", "\\");
  190. text[1].text =json[i][2].ToString().Trim('"').Replace("\\\\", "\\");
  191. sum+=75;
  192. copy.Add(buttonObj);
  193. }
  194. BarChart barchart1 = bar.GetComponent<BarChart>();
  195. var gc1 = barchart1.EnsureChartComponent<GridCoord>();
  196. gc1.left =100;
  197. gc1.right =20;
  198. gc1.top =120;
  199. gc1.bottom =80;
  200. barchart1.SetSize(a, 740);
  201. var title1 = barchart1.EnsureChartComponent<Title>();
  202. title1.text = "";
  203. List<string> xAxisValue1 = new List<string>();
  204. List<int> yAxisValue1 = new List<int>();
  205. for (int e = 0; e<json.Count; e++)
  206. {
  207. xAxisValue1.Add(json[e][0].ToString().Trim('"').Replace("\\\\", "\\"));
  208. double h1 = (double.Parse(json[e][2]));
  209. int hh1 = (int)Math.Round(h1);
  210. yAxisValue1.Add(hh1);
  211. }
  212. barchart1.RemoveData();
  213. barchart1.AddSerie<Bar>("總金額");
  214. foreach (int item in yAxisValue1)
  215. {
  216. barchart1.AddData(0, item);
  217. }
  218. foreach (var xValue in xAxisValue1)
  219. {
  220. barchart1.AddXAxisData(xValue);
  221. }
  222. Legend legend1 = barchart1.EnsureChartComponent<Legend>();
  223. legend1.show = true;
  224. legend1.location =new Location() { align = Location.Align.TopLeft, top = 70, left =100 };
  225. Tooltip tooltip1 = barchart1.EnsureChartComponent<Tooltip>();
  226. tooltip1.show = true;
  227. tooltip1.fixedY=-100;
  228. tooltip1.numericFormatter="0,0";
  229. var yAxis1 = barchart1.GetOrAddChartComponent<YAxis>();
  230. yAxis1.axisLabel.numericFormatter="0,0";
  231. buttonceneter.GetComponent<RectTransform>().sizeDelta = new Vector2(buttonceneter.GetComponent<RectTransform>().sizeDelta.x, sum);
  232. buttonceneter1.GetComponent<RectTransform>().sizeDelta = new Vector2(buttonceneter1.GetComponent<RectTransform>().sizeDelta.x, sum1);
  233. 讀取面板.SetActive(false);
  234. }
  235. private void 降冪(JSONNode json, int a)
  236. {
  237. List<List<string>> tlist = new List<List<string>>();
  238. for (int i = 0; i<json.Count; i++)
  239. {
  240. List<string> list = new List<string>();
  241. for (int j = 0; j < json[i].Count; j++)
  242. {
  243. list.Add(json[i][j].ToString().Trim('"').Replace("\\\\", "\\"));
  244. }
  245. tlist.Add(list);
  246. }
  247. tlist = tlist.OrderByDescending(row => double.Parse(row[a])).ToList();
  248. for (int i = 0; i<json.Count; i++)
  249. {
  250. for (int j = 0; j < json[i].Count; j++)
  251. {
  252. json[i][j] = tlist[i][j];
  253. }
  254. }
  255. }
  256. public void DeletetextCopy()
  257. {
  258. foreach (GameObject i in copy)
  259. {
  260. Destroy(i);
  261. }
  262. copy.Clear();
  263. }
  264. public void DeletetextCopy1()
  265. {
  266. foreach (GameObject i in copy1)
  267. {
  268. Destroy(i);
  269. }
  270. copy1.Clear();
  271. }
  272. IEnumerator AnimateText()
  273. {
  274. string qqq = 讀取.text.ToString();
  275. while (true)
  276. {
  277. yield return new WaitForSeconds(0.5f);
  278. if (dotCount < 3)
  279. {
  280. loadingText += ".";
  281. dotCount++;
  282. }
  283. else
  284. {
  285. loadingText = "";
  286. dotCount = 0;
  287. }
  288. 讀取.text = qqq+loadingText;
  289. }
  290. }
  291. public void 本月()
  292. {
  293. DateTime today = DateTime.Today;
  294. DateTime startDate = new DateTime(today.Year, today.Month, 1);
  295. DateTime endDate = startDate.AddMonths(1).AddDays(-1);
  296. startDateText.text = startDate.ToString("yyyy/MM/dd");
  297. endDateText.text = endDate.ToString("yyyy/MM/dd");
  298. StartCoroutine(物料使用情況());
  299. }
  300. public void 本年()
  301. {
  302. DateTime today = DateTime.Today;
  303. DateTime startDate = new DateTime(today.Year, 1, 1);
  304. DateTime endDate = new DateTime(today.Year, 12, 31);
  305. startDateText.text = startDate.ToString("yyyy/MM/dd");
  306. endDateText.text = endDate.ToString("yyyy/MM/dd");
  307. StartCoroutine(物料使用情況());
  308. }
  309. public void 上一月()
  310. {
  311. DateTime startDate = DateTime.Parse(startDateText.text);
  312. DateTime endDate = DateTime.Parse(endDateText.text);
  313. startDate = startDate.AddMonths(-1);
  314. endDate = startDate.AddMonths(1).AddDays(-1);
  315. startDateText.text = startDate.ToString("yyyy/MM/dd");
  316. endDateText.text = endDate.ToString("yyyy/MM/dd");
  317. StartCoroutine(物料使用情況());
  318. }
  319. public void 上一年()
  320. {
  321. DateTime startDate = DateTime.Parse(startDateText.text);
  322. DateTime endDate = DateTime.Parse(endDateText.text);
  323. startDate = startDate.AddYears(-1);
  324. endDate = new DateTime(startDate.Year, 12, 31);
  325. startDateText.text = startDate.ToString("yyyy/MM/dd");
  326. endDateText.text = endDate.ToString("yyyy/MM/dd");
  327. StartCoroutine(物料使用情況());
  328. }
  329. public void exit()
  330. {
  331. change_Secen(1);
  332. }
  333. public void change_Secen(int _screenNum)
  334. {
  335. //Main.Global.判斷是否第一次=true;
  336. SceneManager.LoadScene(_screenNum);
  337. }
  338. }