Highlight.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package com.github.mikephil.charting.highlight;
  2. import com.github.mikephil.charting.components.YAxis;
  3. /**
  4. * Contains information needed to determine the highlighted value.
  5. *
  6. * @author Philipp Jahoda
  7. */
  8. public class Highlight {
  9. /**
  10. * the x-value of the highlighted value
  11. */
  12. private float mX = Float.NaN;
  13. /**
  14. * the y-value of the highlighted value
  15. */
  16. private float mY = Float.NaN;
  17. /**
  18. * the x-pixel of the highlight
  19. */
  20. private float mXPx;
  21. /**
  22. * the y-pixel of the highlight
  23. */
  24. private float mYPx;
  25. /**
  26. * the index of the data object - in case it refers to more than one
  27. */
  28. private int mDataIndex = -1;
  29. /**
  30. * the index of the dataset the highlighted value is in
  31. */
  32. private int mDataSetIndex;
  33. /**
  34. * index which value of a stacked bar entry is highlighted, default -1
  35. */
  36. private int mStackIndex = -1;
  37. /**
  38. * the axis the highlighted value belongs to
  39. */
  40. private YAxis.AxisDependency axis;
  41. /**
  42. * the x-position (pixels) on which this highlight object was last drawn
  43. */
  44. private float mDrawX;
  45. /**
  46. * the y-position (pixels) on which this highlight object was last drawn
  47. */
  48. private float mDrawY;
  49. public Highlight(float x, float y, int dataSetIndex, int dataIndex) {
  50. this.mX = x;
  51. this.mY = y;
  52. this.mDataSetIndex = dataSetIndex;
  53. this.mDataIndex = dataIndex;
  54. }
  55. public Highlight(float x, float y, int dataSetIndex) {
  56. this.mX = x;
  57. this.mY = y;
  58. this.mDataSetIndex = dataSetIndex;
  59. this.mDataIndex = -1;
  60. }
  61. public Highlight(float x, int dataSetIndex, int stackIndex) {
  62. this(x, Float.NaN, dataSetIndex);
  63. this.mStackIndex = stackIndex;
  64. }
  65. /**
  66. * constructor
  67. *
  68. * @param x the x-value of the highlighted value
  69. * @param y the y-value of the highlighted value
  70. * @param dataSetIndex the index of the DataSet the highlighted value belongs to
  71. */
  72. public Highlight(float x, float y, float xPx, float yPx, int dataSetIndex, YAxis.AxisDependency axis) {
  73. this.mX = x;
  74. this.mY = y;
  75. this.mXPx = xPx;
  76. this.mYPx = yPx;
  77. this.mDataSetIndex = dataSetIndex;
  78. this.axis = axis;
  79. }
  80. /**
  81. * Constructor, only used for stacked-barchart.
  82. *
  83. * @param x the index of the highlighted value on the x-axis
  84. * @param y the y-value of the highlighted value
  85. * @param dataSetIndex the index of the DataSet the highlighted value belongs to
  86. * @param stackIndex references which value of a stacked-bar entry has been
  87. * selected
  88. */
  89. public Highlight(float x, float y, float xPx, float yPx, int dataSetIndex, int stackIndex, YAxis.AxisDependency axis) {
  90. this(x, y, xPx, yPx, dataSetIndex, axis);
  91. this.mStackIndex = stackIndex;
  92. }
  93. /**
  94. * returns the x-value of the highlighted value
  95. *
  96. * @return
  97. */
  98. public float getX() {
  99. return mX;
  100. }
  101. /**
  102. * returns the y-value of the highlighted value
  103. *
  104. * @return
  105. */
  106. public float getY() {
  107. return mY;
  108. }
  109. /**
  110. * returns the x-position of the highlight in pixels
  111. */
  112. public float getXPx() {
  113. return mXPx;
  114. }
  115. /**
  116. * returns the y-position of the highlight in pixels
  117. */
  118. public float getYPx() {
  119. return mYPx;
  120. }
  121. /**
  122. * the index of the data object - in case it refers to more than one
  123. *
  124. * @return
  125. */
  126. public int getDataIndex() {
  127. return mDataIndex;
  128. }
  129. public void setDataIndex(int mDataIndex) {
  130. this.mDataIndex = mDataIndex;
  131. }
  132. /**
  133. * returns the index of the DataSet the highlighted value is in
  134. *
  135. * @return
  136. */
  137. public int getDataSetIndex() {
  138. return mDataSetIndex;
  139. }
  140. /**
  141. * Only needed if a stacked-barchart entry was highlighted. References the
  142. * selected value within the stacked-entry.
  143. *
  144. * @return
  145. */
  146. public int getStackIndex() {
  147. return mStackIndex;
  148. }
  149. public boolean isStacked() {
  150. return mStackIndex >= 0;
  151. }
  152. /**
  153. * Returns the axis the highlighted value belongs to.
  154. *
  155. * @return
  156. */
  157. public YAxis.AxisDependency getAxis() {
  158. return axis;
  159. }
  160. /**
  161. * Sets the x- and y-position (pixels) where this highlight was last drawn.
  162. *
  163. * @param x
  164. * @param y
  165. */
  166. public void setDraw(float x, float y) {
  167. this.mDrawX = x;
  168. this.mDrawY = y;
  169. }
  170. /**
  171. * Returns the x-position in pixels where this highlight object was last drawn.
  172. *
  173. * @return
  174. */
  175. public float getDrawX() {
  176. return mDrawX;
  177. }
  178. /**
  179. * Returns the y-position in pixels where this highlight object was last drawn.
  180. *
  181. * @return
  182. */
  183. public float getDrawY() {
  184. return mDrawY;
  185. }
  186. /**
  187. * Returns true if this highlight object is equal to the other (compares
  188. * xIndex and dataSetIndex)
  189. *
  190. * @param h
  191. * @return
  192. */
  193. public boolean equalTo(Highlight h) {
  194. if (h == null)
  195. return false;
  196. else {
  197. if (this.mDataSetIndex == h.mDataSetIndex && this.mX == h.mX
  198. && this.mStackIndex == h.mStackIndex && this.mDataIndex == h.mDataIndex)
  199. return true;
  200. else
  201. return false;
  202. }
  203. }
  204. @Override
  205. public String toString() {
  206. return "Highlight, x: " + mX + ", y: " + mY + ", dataSetIndex: " + mDataSetIndex
  207. + ", stackIndex (only stacked barentry): " + mStackIndex;
  208. }
  209. }