Açıklama Yok
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.

BaseGraph.API.cs 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. namespace XCharts.Runtime
  6. {
  7. /// <summary>
  8. /// The base class of all graphs or components.
  9. /// |所有图形的基类。
  10. /// </summary>
  11. public partial class BaseGraph
  12. {
  13. /// <summary>
  14. /// The x of graph.
  15. /// |图形的X
  16. /// </summary>
  17. public float graphX { get { return m_GraphX; } }
  18. /// <summary>
  19. /// The y of graph.
  20. /// |图形的Y
  21. /// </summary>
  22. public float graphY { get { return m_GraphY; } }
  23. /// <summary>
  24. /// The width of graph.
  25. /// |图形的宽
  26. /// </summary>
  27. public float graphWidth { get { return m_GraphWidth; } }
  28. /// <summary>
  29. /// The height of graph.
  30. /// |图形的高
  31. /// </summary>
  32. public float graphHeight { get { return m_GraphHeight; } }
  33. /// <summary>
  34. /// The position of graph.
  35. /// |图形的左下角起始坐标。
  36. /// </summary>
  37. public Vector3 graphPosition { get { return m_GraphPosition; } }
  38. public Rect graphRect { get { return m_GraphRect; } }
  39. public Vector2 graphSizeDelta { get { return m_GraphSizeDelta; } }
  40. public Vector2 graphPivot { get { return m_GraphPivot; } }
  41. public Vector2 graphMinAnchor { get { return m_GraphMinAnchor; } }
  42. public Vector2 graphMaxAnchor { get { return m_GraphMaxAnchor; } }
  43. public Vector2 graphAnchoredPosition { get { return m_GraphAnchoredPosition; } }
  44. /// <summary>
  45. /// The postion of pointer.
  46. /// |鼠标位置。
  47. /// </summary>
  48. public Vector2 pointerPos { get; protected set; }
  49. /// <summary>
  50. /// Whether the mouse pointer is in the chart.
  51. /// |鼠标是否在图表内。
  52. /// </summary>
  53. public bool isPointerInChart
  54. { get { return m_PointerEventData != null; } }
  55. /// <summary>
  56. /// 警告信息。
  57. /// </summary>
  58. public string warningInfo { get; protected set; }
  59. /// <summary>
  60. /// 强制开启鼠标事件检测。
  61. /// </summary>
  62. public bool forceOpenRaycastTarget { get { return m_ForceOpenRaycastTarget; } set { m_ForceOpenRaycastTarget = value; } }
  63. /// <summary>
  64. /// 鼠标点击回调。
  65. /// </summary>
  66. public Action<PointerEventData, BaseGraph> onPointerClick { set { m_OnPointerClick = value; m_ForceOpenRaycastTarget = true; } }
  67. /// <summary>
  68. /// 鼠标按下回调。
  69. /// </summary>
  70. public Action<PointerEventData, BaseGraph> onPointerDown { set { m_OnPointerDown = value; m_ForceOpenRaycastTarget = true; } }
  71. /// <summary>
  72. /// 鼠标弹起回调。
  73. /// </summary>
  74. public Action<PointerEventData, BaseGraph> onPointerUp { set { m_OnPointerUp = value; m_ForceOpenRaycastTarget = true; } }
  75. /// <summary>
  76. /// 鼠标进入回调。
  77. /// </summary>
  78. public Action<PointerEventData, BaseGraph> onPointerEnter { set { m_OnPointerEnter = value; m_ForceOpenRaycastTarget = true; } }
  79. /// <summary>
  80. /// 鼠标退出回调。
  81. /// </summary>
  82. public Action<PointerEventData, BaseGraph> onPointerExit { set { m_OnPointerExit = value; m_ForceOpenRaycastTarget = true; } }
  83. /// <summary>
  84. /// 鼠标开始拖拽回调。
  85. /// </summary>
  86. public Action<PointerEventData, BaseGraph> onBeginDrag { set { m_OnBeginDrag = value; m_ForceOpenRaycastTarget = true; } }
  87. /// <summary>
  88. /// 鼠标拖拽回调。
  89. /// </summary>
  90. public Action<PointerEventData, BaseGraph> onDrag { set { m_OnDrag = value; m_ForceOpenRaycastTarget = true; } }
  91. /// <summary>
  92. /// 鼠标结束拖拽回调。
  93. /// </summary>
  94. public Action<PointerEventData, BaseGraph> onEndDrag { set { m_OnEndDrag = value; m_ForceOpenRaycastTarget = true; } }
  95. /// <summary>
  96. /// 鼠标滚动回调。
  97. /// </summary>
  98. public Action<PointerEventData, BaseGraph> onScroll { set { m_OnScroll = value; m_ForceOpenRaycastTarget = true; } }
  99. /// <summary>
  100. /// 设置图形的宽高(在非stretch pivot下才有效,其他情况需要自己调整RectTransform)
  101. /// </summary>
  102. /// <param name="width"></param>
  103. /// <param name="height"></param>
  104. public virtual void SetSize(float width, float height)
  105. {
  106. if (LayerHelper.IsFixedWidthHeight(rectTransform))
  107. {
  108. rectTransform.sizeDelta = new Vector2(width, height);
  109. }
  110. else
  111. {
  112. Debug.LogError("Can't set size on stretch pivot,you need to modify rectTransform by yourself.");
  113. }
  114. }
  115. /// <summary>
  116. /// 重新初始化Painter
  117. /// </summary>
  118. public void SetPainterDirty()
  119. {
  120. m_PainerDirty = true;
  121. }
  122. /// <summary>
  123. /// Redraw graph in next frame.
  124. /// |在下一帧刷新图形。
  125. /// </summary>
  126. public virtual void RefreshGraph()
  127. {
  128. m_RefreshChart = true;
  129. }
  130. public void RefreshAllComponent()
  131. {
  132. SetAllComponentDirty();
  133. RefreshGraph();
  134. }
  135. /// <summary>
  136. /// 检测警告信息。
  137. /// </summary>
  138. /// <returns></returns>
  139. public string CheckWarning()
  140. {
  141. warningInfo = CheckHelper.CheckChart(this);
  142. return warningInfo;
  143. }
  144. /// <summary>
  145. /// 移除并重新创建所有图表的Object。
  146. /// </summary>
  147. public void RebuildChartObject()
  148. {
  149. ChartHelper.DestroyAllChildren(transform);
  150. SetAllComponentDirty();
  151. }
  152. public bool ScreenPointToChartPoint(Vector2 screenPoint, out Vector2 chartPoint)
  153. {
  154. #if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX
  155. var relative = Display.RelativeMouseAt(screenPoint);
  156. if (relative != Vector3.zero)
  157. screenPoint = relative;
  158. #endif
  159. var cam = canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : canvas.worldCamera;
  160. if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform,
  161. screenPoint, cam, out chartPoint))
  162. {
  163. return false;
  164. }
  165. return true;
  166. }
  167. /// <summary>
  168. /// 保存图表为图片。
  169. /// </summary>
  170. /// <param name="imageType">type of image: png, jpg, exr</param>
  171. /// <param name="savePath">save path</param>
  172. public void SaveAsImage(string imageType = "png", string savePath = "")
  173. {
  174. StartCoroutine(SaveAsImageSync(imageType, savePath));
  175. }
  176. private IEnumerator SaveAsImageSync(string imageType, string path)
  177. {
  178. yield return new WaitForEndOfFrame();
  179. ChartHelper.SaveAsImage(rectTransform, canvas, imageType, path);
  180. }
  181. }
  182. }