Skip to content

Commit e894e38

Browse files
committed
Merge pull request PhilJay#1727 from danielgindi/highlights
Highlight enhancements
2 parents 69d1b4d + a002634 commit e894e38

28 files changed

Lines changed: 557 additions & 288 deletions

MPChartLib/src/com/github/mikephil/charting/charts/Chart.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -556,15 +556,24 @@ public void highlightValues(Highlight[] highs) {
556556
* @param dataSetIndex
557557
*/
558558
public void highlightValue(int xIndex, int dataSetIndex) {
559+
highlightValue(xIndex, dataSetIndex, true);
560+
}
561+
562+
/**
563+
* Highlights the value at the given x-index in the given DataSet. Provide
564+
* -1 as the x-index or dataSetIndex to undo all highlighting.
565+
*
566+
* @param xIndex
567+
* @param dataSetIndex
568+
*/
569+
public void highlightValue(int xIndex, int dataSetIndex, boolean callListener) {
559570

560571
if (xIndex < 0 || dataSetIndex < 0 || xIndex >= mData.getXValCount()
561572
|| dataSetIndex >= mData.getDataSetCount()) {
562573

563-
highlightValues(null);
574+
highlightValue(null, callListener);
564575
} else {
565-
highlightValues(new Highlight[]{
566-
new Highlight(xIndex, dataSetIndex)
567-
});
576+
highlightValue(new Highlight(xIndex, dataSetIndex), callListener);
568577
}
569578
}
570579

@@ -598,7 +607,10 @@ public void highlightValue(Highlight high, boolean callListener) {
598607
Log.i(LOG_TAG, "Highlighted: " + high.toString());
599608

600609
e = mData.getEntryForHighlight(high);
601-
if (e == null || e.getXIndex() != high.getXIndex()) {
610+
if (e == null ||
611+
e.getXIndex() != high.getXIndex() ||
612+
(!Float.isNaN(high.getValue()) &&
613+
e.getVal() != high.getValue())) {
602614
mIndicesToHighlight = null;
603615
high = null;
604616
} else {

MPChartLib/src/com/github/mikephil/charting/charts/PieRadarChartBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ public List<SelectionDetail> getSelectionDetailsAtIndex(int xIndex) {
459459

460460
// extract all y-values from all DataSets at the given x-index
461461
final float yVal = dataSet.getYValForXIndex(xIndex);
462-
if (yVal == Float.NaN)
462+
if (Float.isNaN(yVal))
463463
continue;
464464

465465
vals.add(new SelectionDetail(yVal, i, dataSet));

MPChartLib/src/com/github/mikephil/charting/data/ChartData.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,19 @@ protected String[] getDataSetLabels() {
472472
public Entry getEntryForHighlight(Highlight highlight) {
473473
if (highlight.getDataSetIndex() >= mDataSets.size())
474474
return null;
475-
else
476-
return mDataSets.get(highlight.getDataSetIndex()).getEntryForXIndex(
477-
highlight.getXIndex());
475+
else {
476+
// The value of the highlighted entry could be NaN -
477+
// if we are not interested in highlighting a specific value.
478+
479+
List<?> entries = mDataSets.get(highlight.getDataSetIndex())
480+
.getEntriesForXIndex(highlight.getXIndex());
481+
for (Object entry : entries)
482+
if (((Entry)entry).getVal() == highlight.getValue() ||
483+
Float.isNaN(highlight.getValue()))
484+
return (Entry)entry;
485+
486+
return null;
487+
}
478488
}
479489

480490
/**

MPChartLib/src/com/github/mikephil/charting/data/CombinedData.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
package com.github.mikephil.charting.data;
33

4+
import com.github.mikephil.charting.highlight.Highlight;
45
import com.github.mikephil.charting.interfaces.datasets.IBarLineScatterCandleBubbleDataSet;
56

67
import java.util.ArrayList;
@@ -118,4 +119,37 @@ public void notifyDataChanged() {
118119

119120
init(); // recalculate everything
120121
}
122+
123+
/**
124+
* Get the Entry for a corresponding highlight object
125+
*
126+
* @param highlight
127+
* @return the entry that is highlighted
128+
*/
129+
@Override
130+
public Entry getEntryForHighlight(Highlight highlight) {
131+
132+
List<ChartData> dataObjects = getAllData();
133+
134+
if (highlight.getDataIndex() >= dataObjects.size())
135+
return null;
136+
137+
ChartData data = dataObjects.get(highlight.getDataIndex());
138+
139+
if (highlight.getDataSetIndex() >= data.getDataSetCount())
140+
return null;
141+
else {
142+
// The value of the highlighted entry could be NaN -
143+
// if we are not interested in highlighting a specific value.
144+
145+
List<?> entries = data.getDataSetByIndex(highlight.getDataSetIndex())
146+
.getEntriesForXIndex(highlight.getXIndex());
147+
for (Object entry : entries)
148+
if (((Entry)entry).getVal() == highlight.getValue() ||
149+
Float.isNaN(highlight.getValue()))
150+
return (Entry)entry;
151+
152+
return null;
153+
}
154+
}
121155
}

MPChartLib/src/com/github/mikephil/charting/data/DataSet.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,20 @@ public float getYValForXIndex(int xIndex) {
312312
return Float.NaN;
313313
}
314314

315+
@Override
316+
public float[] getYValsForXIndex(int xIndex) {
317+
318+
List<T> entries = getEntriesForXIndex(xIndex);
319+
320+
float[] yVals = new float[entries.size()];
321+
int i = 0;
322+
323+
for (T e : entries)
324+
yVals[i++] = e.getVal();
325+
326+
return yVals;
327+
}
328+
315329
/**
316330
* Returns all Entry objects at the given xIndex. INFORMATION: This method
317331
* does calculations at runtime. Do not over-use in performance critical
@@ -320,6 +334,7 @@ public float getYValForXIndex(int xIndex) {
320334
* @param xIndex
321335
* @return
322336
*/
337+
@Override
323338
public List<T> getEntriesForXIndex(int xIndex) {
324339

325340
List<T> entries = new ArrayList<T>();
@@ -344,12 +359,14 @@ public List<T> getEntriesForXIndex(int xIndex) {
344359
break;
345360
}
346361
}
347-
}
348362

349-
if (xIndex > entry.getXIndex())
350-
low = m + 1;
351-
else
352-
high = m - 1;
363+
break;
364+
} else {
365+
if (xIndex > entry.getXIndex())
366+
low = m + 1;
367+
else
368+
high = m - 1;
369+
}
353370
}
354371

355372
return entries;

MPChartLib/src/com/github/mikephil/charting/data/realm/base/RealmBaseDataSet.java

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.github.mikephil.charting.data.realm.base;
22

3+
import com.github.mikephil.charting.data.BarEntry;
34
import com.github.mikephil.charting.data.BaseDataSet;
45
import com.github.mikephil.charting.data.DataSet;
56
import com.github.mikephil.charting.data.Entry;
67

78
import java.util.ArrayList;
89
import java.util.List;
910

11+
import io.realm.DynamicRealmObject;
1012
import io.realm.RealmObject;
1113
import io.realm.RealmResults;
1214
import io.realm.Sort;
@@ -75,7 +77,20 @@ public RealmBaseDataSet(RealmResults<T> results, String yValuesField, String xIn
7577
/**
7678
* Rebuilds the DataSet based on the given RealmResults.
7779
*/
78-
public abstract void build(RealmResults<T> results);
80+
public void build(RealmResults<T> results) {
81+
82+
int xIndex = 0;
83+
for (T object : results) {
84+
mValues.add(buildEntryFromResultObject(object, xIndex++));
85+
}
86+
}
87+
88+
public S buildEntryFromResultObject(T realmObject, int xIndex) {
89+
DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);
90+
91+
return (S)new Entry(dynamicObject.getFloat(mValuesField),
92+
mIndexField == null ? xIndex : dynamicObject.getInt(mIndexField));
93+
}
7994

8095
@Override
8196
public float getYMin() {
@@ -150,6 +165,25 @@ public S getEntryForXIndex(int xIndex, DataSet.Rounding rounding) {
150165
return null;
151166
}
152167

168+
@Override
169+
public List<S> getEntriesForXIndex(int xIndex) {
170+
171+
List<S> entries = new ArrayList<>();
172+
173+
if (mIndexField == null) {
174+
T object = results.get(xIndex);
175+
if (object != null)
176+
entries.add(buildEntryFromResultObject(object, xIndex));
177+
} else {
178+
RealmResults<T> foundObjects = results.where().equalTo(mIndexField, xIndex).findAll();
179+
180+
for (T e : foundObjects)
181+
entries.add(buildEntryFromResultObject(e, xIndex));
182+
}
183+
184+
return entries;
185+
}
186+
153187
@Override
154188
public S getEntryForIndex(int index) {
155189
//DynamicRealmObject o = new DynamicRealmObject(results.get(index));
@@ -167,14 +201,16 @@ public int getEntryIndex(int x, DataSet.Rounding rounding) {
167201
while (low <= high) {
168202
int m = (high + low) / 2;
169203

170-
if (x == mValues.get(m).getXIndex()) {
204+
S entry = mValues.get(m);
205+
206+
if (x == entry.getXIndex()) {
171207
while (m > 0 && mValues.get(m - 1).getXIndex() == x)
172208
m--;
173209

174210
return m;
175211
}
176212

177-
if (x > mValues.get(m).getXIndex())
213+
if (x > entry.getXIndex())
178214
low = m + 1;
179215
else
180216
high = m - 1;
@@ -214,6 +250,20 @@ public float getYValForXIndex(int xIndex) {
214250
return Float.NaN;
215251
}
216252

253+
@Override
254+
public float[] getYValsForXIndex(int xIndex) {
255+
256+
List<S> entries = getEntriesForXIndex(xIndex);
257+
258+
float[] yVals = new float[entries.size()];
259+
int i = 0;
260+
261+
for (S e : entries)
262+
yVals[i++] = e.getVal();
263+
264+
return yVals;
265+
}
266+
217267
@Override
218268
public boolean addEntry(S e) {
219269

MPChartLib/src/com/github/mikephil/charting/data/realm/base/RealmLineRadarDataSet.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,6 @@ public RealmLineRadarDataSet(RealmResults<T> results, String yValuesField, Strin
4747
super(results, yValuesField, xIndexField);
4848
}
4949

50-
@Override
51-
public void build(RealmResults<T> results) {
52-
53-
if (mIndexField == null) { // x-index not available
54-
55-
int xIndex = 0;
56-
57-
for (T object : results) {
58-
59-
DynamicRealmObject dynamicObject = new DynamicRealmObject(object);
60-
mValues.add(new Entry(dynamicObject.getFloat(mValuesField), xIndex));
61-
xIndex++;
62-
}
63-
64-
} else {
65-
66-
for (T object : results) {
67-
68-
DynamicRealmObject dynamicObject = new DynamicRealmObject(object);
69-
mValues.add(new Entry(dynamicObject.getFloat(mValuesField), dynamicObject.getInt(mIndexField)));
70-
}
71-
}
72-
}
73-
7450
@Override
7551
public int getFillColor() {
7652
return mFillColor;

MPChartLib/src/com/github/mikephil/charting/data/realm/implementation/RealmBarDataSet.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import android.graphics.Color;
44

55
import com.github.mikephil.charting.data.BarEntry;
6+
import com.github.mikephil.charting.data.Entry;
67
import com.github.mikephil.charting.data.realm.base.RealmBarLineScatterCandleBubbleDataSet;
78
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
89

910
import io.realm.DynamicRealmObject;
11+
import io.realm.RealmFieldType;
1012
import io.realm.RealmList;
1113
import io.realm.RealmObject;
1214
import io.realm.RealmResults;
@@ -78,31 +80,33 @@ public RealmBarDataSet(RealmResults<T> results, String yValuesField, String xInd
7880
@Override
7981
public void build(RealmResults<T> results) {
8082

81-
for (T realmObject : results) {
83+
super.build(results);
8284

83-
DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);
84-
85-
try { // normal entry
85+
calcStackSize();
86+
}
8687

87-
float value = dynamicObject.getFloat(mValuesField);
88-
mValues.add(new BarEntry(value, dynamicObject.getInt(mIndexField)));
88+
@Override
89+
public BarEntry buildEntryFromResultObject(T realmObject, int xIndex) {
90+
DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);
8991

90-
} catch (IllegalArgumentException e) { // stacked entry
92+
if (dynamicObject.getFieldType(mValuesField) == RealmFieldType.LIST) {
9193

92-
RealmList<DynamicRealmObject> list = dynamicObject.getList(mValuesField);
93-
float[] values = new float[list.size()];
94+
RealmList<DynamicRealmObject> list = dynamicObject.getList(mValuesField);
95+
float[] values = new float[list.size()];
9496

95-
int i = 0;
96-
for (DynamicRealmObject o : list) {
97-
values[i] = o.getFloat(mStackValueFieldName);
98-
i++;
99-
}
100-
101-
mValues.add(new BarEntry(values, dynamicObject.getInt(mIndexField)));
97+
int i = 0;
98+
for (DynamicRealmObject o : list) {
99+
values[i] = o.getFloat(mStackValueFieldName);
100+
i++;
102101
}
103-
}
104102

105-
calcStackSize();
103+
return new BarEntry(values,
104+
mIndexField == null ? xIndex : dynamicObject.getInt(mIndexField));
105+
} else {
106+
float value = dynamicObject.getFloat(mValuesField);
107+
return new BarEntry(value,
108+
mIndexField == null ? xIndex : dynamicObject.getInt(mIndexField));
109+
}
106110
}
107111

108112
@Override

0 commit comments

Comments
 (0)