Highlighter cleanup
This commit is contained in:
parent
694eb49162
commit
82ca5e7e91
4 changed files with 55 additions and 53 deletions
|
@ -1,17 +1,11 @@
|
|||
package com.github.mikephil.charting.highlight;
|
||||
|
||||
import com.github.mikephil.charting.components.YAxis;
|
||||
import com.github.mikephil.charting.data.BarData;
|
||||
import com.github.mikephil.charting.data.BarEntry;
|
||||
import com.github.mikephil.charting.data.Entry;
|
||||
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
|
||||
import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider;
|
||||
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
|
||||
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
|
||||
import com.github.mikephil.charting.utils.PointD;
|
||||
import com.github.mikephil.charting.utils.SelectionDetail;
|
||||
import com.github.mikephil.charting.utils.Utils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Philipp Jahoda on 22/07/15.
|
||||
|
@ -60,11 +54,7 @@ public class BarHighlighter extends ChartHighlighter<BarDataProvider> {
|
|||
* @param yValue
|
||||
* @return
|
||||
*/
|
||||
protected Highlight getStackedHighlight(
|
||||
SelectionDetail selectionDetail,
|
||||
IBarDataSet set,
|
||||
float xVal,
|
||||
double yValue) {
|
||||
protected Highlight getStackedHighlight(SelectionDetail selectionDetail, IBarDataSet set, float xVal, double yValue) {
|
||||
|
||||
BarEntry entry = set.getEntryForXPos(xVal);
|
||||
|
||||
|
@ -159,7 +149,7 @@ public class BarHighlighter extends ChartHighlighter<BarDataProvider> {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected float getDistance(float x, float y, float selX, float selY) {
|
||||
return Math.abs(x - selX);
|
||||
protected float getDistance(float x1, float y1, float x2, float y2) {
|
||||
return Math.abs(x1 - x2);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,6 @@ import com.github.mikephil.charting.interfaces.datasets.IDataSet;
|
|||
import com.github.mikephil.charting.interfaces.dataprovider.BarLineScatterCandleBubbleDataProvider;
|
||||
import com.github.mikephil.charting.utils.PointD;
|
||||
import com.github.mikephil.charting.utils.SelectionDetail;
|
||||
import com.github.mikephil.charting.utils.Utils;
|
||||
|
||||
/**
|
||||
* Created by Philipp Jahoda on 21/07/15.
|
||||
|
@ -69,14 +68,14 @@ public class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider>
|
|||
*/
|
||||
protected SelectionDetail getSelectionDetail(float xVal, float x, float y) {
|
||||
|
||||
List<SelectionDetail> valsAtIndex = getSelectionDetailsAtIndex(xVal);
|
||||
List<SelectionDetail> closestValues = getSelectionDetailsAtXPos(xVal);
|
||||
|
||||
float leftdist = getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.LEFT);
|
||||
float rightdist = getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.RIGHT);
|
||||
float leftAxisMinDist = getMinimumDistance(closestValues, y, YAxis.AxisDependency.LEFT);
|
||||
float rightAxisMinDist = getMinimumDistance(closestValues, y, YAxis.AxisDependency.RIGHT);
|
||||
|
||||
YAxis.AxisDependency axis = leftdist < rightdist ? YAxis.AxisDependency.LEFT : YAxis.AxisDependency.RIGHT;
|
||||
YAxis.AxisDependency axis = leftAxisMinDist < rightAxisMinDist ? YAxis.AxisDependency.LEFT : YAxis.AxisDependency.RIGHT;
|
||||
|
||||
SelectionDetail detail = getClosestSelectionDetailByPixel(valsAtIndex, x, y, axis, mChart
|
||||
SelectionDetail detail = getClosestSelectionDetailByPixel(closestValues, x, y, axis, mChart
|
||||
.getMaxHighlightDistance());
|
||||
|
||||
return detail;
|
||||
|
@ -86,26 +85,24 @@ public class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider>
|
|||
* Returns the minimum distance from a touch value (in pixels) to the
|
||||
* closest value (in pixels) that is displayed in the chart.
|
||||
*
|
||||
* @param valsAtIndex
|
||||
* @param closestValues
|
||||
* @param pos
|
||||
* @param axis
|
||||
* @return
|
||||
*/
|
||||
protected float getMinimumDistance(List<SelectionDetail> valsAtIndex,
|
||||
float pos,
|
||||
YAxis.AxisDependency axis) {
|
||||
protected float getMinimumDistance(List<SelectionDetail> closestValues, float pos, YAxis.AxisDependency axis) {
|
||||
|
||||
float distance = Float.MAX_VALUE;
|
||||
|
||||
for (int i = 0; i < valsAtIndex.size(); i++) {
|
||||
for (int i = 0; i < closestValues.size(); i++) {
|
||||
|
||||
SelectionDetail sel = valsAtIndex.get(i);
|
||||
SelectionDetail sel = closestValues.get(i);
|
||||
|
||||
if (sel.dataSet.getAxisDependency() == axis) {
|
||||
|
||||
float cdistance = Math.abs(getSelectionPos(sel) - pos);
|
||||
if (cdistance < distance) {
|
||||
distance = cdistance;
|
||||
float tempDistance = Math.abs(getSelectionPos(sel) - pos);
|
||||
if (tempDistance < distance) {
|
||||
distance = tempDistance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -118,12 +115,13 @@ public class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider>
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns a list of SelectionDetail object corresponding to the given xVal.
|
||||
* Returns a list of SelectionDetail objects representing the entries closest to the given xVal.
|
||||
* The returned list contains two objects per DataSet (closest rounding up, closest rounding down).
|
||||
*
|
||||
* @param xVal
|
||||
* @return
|
||||
*/
|
||||
protected List<SelectionDetail> getSelectionDetailsAtIndex(float xVal) {
|
||||
protected List<SelectionDetail> getSelectionDetailsAtXPos(float xVal) {
|
||||
|
||||
List<SelectionDetail> vals = new ArrayList<SelectionDetail>();
|
||||
|
||||
|
@ -137,14 +135,23 @@ public class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider>
|
|||
if (!dataSet.isHighlightEnabled())
|
||||
continue;
|
||||
|
||||
vals.add(getDetails(dataSet, i, xVal, DataSet.Rounding.UP));
|
||||
vals.add(getDetails(dataSet, i, xVal, DataSet.Rounding.DOWN));
|
||||
vals.add(getDetail(dataSet, i, xVal, DataSet.Rounding.UP));
|
||||
vals.add(getDetail(dataSet, i, xVal, DataSet.Rounding.DOWN));
|
||||
}
|
||||
|
||||
return vals;
|
||||
}
|
||||
|
||||
protected SelectionDetail getDetails(IDataSet set, int dataSetIndex, float xVal, DataSet.Rounding rounding) {
|
||||
/**
|
||||
* Returns the SelectionDetail object corresponding to the selected xValue and dataSetIndex.
|
||||
*
|
||||
* @param set
|
||||
* @param dataSetIndex
|
||||
* @param xVal
|
||||
* @param rounding
|
||||
* @return
|
||||
*/
|
||||
protected SelectionDetail getDetail(IDataSet set, int dataSetIndex, float xVal, DataSet.Rounding rounding) {
|
||||
|
||||
final Entry e = set.getEntryForXPos(xVal, rounding);
|
||||
|
||||
|
@ -160,20 +167,22 @@ public class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider>
|
|||
* Returns the SelectionDetail of the DataSet that contains the closest value on the
|
||||
* y-axis.
|
||||
*
|
||||
* @param valsAtIndex all the values at a specific index
|
||||
* @param closestValues contains two values per DataSet closest to the selected x-position (determined by rounding up and
|
||||
* down)
|
||||
* @param x
|
||||
* @param y
|
||||
* @param axis the closest axis
|
||||
* @return
|
||||
*/
|
||||
public SelectionDetail getClosestSelectionDetailByPixel(
|
||||
List<SelectionDetail> valsAtIndex,
|
||||
float x, float y,
|
||||
YAxis.AxisDependency axis, float minSelectionDistance) {
|
||||
public SelectionDetail getClosestSelectionDetailByPixel(List<SelectionDetail> closestValues, float x, float y,
|
||||
YAxis.AxisDependency axis, float minSelectionDistance) {
|
||||
|
||||
SelectionDetail closest = null;
|
||||
float distance = minSelectionDistance;
|
||||
|
||||
for (int i = 0; i < valsAtIndex.size(); i++) {
|
||||
for (int i = 0; i < closestValues.size(); i++) {
|
||||
|
||||
SelectionDetail sel = valsAtIndex.get(i);
|
||||
SelectionDetail sel = closestValues.get(i);
|
||||
|
||||
if (axis == null || sel.dataSet.getAxisDependency() == axis) {
|
||||
|
||||
|
@ -189,7 +198,16 @@ public class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider>
|
|||
return closest;
|
||||
}
|
||||
|
||||
protected float getDistance(float x, float y, float selX, float selY) {
|
||||
return (float) Math.hypot(x - selX, y - selY);
|
||||
/**
|
||||
* Calculates the distance between the two given points.
|
||||
*
|
||||
* @param x1
|
||||
* @param y1
|
||||
* @param x2
|
||||
* @param y2
|
||||
* @return
|
||||
*/
|
||||
protected float getDistance(float x1, float y1, float x2, float y2) {
|
||||
return (float) Math.hypot(x1 - x2, y1 - y2);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,14 +19,8 @@ public class CombinedHighlighter extends ChartHighlighter<BarLineScatterCandleBu
|
|||
super(chart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of SelectionDetail object corresponding to the given xValue.
|
||||
*
|
||||
* @param xVal
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
protected List<SelectionDetail> getSelectionDetailsAtIndex(float xVal) {
|
||||
protected List<SelectionDetail> getSelectionDetailsAtXPos(float xVal) {
|
||||
|
||||
List<SelectionDetail> vals = new ArrayList<SelectionDetail>();
|
||||
|
||||
|
@ -45,11 +39,11 @@ public class CombinedHighlighter extends ChartHighlighter<BarLineScatterCandleBu
|
|||
if (!dataSet.isHighlightEnabled())
|
||||
continue;
|
||||
|
||||
SelectionDetail s1 = getDetails(dataSet, j, xVal, DataSet.Rounding.UP);
|
||||
SelectionDetail s1 = getDetail(dataSet, j, xVal, DataSet.Rounding.UP);
|
||||
s1.dataIndex = i;
|
||||
vals.add(s1);
|
||||
|
||||
SelectionDetail s2 = getDetails(dataSet, j, xVal, DataSet.Rounding.DOWN);
|
||||
SelectionDetail s2 = getDetail(dataSet, j, xVal, DataSet.Rounding.DOWN);
|
||||
s2.dataIndex = i;
|
||||
vals.add(s2);
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ public class HorizontalBarHighlighter extends BarHighlighter {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected SelectionDetail getDetails(IDataSet set, int dataSetIndex, float xVal, DataSet.Rounding rounding) {
|
||||
protected SelectionDetail getDetail(IDataSet set, int dataSetIndex, float xVal, DataSet.Rounding rounding) {
|
||||
|
||||
final Entry e = set.getEntryForXPos(xVal, rounding);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue