Tooltip.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace XCharts.Runtime
  5. {
  6. /// <summary>
  7. /// Tooltip component.
  8. /// ||提示框组件。
  9. /// </summary>
  10. [System.Serializable]
  11. [ComponentHandler(typeof(TooltipHandler), true)]
  12. public class Tooltip : MainComponent
  13. {
  14. /// <summary>
  15. /// Indicator type.
  16. /// ||指示器类型。
  17. /// </summary>
  18. public enum Type
  19. {
  20. /// <summary>
  21. /// line indicator.
  22. /// ||直线指示器
  23. /// </summary>
  24. Line,
  25. /// <summary>
  26. /// shadow crosshair indicator.
  27. /// ||阴影指示器
  28. /// </summary>
  29. Shadow,
  30. /// <summary>
  31. /// no indicator displayed.
  32. /// ||无指示器
  33. /// </summary>
  34. None,
  35. /// <summary>
  36. /// crosshair indicator, which is actually the shortcut of enable two axisPointers of two orthometric axes.
  37. /// ||十字准星指示器。坐标轴显示Label和交叉线。
  38. /// </summary>
  39. Corss,
  40. /// <summary>
  41. /// Auto select indicator according to serie type.
  42. /// ||根据serie的类型自动选择显示指示器。
  43. /// </summary>
  44. Auto
  45. }
  46. /// <summary>
  47. /// Trigger strategy.
  48. /// ||触发类型。
  49. /// </summary>
  50. public enum Trigger
  51. {
  52. /// <summary>
  53. /// Triggered by data item, which is mainly used for charts that don't have a category axis like scatter charts or pie charts.
  54. /// ||数据项图形触发,主要在散点图,饼图等无类目轴的图表中使用。
  55. /// </summary>
  56. Item,
  57. /// <summary>
  58. /// Triggered by axes, which is mainly used for charts that have category axes, like bar charts or line charts.
  59. /// ||坐标轴触发,主要在柱状图,折线图等会使用类目轴的图表中使用。
  60. /// </summary>
  61. Axis,
  62. /// <summary>
  63. /// Trigger nothing.
  64. /// ||什么都不触发。
  65. /// </summary>
  66. None,
  67. /// <summary>
  68. /// Auto select trigger according to serie type.
  69. /// ||根据serie的类型自动选择触发类型。
  70. /// </summary>
  71. Auto
  72. }
  73. /// <summary>
  74. /// Position type.
  75. /// ||坐标类型。
  76. /// </summary>
  77. public enum Position
  78. {
  79. /// <summary>
  80. /// Auto. The mobile platform is displayed at the top, and the non-mobile platform follows the mouse position.
  81. /// ||自适应。移动平台靠顶部显示,非移动平台跟随鼠标位置。
  82. /// </summary>
  83. Auto,
  84. /// <summary>
  85. /// Custom. Fully customize display position (x,y).
  86. /// ||自定义。完全自定义显示位置(x,y)。
  87. /// </summary>
  88. Custom,
  89. /// <summary>
  90. /// Just fix the coordinate X. Y follows the mouse position.
  91. /// ||只固定坐标X。Y跟随鼠标位置。
  92. /// </summary>
  93. FixedX,
  94. /// <summary>
  95. /// Just fix the coordinate Y. X follows the mouse position.
  96. /// ||只固定坐标Y。X跟随鼠标位置。
  97. FixedY
  98. }
  99. [SerializeField] private bool m_Show = true;
  100. [SerializeField] private Type m_Type = Type.Auto;
  101. [SerializeField] private Trigger m_Trigger = Trigger.Auto;
  102. [SerializeField][Since("v3.3.0")] private Position m_Position = Position.Auto;
  103. [SerializeField] private string m_ItemFormatter;
  104. [SerializeField] private string m_TitleFormatter;
  105. [SerializeField] private string m_Marker = "●";
  106. [SerializeField] private float m_FixedWidth = 0;
  107. [SerializeField] private float m_FixedHeight = 0;
  108. [SerializeField] private float m_MinWidth = 0;
  109. [SerializeField] private float m_MinHeight = 0;
  110. [SerializeField] private string m_NumericFormatter = "";
  111. [SerializeField] private int m_PaddingLeftRight = 10;
  112. [SerializeField] private int m_PaddingTopBottom = 10;
  113. [SerializeField] private bool m_IgnoreDataShow = false;
  114. [SerializeField] private string m_IgnoreDataDefaultContent = "-";
  115. [SerializeField] private bool m_ShowContent = true;
  116. [SerializeField] private bool m_AlwayShowContent = false;
  117. [SerializeField] private Vector2 m_Offset = new Vector2(18f, -25f);
  118. [SerializeField] private Sprite m_BackgroundImage;
  119. [SerializeField] private Image.Type m_BackgroundType = Image.Type.Simple;
  120. [SerializeField] private Color m_BackgroundColor;
  121. [SerializeField] private float m_BorderWidth = 2f;
  122. [SerializeField] private float m_FixedX = 0f;
  123. [SerializeField] private float m_FixedY = 0.7f;
  124. [SerializeField] private float m_TitleHeight = 25f;
  125. [SerializeField] private float m_ItemHeight = 25f;
  126. [SerializeField] private Color32 m_BorderColor = new Color32(230, 230, 230, 255);
  127. [SerializeField] private LineStyle m_LineStyle = new LineStyle(LineStyle.Type.None);
  128. [SerializeField]
  129. private LabelStyle m_TitleLabelStyle = new LabelStyle()
  130. {
  131. textStyle = new TextStyle() { alignment = TextAnchor.MiddleLeft }
  132. };
  133. [SerializeField]
  134. private List<LabelStyle> m_ContentLabelStyles = new List<LabelStyle>()
  135. {
  136. new LabelStyle() { textPadding = new TextPadding(0, 5, 0, 0), textStyle = new TextStyle() { alignment = TextAnchor.MiddleLeft } },
  137. new LabelStyle() { textPadding = new TextPadding(0, 20, 0, 0), textStyle = new TextStyle() { alignment = TextAnchor.MiddleLeft } },
  138. new LabelStyle() { textPadding = new TextPadding(0, 0, 0, 0), textStyle = new TextStyle() { alignment = TextAnchor.MiddleRight } }
  139. };
  140. public TooltipContext context = new TooltipContext();
  141. public TooltipView view;
  142. /// <summary>
  143. /// Whether to show the tooltip component.
  144. /// ||是否显示提示框组件。
  145. /// </summary>
  146. public bool show
  147. {
  148. get { return m_Show; }
  149. set { if (PropertyUtil.SetStruct(ref m_Show, value)) { SetAllDirty(); SetActive(value); } }
  150. }
  151. /// <summary>
  152. /// Indicator type.
  153. /// ||提示框指示器类型。
  154. /// </summary>
  155. public Type type
  156. {
  157. get { return m_Type; }
  158. set { if (PropertyUtil.SetStruct(ref m_Type, value)) SetAllDirty(); }
  159. }
  160. /// <summary>
  161. /// Type of triggering.
  162. /// ||触发类型。
  163. /// </summary>
  164. public Trigger trigger
  165. {
  166. get { return m_Trigger; }
  167. set { if (PropertyUtil.SetStruct(ref m_Trigger, value)) SetAllDirty(); }
  168. }
  169. /// <summary>
  170. /// Type of position.
  171. /// ||显示位置类型。
  172. /// </summary>
  173. public Position position
  174. {
  175. get { return m_Position; }
  176. set { if (PropertyUtil.SetStruct(ref m_Position, value)) SetAllDirty(); }
  177. }
  178. /// <summary>
  179. /// String template formatter for tooltip title content. \n line wrapping is supported. The placeholder {i} can be set separately to indicate that title is ignored and not displayed.
  180. /// Template variables are {.}, {a}, {b}, {c}, {d}, {e}, {f}, and {g}. <br />
  181. /// {.} is the dot of the corresponding color of serie currently indicated or index 0. <br />
  182. /// {a} is the series name name of serie currently indicated or index 0. <br />
  183. /// {b} is the name of the serie data item serieData currently indicated or index 0, or the category value (such as the X-axis of a line chart). <br />
  184. /// {c} is the value of the serie y-dimension (dimesion is 1) currently indicated or index is 0. <br />
  185. /// {d} is the serie y-dimensional (dimesion 1) percentage value of the currently indicated or index 0, note without the % sign. <br />
  186. /// {e} is the name of the serie data item serieData currently indicated or whose index is 0. <br />
  187. /// {h} is the hexadecimal color value of serieData for the serie data item currently indicated or index 0. <br />
  188. /// {f} is the sum of data. <br />
  189. /// {g} indicates the total number of data. <br />
  190. /// {y} is category value of y axis. <br />
  191. /// {.1} represents a dot of the corresponding color with serie specified as index 1. <br />
  192. /// The 1 in {a1}, {b1}, {c1} represents serie where index is specified as 1. <br />
  193. /// {c1:2} represents the third data of the current indicator data item in serie with index 1 (one data item has multiple data, index 2 represents the third data). <br />
  194. /// {c1:2-2} represents the third data of serie third data item with index 1 (that is, the number of data items must be specified when specifying the number of data items). <br />
  195. /// {d1:2:f2} indicates that a format string with a single value is f2 (numericFormatter is used if no value is specified). <br />
  196. /// {d:0.##} indicates that the format string with a value specified alone is 0.## # (for percentages, preserving a 2-digit significant number while avoiding the "100.00%" situation with f2). <br />
  197. /// example: "{a}, {c}", "{a1}, {c1: f1}", "{a1}, {c1:0: f1}", "{a1}, {c1:1-1: f1}"
  198. /// ||提示框标题内容的字符串模版格式器。支持用 \n 换行。可以单独设置占位符{i}表示忽略不显示title。
  199. /// 模板变量有{.}、{a}、{b}、{c}、{d}、{e}、{f}、{g}。<br/>
  200. /// {.}为当前所指示或index为0的serie的对应颜色的圆点。<br/>
  201. /// {a}为当前所指示或index为0的serie的系列名name。<br/>
  202. /// {b}为当前所指示或index为0的serie的数据项serieData的name,或者类目值(如折线图的X轴)。<br/>
  203. /// {c}为当前所指示或index为0的serie的y维(dimesion为1)的数值。<br/>
  204. /// {d}为当前所指示或index为0的serie的y维(dimesion为1)百分比值,注意不带%号。<br/>
  205. /// {e}为当前所指示或index为0的serie的数据项serieData的name。<br/>
  206. /// {h}为当前所指示或index为0的serie的数据项serieData的十六进制颜色值。<br/>
  207. /// {f}为数据总和。<br/>
  208. /// {g}为数据总个数。<br/>
  209. /// {f}为value所对应的y轴的类目值。<br/>
  210. /// {.1}表示指定index为1的serie对应颜色的圆点。<br/>
  211. /// {a1}、{b1}、{c1}中的1表示指定index为1的serie。<br/>
  212. /// {c1:2}表示索引为1的serie的当前指示数据项的第3个数据(一个数据项有多个数据,index为2表示第3个数据)。<br/>
  213. /// {c1:2-2}表示索引为1的serie的第3个数据项的第3个数据(也就是要指定第几个数据项时必须要指定第几个数据)。<br/>
  214. /// {d1:2:f2}表示单独指定了数值的格式化字符串为f2(不指定时用numericFormatter)。<br/>
  215. /// {d:0.##} 表示单独指定了数值的格式化字符串为 0.## (用于百分比,保留2位有效数同时又能避免使用 f2 而出现的类似于"100.00%"的情况 )。<br/>
  216. /// 示例:"{a}:{c}"、"{a1}:{c1:f1}"、"{a1}:{c1:0:f1}"、"{a1}:{c1:1-1:f1}"
  217. /// </summary>
  218. public string titleFormatter { get { return m_TitleFormatter; } set { m_TitleFormatter = value; } }
  219. /// <summary>
  220. /// a string template formatter for a single Serie or data item content. Support for wrapping lines with \n.
  221. /// Template variables are {.}, {a}, {b}, {c}, {d}.<br/>
  222. /// {.} is the dot of the corresponding color of a Serie that is currently indicated or whose index is 0.<br/>
  223. /// {a} is the series name of the serie that is currently indicated or whose index is 0.<br/>
  224. /// {b} is the name of the data item serieData that is currently indicated or whose index is 0, or a category value (such as the X-axis of a line chart).<br/>
  225. /// {c} is the value of a Y-dimension (dimesion is 1) from a Serie that is currently indicated or whose index is 0.<br/>
  226. /// {d} is the percentage value of Y-dimensions (dimesion is 1) from serie that is currently indicated or whose index is 0, with no % sign.<br/>
  227. /// {e} is the name of the data item serieData that is currently indicated or whose index is 0.<br/>
  228. /// {f} is sum of data.<br/>
  229. /// {y} is category value of y axis.<br/>
  230. /// {.1} represents a dot from serie corresponding color that specifies index as 1.<br/>
  231. /// 1 in {a1}, {b1}, {c1} represents a serie that specifies an index of 1.<br/>
  232. /// {c1:2} represents the third data from serie's current indication data item indexed to 1 (a data item has multiple data, index 2 represents the third data).<br/>
  233. /// {c1:2-2} represents the third data item from serie's third data item indexed to 1 (i.e., which data item must be specified to specify).<br/>
  234. /// {d1:2: F2} indicates that a formatted string with a value specified separately is F2 (numericFormatter is used when numericFormatter is not specified).<br/>
  235. /// {d:0.##} indicates that a formatted string with a value specified separately is 0.## (used for percentage, reserved 2 valid digits while avoiding the situation similar to "100.00%" when using f2 ).<br/>
  236. /// Example: "{a}, {c}", "{a1}, {c1: f1}", "{a1}, {c1:0: f1}", "{a1} : {c1:1-1: f1}"<br/>
  237. /// ||提示框单个serie或数据项内容的字符串模版格式器。支持用 \n 换行。用|来表示多个列的分隔。
  238. /// 模板变量有{.}、{a}、{b}、{c}、{d}、{e}、{f}、{g}。<br/>
  239. /// {i}或-表示忽略当前项。
  240. /// {.}为当前所指示的serie或数据项的对应颜色的圆点。<br/>
  241. /// {a}为当前所指示的serie或数据项的系列名name。<br/>
  242. /// {b}为当前所指示的serie或数据项的数据项serieData的name,或者类目值(如折线图的X轴)。<br/>
  243. /// {c}为当前所指示的serie或数据项的y维(dimesion为1)的数值。<br/>
  244. /// {d}为当前所指示的serie或数据项的y维(dimesion为1)百分比值,注意不带%号。<br/>
  245. /// {e}为当前所指示的serie或数据项的数据项serieData的name。<br/>
  246. /// {f}为当前所指示的serie的默认维度的数据总和。<br/>
  247. /// {g}为当前所指示的serie的数据总个数。<br/>
  248. /// {h}为当前所指示的serie的十六进制颜色值。<br/>
  249. /// {y}为当前所指示的serie的y轴的类目值。<br/>
  250. /// {c0}表示当前数据项维度为0的数据。<br/>
  251. /// {c1}表示当前数据项维度为1的数据。<br/>
  252. /// {d3}表示维度3的数据的百分比。它的分母是默认维度(一般是1维度)数据。<br/>
  253. /// |表示多个列的分隔。<br/>
  254. /// 示例:"{i}", "{.}|{a}|{c}", "{.}|{b}|{c2:f2}", "{.}|{b}|{y}"
  255. /// </summary>
  256. public string itemFormatter { get { return m_ItemFormatter; } set { m_ItemFormatter = value; } }
  257. /// <summary>
  258. /// Standard number and date format string. Used to format a Double value or a DateTime date as a string. numericFormatter is used as an argument to either `Double.ToString ()` or `DateTime.ToString()`. <br />
  259. /// The number format uses the Axx format: A is a single-character format specifier that supports C currency, D decimal, E exponent, F fixed-point number, G regular, N digit, P percentage, R round trip, and X hexadecimal. xx is precision specification, from 0-99. E.g. F1, E2<br />
  260. /// Date format Common date formats are: yyyy year, MM month, dd day, HH hour, mm minute, ss second, fff millisecond. For example: yyyy-MM-dd HH:mm:ss<br />
  261. /// number format reference: https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings<br/>
  262. /// date format reference: https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings<br/>
  263. /// ||标准数字和日期格式字符串。用于将Double数值或DateTime日期格式化显示为字符串。numericFormatter用来作为Double.ToString()或DateTime.ToString()的参数。<br/>
  264. /// 数字格式使用Axx的形式:A是格式说明符的单字符,支持C货币、D十进制、E指数、F定点数、G常规、N数字、P百分比、R往返、X十六进制的。xx是精度说明,从0-99。如:F1, E2<br/>
  265. /// 日期格式常见的格式:yyyy年,MM月,dd日,HH时,mm分,ss秒,fff毫秒。如:yyyy-MM-dd HH:mm:ss<br/>
  266. /// 数值格式化参考:https://docs.microsoft.com/zh-cn/dotnet/standard/base-types/standard-numeric-format-strings <br/>
  267. /// 日期格式化参考:https://learn.microsoft.com/zh-cn/dotnet/standard/base-types/standard-date-and-time-format-strings
  268. /// </summary>
  269. public string numericFormatter
  270. {
  271. get { return m_NumericFormatter; }
  272. set { if (PropertyUtil.SetClass(ref m_NumericFormatter, value)) SetComponentDirty(); }
  273. }
  274. /// <summary>
  275. /// the marker of serie.
  276. /// ||serie的符号标志。
  277. /// </summary>
  278. public string marker { get { return m_Marker; } set { m_Marker = value; } }
  279. /// <summary>
  280. /// Fixed width. Higher priority than minWidth.
  281. /// ||固定宽度。比 minWidth 优先。
  282. /// </summary>
  283. public float fixedWidth { get { return m_FixedWidth; } set { m_FixedWidth = value; } }
  284. /// <summary>
  285. /// Fixed height. Higher priority than minHeight.
  286. /// ||固定高度。比 minHeight 优先。
  287. /// </summary>
  288. public float fixedHeight { get { return m_FixedHeight; } set { m_FixedHeight = value; } }
  289. /// <summary>
  290. /// Minimum width. If fixedWidth has a value, get fixedWidth first.
  291. /// ||最小宽度。如若 fixedWidth 设有值,优先取 fixedWidth。
  292. /// </summary>
  293. public float minWidth { get { return m_MinWidth; } set { m_MinWidth = value; } }
  294. /// <summary>
  295. /// Minimum height. If fixedHeight has a value, take priority over fixedHeight.
  296. /// ||最小高度。如若 fixedHeight 设有值,优先取 fixedHeight。
  297. /// </summary>
  298. public float minHeight { get { return m_MinHeight; } set { m_MinHeight = value; } }
  299. /// <summary>
  300. /// the text padding of left and right. defaut:5.
  301. /// ||左右边距。
  302. /// </summary>
  303. public int paddingLeftRight { get { return m_PaddingLeftRight; } set { m_PaddingLeftRight = value; } }
  304. /// <summary>
  305. /// the text padding of top and bottom. defaut:5.
  306. /// ||上下边距。
  307. /// </summary>
  308. public int paddingTopBottom { get { return m_PaddingTopBottom; } set { m_PaddingTopBottom = value; } }
  309. /// <summary>
  310. /// Whether to show ignored data on tooltip.
  311. /// ||是否显示忽略数据在tooltip上。
  312. /// </summary>
  313. public bool ignoreDataShow { get { return m_IgnoreDataShow; } set { m_IgnoreDataShow = value; } }
  314. /// <summary>
  315. /// The default display character information for ignored data.
  316. /// ||被忽略数据的默认显示字符信息。如果设置为空,则表示完全不显示忽略数据。
  317. /// </summary>
  318. public string ignoreDataDefaultContent { get { return m_IgnoreDataDefaultContent; } set { m_IgnoreDataDefaultContent = value; } }
  319. /// <summary>
  320. /// The background image of tooltip.
  321. /// ||提示框的背景图片。
  322. /// </summary>
  323. public Sprite backgroundImage { get { return m_BackgroundImage; } set { m_BackgroundImage = value; SetComponentDirty(); } }
  324. /// <summary>
  325. /// The background type of tooltip.
  326. /// ||提示框的背景图片显示类型。
  327. /// </summary>
  328. public Image.Type backgroundType { get { return m_BackgroundType; } set { m_BackgroundType = value; SetComponentDirty(); } }
  329. /// <summary>
  330. /// The background color of tooltip.
  331. /// ||提示框的背景颜色。
  332. /// </summary>
  333. public Color backgroundColor { get { return m_BackgroundColor; } set { m_BackgroundColor = value; SetComponentDirty(); } }
  334. /// <summary>
  335. /// Whether to trigger after always display.
  336. /// ||是否触发后一直显示提示框浮层。
  337. /// </summary>
  338. public bool alwayShowContent { get { return m_AlwayShowContent; } set { m_AlwayShowContent = value; } }
  339. /// <summary>
  340. /// Whether to show the tooltip floating layer, whose default value is true.
  341. /// It should be configurated to be false, if you only need tooltip to trigger the event or show the axisPointer without content.
  342. /// ||是否显示提示框浮层,默认显示。只需tooltip触发事件或显示axisPointer而不需要显示内容时可配置该项为false。
  343. /// </summary>
  344. public bool showContent { get { return m_ShowContent; } set { m_ShowContent = value; } }
  345. /// <summary>
  346. /// The position offset of tooltip relative to the mouse position.
  347. /// ||提示框相对于鼠标位置的偏移。
  348. /// </summary>
  349. public Vector2 offset { get { return m_Offset; } set { m_Offset = value; } }
  350. /// <summary>
  351. /// the width of tooltip border.
  352. /// ||边框线宽。
  353. /// </summary>
  354. public float borderWidth
  355. {
  356. get { return m_BorderWidth; }
  357. set { if (PropertyUtil.SetStruct(ref m_BorderWidth, value)) SetVerticesDirty(); }
  358. }
  359. /// <summary>
  360. /// the color of tooltip border.
  361. /// ||边框颜色。
  362. /// </summary>
  363. public Color32 borderColor
  364. {
  365. get { return m_BorderColor; }
  366. set { if (PropertyUtil.SetColor(ref m_BorderColor, value)) SetVerticesDirty(); }
  367. }
  368. /// <summary>
  369. /// the x positionn of fixedX.
  370. /// ||固定X位置的坐标。
  371. /// </summary>
  372. public float fixedX
  373. {
  374. get { return m_FixedX; }
  375. set { if (PropertyUtil.SetStruct(ref m_FixedX, value)) SetVerticesDirty(); }
  376. }
  377. /// <summary>
  378. /// the y position of fixedY.
  379. /// ||固定Y位置的坐标。
  380. /// </summary>
  381. public float fixedY
  382. {
  383. get { return m_FixedY; }
  384. set { if (PropertyUtil.SetStruct(ref m_FixedY, value)) SetVerticesDirty(); }
  385. }
  386. /// <summary>
  387. /// height of title text.
  388. /// ||标题文本的高。
  389. /// </summary>
  390. public float titleHeight
  391. {
  392. get { return m_TitleHeight; }
  393. set { if (PropertyUtil.SetStruct(ref m_TitleHeight, value)) SetComponentDirty(); }
  394. }
  395. /// <summary>
  396. /// height of content text.
  397. /// ||数据项文本的高。
  398. /// </summary>
  399. public float itemHeight
  400. {
  401. get { return m_ItemHeight; }
  402. set { if (PropertyUtil.SetStruct(ref m_ItemHeight, value)) SetComponentDirty(); }
  403. }
  404. /// <summary>
  405. /// the textstyle of title.
  406. /// ||标题的文本样式。
  407. /// </summary>
  408. public LabelStyle titleLabelStyle
  409. {
  410. get { return m_TitleLabelStyle; }
  411. set { if (value != null) { m_TitleLabelStyle = value; SetComponentDirty(); } }
  412. }
  413. /// <summary>
  414. /// the textstyle list of content.
  415. /// ||内容部分的文本样式列表。和列一一对应。
  416. /// </summary>
  417. public List<LabelStyle> contentLabelStyles
  418. {
  419. get { return m_ContentLabelStyles; }
  420. set { if (value != null) { m_ContentLabelStyles = value; SetComponentDirty(); } }
  421. }
  422. /// <summary>
  423. /// the line style of indicator line.
  424. /// ||指示线样式。
  425. /// </summary>
  426. public LineStyle lineStyle
  427. {
  428. get { return m_LineStyle; }
  429. set { if (value != null) m_LineStyle = value; SetComponentDirty(); }
  430. }
  431. /// <summary>
  432. /// 组件是否需要刷新
  433. /// </summary>
  434. public override bool componentDirty
  435. {
  436. get { return m_ComponentDirty || lineStyle.componentDirty; }
  437. }
  438. public override void ClearComponentDirty()
  439. {
  440. base.ClearComponentDirty();
  441. lineStyle.ClearComponentDirty();
  442. }
  443. /// <summary>
  444. /// 当前提示框所指示的Serie索引(目前只对散点图有效)。
  445. /// </summary>
  446. public Dictionary<int, List<int>> runtimeSerieIndex = new Dictionary<int, List<int>>();
  447. /// <summary>
  448. /// The data index currently indicated by Tooltip.
  449. /// ||当前提示框所指示的数据项索引。
  450. /// </summary>
  451. public List<int> runtimeDataIndex { get { return m_RuntimeDateIndex; } internal set { m_RuntimeDateIndex = value; } }
  452. private List<int> m_RuntimeDateIndex = new List<int>() { -1, -1 };
  453. /// <summary>
  454. /// Keep Tooltiop displayed at the top.
  455. /// ||保持Tooltiop显示在最顶上
  456. /// </summary>
  457. public void KeepTop()
  458. {
  459. gameObject.transform.SetAsLastSibling();
  460. }
  461. public override void ClearData()
  462. {
  463. ClearValue();
  464. }
  465. /// <summary>
  466. /// 清除提示框指示数据
  467. /// </summary>
  468. internal void ClearValue()
  469. {
  470. for (int i = 0; i < runtimeDataIndex.Count; i++) runtimeDataIndex[i] = -1;
  471. }
  472. /// <summary>
  473. /// 提示框是否显示
  474. /// </summary>
  475. /// <returns></returns>
  476. public bool IsActive()
  477. {
  478. return gameObject != null && gameObject.activeInHierarchy;
  479. }
  480. /// <summary>
  481. /// 设置Tooltip组件是否显示
  482. /// </summary>
  483. /// <param name="flag"></param>
  484. public void SetActive(bool flag)
  485. {
  486. if (gameObject && gameObject.activeInHierarchy != flag)
  487. {
  488. gameObject.SetActive(alwayShowContent ? true : flag);
  489. }
  490. SetContentActive(flag);
  491. }
  492. /// <summary>
  493. /// 更新文本框位置
  494. /// </summary>
  495. /// <param name="pos"></param>
  496. public void UpdateContentPos(Vector2 pos, float width, float height)
  497. {
  498. if (view != null)
  499. {
  500. switch (m_Position)
  501. {
  502. case Position.Auto:
  503. #if UNITY_ANDROID || UNITY_IOS
  504. if (m_FixedY == 0) pos.y = ChartHelper.GetActualValue(0.7f, height);
  505. else pos.y = ChartHelper.GetActualValue(m_FixedY, height);
  506. #endif
  507. break;
  508. case Position.Custom:
  509. pos.x = ChartHelper.GetActualValue(m_FixedX, width);
  510. pos.y = ChartHelper.GetActualValue(m_FixedY, height);
  511. break;
  512. case Position.FixedX:
  513. pos.x = ChartHelper.GetActualValue(m_FixedX, width);
  514. break;
  515. case Position.FixedY:
  516. pos.y = ChartHelper.GetActualValue(m_FixedY, height);
  517. break;
  518. }
  519. view.UpdatePosition(pos);
  520. }
  521. }
  522. /// <summary>
  523. /// 设置文本框是否显示
  524. /// </summary>
  525. /// <param name="flag"></param>
  526. public void SetContentActive(bool flag)
  527. {
  528. if (view == null)
  529. return;
  530. view.SetActive(alwayShowContent ? true : flag);
  531. }
  532. /// <summary>
  533. /// 当前提示框是否选中数据项
  534. /// </summary>
  535. /// <returns></returns>
  536. public bool IsSelected()
  537. {
  538. foreach (var index in runtimeDataIndex)
  539. if (index >= 0) return true;
  540. return false;
  541. }
  542. /// <summary>
  543. /// 指定索引的数据项是否被提示框选中
  544. /// </summary>
  545. /// <param name="index"></param>
  546. /// <returns></returns>
  547. public bool IsSelected(int index)
  548. {
  549. foreach (var temp in runtimeDataIndex)
  550. if (temp == index) return true;
  551. return false;
  552. }
  553. public void ClearSerieDataIndex()
  554. {
  555. foreach (var kv in runtimeSerieIndex)
  556. {
  557. kv.Value.Clear();
  558. }
  559. }
  560. public void AddSerieDataIndex(int serieIndex, int dataIndex)
  561. {
  562. if (!runtimeSerieIndex.ContainsKey(serieIndex))
  563. {
  564. runtimeSerieIndex[serieIndex] = new List<int>();
  565. }
  566. runtimeSerieIndex[serieIndex].Add(dataIndex);
  567. }
  568. public bool isAnySerieDataIndex()
  569. {
  570. foreach (var kv in runtimeSerieIndex)
  571. {
  572. if (kv.Value.Count > 0) return true;
  573. }
  574. return false;
  575. }
  576. public bool IsTriggerItem()
  577. {
  578. return trigger == Trigger.Auto ? context.trigger == Trigger.Item : trigger == Trigger.Item;
  579. }
  580. public bool IsTriggerAxis()
  581. {
  582. return trigger == Trigger.Auto ? context.trigger == Trigger.Axis : trigger == Trigger.Axis;
  583. }
  584. public LabelStyle GetContentLabelStyle(int index)
  585. {
  586. if (m_ContentLabelStyles.Count == 0)
  587. return null;
  588. if (index < 0)
  589. index = 0;
  590. else if (index > m_ContentLabelStyles.Count - 1)
  591. index = m_ContentLabelStyles.Count - 1;
  592. return m_ContentLabelStyles[index];
  593. }
  594. }
  595. }