RadarHighlighter.java
2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.github.mikephil.charting.highlight;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Utils;
import java.util.List;
/**
* Created by philipp on 12/06/16.
*/
public class RadarHighlighter extends PieRadarHighlighter<RadarChart> {
public RadarHighlighter(RadarChart chart) {
super(chart);
}
@Override
protected Highlight getClosestHighlight(int index, float x, float y) {
List<Highlight> highlights = getHighlightsAtIndex(index);
float distanceToCenter = mChart.distanceToCenter(x, y) / mChart.getFactor();
Highlight closest = null;
float distance = Float.MAX_VALUE;
for (int i = 0; i < highlights.size(); i++) {
Highlight high = highlights.get(i);
float cdistance = Math.abs(high.getY() - distanceToCenter);
if (cdistance < distance) {
closest = high;
distance = cdistance;
}
}
return closest;
}
/**
* Returns an array of Highlight objects for the given index. The Highlight
* objects give information about the value at the selected index and the
* DataSet it belongs to. INFORMATION: This method does calculations at
* runtime. Do not over-use in performance critical situations.
*
* @param index
* @return
*/
protected List<Highlight> getHighlightsAtIndex(int index) {
mHighlightBuffer.clear();
float phaseX = mChart.getAnimator().getPhaseX();
float phaseY = mChart.getAnimator().getPhaseY();
float sliceangle = mChart.getSliceAngle();
float factor = mChart.getFactor();
MPPointF pOut = MPPointF.getInstance(0,0);
for (int i = 0; i < mChart.getData().getDataSetCount(); i++) {
IDataSet<?> dataSet = mChart.getData().getDataSetByIndex(i);
final Entry entry = dataSet.getEntryForIndex(index);
float y = (entry.getY() - mChart.getYChartMin());
Utils.getPosition(
mChart.getCenterOffsets(), y * factor * phaseY,
sliceangle * index * phaseX + mChart.getRotationAngle(), pOut);
mHighlightBuffer.add(new Highlight(index, entry.getY(), pOut.x, pOut.y, i, dataSet.getAxisDependency()));
}
return mHighlightBuffer;
}
}