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