Work on combined highlighting
This commit is contained in:
parent
4b00e83479
commit
0d9c8545f2
9 changed files with 95 additions and 58 deletions
|
@ -149,7 +149,8 @@ public class CombinedChartActivity extends DemoBase {
|
|||
set1.setValueTextSize(10f);
|
||||
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
|
||||
|
||||
BarDataSet set2 = new BarDataSet(entries2, "Bar 2");
|
||||
BarDataSet set2 = new BarDataSet(entries2, "");
|
||||
set2.setStackLabels(new String[]{"Stack 1", "Stack 2"});
|
||||
set2.setColors(new int[]{Color.rgb(61, 165, 255), Color.rgb(23, 197, 255)});
|
||||
set2.setValueTextColor(Color.rgb(61, 165, 255));
|
||||
set2.setValueTextSize(10f);
|
||||
|
|
|
@ -14,6 +14,7 @@ import com.github.mikephil.charting.highlight.CombinedHighlighter;
|
|||
import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider;
|
||||
import com.github.mikephil.charting.interfaces.dataprovider.BubbleDataProvider;
|
||||
import com.github.mikephil.charting.interfaces.dataprovider.CandleDataProvider;
|
||||
import com.github.mikephil.charting.interfaces.dataprovider.CombinedDataProvider;
|
||||
import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider;
|
||||
import com.github.mikephil.charting.interfaces.dataprovider.ScatterDataProvider;
|
||||
import com.github.mikephil.charting.interfaces.datasets.IBubbleDataSet;
|
||||
|
@ -25,8 +26,7 @@ import com.github.mikephil.charting.renderer.CombinedChartRenderer;
|
|||
*
|
||||
* @author Philipp Jahoda
|
||||
*/
|
||||
public class CombinedChart extends BarLineChartBase<CombinedData> implements LineDataProvider,
|
||||
BarDataProvider, ScatterDataProvider, CandleDataProvider, BubbleDataProvider {
|
||||
public class CombinedChart extends BarLineChartBase<CombinedData> implements CombinedDataProvider {
|
||||
|
||||
// /**
|
||||
// * flag that enables or disables the highlighting arrow
|
||||
|
@ -82,11 +82,17 @@ public class CombinedChart extends BarLineChartBase<CombinedData> implements Lin
|
|||
// mViewPortHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CombinedData getCombinedData() {
|
||||
return mData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setData(CombinedData data) {
|
||||
mData = null;
|
||||
mRenderer = null;
|
||||
super.setData(data);
|
||||
setHighlighter(new CombinedHighlighter(this, this));
|
||||
mRenderer = new CombinedChartRenderer(this, mAnimator, mViewPortHandler);
|
||||
mRenderer.initBuffers();
|
||||
}
|
||||
|
|
|
@ -97,6 +97,10 @@ public class CombinedData extends BarLineScatterCandleBubbleData<IBarLineScatter
|
|||
return data;
|
||||
}
|
||||
|
||||
public ChartData getDataByIndex(int index) {
|
||||
return getAllData().get(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyDataChanged() {
|
||||
if (mLineData != null)
|
||||
|
@ -145,4 +149,8 @@ public class CombinedData extends BarLineScatterCandleBubbleData<IBarLineScatter
|
|||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public int getDataIndex(ChartData data) {
|
||||
return getAllData().indexOf(data);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ public class BarHighlighter extends ChartHighlighter<BarDataProvider> {
|
|||
* @param yVal
|
||||
* @return
|
||||
*/
|
||||
protected Highlight getStackedHighlight(Highlight high, IBarDataSet set, float xVal, float yVal) {
|
||||
public Highlight getStackedHighlight(Highlight high, IBarDataSet set, float xVal, float yVal) {
|
||||
|
||||
BarEntry entry = set.getEntryForXPos(xVal);
|
||||
|
||||
|
@ -65,15 +65,18 @@ public class BarHighlighter extends ChartHighlighter<BarDataProvider> {
|
|||
if (ranges.length > 0) {
|
||||
int stackIndex = getClosestStackIndex(ranges, yVal);
|
||||
|
||||
Range range = ranges[stackIndex];
|
||||
|
||||
PointD pixels = mChart.getTransformer(set.getAxisDependency()).getPixelsForValues(high.getX(), range.to);
|
||||
|
||||
Highlight stackedHigh = new Highlight(
|
||||
entry.getX(),
|
||||
entry.getPositiveSum() - entry.getNegativeSum(),
|
||||
high.getXPx(),
|
||||
high.getYPx(),
|
||||
entry.getY(),
|
||||
(float) pixels.x,
|
||||
(float) pixels.y,
|
||||
high.getDataSetIndex(),
|
||||
stackIndex,
|
||||
ranges[stackIndex],
|
||||
range,
|
||||
high.getAxis()
|
||||
);
|
||||
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
package com.github.mikephil.charting.highlight;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.github.mikephil.charting.components.YAxis;
|
||||
import com.github.mikephil.charting.data.BarLineScatterCandleBubbleData;
|
||||
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.interfaces.datasets.IDataSet;
|
||||
import com.github.mikephil.charting.utils.PointD;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Philipp Jahoda on 21/07/15.
|
||||
*/
|
||||
|
@ -57,7 +57,7 @@ public class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider>
|
|||
*/
|
||||
protected Highlight getHighlightForX(float xVal, float x, float y) {
|
||||
|
||||
List<Highlight> closestValues = getHighlightsAtXPos(xVal);
|
||||
List<Highlight> closestValues = getHighlightsAtXPos(xVal, x, y);
|
||||
|
||||
float leftAxisMinDist = getMinimumDistance(closestValues, y, YAxis.AxisDependency.LEFT);
|
||||
float rightAxisMinDist = getMinimumDistance(closestValues, y, YAxis.AxisDependency.RIGHT);
|
||||
|
@ -106,10 +106,12 @@ public class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider>
|
|||
* 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
|
||||
* @param xVal the transformed x-value of the x-touch position
|
||||
* @param x touch position
|
||||
* @param y touch position
|
||||
* @return
|
||||
*/
|
||||
protected List<Highlight> getHighlightsAtXPos(float xVal) {
|
||||
protected List<Highlight> getHighlightsAtXPos(float xVal, float x, float y) {
|
||||
|
||||
List<Highlight> vals = new ArrayList<Highlight>();
|
||||
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package com.github.mikephil.charting.highlight;
|
||||
|
||||
import com.github.mikephil.charting.data.BarData;
|
||||
import com.github.mikephil.charting.data.ChartData;
|
||||
import com.github.mikephil.charting.data.CombinedData;
|
||||
import com.github.mikephil.charting.data.DataSet;
|
||||
import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider;
|
||||
import com.github.mikephil.charting.interfaces.dataprovider.CombinedDataProvider;
|
||||
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
|
||||
import com.github.mikephil.charting.interfaces.dataprovider.BarLineScatterCandleBubbleDataProvider;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -13,25 +13,61 @@ import java.util.List;
|
|||
/**
|
||||
* Created by Philipp Jahoda on 12/09/15.
|
||||
*/
|
||||
public class CombinedHighlighter extends ChartHighlighter<BarLineScatterCandleBubbleDataProvider> implements Highlighter {
|
||||
public class CombinedHighlighter extends ChartHighlighter<CombinedDataProvider> implements Highlighter {
|
||||
|
||||
/**
|
||||
* bar highlighter for supporting stacked highlighting
|
||||
*/
|
||||
protected BarHighlighter barHighlighter;
|
||||
|
||||
public CombinedHighlighter(BarLineScatterCandleBubbleDataProvider chart, BarDataProvider barChart) {
|
||||
public CombinedHighlighter(CombinedDataProvider chart, BarDataProvider barChart) {
|
||||
super(chart);
|
||||
|
||||
// if there is BarData, create a BarHighlighter
|
||||
barHighlighter = barChart.getBarData() == null ? null : new BarHighlighter(barChart);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Highlight getHighlight(float x, float y) {
|
||||
protected List<Highlight> getHighlightsAtXPos(float xVal, float x, float y) {
|
||||
|
||||
Highlight h1 = super.getHighlight(x, y);
|
||||
Highlight h2 = barHighlighter == null ? null : barHighlighter.getHighlight(x, y);
|
||||
List<Highlight> vals = new ArrayList<Highlight>();
|
||||
|
||||
return (h2 != null && h2.isStacked()) ? h2 : h1;
|
||||
List<ChartData> dataObjects = mChart.getCombinedData().getAllData();
|
||||
|
||||
for (int i = 0; i < dataObjects.size(); i++) {
|
||||
|
||||
ChartData dataObject = dataObjects.get(i);
|
||||
|
||||
// in case of BarData, let the BarHighlighter take over
|
||||
if (barHighlighter != null && dataObject instanceof BarData) {
|
||||
Highlight high = barHighlighter.getHighlight(x, y);
|
||||
|
||||
if (high != null) {
|
||||
high.setDataIndex(i);
|
||||
vals.add(high);
|
||||
}
|
||||
} else {
|
||||
|
||||
for (int j = 0, dataSetCount = dataObject.getDataSetCount(); j < dataSetCount; j++) {
|
||||
|
||||
IDataSet dataSet = dataObjects.get(i).getDataSetByIndex(j);
|
||||
|
||||
// don't include datasets that cannot be highlighted
|
||||
if (!dataSet.isHighlightEnabled())
|
||||
continue;
|
||||
|
||||
Highlight s1 = buildHighlight(dataSet, j, xVal, DataSet.Rounding.UP);
|
||||
s1.setDataIndex(i);
|
||||
vals.add(s1);
|
||||
|
||||
Highlight s2 = buildHighlight(dataSet, j, xVal, DataSet.Rounding.DOWN);
|
||||
s2.setDataIndex(i);
|
||||
vals.add(s2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return vals;
|
||||
}
|
||||
|
||||
// protected Highlight getClosest(float x, float y, Highlight... highs) {
|
||||
|
@ -41,6 +77,9 @@ public class CombinedHighlighter extends ChartHighlighter<BarLineScatterCandleBu
|
|||
//
|
||||
// for (Highlight high : highs) {
|
||||
//
|
||||
// if (high == null)
|
||||
// continue;
|
||||
//
|
||||
// float tempDistance = getDistance(x, y, high.getXPx(), high.getYPx());
|
||||
//
|
||||
// if (tempDistance < minDistance) {
|
||||
|
@ -51,37 +90,4 @@ public class CombinedHighlighter extends ChartHighlighter<BarLineScatterCandleBu
|
|||
//
|
||||
// return closest;
|
||||
// }
|
||||
|
||||
@Override
|
||||
protected List<Highlight> getHighlightsAtXPos(float xVal) {
|
||||
|
||||
List<Highlight> vals = new ArrayList<Highlight>();
|
||||
|
||||
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, dataSetCount = dataObjects.get(i).getDataSetCount(); j < dataSetCount; j++) {
|
||||
|
||||
IDataSet dataSet = dataObjects.get(i).getDataSetByIndex(j);
|
||||
|
||||
// don't include datasets that cannot be highlighted
|
||||
if (!dataSet.isHighlightEnabled())
|
||||
continue;
|
||||
|
||||
Highlight s1 = buildHighlight(dataSet, j, xVal, DataSet.Rounding.UP);
|
||||
s1.setDataIndex(i);
|
||||
vals.add(s1);
|
||||
|
||||
Highlight s2 = buildHighlight(dataSet, j, xVal, DataSet.Rounding.DOWN);
|
||||
s2.setDataIndex(i);
|
||||
vals.add(s2);
|
||||
}
|
||||
}
|
||||
|
||||
return vals;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,7 @@ public class Highlight {
|
|||
/**
|
||||
* the index of the data object - in case it refers to more than one
|
||||
*/
|
||||
private int mDataIndex;
|
||||
private int mDataIndex = -1;
|
||||
|
||||
/**
|
||||
* the index of the dataset the highlighted value is in
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package com.github.mikephil.charting.interfaces.dataprovider;
|
||||
|
||||
import com.github.mikephil.charting.data.CombinedData;
|
||||
|
||||
/**
|
||||
* Created by philipp on 11/06/16.
|
||||
*/
|
||||
public interface CombinedDataProvider extends LineDataProvider, BarDataProvider, BubbleDataProvider, CandleDataProvider, ScatterDataProvider {
|
||||
|
||||
CombinedData getCombinedData();
|
||||
}
|
|
@ -199,7 +199,7 @@ public class BubbleChartRenderer extends BarLineScatterCandleBubbleRenderer {
|
|||
if (dataSet == null || !dataSet.isHighlightEnabled())
|
||||
continue;
|
||||
|
||||
final BubbleEntry entry = (BubbleEntry) bubbleData.getEntryForHighlight(high);
|
||||
final BubbleEntry entry = dataSet.getEntryForXPos(high.getX());
|
||||
|
||||
if (entry == null)
|
||||
continue;
|
||||
|
|
Loading…
Add table
Reference in a new issue