Remove SelectionDetail class

This commit is contained in:
Philipp Jahoda 2016-06-11 00:44:54 +02:00
parent f283de1698
commit 4b00e83479
10 changed files with 228 additions and 206 deletions

View file

@ -31,6 +31,7 @@ import com.github.mikephil.charting.animation.EasingFunction;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.DefaultValueFormatter;
@ -633,7 +634,7 @@ public abstract class Chart<T extends ChartData<? extends IDataSet<? extends Ent
} else {
if (this instanceof BarLineChartBase
&& ((BarLineChartBase) this).isHighlightFullBarEnabled())
high = new Highlight(high.getX(), Float.NaN, -1, -1);
high = new Highlight(high.getX(), Float.NaN, Float.NaN, Float.NaN, -1, YAxis.AxisDependency.LEFT);
// set the indices to highlight
mIndicesToHighlight = new Highlight[]{

View file

@ -17,9 +17,9 @@ import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
import com.github.mikephil.charting.listener.PieRadarChartTouchListener;
import com.github.mikephil.charting.utils.SelectionDetail;
import com.github.mikephil.charting.utils.Utils;
import java.util.ArrayList;
@ -27,22 +27,30 @@ import java.util.List;
/**
* Baseclass of PieChart and RadarChart.
*
*
* @author Philipp Jahoda
*/
public abstract class PieRadarChartBase<T extends ChartData<? extends IDataSet<? extends Entry>>>
extends Chart<T> {
/** holds the normalized version of the current rotation angle of the chart */
/**
* holds the normalized version of the current rotation angle of the chart
*/
private float mRotationAngle = 270f;
/** holds the raw version of the current rotation angle of the chart */
/**
* holds the raw version of the current rotation angle of the chart
*/
private float mRawRotationAngle = 270f;
/** flag that indicates if rotation is enabled or not */
/**
* flag that indicates if rotation is enabled or not
*/
protected boolean mRotateEnabled = true;
/** Sets the minimum offset (padding) around the chart, defaults to 0.f */
/**
* Sets the minimum offset (padding) around the chart, defaults to 0.f
*/
protected float mMinOffset = 0.f;
public PieRadarChartBase(Context context) {
@ -115,8 +123,7 @@ public abstract class PieRadarChartBase<T extends ChartData<? extends IDataSet<?
mLegend.getFormSize() + mLegend.getFormToTextSpace();
switch (mLegend.getOrientation()) {
case VERTICAL:
{
case VERTICAL: {
float xLegendOffset = 0.f;
if (mLegend.getHorizontalAlignment() == Legend.LegendHorizontalAlignment.LEFT
@ -182,7 +189,7 @@ public abstract class PieRadarChartBase<T extends ChartData<? extends IDataSet<?
break;
}
}
break;
break;
case HORIZONTAL:
float yLegendOffset = 0.f;
@ -247,7 +254,7 @@ public abstract class PieRadarChartBase<T extends ChartData<? extends IDataSet<?
* returns the angle relative to the chart center for the given point on the
* chart in degrees. The angle is always between 0 and 360°, 0° is NORTH,
* 90° is EAST, ...
*
*
* @param x
* @param y
* @return
@ -278,10 +285,10 @@ public abstract class PieRadarChartBase<T extends ChartData<? extends IDataSet<?
/**
* Calculates the position around a center point, depending on the distance
* from the center, and the angle of the position around the center.
*
*
* @param center
* @param dist
* @param angle in degrees, converted to radians internally
* @param angle in degrees, converted to radians internally
* @return
*/
protected PointF getPosition(PointF center, float dist, float angle) {
@ -329,7 +336,7 @@ public abstract class PieRadarChartBase<T extends ChartData<? extends IDataSet<?
/**
* Returns the xIndex for the given angle around the center of the chart.
* Returns -1 if not found / outofbounds.
*
*
* @param angle
* @return
*/
@ -338,7 +345,7 @@ public abstract class PieRadarChartBase<T extends ChartData<? extends IDataSet<?
/**
* Set an offset for the rotation of the RadarChart in degrees. Default 270f
* --> top (NORTH)
*
*
* @param angle
*/
public void setRotationAngle(float angle) {
@ -371,7 +378,7 @@ public abstract class PieRadarChartBase<T extends ChartData<? extends IDataSet<?
/**
* Set this to true to enable the rotation / spinning of the chart by touch.
* Set it to false to disable it. Default: true
*
*
* @param enabled
*/
public void setRotationEnabled(boolean enabled) {
@ -380,26 +387,30 @@ public abstract class PieRadarChartBase<T extends ChartData<? extends IDataSet<?
/**
* Returns true if rotation of the chart by touch is enabled, false if not.
*
*
* @return
*/
public boolean isRotationEnabled() {
return mRotateEnabled;
}
/** Gets the minimum offset (padding) around the chart, defaults to 0.f */
/**
* Gets the minimum offset (padding) around the chart, defaults to 0.f
*/
public float getMinOffset() {
return mMinOffset;
}
/** Sets the minimum offset (padding) around the chart, defaults to 0.f */
/**
* Sets the minimum offset (padding) around the chart, defaults to 0.f
*/
public void setMinOffset(float minOffset) {
mMinOffset = minOffset;
}
/**
* returns the diameter of the pie- or radar-chart
*
*
* @return
*/
public float getDiameter() {
@ -413,14 +424,14 @@ public abstract class PieRadarChartBase<T extends ChartData<? extends IDataSet<?
/**
* Returns the radius of the chart in pixels.
*
*
* @return
*/
public abstract float getRadius();
/**
* Returns the required offset for the chart legend.
*
*
* @return
*/
protected abstract float getRequiredLegendOffset();
@ -428,7 +439,7 @@ public abstract class PieRadarChartBase<T extends ChartData<? extends IDataSet<?
/**
* Returns the base offset needed for the chart without calculating the
* legend size.
*
*
* @return
*/
protected abstract float getRequiredBaseOffset();
@ -446,16 +457,16 @@ public abstract class PieRadarChartBase<T extends ChartData<? extends IDataSet<?
}
/**
* Returns an array of SelectionDetail objects for the given x-index. The SelectionDetail
* Returns an array of Highlight objects for the given x-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.
*
* @return
*/
public List<SelectionDetail> getSelectionDetailsAtIndex(int xIndex) {
public List<Highlight> getSelectionDetailsAtIndex(int xIndex) {
List<SelectionDetail> vals = new ArrayList<SelectionDetail>();
List<Highlight> vals = new ArrayList<Highlight>();
for (int i = 0; i < mData.getDataSetCount(); i++) {
@ -466,7 +477,7 @@ public abstract class PieRadarChartBase<T extends ChartData<? extends IDataSet<?
if (Float.isNaN(yVal))
continue;
vals.add(new SelectionDetail(0f, yVal, i, dataSet));
vals.add(new Highlight(0f, yVal, 0f, 0f, i, dataSet.getAxisDependency()));
}
return vals;
@ -479,7 +490,7 @@ public abstract class PieRadarChartBase<T extends ChartData<? extends IDataSet<?
/**
* Applys a spin animation to the Chart.
*
*
* @param durationmillis
* @param fromangle
* @param toangle

View file

@ -6,7 +6,6 @@ import com.github.mikephil.charting.data.BarLineScatterCandleBubbleData;
import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.PointD;
import com.github.mikephil.charting.utils.SelectionDetail;
/**
* Created by Philipp Jahoda on 22/07/15.
@ -24,37 +23,33 @@ public class BarHighlighter extends ChartHighlighter<BarDataProvider> {
PointD pos = getValsForTouch(x, y);
SelectionDetail selectionDetail = getSelectionDetail((float) pos.x, x, y);
if (selectionDetail == null)
Highlight high = getHighlightForX((float) pos.x, x, y);
if (high == null)
return null;
IBarDataSet set = barData.getDataSetByIndex(selectionDetail.dataSetIndex);
IBarDataSet set = barData.getDataSetByIndex(high.getDataSetIndex());
if (set.isStacked()) {
return getStackedHighlight(selectionDetail,
return getStackedHighlight(high,
set,
(float) pos.x,
(float) pos.y);
}
return new Highlight(
selectionDetail.xValue,
selectionDetail.yValue,
selectionDetail.dataIndex,
selectionDetail.dataSetIndex);
return high;
}
/**
* This method creates the Highlight object that also indicates which value of a stacked BarEntry has been
* selected.
*
* @param selectionDetail the selection detail to work with looking for stacked values
* @param high the Highlight to work with looking for stacked values
* @param set
* @param xVal
* @param yValue
* @param yVal
* @return
*/
protected Highlight getStackedHighlight(SelectionDetail selectionDetail, IBarDataSet set, float xVal, double yValue) {
protected Highlight getStackedHighlight(Highlight high, IBarDataSet set, float xVal, float yVal) {
BarEntry entry = set.getEntryForXPos(xVal);
@ -63,23 +58,26 @@ public class BarHighlighter extends ChartHighlighter<BarDataProvider> {
// not stacked
if (entry.getYVals() == null) {
return new Highlight(entry.getX(),
entry.getY(),
selectionDetail.dataIndex,
selectionDetail.dataSetIndex);
return high;
} else {
Range[] ranges = getRanges(entry);
if (ranges.length > 0) {
int stackIndex = getClosestStackIndex(ranges, (float) yValue);
return new Highlight(
int stackIndex = getClosestStackIndex(ranges, yVal);
Highlight stackedHigh = new Highlight(
entry.getX(),
entry.getPositiveSum() - entry.getNegativeSum(),
selectionDetail.dataIndex,
selectionDetail.dataSetIndex,
high.getXPx(),
high.getYPx(),
high.getDataSetIndex(),
stackIndex,
ranges[stackIndex]
ranges[stackIndex],
high.getAxis()
);
return stackedHigh;
}
}

View file

@ -5,13 +5,11 @@ import java.util.List;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarLineScatterCandleBubbleData;
import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.data.DataSet;
import com.github.mikephil.charting.data.Entry;
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;
/**
* Created by Philipp Jahoda on 21/07/15.
@ -32,14 +30,8 @@ public class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider>
float xVal = (float) getValsForTouch(x, y).x;
SelectionDetail selectionDetail = getSelectionDetail(xVal, x, y);
if (selectionDetail == null)
return null;
return new Highlight(selectionDetail.xValue,
selectionDetail.yValue,
selectionDetail.dataIndex,
selectionDetail.dataSetIndex);
Highlight high = getHighlightForX(xVal, x, y);
return high;
}
/**
@ -56,22 +48,23 @@ public class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider>
}
/**
* Returns the corresponding SelectionDetail for a given xVal and y-touch position in pixels.
* Returns the corresponding Highlight for a given xVal and x- and y-touch position in pixels.
*
* @param xVal
* @param x
* @param y
* @return
*/
protected SelectionDetail getSelectionDetail(float xVal, float x, float y) {
protected Highlight getHighlightForX(float xVal, float x, float y) {
List<SelectionDetail> closestValues = getSelectionDetailsAtXPos(xVal);
List<Highlight> closestValues = getHighlightsAtXPos(xVal);
float leftAxisMinDist = getMinimumDistance(closestValues, y, YAxis.AxisDependency.LEFT);
float rightAxisMinDist = getMinimumDistance(closestValues, y, YAxis.AxisDependency.RIGHT);
YAxis.AxisDependency axis = leftAxisMinDist < rightAxisMinDist ? YAxis.AxisDependency.LEFT : YAxis.AxisDependency.RIGHT;
SelectionDetail detail = getClosestSelectionDetailByPixel(closestValues, x, y, axis, mChart.getMaxHighlightDistance());
Highlight detail = getClosestHighlightByPixel(closestValues, x, y, axis, mChart.getMaxHighlightDistance());
return detail;
}
@ -85,17 +78,17 @@ public class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider>
* @param axis
* @return
*/
protected float getMinimumDistance(List<SelectionDetail> closestValues, float pos, YAxis.AxisDependency axis) {
protected float getMinimumDistance(List<Highlight> closestValues, float pos, YAxis.AxisDependency axis) {
float distance = Float.MAX_VALUE;
for (int i = 0; i < closestValues.size(); i++) {
SelectionDetail sel = closestValues.get(i);
Highlight high = closestValues.get(i);
if (sel.dataSet.getAxisDependency() == axis) {
if (high.getAxis() == axis) {
float tempDistance = Math.abs(getSelectionPos(sel) - pos);
float tempDistance = Math.abs(getHighlightPos(high) - pos);
if (tempDistance < distance) {
distance = tempDistance;
}
@ -105,20 +98,20 @@ public class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider>
return distance;
}
protected float getSelectionPos(SelectionDetail sel) {
return sel.yPx;
protected float getHighlightPos(Highlight h) {
return h.getYPx();
}
/**
* Returns a list of SelectionDetail objects representing the entries closest to the given xVal.
* Returns a list of Highlight 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> getSelectionDetailsAtXPos(float xVal) {
protected List<Highlight> getHighlightsAtXPos(float xVal) {
List<SelectionDetail> vals = new ArrayList<SelectionDetail>();
List<Highlight> vals = new ArrayList<Highlight>();
BarLineScatterCandleBubbleData data = getData();
@ -133,15 +126,15 @@ public class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider>
if (!dataSet.isHighlightEnabled())
continue;
vals.add(getDetail(dataSet, i, xVal, DataSet.Rounding.UP));
vals.add(getDetail(dataSet, i, xVal, DataSet.Rounding.DOWN));
vals.add(buildHighlight(dataSet, i, xVal, DataSet.Rounding.UP));
vals.add(buildHighlight(dataSet, i, xVal, DataSet.Rounding.DOWN));
}
return vals;
}
/**
* Returns the SelectionDetail object corresponding to the selected xValue and dataSetIndex.
* Returns the Highlight object corresponding to the selected xValue and dataSetIndex.
*
* @param set
* @param dataSetIndex
@ -149,7 +142,7 @@ public class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider>
* @param rounding
* @return
*/
protected SelectionDetail getDetail(IDataSet set, int dataSetIndex, float xVal, DataSet.Rounding rounding) {
protected Highlight buildHighlight(IDataSet set, int dataSetIndex, float xVal, DataSet.Rounding rounding) {
final Entry e = set.getEntryForXPos(xVal, rounding);
@ -158,36 +151,37 @@ public class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider>
PointD pixels = mChart.getTransformer(set.getAxisDependency()).getPixelsForValues(e.getX(), e.getY());
return new SelectionDetail((float) pixels.x, (float) pixels.y, e.getX(), e.getY(), dataSetIndex, set);
return new Highlight(e.getX(), e.getY(), (float) pixels.x, (float) pixels.y, dataSetIndex, set.getAxisDependency());
}
/**
* Returns the SelectionDetail of the DataSet that contains the closest value on the
* Returns the Highlight of the DataSet that contains the closest value on the
* y-axis.
*
* @param closestValues contains two values per DataSet closest to the selected x-position (determined by rounding up and
* down)
* @param closestValues contains two Highlight objects per DataSet closest to the selected x-position (determined by
* rounding up an down)
* @param x
* @param y
* @param axis the closest axis
* @param axis the closest axis
* @param minSelectionDistance
* @return
*/
public SelectionDetail getClosestSelectionDetailByPixel(List<SelectionDetail> closestValues, float x, float y,
YAxis.AxisDependency axis, float minSelectionDistance) {
public Highlight getClosestHighlightByPixel(List<Highlight> closestValues, float x, float y,
YAxis.AxisDependency axis, float minSelectionDistance) {
SelectionDetail closest = null;
Highlight closest = null;
float distance = minSelectionDistance;
for (int i = 0; i < closestValues.size(); i++) {
SelectionDetail sel = closestValues.get(i);
Highlight high = closestValues.get(i);
if (axis == null || sel.dataSet.getAxisDependency() == axis) {
if (axis == null || high.getAxis() == axis) {
float cDistance = getDistance(x, y, sel.xPx, sel.yPx);
float cDistance = getDistance(x, y, high.getXPx(), high.getYPx());
if (cDistance < distance) {
closest = sel;
closest = high;
distance = cDistance;
}
}

View file

@ -6,7 +6,6 @@ import com.github.mikephil.charting.data.DataSet;
import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
import com.github.mikephil.charting.interfaces.dataprovider.BarLineScatterCandleBubbleDataProvider;
import com.github.mikephil.charting.utils.SelectionDetail;
import java.util.ArrayList;
import java.util.List;
@ -16,30 +15,47 @@ import java.util.List;
*/
public class CombinedHighlighter extends ChartHighlighter<BarLineScatterCandleBubbleDataProvider> implements Highlighter {
/**
* bar highlighter for supporting stacked highlighting
*/
protected BarHighlighter barHighlighter;
public CombinedHighlighter(BarLineScatterCandleBubbleDataProvider chart, BarDataProvider barChart) {
super(chart);
barHighlighter = new BarHighlighter(barChart);
barHighlighter = barChart.getBarData() == null ? null : new BarHighlighter(barChart);
}
@Override
public Highlight getHighlight(float x, float y) {
Highlight h1 = super.getHighlight(x, y);
Highlight h2 = barHighlighter.getHighlight(x, y);
Highlight h2 = barHighlighter == null ? null : barHighlighter.getHighlight(x, y);
return getClosest(x, y, h1, h2);
return (h2 != null && h2.isStacked()) ? h2 : h1;
}
protected Highlight getClosest(float x, float y, Highlight... highs) {
return null;
}
// protected Highlight getClosest(float x, float y, Highlight... highs) {
//
// Highlight closest = null;
// float minDistance = Float.MAX_VALUE;
//
// for (Highlight high : highs) {
//
// float tempDistance = getDistance(x, y, high.getXPx(), high.getYPx());
//
// if (tempDistance < minDistance) {
// minDistance = tempDistance;
// closest = high;
// }
// }
//
// return closest;
// }
@Override
protected List<SelectionDetail> getSelectionDetailsAtXPos(float xVal) {
protected List<Highlight> getHighlightsAtXPos(float xVal) {
List<SelectionDetail> vals = new ArrayList<SelectionDetail>();
List<Highlight> vals = new ArrayList<Highlight>();
CombinedData data = (CombinedData) mChart.getData();
@ -56,12 +72,12 @@ public class CombinedHighlighter extends ChartHighlighter<BarLineScatterCandleBu
if (!dataSet.isHighlightEnabled())
continue;
SelectionDetail s1 = getDetail(dataSet, j, xVal, DataSet.Rounding.UP);
s1.dataIndex = i;
Highlight s1 = buildHighlight(dataSet, j, xVal, DataSet.Rounding.UP);
s1.setDataIndex(i);
vals.add(s1);
SelectionDetail s2 = getDetail(dataSet, j, xVal, DataSet.Rounding.DOWN);
s2.dataIndex = i;
Highlight s2 = buildHighlight(dataSet, j, xVal, DataSet.Rounding.DOWN);
s2.setDataIndex(i);
vals.add(s2);
}
}

View file

@ -1,77 +1,96 @@
package com.github.mikephil.charting.highlight;
import com.github.mikephil.charting.components.YAxis;
/**
* Contains information needed to determine the highlighted value.
*
*
* @author Philipp Jahoda
*/
public class Highlight {
/** the x-value of the highlighted value */
/**
* the x-value of the highlighted value
*/
private float mX = Float.NaN;
/** the y-value of the highlighted value */
/**
* the y-value of the highlighted value
*/
private float mY = Float.NaN;
/** the x-pixel of the highlight */
/**
* the x-pixel of the highlight
*/
private float mXPx;
/** the y-pixel of the highlight */
/**
* the y-pixel of the highlight
*/
private float mYPx;
/** the index of the data object - in case it refers to more than one */
/**
* the index of the data object - in case it refers to more than one
*/
private int mDataIndex;
/** the index of the dataset the highlighted value is in */
/**
* the index of the dataset the highlighted value is in
*/
private int mDataSetIndex;
/** index which value of a stacked bar entry is highlighted, default -1 */
/**
* index which value of a stacked bar entry is highlighted, default -1
*/
private int mStackIndex = -1;
/** the range of the bar that is selected (only for stacked-barchart) */
/**
* the range of the bar that is selected (only for stacked-barchart)
*/
private Range mRange;
/**
* constructor
*
* @param x the x-value of the highlighted value
* @param y the y-value of the highlighted value
* @param dataIndex the index of the Data the highlighted value belongs to
* @param dataSetIndex the index of the DataSet the highlighted value belongs to
* the axis the highlighted value belongs to
*/
public Highlight(float x, float y, int dataIndex, int dataSetIndex) {
private YAxis.AxisDependency axis;
public Highlight(float x, int dataSetIndex) {
this.mX = x;
this.mY = y;
this.mDataIndex = dataIndex;
this.mDataSetIndex = dataSetIndex;
}
/**
* Constructor, only used for stacked-barchart.
* constructor
*
* @param x the index of the highlighted value on the x-axis
* @param y the y-value of the highlighted value
* @param dataIndex the index of the Data the highlighted value belongs to
* @param x the x-value of the highlighted value
* @param y the y-value of the highlighted value
* @param dataSetIndex the index of the DataSet the highlighted value belongs to
* @param stackIndex references which value of a stacked-bar entry has been
* selected
* @param range the range the selected stack-value is in
*/
public Highlight(float x, float y, int dataIndex, int dataSetIndex, int stackIndex, Range range) {
this(x, y, dataIndex, dataSetIndex);
this.mStackIndex = stackIndex;
this.mRange = range;
public Highlight(float x, float y, float xPx, float yPx, int dataSetIndex, YAxis.AxisDependency axis) {
this.mX = x;
this.mY = y;
this.mXPx = xPx;
this.mYPx = yPx;
this.mDataSetIndex = dataSetIndex;
this.axis = axis;
}
/**
* Constructor, only used for stacked-barchart.
*
* @param x the x-value of the highlighted value on the x-axis
* @param x the index of the highlighted value on the x-axis
* @param y the y-value of the highlighted value
* @param dataSetIndex the index of the DataSet the highlighted value belongs to
* @param stackIndex references which value of a stacked-bar entry has been
* selected
* @param range the range the selected stack-value is in
*/
public Highlight(float x, int dataSetIndex) {
this(x, Float.NaN, 0, dataSetIndex);
public Highlight(float x, float y, float xPx, float yPx, int dataSetIndex, int stackIndex, Range range,
YAxis.AxisDependency axis) {
this(x, y, xPx, yPx, dataSetIndex, axis);
this.mStackIndex = stackIndex;
this.mRange = range;
}
/**
@ -92,6 +111,20 @@ public class Highlight {
return mY;
}
/**
* returns the x-position of the highlight in pixels
*/
public float getXPx() {
return mXPx;
}
/**
* returns the y-position of the highlight in pixels
*/
public float getYPx() {
return mYPx;
}
/**
* the index of the data object - in case it refers to more than one
*
@ -101,6 +134,10 @@ public class Highlight {
return mDataIndex;
}
public void setDataIndex(int mDataIndex) {
this.mDataIndex = mDataIndex;
}
/**
* returns the index of the DataSet the highlighted value is in
*
@ -113,7 +150,7 @@ public class Highlight {
/**
* Only needed if a stacked-barchart entry was highlighted. References the
* selected value within the stacked-entry.
*
*
* @return
*/
public int getStackIndex() {
@ -122,16 +159,30 @@ public class Highlight {
/**
* Returns the range of values the selected value of a stacked bar is in. (this is only relevant for stacked-barchart)
*
* @return
*/
public Range getRange() {
return mRange;
}
public boolean isStacked() {
return mStackIndex >= 0;
}
/**
* Returns the axis the highlighted value belongs to.
*
* @return
*/
public YAxis.AxisDependency getAxis() {
return axis;
}
/**
* Returns true if this highlight object is equal to the other (compares
* xIndex and dataSetIndex)
*
*
* @param h
* @return
*/

View file

@ -1,6 +1,5 @@
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.DataSet;
import com.github.mikephil.charting.data.Entry;
@ -8,10 +7,6 @@ import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
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.
@ -29,38 +24,34 @@ public class HorizontalBarHighlighter extends BarHighlighter {
PointD pos = getValsForTouch(y, x);
SelectionDetail selectionDetail = getSelectionDetail((float) pos.y, y, x);
if (selectionDetail == null)
Highlight high = getHighlightForX((float) pos.y, y, x);
if (high == null)
return null;
IBarDataSet set = barData.getDataSetByIndex(selectionDetail.dataSetIndex);
IBarDataSet set = barData.getDataSetByIndex(high.getDataSetIndex());
if (set.isStacked()) {
return getStackedHighlight(selectionDetail,
return getStackedHighlight(high,
set,
(float) pos.y,
(float) pos.x);
}
return new Highlight(
selectionDetail.xValue,
selectionDetail.yValue,
selectionDetail.dataIndex,
selectionDetail.dataSetIndex);
return high;
}
@Override
protected SelectionDetail getDetail(IDataSet set, int dataSetIndex, float xVal, DataSet.Rounding rounding) {
protected Highlight buildHighlight(IDataSet set, int dataSetIndex, float xVal, DataSet.Rounding rounding) {
final Entry e = set.getEntryForXPos(xVal, rounding);
PointD pixels = mChart.getTransformer(set.getAxisDependency()).getPixelsForValues(e.getY(), e.getX());
return new SelectionDetail((float) pixels.x, (float) pixels.y, e.getX(), e.getY(), dataSetIndex, set);
return new Highlight(e.getX(), e.getY(), (float) pixels.x, (float) pixels.y, dataSetIndex, set.getAxisDependency());
}
@Override
protected float getDistance(float x, float y, float selX, float selY) {
return Math.abs(y - selY);
protected float getDistance(float x1, float y1, float x2, float y2) {
return Math.abs(y1 - y2);
}
}

View file

@ -11,7 +11,6 @@ import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.charts.PieRadarChartBase;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.SelectionDetail;
import com.github.mikephil.charting.utils.Utils;
import java.util.ArrayList;
@ -183,7 +182,7 @@ public class PieRadarChartTouchListener extends ChartTouchListener<PieRadarChart
} else {
List<SelectionDetail> valsAtIndex = mChart.getSelectionDetailsAtIndex(index);
List<Highlight> valsAtIndex = mChart.getSelectionDetailsAtIndex(index);
int dataSetIndex = 0;

View file

@ -1,40 +0,0 @@
package com.github.mikephil.charting.utils;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
/**
* Class that encapsulates information of a value that has been
* selected/highlighted and its DataSet index. The SelectionDetail objects give
* information about the value at the selected index and the DataSet it belongs
* to. Needed only for highlighting onTouch().
*
* @author Philipp Jahoda
*/
public class SelectionDetail {
public float yPx;
public float xPx;
public float yValue;
public float xValue;
public int dataIndex;
public int dataSetIndex;
public IDataSet dataSet;
public SelectionDetail(float xPx, float yPx, float xValue, float yValue, int dataIndex, int dataSetIndex, IDataSet set) {
this.xPx = xPx;
this.yPx = yPx;
this.xValue = xValue;
this.yValue = yValue;
this.dataIndex = dataIndex;
this.dataSetIndex = dataSetIndex;
this.dataSet = set;
}
public SelectionDetail(float xPx, float yPx, float xValue, float yValue, int dataSetIndex, IDataSet set) {
this(xPx, yPx, xValue, yValue, 0, dataSetIndex, set);
}
public SelectionDetail(float xValue, float yValue, int dataSetIndex, IDataSet set) {
this(Float.NaN, Float.NaN, xValue, yValue, 0, dataSetIndex, set);
}
}

View file

@ -22,6 +22,7 @@ import android.view.ViewConfiguration;
import com.github.mikephil.charting.components.YAxis.AxisDependency;
import com.github.mikephil.charting.formatter.DefaultValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import java.util.List;
@ -388,15 +389,15 @@ public abstract class Utils {
* @param valsAtIndex all the values at a specific index
* @return
*/
public static int getClosestDataSetIndexByValue(List<SelectionDetail> valsAtIndex, float value,
AxisDependency axis) {
public static int getClosestDataSetIndexByValue(List<Highlight> valsAtIndex, float value,
AxisDependency axis) {
SelectionDetail sel = getClosestSelectionDetailByValue(valsAtIndex, value, axis);
Highlight sel = getClosestSelectionDetailByValue(valsAtIndex, value, axis);
if (sel == null)
return -Integer.MAX_VALUE;
return sel.dataSetIndex;
return sel.getDataSetIndex();
}
/**
@ -406,21 +407,21 @@ public abstract class Utils {
* @param valsAtIndex all the values at a specific index
* @return
*/
public static SelectionDetail getClosestSelectionDetailByValue(
List<SelectionDetail> valsAtIndex,
public static Highlight getClosestSelectionDetailByValue(
List<Highlight> valsAtIndex,
float value,
AxisDependency axis) {
SelectionDetail closest = null;
Highlight closest = null;
float distance = Float.MAX_VALUE;
for (int i = 0; i < valsAtIndex.size(); i++) {
SelectionDetail sel = valsAtIndex.get(i);
Highlight sel = valsAtIndex.get(i);
if (axis == null || sel.dataSet.getAxisDependency() == axis) {
if (axis == null || sel.getAxis() == axis) {
float cdistance = Math.abs(sel.yValue - value);
float cdistance = Math.abs(sel.getY() - value);
if (cdistance < distance) {
closest = sel;
distance = cdistance;