Java源码示例:com.github.mikephil.charting.utils.SelectionDetail
示例1
@Override
protected SelectionDetail getSelectionDetail(int xIndex, float y, int dataSetIndex) {
dataSetIndex = Math.max(dataSetIndex, 0);
BarData barData = mChart.getBarData();
IDataSet dataSet = barData.getDataSetCount() > dataSetIndex
? barData.getDataSetByIndex(dataSetIndex)
: null;
if (dataSet == null)
return null;
final float yValue = dataSet.getYValForXIndex(xIndex);
if (yValue == Double.NaN) return null;
return new SelectionDetail(
yValue,
dataSetIndex,
dataSet);
}
示例2
/**
* 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 set
* @param xIndex
* @param yValue
* @return
*/
protected Highlight getStackedHighlight(
SelectionDetail selectionDetail,
IBarDataSet set,
int xIndex,
double yValue) {
BarEntry entry = set.getEntryForXIndex(xIndex);
if (entry == null)
return null;
if (entry.getVals() == null) {
return new Highlight(xIndex,
entry.getVal(),
selectionDetail.dataIndex,
selectionDetail.dataSetIndex);
}
Range[] ranges = getRanges(entry);
if (ranges.length > 0) {
int stackIndex = getClosestStackIndex(ranges, (float)yValue);
return new Highlight(
xIndex,
entry.getPositiveSum() - entry.getNegativeSum(),
selectionDetail.dataIndex,
selectionDetail.dataSetIndex,
stackIndex,
ranges[stackIndex]
);
}
return null;
}
示例3
/**
* Returns a list of SelectionDetail object corresponding to the given xIndex.
*
* @param xIndex
* @return
*/
@Override
protected List<SelectionDetail> getSelectionDetailsAtIndex(int xIndex, int dataSetIndex) {
List<SelectionDetail> vals = new ArrayList<SelectionDetail>();
float[] pts = new float[2];
CombinedData data = (CombinedData) mChart.getData();
// get all chartdata objects
List<ChartData> dataObjects = data.getAllData();
for (int i = 0; i < dataObjects.size(); i++) {
for(int j = 0; j < dataObjects.get(i).getDataSetCount(); j++) {
IDataSet dataSet = dataObjects.get(i).getDataSetByIndex(j);
// dont include datasets that cannot be highlighted
if (!dataSet.isHighlightEnabled())
continue;
// extract all y-values from all DataSets at the given x-index
final float yVals[] = dataSet.getYValsForXIndex(xIndex);
for (float yVal : yVals) {
pts[1] = yVal;
mChart.getTransformer(dataSet.getAxisDependency()).pointValuesToPixel(pts);
if (!Float.isNaN(pts[1])) {
vals.add(new SelectionDetail(pts[1], yVal, i, j, dataSet));
}
}
}
}
return vals;
}
示例4
/**
* Returns a Highlight object corresponding to the given x- and y- touch positions in pixels.
*
* @param x
* @param y
* @return
*/
public Highlight getHighlight(float x, float y) {
int xIndex = getXIndex(x);
SelectionDetail selectionDetail = getSelectionDetail(xIndex, y, -1);
if (selectionDetail == null)
return null;
return new Highlight(xIndex,
selectionDetail.value,
selectionDetail.dataIndex,
selectionDetail.dataSetIndex);
}
示例5
/**
* Returns an array of SelectionDetail objects for the given x-index. The SelectionDetail
* 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) {
List<SelectionDetail> vals = new ArrayList<SelectionDetail>();
for (int i = 0; i < mData.getDataSetCount(); i++) {
DataSet<?> dataSet = mData.getDataSetByIndex(i);
// extract all y-values from all DataSets at the given x-index
final float yVal = dataSet.getYValForXIndex(xIndex);
if (yVal == Float.NaN)
continue;
vals.add(new SelectionDetail(yVal, i, dataSet));
}
return vals;
}
示例6
@Override
public boolean onSingleTapUp(MotionEvent e) {
OnChartGestureListener l = mChart.getOnChartGestureListener();
if (l != null) {
l.onChartSingleTapped(e);
}
float distance = mChart.distanceToCenter(e.getX(), e.getY());
// check if a slice was touched
if (distance > mChart.getRadius()) {
// if no slice was touched, highlight nothing
mChart.highlightValues(null);
mLastHighlighted = null;
} else {
float angle = mChart.getAngleForPoint(e.getX(), e.getY());
if (mChart instanceof PieChart) {
angle /= mChart.getAnimator().getPhaseY();
}
int index = mChart.getIndexForAngle(angle);
// check if the index could be found
if (index < 0) {
mChart.highlightValues(null);
mLastHighlighted = null;
} else {
List<SelectionDetail> valsAtIndex = mChart.getSelectionDetailsAtIndex(index);
int dataSetIndex = 0;
// get the dataset that is closest to the selection (PieChart
// only
// has one DataSet)
if (mChart instanceof RadarChart) {
dataSetIndex = Utils.getClosestDataSetIndex(valsAtIndex, distance
/ ((RadarChart) mChart).getFactor(), null);
}
if (dataSetIndex < 0) {
mChart.highlightValues(null);
mLastHighlighted = null;
} else {
Highlight h = new Highlight(index, dataSetIndex);
if (h.equalTo(mLastHighlighted)) {
mChart.highlightTouch(null);
mLastHighlighted = null;
} else {
mChart.highlightTouch(h);
mLastHighlighted = h;
}
}
}
}
return true;
}
示例7
/**
* Returns an array of SelectionDetail objects for the given x-index. The SelectionDetail
* 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) {
List<SelectionDetail> vals = new ArrayList<SelectionDetail>();
for (int i = 0; i < mData.getDataSetCount(); i++) {
IDataSet<?> dataSet = mData.getDataSetByIndex(i);
// extract all y-values from all DataSets at the given x-index
final float yVal = dataSet.getYValForXIndex(xIndex);
if (yVal == Float.NaN)
continue;
vals.add(new SelectionDetail(yVal, i, dataSet));
}
return vals;
}
示例8
@Override
public boolean onSingleTapUp(MotionEvent e) {
mLastGesture = ChartGesture.SINGLE_TAP;
OnChartGestureListener l = mChart.getOnChartGestureListener();
if (l != null) {
l.onChartSingleTapped(e);
}
if(!mChart.isHighlightPerTapEnabled()) {
return false;
}
float distance = mChart.distanceToCenter(e.getX(), e.getY());
// check if a slice was touched
if (distance > mChart.getRadius()) {
// if no slice was touched, highlight nothing
if (mLastHighlighted == null)
mChart.highlightValues(null); // no listener callback
else
mChart.highlightTouch(null); // listener callback
mLastHighlighted = null;
} else {
float angle = mChart.getAngleForPoint(e.getX(), e.getY());
if (mChart instanceof PieChart) {
angle /= mChart.getAnimator().getPhaseY();
}
int index = mChart.getIndexForAngle(angle);
// check if the index could be found
if (index < 0) {
mChart.highlightValues(null);
mLastHighlighted = null;
} else {
List<SelectionDetail> valsAtIndex = mChart.getSelectionDetailsAtIndex(index);
int dataSetIndex = 0;
// get the dataset that is closest to the selection (PieChart
// only
// has one DataSet)
if (mChart instanceof RadarChart) {
dataSetIndex = Utils.getClosestDataSetIndex(valsAtIndex, distance
/ ((RadarChart) mChart).getFactor(), null);
}
if (dataSetIndex < 0) {
mChart.highlightValues(null);
mLastHighlighted = null;
} else {
Highlight h = new Highlight(index, dataSetIndex);
performHighlight(h, e);
}
}
}
return true;
}
示例9
/**
* Returns an array of SelectionDetail objects for the given x-index. The SelectionDetail
* 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) {
List<SelectionDetail> vals = new ArrayList<SelectionDetail>();
for (int i = 0; i < mData.getDataSetCount(); i++) {
IDataSet<?> dataSet = mData.getDataSetByIndex(i);
// extract all y-values from all DataSets at the given x-index
final float yVal = dataSet.getYValForXIndex(xIndex);
if (Float.isNaN(yVal))
continue;
vals.add(new SelectionDetail(yVal, i, dataSet));
}
return vals;
}
示例10
@Override
public boolean onSingleTapUp(MotionEvent e) {
mLastGesture = ChartGesture.SINGLE_TAP;
OnChartGestureListener l = mChart.getOnChartGestureListener();
if (l != null) {
l.onChartSingleTapped(e);
}
if(!mChart.isHighlightPerTapEnabled()) {
return false;
}
float distance = mChart.distanceToCenter(e.getX(), e.getY());
// check if a slice was touched
if (distance > mChart.getRadius()) {
// if no slice was touched, highlight nothing
if (mLastHighlighted == null)
mChart.highlightValues(null); // no listener callback
else
mChart.highlightTouch(null); // listener callback
mLastHighlighted = null;
} else {
float angle = mChart.getAngleForPoint(e.getX(), e.getY());
if (mChart instanceof PieChart) {
angle /= mChart.getAnimator().getPhaseY();
}
int index = mChart.getIndexForAngle(angle);
// check if the index could be found
if (index < 0) {
mChart.highlightValues(null);
mLastHighlighted = null;
} else {
List<SelectionDetail> valsAtIndex = mChart.getSelectionDetailsAtIndex(index);
int dataSetIndex = 0;
// get the dataset that is closest to the selection (PieChart
// only
// has one DataSet)
if (mChart instanceof RadarChart) {
dataSetIndex = Utils.getClosestDataSetIndexByValue(
valsAtIndex,
distance / ((RadarChart) mChart).getFactor(),
null);
}
if (dataSetIndex < 0) {
mChart.highlightValues(null);
mLastHighlighted = null;
} else {
Highlight h = new Highlight(index, dataSetIndex);
performHighlight(h, e);
}
}
}
return true;
}
示例11
@Override
public Highlight getHighlight(float x, float y) {
BarData barData = mChart.getBarData();
final int xIndex = getXIndex(x);
final float baseNoSpace = getBase(x);
final int setCount = barData.getDataSetCount();
int dataSetIndex = ((int)baseNoSpace) % setCount;
if (dataSetIndex < 0) {
dataSetIndex = 0;
} else if (dataSetIndex >= setCount) {
dataSetIndex = setCount - 1;
}
SelectionDetail selectionDetail = getSelectionDetail(xIndex, y, dataSetIndex);
if (selectionDetail == null)
return null;
IBarDataSet set = barData.getDataSetByIndex(dataSetIndex);
if (set.isStacked()) {
float[] pts = new float[2];
pts[1] = y;
// take any transformer to determine the x-axis value
mChart.getTransformer(set.getAxisDependency()).pixelsToValue(pts);
return getStackedHighlight(selectionDetail,
set,
xIndex,
pts[1]);
}
return new Highlight(
xIndex,
selectionDetail.value,
selectionDetail.dataIndex,
selectionDetail.dataSetIndex,
-1);
}
示例12
/**
* Returns a list of SelectionDetail object corresponding to the given xIndex.
*
* @param xIndex
* @param dataSetIndex dataSet index to look at. -1 if unspecified.
* @return
*/
protected List<SelectionDetail> getSelectionDetailsAtIndex(int xIndex, int dataSetIndex) {
List<SelectionDetail> vals = new ArrayList<SelectionDetail>();
if (mChart.getData() == null) return vals;
float[] pts = new float[2];
for (int i = 0, dataSetCount = mChart.getData().getDataSetCount();
i < dataSetCount;
i++) {
if (dataSetIndex > -1 && dataSetIndex != i)
continue;
IDataSet dataSet = mChart.getData().getDataSetByIndex(i);
// dont include datasets that cannot be highlighted
if (!dataSet.isHighlightEnabled())
continue;
// extract all y-values from all DataSets at the given x-index
final float[] yVals = dataSet.getYValsForXIndex(xIndex);
for (float yVal : yVals) {
if (Float.isNaN(yVal))
continue;
pts[1] = yVal;
mChart.getTransformer(dataSet.getAxisDependency()).pointValuesToPixel(pts);
if (!Float.isNaN(pts[1]))
{
vals.add(new SelectionDetail(pts[1], yVal, i, dataSet));
}
}
}
return vals;
}
示例13
@Override
public Highlight getHighlight(float x, float y) {
BarData barData = mChart.getBarData();
final int xIndex = getXIndex(x);
final float baseNoSpace = getBase(x);
final int setCount = barData.getDataSetCount();
int dataSetIndex = ((int)baseNoSpace) % setCount;
if (dataSetIndex < 0) {
dataSetIndex = 0;
} else if (dataSetIndex >= setCount) {
dataSetIndex = setCount - 1;
}
SelectionDetail selectionDetail = getSelectionDetail(xIndex, y, dataSetIndex);
if (selectionDetail == null)
return null;
IBarDataSet set = barData.getDataSetByIndex(dataSetIndex);
if (set.isStacked()) {
float[] pts = new float[2];
pts[0] = y;
// take any transformer to determine the x-axis value
mChart.getTransformer(set.getAxisDependency()).pixelsToValue(pts);
return getStackedHighlight(selectionDetail,
set,
xIndex,
pts[0]);
}
return new Highlight(
xIndex,
selectionDetail.value,
selectionDetail.dataIndex,
selectionDetail.dataSetIndex,
-1);
}
示例14
/**
* Returns a list of SelectionDetail object corresponding to the given xIndex.
*
* @param xIndex
* @return
*/
@Override
protected List<SelectionDetail> getSelectionDetailsAtIndex(int xIndex) {
CombinedData data = (CombinedData) mChart.getData();
// get all chartdata objects
List<ChartData> dataObjects = data.getAllData();
List<SelectionDetail> vals = new ArrayList<SelectionDetail>();
float[] pts = new float[2];
for (int i = 0; i < dataObjects.size(); i++) {
for(int j = 0; j < dataObjects.get(i).getDataSetCount(); j++) {
DataSet<?> dataSet = dataObjects.get(i).getDataSetByIndex(j);
// dont include datasets that cannot be highlighted
if (!dataSet.isHighlightEnabled())
continue;
// extract all y-values from all DataSets at the given x-index
final float yVal = dataSet.getYValForXIndex(xIndex);
if (yVal == Float.NaN)
continue;
pts[1] = yVal;
mChart.getTransformer(dataSet.getAxisDependency()).pointValuesToPixel(pts);
if (!Float.isNaN(pts[1])) {
vals.add(new SelectionDetail(pts[1], j, dataSet));
}
}
}
return vals;
}
示例15
/**
* Returns the corresponding dataset-index for a given xIndex and xy-touch position in pixels.
*
* @param xIndex
* @param x
* @param y
* @return
*/
protected int getDataSetIndex(int xIndex, float x, float y) {
List<SelectionDetail> valsAtIndex = getSelectionDetailsAtIndex(xIndex);
float leftdist = Utils.getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.LEFT);
float rightdist = Utils.getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.RIGHT);
YAxis.AxisDependency axis = leftdist < rightdist ? YAxis.AxisDependency.LEFT : YAxis.AxisDependency.RIGHT;
int dataSetIndex = Utils.getClosestDataSetIndex(valsAtIndex, y, axis);
return dataSetIndex;
}
示例16
/**
* Returns a list of SelectionDetail object corresponding to the given xIndex.
*
* @param xIndex
* @return
*/
protected List<SelectionDetail> getSelectionDetailsAtIndex(int xIndex) {
List<SelectionDetail> vals = new ArrayList<SelectionDetail>();
float[] pts = new float[2];
for (int i = 0; i < mChart.getData().getDataSetCount(); i++) {
DataSet<?> dataSet = mChart.getData().getDataSetByIndex(i);
// dont include datasets that cannot be highlighted
if (!dataSet.isHighlightEnabled())
continue;
// extract all y-values from all DataSets at the given x-index
final float yVal = dataSet.getYValForXIndex(xIndex);
if (yVal == Float.NaN)
continue;
pts[1] = yVal;
mChart.getTransformer(dataSet.getAxisDependency()).pointValuesToPixel(pts);
if (!Float.isNaN(pts[1])) {
vals.add(new SelectionDetail(pts[1], i, dataSet));
}
}
return vals;
}
示例17
/**
* Returns a list of SelectionDetail object corresponding to the given xIndex.
*
* @param xIndex
* @return
*/
@Override
protected List<SelectionDetail> getSelectionDetailsAtIndex(int xIndex) {
CombinedData data = (CombinedData) mChart.getData();
// get all chartdata objects
List<ChartData> dataObjects = data.getAllData();
List<SelectionDetail> vals = new ArrayList<SelectionDetail>();
float[] pts = new float[2];
for (int i = 0; i < dataObjects.size(); i++) {
for(int j = 0; j < dataObjects.get(i).getDataSetCount(); j++) {
IDataSet dataSet = dataObjects.get(i).getDataSetByIndex(j);
// dont include datasets that cannot be highlighted
if (!dataSet.isHighlightEnabled())
continue;
// extract all y-values from all DataSets at the given x-index
final float yVal = dataSet.getYValForXIndex(xIndex);
if (yVal == Float.NaN)
continue;
pts[1] = yVal;
mChart.getTransformer(dataSet.getAxisDependency()).pointValuesToPixel(pts);
if (!Float.isNaN(pts[1])) {
vals.add(new SelectionDetail(pts[1], j, dataSet));
}
}
}
return vals;
}
示例18
/**
* Returns the corresponding dataset-index for a given xIndex and xy-touch position in pixels.
*
* @param xIndex
* @param x
* @param y
* @return
*/
protected int getDataSetIndex(int xIndex, float x, float y) {
List<SelectionDetail> valsAtIndex = getSelectionDetailsAtIndex(xIndex);
float leftdist = Utils.getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.LEFT);
float rightdist = Utils.getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.RIGHT);
YAxis.AxisDependency axis = leftdist < rightdist ? YAxis.AxisDependency.LEFT : YAxis.AxisDependency.RIGHT;
int dataSetIndex = Utils.getClosestDataSetIndex(valsAtIndex, y, axis);
return dataSetIndex;
}
示例19
/**
* Returns a list of SelectionDetail object corresponding to the given xIndex.
*
* @param xIndex
* @return
*/
protected List<SelectionDetail> getSelectionDetailsAtIndex(int xIndex) {
List<SelectionDetail> vals = new ArrayList<SelectionDetail>();
float[] pts = new float[2];
for (int i = 0; i < mChart.getData().getDataSetCount(); i++) {
IDataSet dataSet = mChart.getData().getDataSetByIndex(i);
// dont include datasets that cannot be highlighted
if (!dataSet.isHighlightEnabled())
continue;
// extract all y-values from all DataSets at the given x-index
final float yVal = dataSet.getYValForXIndex(xIndex);
if (yVal == Float.NaN)
continue;
pts[1] = yVal;
mChart.getTransformer(dataSet.getAxisDependency()).pointValuesToPixel(pts);
if (!Float.isNaN(pts[1])) {
vals.add(new SelectionDetail(pts[1], i, dataSet));
}
}
return vals;
}
示例20
/**
* Returns the corresponding SelectionDetail for a given xIndex and y-touch position in pixels.
*
* @param xIndex
* @param y
* @param dataSetIndex
* @return
*/
protected SelectionDetail getSelectionDetail(int xIndex, float y, int dataSetIndex) {
List<SelectionDetail> valsAtIndex = getSelectionDetailsAtIndex(xIndex, dataSetIndex);
float leftdist = Utils.getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.LEFT);
float rightdist = Utils.getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.RIGHT);
YAxis.AxisDependency axis = leftdist < rightdist ? YAxis.AxisDependency.LEFT : YAxis.AxisDependency.RIGHT;
SelectionDetail detail = Utils.getClosestSelectionDetailByPixelY(valsAtIndex, y, axis);
return detail;
}