DataTypeManager.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using LitJson;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. using XCharts.Runtime;
  9. using static Manager;
  10. public class DataTypeManager : MonoBehaviour
  11. {
  12. public Text runttext;
  13. public RingChart pc2;
  14. JsonData jsonData;
  15. public Text TimeText;
  16. public List<GameObject> typelist;
  17. List<GameObject> objlist=new List<GameObject>();
  18. float total =0;
  19. float runct =0;
  20. public void init(JsonData data)
  21. {
  22. jsonData=data;
  23. int max =this.transform.childCount;
  24. for (int i = 0;i<objlist.Count;i++)
  25. {
  26. Destroy( objlist[i]);
  27. }
  28. total =0;
  29. runct=0;
  30. Debug.Log("init===>"+data["value"].Count);
  31. for (int i = 0;i<data["value"].Count;i++)
  32. {
  33. total+=float.Parse(data["value"][i]["count"].ToString());
  34. if(int.Parse(data["value"][i]["status"].ToString())==1)
  35. {
  36. runct+=float.Parse(data["value"][i]["count"].ToString());
  37. }
  38. }
  39. for (int i = 0;i<data["value"].Count;i++)
  40. {
  41. GameObject go =new GameObject("item_"+i);
  42. objlist.Add(go);
  43. go.transform.parent=this.transform;
  44. go.transform.localPosition = Vector3.zero;
  45. go.transform.localScale = Vector3.one;
  46. RawImage rw = go.AddComponent<RawImage>();
  47. FKItem rfkw = go.AddComponent<FKItem>();
  48. rfkw.init(data["value"][i]);
  49. rw.color = GetColor(int.Parse(data["value"][i]["status"].ToString()));
  50. go.GetComponent<RectTransform>().sizeDelta = new Vector2(float.Parse(data["value"][i]["count"].ToString())/total*100f, go.GetComponent<RectTransform>().sizeDelta.y);
  51. go.GetComponent<RectTransform>().pivot = new Vector2(0,0.5f);
  52. if(TimeText!=null)
  53. {
  54. // 创建一个新的事件触发器
  55. EventTrigger trigger = go.AddComponent<EventTrigger>();
  56. // 创建一个新的事件
  57. EventTrigger.Entry entry = new EventTrigger.Entry();
  58. entry.eventID = EventTriggerType.PointerEnter;
  59. // 设置回调函数
  60. entry.callback.AddListener((data) => { OnButtonEnter((PointerEventData)data); });
  61. // 添加事件到事件触发器
  62. trigger.triggers.Add(entry);
  63. // 创建一个新的事件
  64. EventTrigger.Entry entry2 = new EventTrigger.Entry();
  65. entry2.eventID = EventTriggerType.PointerExit;
  66. // 设置回调函数
  67. entry2.callback.AddListener((data) => { OnButtonExit((PointerEventData)data); });
  68. // 添加事件到事件触发器
  69. trigger.triggers.Add(entry2);
  70. }
  71. }
  72. if(pc2!=null)
  73. {
  74. if(total==0)
  75. {
  76. total=1;
  77. }
  78. int guzhanglv =(int)(100-runct/total*100);
  79. int runlv =(int)100-guzhanglv;
  80. SerieData data1 = pc2.series[0].GetSerieData(0);
  81. data1.data[0] = float.Parse(jsonData["ratio"]["value"].ToString())*100 ;
  82. data1.data[1] = 100;
  83. Debug.Log("运行率:"+runlv+"%");
  84. if(runttext!=null)
  85. {
  86. runttext.text = "累计运行时长:"+(int)(1440*float.Parse(jsonData["ratio"]["value"].ToString()))+"分钟";
  87. }
  88. pc2.RefreshChart();
  89. }
  90. }
  91. private void OnButtonEnter(PointerEventData data)
  92. {
  93. float bz = data.pointerEnter.GetComponent<RectTransform>().anchoredPosition.x/100f * 23f;
  94. int fz = (int)bz;
  95. int miao = (int)((bz - fz)*60f);
  96. string miaostr =miao.ToString();
  97. if(miao<10)
  98. {
  99. miaostr="0"+miao;
  100. }
  101. string fzstr =fz.ToString();
  102. if(fz<10)
  103. {
  104. fzstr="0"+fz;
  105. }
  106. string timestr = fzstr+":"+miaostr;
  107. Debug.Log("time====>"+timestr );
  108. for (int i = 0;i<typelist.Count;i++)
  109. {
  110. typelist[i].SetActive(false);
  111. }
  112. TimeText.text = "时间:"+timestr;
  113. typelist[int.Parse(data.pointerEnter.GetComponent<FKItem>().data["status"].ToString())-1].SetActive(true);
  114. }
  115. private void OnButtonExit(PointerEventData data)
  116. {
  117. for (int i = 0;i<typelist.Count;i++)
  118. {
  119. typelist[i].SetActive(false);
  120. }
  121. TimeText.text = "";
  122. }
  123. public static Color GetColor(int colorid)
  124. {
  125. switch(colorid)
  126. {
  127. case 1:
  128. return Color.green;
  129. break;
  130. case 2:
  131. return Color.yellow;
  132. break;
  133. case 3:
  134. return Color.red;
  135. break;
  136. case 4:
  137. return Color.gray;
  138. break;
  139. }
  140. return Color.gray;
  141. }
  142. }