Improvements to chart highlighting
This commit is contained in:
parent
f83a7e3b26
commit
6878220e2e
6 changed files with 102 additions and 60 deletions
|
@ -183,6 +183,18 @@ public class BarChart extends BarLineChartBase<BarData> implements BarDataProvid
|
|||
return mDrawBarShadow;
|
||||
}
|
||||
|
||||
/**
|
||||
* Highlights the value at the given x-position in the given DataSet. Provide
|
||||
* -1 as the dataSetIndex to undo all highlighting.
|
||||
*
|
||||
* @param x
|
||||
* @param dataSetIndex
|
||||
* @param stackIndex the index inside the stack - only relevant for stacked entries
|
||||
*/
|
||||
public void highlightValue(float x, int dataSetIndex, int stackIndex) {
|
||||
highlightValue(new Highlight(x, dataSetIndex, stackIndex), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BarData getBarData() {
|
||||
return mData;
|
||||
|
|
|
@ -2,6 +2,8 @@ package com.github.mikephil.charting.data;
|
|||
|
||||
import android.annotation.SuppressLint;
|
||||
|
||||
import com.github.mikephil.charting.highlight.Range;
|
||||
|
||||
/**
|
||||
* Entry class for the BarChart. (especially stacked bars)
|
||||
*
|
||||
|
@ -15,6 +17,11 @@ public class BarEntry extends Entry {
|
|||
*/
|
||||
private float[] mYVals;
|
||||
|
||||
/**
|
||||
* the ranges for the individual stack values - automatically calculated
|
||||
*/
|
||||
private Range[] mRanges;
|
||||
|
||||
/**
|
||||
* the sum of all negative values this entry (if stacked) contains
|
||||
*/
|
||||
|
@ -35,6 +42,7 @@ public class BarEntry extends Entry {
|
|||
super(x, calcSum(vals));
|
||||
|
||||
this.mYVals = vals;
|
||||
calcRanges();
|
||||
calcPosNegSum();
|
||||
}
|
||||
|
||||
|
@ -59,6 +67,7 @@ public class BarEntry extends Entry {
|
|||
super(x, calcSum(vals), label);
|
||||
|
||||
this.mYVals = vals;
|
||||
calcRanges();
|
||||
calcPosNegSum();
|
||||
}
|
||||
|
||||
|
@ -102,6 +111,7 @@ public class BarEntry extends Entry {
|
|||
setY(calcSum(vals));
|
||||
mYVals = vals;
|
||||
calcPosNegSum();
|
||||
calcRanges();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -114,6 +124,15 @@ public class BarEntry extends Entry {
|
|||
return super.getY();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ranges of the individual stack-entries. Will return null if this entry is not stacked.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Range[] getRanges() {
|
||||
return mRanges;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this BarEntry is stacked (has a values array), false if not.
|
||||
*
|
||||
|
@ -197,4 +216,32 @@ public class BarEntry extends Entry {
|
|||
|
||||
return sum;
|
||||
}
|
||||
|
||||
protected void calcRanges() {
|
||||
|
||||
float[] values = getYVals();
|
||||
|
||||
if (values == null || values.length == 0)
|
||||
return;
|
||||
|
||||
mRanges = new Range[values.length];
|
||||
|
||||
float negRemain = -getNegativeSum();
|
||||
float posRemain = 0f;
|
||||
|
||||
for (int i = 0; i < mRanges.length; i++) {
|
||||
|
||||
float value = values[i];
|
||||
|
||||
if (value < 0) {
|
||||
mRanges[i] = new Range(negRemain, negRemain + Math.abs(value));
|
||||
negRemain += Math.abs(value);
|
||||
} else {
|
||||
mRanges[i] = new Range(posRemain, posRemain + value);
|
||||
posRemain += value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -61,14 +61,12 @@ public class BarHighlighter extends ChartHighlighter<BarDataProvider> {
|
|||
if (entry.getYVals() == null) {
|
||||
return high;
|
||||
} else {
|
||||
Range[] ranges = getRanges(entry);
|
||||
Range[] ranges = entry.getRanges();
|
||||
|
||||
if (ranges.length > 0) {
|
||||
int stackIndex = getClosestStackIndex(ranges, yVal);
|
||||
|
||||
Range range = ranges[stackIndex];
|
||||
|
||||
PointD pixels = mChart.getTransformer(set.getAxisDependency()).getPixelsForValues(high.getX(), range.to);
|
||||
PointD pixels = mChart.getTransformer(set.getAxisDependency()).getPixelsForValues(high.getX(), ranges[stackIndex].to);
|
||||
|
||||
Highlight stackedHigh = new Highlight(
|
||||
entry.getX(),
|
||||
|
@ -77,7 +75,6 @@ public class BarHighlighter extends ChartHighlighter<BarDataProvider> {
|
|||
(float) pixels.y,
|
||||
high.getDataSetIndex(),
|
||||
stackIndex,
|
||||
range,
|
||||
high.getAxis()
|
||||
);
|
||||
|
||||
|
@ -116,39 +113,39 @@ public class BarHighlighter extends ChartHighlighter<BarDataProvider> {
|
|||
return (value > ranges[length].to) ? length : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits up the stack-values of the given bar-entry into Range objects.
|
||||
*
|
||||
* @param entry
|
||||
* @return
|
||||
*/
|
||||
protected Range[] getRanges(BarEntry entry) {
|
||||
|
||||
float[] values = entry.getYVals();
|
||||
|
||||
if (values == null || values.length == 0)
|
||||
return new Range[0];
|
||||
|
||||
Range[] ranges = new Range[values.length];
|
||||
|
||||
float negRemain = -entry.getNegativeSum();
|
||||
float posRemain = 0f;
|
||||
|
||||
for (int i = 0; i < ranges.length; i++) {
|
||||
|
||||
float value = values[i];
|
||||
|
||||
if (value < 0) {
|
||||
ranges[i] = new Range(negRemain, negRemain + Math.abs(value));
|
||||
negRemain += Math.abs(value);
|
||||
} else {
|
||||
ranges[i] = new Range(posRemain, posRemain + value);
|
||||
posRemain += value;
|
||||
}
|
||||
}
|
||||
|
||||
return ranges;
|
||||
}
|
||||
// /**
|
||||
// * Splits up the stack-values of the given bar-entry into Range objects.
|
||||
// *
|
||||
// * @param entry
|
||||
// * @return
|
||||
// */
|
||||
// protected Range[] getRanges(BarEntry entry) {
|
||||
//
|
||||
// float[] values = entry.getYVals();
|
||||
//
|
||||
// if (values == null || values.length == 0)
|
||||
// return new Range[0];
|
||||
//
|
||||
// Range[] ranges = new Range[values.length];
|
||||
//
|
||||
// float negRemain = -entry.getNegativeSum();
|
||||
// float posRemain = 0f;
|
||||
//
|
||||
// for (int i = 0; i < ranges.length; i++) {
|
||||
//
|
||||
// float value = values[i];
|
||||
//
|
||||
// if (value < 0) {
|
||||
// ranges[i] = new Range(negRemain, negRemain + Math.abs(value));
|
||||
// negRemain += Math.abs(value);
|
||||
// } else {
|
||||
// ranges[i] = new Range(posRemain, posRemain + value);
|
||||
// posRemain += value;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return ranges;
|
||||
// }
|
||||
|
||||
@Override
|
||||
protected float getDistance(float x1, float y1, float x2, float y2) {
|
||||
|
|
|
@ -33,7 +33,6 @@ public class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider>
|
|||
Highlight high = getHighlightForX(xVal, x, y);
|
||||
return high;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the corresponding xPos for a given touch-position in pixels.
|
||||
*
|
||||
|
|
|
@ -45,11 +45,6 @@ public class Highlight {
|
|||
*/
|
||||
private int mStackIndex = -1;
|
||||
|
||||
/**
|
||||
* the range of the bar that is selected (only for stacked-barchart)
|
||||
*/
|
||||
private Range mRange;
|
||||
|
||||
/**
|
||||
* the axis the highlighted value belongs to
|
||||
*/
|
||||
|
@ -99,13 +94,10 @@ public class Highlight {
|
|||
* @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
|
||||
*/
|
||||
protected Highlight(float x, float y, float xPx, float yPx, int dataSetIndex, int stackIndex, Range range,
|
||||
YAxis.AxisDependency axis) {
|
||||
protected Highlight(float x, float y, float xPx, float yPx, int dataSetIndex, int stackIndex, YAxis.AxisDependency axis) {
|
||||
this(x, y, xPx, yPx, dataSetIndex, axis);
|
||||
this.mStackIndex = stackIndex;
|
||||
this.mRange = range;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -172,15 +164,6 @@ public class Highlight {
|
|||
return mStackIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import com.github.mikephil.charting.buffer.BarBuffer;
|
|||
import com.github.mikephil.charting.data.BarData;
|
||||
import com.github.mikephil.charting.data.BarEntry;
|
||||
import com.github.mikephil.charting.highlight.Highlight;
|
||||
import com.github.mikephil.charting.highlight.Range;
|
||||
import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider;
|
||||
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
|
||||
import com.github.mikephil.charting.utils.Transformer;
|
||||
|
@ -343,14 +344,17 @@ public class BarChartRenderer extends BarLineScatterCandleBubbleRenderer {
|
|||
mHighlightPaint.setColor(set.getHighLightColor());
|
||||
mHighlightPaint.setAlpha(set.getHighLightAlpha());
|
||||
|
||||
boolean isStack = high.getStackIndex() < 0 ? false : true;
|
||||
boolean isStack = (high.getStackIndex() >= 0 && e.isStacked()) ? true : false;
|
||||
|
||||
final float y1;
|
||||
final float y2;
|
||||
|
||||
if (isStack) {
|
||||
y1 = high.getRange().from;
|
||||
y2 = high.getRange().to;
|
||||
|
||||
Range range = e.getRanges()[high.getStackIndex()];
|
||||
|
||||
y1 = range.from;
|
||||
y2 = range.to;
|
||||
} else {
|
||||
y1 = e.getY();
|
||||
y2 = 0.f;
|
||||
|
|
Loading…
Add table
Reference in a new issue