|
4 | 4 | import java.util.List; |
5 | 5 |
|
6 | 6 | import com.github.mikephil.charting.components.YAxis; |
| 7 | +import com.github.mikephil.charting.data.Entry; |
7 | 8 | import com.github.mikephil.charting.interfaces.datasets.IDataSet; |
8 | 9 | import com.github.mikephil.charting.interfaces.dataprovider.BarLineScatterCandleBubbleDataProvider; |
9 | 10 | import com.github.mikephil.charting.utils.SelectionDetail; |
|
14 | 15 | */ |
15 | 16 | public class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider> { |
16 | 17 |
|
17 | | - /** instance of the data-provider */ |
18 | | - protected T mChart; |
| 18 | + /** |
| 19 | + * instance of the data-provider |
| 20 | + */ |
| 21 | + protected T mChart; |
19 | 22 |
|
20 | | - public ChartHighlighter(T chart) { |
21 | | - this.mChart = chart; |
22 | | - } |
| 23 | + public ChartHighlighter(T chart) { |
| 24 | + this.mChart = chart; |
| 25 | + } |
23 | 26 |
|
24 | | - /** |
25 | | - * Returns a Highlight object corresponding to the given x- and y- touch positions in pixels. |
26 | | - * |
27 | | - * @param x |
28 | | - * @param y |
29 | | - * @return |
30 | | - */ |
31 | | - public Highlight getHighlight(float x, float y) { |
| 27 | + /** |
| 28 | + * Returns a Highlight object corresponding to the given x- and y- touch positions in pixels. |
| 29 | + * |
| 30 | + * @param x |
| 31 | + * @param y |
| 32 | + * @return |
| 33 | + */ |
| 34 | + public Highlight getHighlight(float x, float y) { |
32 | 35 |
|
33 | | - float xVal = getXForTouch(x); |
| 36 | + float xVal = getXForTouch(x); |
34 | 37 |
|
35 | | - SelectionDetail selectionDetail = getSelectionDetail(xVal, y, -1); |
36 | | - if (selectionDetail == null) |
37 | | - return null; |
| 38 | + SelectionDetail selectionDetail = getSelectionDetail(xVal, x, y, -1); |
| 39 | + if (selectionDetail == null) |
| 40 | + return null; |
38 | 41 |
|
39 | | - return new Highlight(xVal, |
40 | | - selectionDetail.value, |
41 | | - selectionDetail.dataIndex, |
42 | | - selectionDetail.dataSetIndex); |
43 | | - } |
| 42 | + return new Highlight(xVal, |
| 43 | + selectionDetail.value, |
| 44 | + selectionDetail.dataIndex, |
| 45 | + selectionDetail.dataSetIndex); |
| 46 | + } |
44 | 47 |
|
45 | | - /** |
46 | | - * Returns the corresponding x-index for a given touch-position in pixels. |
47 | | - * |
48 | | - * @param x |
49 | | - * @return |
50 | | - */ |
51 | | - protected float getXForTouch(float x) { |
| 48 | + /** |
| 49 | + * Returns the corresponding x-index for a given touch-position in pixels. |
| 50 | + * |
| 51 | + * @param x |
| 52 | + * @return |
| 53 | + */ |
| 54 | + protected float getXForTouch(float x) { |
52 | 55 |
|
53 | | - // create an array of the touch-point |
54 | | - float[] pts = new float[2]; |
55 | | - pts[0] = x; |
| 56 | + // create an array of the touch-point |
| 57 | + float[] pts = new float[2]; |
| 58 | + pts[0] = x; |
56 | 59 |
|
57 | | - // take any transformer to determine the x-axis value |
58 | | - mChart.getTransformer(YAxis.AxisDependency.LEFT).pixelsToValue(pts); |
| 60 | + // take any transformer to determine the x-axis value |
| 61 | + mChart.getTransformer(YAxis.AxisDependency.LEFT).pixelsToValue(pts); |
59 | 62 |
|
60 | | - return Math.round(pts[0]); |
61 | | - } |
| 63 | + return Math.round(pts[0]); |
| 64 | + } |
62 | 65 |
|
63 | | - /** |
64 | | - * Returns the corresponding SelectionDetail for a given xVal and y-touch position in pixels. |
65 | | - * |
66 | | - * @param xVal |
67 | | - * @param y |
68 | | - * @param dataSetIndex |
69 | | - * @return |
70 | | - */ |
71 | | - protected SelectionDetail getSelectionDetail(float xVal, float y, int dataSetIndex) { |
| 66 | + /** |
| 67 | + * Returns the corresponding SelectionDetail for a given xVal and y-touch position in pixels. |
| 68 | + * |
| 69 | + * @param xVal |
| 70 | + * @param y |
| 71 | + * @param dataSetIndex |
| 72 | + * @return |
| 73 | + */ |
| 74 | + protected SelectionDetail getSelectionDetail(float xVal, float x, float y, int dataSetIndex) { |
72 | 75 |
|
73 | | - List<SelectionDetail> valsAtIndex = getSelectionDetailsAtIndex(xVal, dataSetIndex); |
| 76 | + List<SelectionDetail> valsAtIndex = getSelectionDetailsAtIndex(xVal, dataSetIndex); |
74 | 77 |
|
75 | | - float leftdist = Utils.getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.LEFT); |
76 | | - float rightdist = Utils.getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.RIGHT); |
| 78 | + float leftdist = Utils.getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.LEFT); |
| 79 | + float rightdist = Utils.getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.RIGHT); |
77 | 80 |
|
78 | | - YAxis.AxisDependency axis = leftdist < rightdist ? YAxis.AxisDependency.LEFT : YAxis.AxisDependency.RIGHT; |
| 81 | + YAxis.AxisDependency axis = leftdist < rightdist ? YAxis.AxisDependency.LEFT : YAxis.AxisDependency.RIGHT; |
79 | 82 |
|
80 | | - SelectionDetail detail = Utils.getClosestSelectionDetailByPixelY(valsAtIndex, y, axis); |
| 83 | + SelectionDetail detail = Utils.getClosestSelectionDetailByPixel(valsAtIndex, x, y, axis); |
81 | 84 |
|
82 | | - return detail; |
83 | | - } |
| 85 | + return detail; |
| 86 | + } |
84 | 87 |
|
85 | | - /** |
86 | | - * Returns a list of SelectionDetail object corresponding to the given xIndex. |
87 | | - * |
88 | | - * @param xVal |
89 | | - * @param dataSetIndex dataSet index to look at. -1 if unspecified. |
90 | | - * @return |
91 | | - */ |
92 | | - protected List<SelectionDetail> getSelectionDetailsAtIndex(float xVal, int dataSetIndex) { |
| 88 | + /** |
| 89 | + * Returns a list of SelectionDetail object corresponding to the given xIndex. |
| 90 | + * |
| 91 | + * @param xVal |
| 92 | + * @param dataSetIndex dataSet index to look at. -1 if unspecified. |
| 93 | + * @return |
| 94 | + */ |
| 95 | + protected List<SelectionDetail> getSelectionDetailsAtIndex(float xVal, int dataSetIndex) { |
93 | 96 |
|
94 | | - List<SelectionDetail> vals = new ArrayList<SelectionDetail>(); |
| 97 | + List<SelectionDetail> vals = new ArrayList<SelectionDetail>(); |
95 | 98 |
|
96 | | - if (mChart.getData() == null) return vals; |
| 99 | + if (mChart.getData() == null) return vals; |
97 | 100 |
|
98 | | - float[] pts = new float[2]; |
| 101 | + float[] pts = new float[2]; |
99 | 102 |
|
100 | | - for (int i = 0, dataSetCount = mChart.getData().getDataSetCount(); |
101 | | - i < dataSetCount; |
102 | | - i++) { |
| 103 | + for (int i = 0, dataSetCount = mChart.getData().getDataSetCount(); i < dataSetCount; i++) { |
103 | 104 |
|
104 | | - if (dataSetIndex > -1 && dataSetIndex != i) |
105 | | - continue; |
| 105 | + if (dataSetIndex > -1 && dataSetIndex != i) |
| 106 | + continue; |
106 | 107 |
|
107 | | - IDataSet dataSet = mChart.getData().getDataSetByIndex(i); |
| 108 | + IDataSet dataSet = mChart.getData().getDataSetByIndex(i); |
108 | 109 |
|
109 | | - // dont include datasets that cannot be highlighted |
110 | | - if (!dataSet.isHighlightEnabled()) |
111 | | - continue; |
| 110 | + // dont include datasets that cannot be highlighted |
| 111 | + if (!dataSet.isHighlightEnabled()) |
| 112 | + continue; |
112 | 113 |
|
113 | | - // extract all y-values from all DataSets at the given x-index |
114 | | - final float[] yVals = dataSet.getYValuesForXPos(xVal); |
115 | | - for (float yVal : yVals) { |
116 | | - if (Float.isNaN(yVal)) |
117 | | - continue; |
| 114 | + // extract all y-values from all DataSets at the given x-index |
| 115 | + final Entry e = dataSet.getEntryForXPos(xVal); |
118 | 116 |
|
119 | | - pts[1] = yVal; |
| 117 | + pts[0] = e.getX(); |
| 118 | + pts[1] = e.getY(); |
120 | 119 |
|
121 | | - mChart.getTransformer(dataSet.getAxisDependency()).pointValuesToPixel(pts); |
| 120 | + mChart.getTransformer(dataSet.getAxisDependency()).pointValuesToPixel(pts); |
122 | 121 |
|
123 | | - if (!Float.isNaN(pts[1])) { |
124 | | - vals.add(new SelectionDetail(pts[1], yVal, i, dataSet)); |
125 | | - } |
126 | | - } |
127 | | - } |
| 122 | + if (!Float.isNaN(pts[1])) { |
| 123 | + vals.add(new SelectionDetail(pts[0], pts[1], e.getY(), i, dataSet)); |
| 124 | + } |
| 125 | + } |
128 | 126 |
|
129 | | - return vals; |
130 | | - } |
| 127 | + return vals; |
| 128 | + } |
131 | 129 | } |
0 commit comments