Skip to content

Commit 530ab11

Browse files
committed
Work on highlighting
1 parent 690dd87 commit 530ab11

10 files changed

Lines changed: 139 additions & 120 deletions

File tree

‎MPChartExample/build.gradle‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ android {
88
targetSdkVersion 23
99
versionCode 51
1010
versionName '2.2.5'
11+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1112

1213
sourceSets {
1314
main {
@@ -57,4 +58,6 @@ dependencies {
5758
compile 'com.android.support:appcompat-v7:23.1.1'
5859
compile 'io.realm:realm-android:0.87.5' // dependency for realm-database API (http://realm.io)
5960
//compile 'com.github.PhilJay:MPAndroidChart:v2.2.0'
61+
62+
testCompile 'junit:junit:4.12'
6063
}

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ private void setData(int count, float range) {
193193
// xIndex (even if from different DataSets), since no values can be
194194
// drawn above each other.
195195
for (int i = 0; i < count + 1; i++) {
196-
yVals1.add(new Entry((float) (Math.random() * mult) + mult / 5, i));
196+
yVals1.add(new Entry(i, (float) (Math.random() * mult) + mult / 5));
197197
}
198198

199199
ArrayList<XAxisValue> xVals = new ArrayList<XAxisValue>();

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/ScatterChartActivity.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,17 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
174174

175175
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
176176
float val = (float) (Math.random() * mSeekBarY.getProgress()) + 3;
177-
yVals1.add(new Entry(val, i));
177+
yVals1.add(new Entry(i, val));
178178
}
179179

180180
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
181181
float val = (float) (Math.random() * mSeekBarY.getProgress()) + 3;
182-
yVals2.add(new Entry(val, i));
182+
yVals2.add(new Entry(i+0.33f, val));
183183
}
184184

185185
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
186186
float val = (float) (Math.random() * mSeekBarY.getProgress()) + 3;
187-
yVals3.add(new Entry(val, i));
187+
yVals3.add(new Entry(i+0.66f, val));
188188
}
189189

190190
// create a dataset and give it a type

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/ScrollViewActivity.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private void setData(int count) {
5656

5757
for (int i = 0; i < count; i++) {
5858
float val = (float) (Math.random() * count) + 15;
59-
yVals.add(new BarEntry((int) val, i));
59+
yVals.add(new BarEntry(i, (int) val));
6060
xVals.add(new XAxisValue(i, (int) val + ""));
6161
}
6262

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.xxmassdeveloper.mpchartexample.test;
2+
3+
import com.github.mikephil.charting.data.Entry;
4+
import com.github.mikephil.charting.data.ScatterDataSet;
5+
6+
//import org.junit.Test;
7+
//
8+
//import java.util.ArrayList;
9+
//import java.util.List;
10+
//import static org.junit.Assert.*;
11+
//
12+
///**
13+
// * Created by philipp on 30/05/16.
14+
// */
15+
//public class DataSetTest {
16+
//
17+
//
18+
// @Test
19+
// public void testGetEntryForXPos() {
20+
//
21+
// List<Entry> vals = new ArrayList<>();
22+
// vals.add(new Entry(10, 10));
23+
// vals.add(new Entry(15, 5));
24+
// vals.add(new Entry(17, 10));
25+
//
26+
// ScatterDataSet set = new ScatterDataSet(vals, "");
27+
//
28+
// Entry e1 = set.getEntryForXPos(13);
29+
// assertEquals(15, e1.getX(), 0.01f);
30+
// assertEquals(5, e1.getY(), 0.01f);
31+
// }
32+
//}
33+

‎MPChartLib/src/com/github/mikephil/charting/highlight/BarHighlighter.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public Highlight getHighlight(float x, float y) {
3333
dataSetIndex = setCount - 1;
3434
}
3535

36-
SelectionDetail selectionDetail = getSelectionDetail(xVal, y, dataSetIndex);
36+
SelectionDetail selectionDetail = getSelectionDetail(xVal, x, y, dataSetIndex);
3737
if (selectionDetail == null)
3838
return null;
3939

@@ -84,7 +84,7 @@ else if (xIndex >= valCount)
8484
}
8585

8686
@Override
87-
protected SelectionDetail getSelectionDetail(float xVal, float y, int dataSetIndex) {
87+
protected SelectionDetail getSelectionDetail(float xVal, float x, float y, int dataSetIndex) {
8888

8989
dataSetIndex = Math.max(dataSetIndex, 0);
9090

‎MPChartLib/src/com/github/mikephil/charting/highlight/ChartHighlighter.java‎

Lines changed: 84 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55

66
import com.github.mikephil.charting.components.YAxis;
7+
import com.github.mikephil.charting.data.Entry;
78
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
89
import com.github.mikephil.charting.interfaces.dataprovider.BarLineScatterCandleBubbleDataProvider;
910
import com.github.mikephil.charting.utils.SelectionDetail;
@@ -14,118 +15,115 @@
1415
*/
1516
public class ChartHighlighter<T extends BarLineScatterCandleBubbleDataProvider> {
1617

17-
/** instance of the data-provider */
18-
protected T mChart;
18+
/**
19+
* instance of the data-provider
20+
*/
21+
protected T mChart;
1922

20-
public ChartHighlighter(T chart) {
21-
this.mChart = chart;
22-
}
23+
public ChartHighlighter(T chart) {
24+
this.mChart = chart;
25+
}
2326

24-
/**
25-
* Returns a Highlight object corresponding to the given x- and y- touch positions in pixels.
26-
*
27-
* @param x
28-
* @param y
29-
* @return
30-
*/
31-
public Highlight getHighlight(float x, float y) {
27+
/**
28+
* Returns a Highlight object corresponding to the given x- and y- touch positions in pixels.
29+
*
30+
* @param x
31+
* @param y
32+
* @return
33+
*/
34+
public Highlight getHighlight(float x, float y) {
3235

33-
float xVal = getXForTouch(x);
36+
float xVal = getXForTouch(x);
3437

35-
SelectionDetail selectionDetail = getSelectionDetail(xVal, y, -1);
36-
if (selectionDetail == null)
37-
return null;
38+
SelectionDetail selectionDetail = getSelectionDetail(xVal, x, y, -1);
39+
if (selectionDetail == null)
40+
return null;
3841

39-
return new Highlight(xVal,
40-
selectionDetail.value,
41-
selectionDetail.dataIndex,
42-
selectionDetail.dataSetIndex);
43-
}
42+
return new Highlight(xVal,
43+
selectionDetail.value,
44+
selectionDetail.dataIndex,
45+
selectionDetail.dataSetIndex);
46+
}
4447

45-
/**
46-
* Returns the corresponding x-index for a given touch-position in pixels.
47-
*
48-
* @param x
49-
* @return
50-
*/
51-
protected float getXForTouch(float x) {
48+
/**
49+
* Returns the corresponding x-index for a given touch-position in pixels.
50+
*
51+
* @param x
52+
* @return
53+
*/
54+
protected float getXForTouch(float x) {
5255

53-
// create an array of the touch-point
54-
float[] pts = new float[2];
55-
pts[0] = x;
56+
// create an array of the touch-point
57+
float[] pts = new float[2];
58+
pts[0] = x;
5659

57-
// take any transformer to determine the x-axis value
58-
mChart.getTransformer(YAxis.AxisDependency.LEFT).pixelsToValue(pts);
60+
// take any transformer to determine the x-axis value
61+
mChart.getTransformer(YAxis.AxisDependency.LEFT).pixelsToValue(pts);
5962

60-
return Math.round(pts[0]);
61-
}
63+
return Math.round(pts[0]);
64+
}
6265

63-
/**
64-
* Returns the corresponding SelectionDetail for a given xVal and y-touch position in pixels.
65-
*
66-
* @param xVal
67-
* @param y
68-
* @param dataSetIndex
69-
* @return
70-
*/
71-
protected SelectionDetail getSelectionDetail(float xVal, float y, int dataSetIndex) {
66+
/**
67+
* Returns the corresponding SelectionDetail for a given xVal and y-touch position in pixels.
68+
*
69+
* @param xVal
70+
* @param y
71+
* @param dataSetIndex
72+
* @return
73+
*/
74+
protected SelectionDetail getSelectionDetail(float xVal, float x, float y, int dataSetIndex) {
7275

73-
List<SelectionDetail> valsAtIndex = getSelectionDetailsAtIndex(xVal, dataSetIndex);
76+
List<SelectionDetail> valsAtIndex = getSelectionDetailsAtIndex(xVal, dataSetIndex);
7477

75-
float leftdist = Utils.getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.LEFT);
76-
float rightdist = Utils.getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.RIGHT);
78+
float leftdist = Utils.getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.LEFT);
79+
float rightdist = Utils.getMinimumDistance(valsAtIndex, y, YAxis.AxisDependency.RIGHT);
7780

78-
YAxis.AxisDependency axis = leftdist < rightdist ? YAxis.AxisDependency.LEFT : YAxis.AxisDependency.RIGHT;
81+
YAxis.AxisDependency axis = leftdist < rightdist ? YAxis.AxisDependency.LEFT : YAxis.AxisDependency.RIGHT;
7982

80-
SelectionDetail detail = Utils.getClosestSelectionDetailByPixelY(valsAtIndex, y, axis);
83+
SelectionDetail detail = Utils.getClosestSelectionDetailByPixel(valsAtIndex, x, y, axis);
8184

82-
return detail;
83-
}
85+
return detail;
86+
}
8487

85-
/**
86-
* Returns a list of SelectionDetail object corresponding to the given xIndex.
87-
*
88-
* @param xVal
89-
* @param dataSetIndex dataSet index to look at. -1 if unspecified.
90-
* @return
91-
*/
92-
protected List<SelectionDetail> getSelectionDetailsAtIndex(float xVal, int dataSetIndex) {
88+
/**
89+
* Returns a list of SelectionDetail object corresponding to the given xIndex.
90+
*
91+
* @param xVal
92+
* @param dataSetIndex dataSet index to look at. -1 if unspecified.
93+
* @return
94+
*/
95+
protected List<SelectionDetail> getSelectionDetailsAtIndex(float xVal, int dataSetIndex) {
9396

94-
List<SelectionDetail> vals = new ArrayList<SelectionDetail>();
97+
List<SelectionDetail> vals = new ArrayList<SelectionDetail>();
9598

96-
if (mChart.getData() == null) return vals;
99+
if (mChart.getData() == null) return vals;
97100

98-
float[] pts = new float[2];
101+
float[] pts = new float[2];
99102

100-
for (int i = 0, dataSetCount = mChart.getData().getDataSetCount();
101-
i < dataSetCount;
102-
i++) {
103+
for (int i = 0, dataSetCount = mChart.getData().getDataSetCount(); i < dataSetCount; i++) {
103104

104-
if (dataSetIndex > -1 && dataSetIndex != i)
105-
continue;
105+
if (dataSetIndex > -1 && dataSetIndex != i)
106+
continue;
106107

107-
IDataSet dataSet = mChart.getData().getDataSetByIndex(i);
108+
IDataSet dataSet = mChart.getData().getDataSetByIndex(i);
108109

109-
// dont include datasets that cannot be highlighted
110-
if (!dataSet.isHighlightEnabled())
111-
continue;
110+
// dont include datasets that cannot be highlighted
111+
if (!dataSet.isHighlightEnabled())
112+
continue;
112113

113-
// extract all y-values from all DataSets at the given x-index
114-
final float[] yVals = dataSet.getYValuesForXPos(xVal);
115-
for (float yVal : yVals) {
116-
if (Float.isNaN(yVal))
117-
continue;
114+
// extract all y-values from all DataSets at the given x-index
115+
final Entry e = dataSet.getEntryForXPos(xVal);
118116

119-
pts[1] = yVal;
117+
pts[0] = e.getX();
118+
pts[1] = e.getY();
120119

121-
mChart.getTransformer(dataSet.getAxisDependency()).pointValuesToPixel(pts);
120+
mChart.getTransformer(dataSet.getAxisDependency()).pointValuesToPixel(pts);
122121

123-
if (!Float.isNaN(pts[1])) {
124-
vals.add(new SelectionDetail(pts[1], yVal, i, dataSet));
125-
}
126-
}
127-
}
122+
if (!Float.isNaN(pts[1])) {
123+
vals.add(new SelectionDetail(pts[0], pts[1], e.getY(), i, dataSet));
124+
}
125+
}
128126

129-
return vals;
130-
}
127+
return vals;
128+
}
131129
}

‎MPChartLib/src/com/github/mikephil/charting/highlight/HorizontalBarHighlighter.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public Highlight getHighlight(float x, float y) {
3131
dataSetIndex = setCount - 1;
3232
}
3333

34-
SelectionDetail selectionDetail = getSelectionDetail(xVal, y, dataSetIndex);
34+
SelectionDetail selectionDetail = getSelectionDetail(xVal, x, y, dataSetIndex);
3535
if (selectionDetail == null)
3636
return null;
3737

‎MPChartLib/src/com/github/mikephil/charting/utils/SelectionDetail.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,23 @@
1313
public class SelectionDetail {
1414

1515
public float y;
16+
public float x;
1617
public float value;
1718
public int dataIndex;
1819
public int dataSetIndex;
1920
public IDataSet dataSet;
2021

21-
public SelectionDetail(float y, float value, int dataIndex, int dataSetIndex, IDataSet set) {
22+
public SelectionDetail(float x, float y, float value, int dataIndex, int dataSetIndex, IDataSet set) {
23+
this.x = x;
2224
this.y = y;
2325
this.value = value;
2426
this.dataIndex = dataIndex;
2527
this.dataSetIndex = dataSetIndex;
2628
this.dataSet = set;
2729
}
2830

29-
public SelectionDetail(float y, float value, int dataSetIndex, IDataSet set) {
30-
this(y, value, 0, dataSetIndex, set);
31+
public SelectionDetail(float x, float y, float value, int dataSetIndex, IDataSet set) {
32+
this(x, y, value, 0, dataSetIndex, set);
3133
}
3234

3335
public SelectionDetail(float value, int dataSetIndex, IDataSet set) {

0 commit comments

Comments
 (0)