Skip to content

Commit c2d1c5a

Browse files
committed
Add more constructors to realm datasets
1 parent d64d70d commit c2d1c5a

File tree

8 files changed

+191
-46
lines changed

8 files changed

+191
-46
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public RealmBaseDataSet(RealmResults<T> results, String yValuesField) {
4949
this.results = results;
5050
this.mValuesField = yValuesField;
5151
this.mValues = new ArrayList<S>();
52-
this.results.sort(mIndexField, true);
52+
53+
if (mIndexField != null)
54+
this.results.sort(mIndexField, true);
5355
}
5456

5557
/**
@@ -64,7 +66,9 @@ public RealmBaseDataSet(RealmResults<T> results, String yValuesField, String xIn
6466
this.mValuesField = yValuesField;
6567
this.mIndexField = xIndexField;
6668
this.mValues = new ArrayList<S>();
67-
this.results.sort(mIndexField, true);
69+
70+
if (mIndexField != null) ;
71+
this.results.sort(mIndexField, true);
6872
}
6973

7074
/**

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
import io.realm.RealmObject;
1010
import io.realm.RealmResults;
11+
import io.realm.dynamic.DynamicRealmObject;
1112

1213
/**
1314
* Created by Philipp Jahoda on 08/11/15.
1415
*/
15-
public abstract class RealmLineRadarDataSet<T extends RealmObject, S extends Entry> extends RealmLineScatterCandleRadarDataSet<T, S> implements ILineRadarDataSet<S> {
16+
public abstract class RealmLineRadarDataSet<T extends RealmObject> extends RealmLineScatterCandleRadarDataSet<T, Entry> implements ILineRadarDataSet<Entry> {
1617

1718
/** the color that is used for filling the line surface */
1819
private int mFillColor = Color.rgb(140, 234, 255);
@@ -42,6 +43,30 @@ public RealmLineRadarDataSet(RealmResults<T> results, String yValuesField, Strin
4243
super(results, yValuesField, xIndexField);
4344
}
4445

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

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,29 @@ public class RealmBubbleDataSet<T extends RealmObject> extends RealmBarLineScatt
2424

2525
private float mHighlightCircleWidth = 2.5f;
2626

27+
/**
28+
* Constructor for creating a CandleDataSet with realm data.
29+
*
30+
* @param result the queried results from the realm database
31+
* @param yValuesField the name of the field in your data object that represents the y-value
32+
* @param sizeField the name of the field in your data object that represents the bubble size
33+
*/
34+
public RealmBubbleDataSet(RealmResults<T> result, String yValuesField, String sizeField) {
35+
super(result, yValuesField);
36+
this.mSizeField = sizeField;
37+
38+
build(this.results);
39+
calcMinMax(0, results.size());
40+
}
41+
42+
/**
43+
* Constructor for creating a CandleDataSet with realm data.
44+
*
45+
* @param result the queried results from the realm database
46+
* @param yValuesField the name of the field in your data object that represents the y-value
47+
* @param xIndexField the name of the field in your data object that represents the x-index
48+
* @param sizeField the name of the field in your data object that represents the bubble size
49+
*/
2750
public RealmBubbleDataSet(RealmResults<T> result, String yValuesField, String xIndexField, String sizeField) {
2851
super(result, yValuesField, xIndexField);
2952
this.mSizeField = sizeField;
@@ -35,10 +58,23 @@ public RealmBubbleDataSet(RealmResults<T> result, String yValuesField, String xI
3558
@Override
3659
public void build(RealmResults<T> results) {
3760

38-
for (T object : results) {
61+
if(mIndexField == null) {
62+
63+
int xIndex = 0;
64+
65+
for (T object : results) {
3966

40-
DynamicRealmObject dynamicObject = new DynamicRealmObject(object);
41-
mValues.add(new BubbleEntry(dynamicObject.getInt(mIndexField), dynamicObject.getFloat(mValuesField), dynamicObject.getFloat(mSizeField)));
67+
DynamicRealmObject dynamicObject = new DynamicRealmObject(object);
68+
mValues.add(new BubbleEntry(xIndex, dynamicObject.getFloat(mValuesField), dynamicObject.getFloat(mSizeField)));
69+
xIndex++;
70+
}
71+
} else {
72+
73+
for (T object : results) {
74+
75+
DynamicRealmObject dynamicObject = new DynamicRealmObject(object);
76+
mValues.add(new BubbleEntry(dynamicObject.getInt(mIndexField), dynamicObject.getFloat(mValuesField), dynamicObject.getFloat(mSizeField)));
77+
}
4278
}
4379
}
4480

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

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,38 @@ public class RealmCandleDataSet<T extends RealmObject> extends RealmLineScatterC
6363
*/
6464
protected int mShadowColor = ColorTemplate.COLOR_NONE;
6565

66+
/**
67+
* Constructor for creating a LineDataSet with realm data.
68+
*
69+
* @param result the queried results from the realm database
70+
* @param highField the name of the field in your data object that represents the "high" value
71+
* @param lowField the name of the field in your data object that represents the "low" value
72+
* @param openField the name of the field in your data object that represents the "open" value
73+
* @param closeField the name of the field in your data object that represents the "close" value
74+
*/
75+
public RealmCandleDataSet(RealmResults<T> result, String highField, String lowField, String openField, String closeField) {
76+
super(result, null);
77+
this.mHighField = highField;
78+
this.mLowField = lowField;
79+
this.mOpenField = openField;
80+
this.mCloseField = closeField;
81+
82+
build(this.results);
83+
calcMinMax(0, this.results.size());
84+
}
85+
86+
/**
87+
* Constructor for creating a LineDataSet with realm data.
88+
*
89+
* @param result the queried results from the realm database
90+
* @param highField the name of the field in your data object that represents the "high" value
91+
* @param lowField the name of the field in your data object that represents the "low" value
92+
* @param openField the name of the field in your data object that represents the "open" value
93+
* @param closeField the name of the field in your data object that represents the "close" value
94+
* @param xIndexField the name of the field in your data object that represents the x-index
95+
*/
6696
public RealmCandleDataSet(RealmResults<T> result, String highField, String lowField, String openField, String closeField, String xIndexField) {
67-
super(result, "", xIndexField);
97+
super(result, null, xIndexField);
6898
this.mHighField = highField;
6999
this.mLowField = lowField;
70100
this.mOpenField = openField;
@@ -77,11 +107,25 @@ public RealmCandleDataSet(RealmResults<T> result, String highField, String lowFi
77107
@Override
78108
public void build(RealmResults<T> results) {
79109

80-
for (T object : results) {
110+
if (mIndexField == null) {
111+
112+
int xIndex = 0;
113+
114+
for (T object : results) {
115+
116+
DynamicRealmObject dynamicObject = new DynamicRealmObject(object);
117+
mValues.add(new CandleEntry(xIndex, dynamicObject.getFloat(mHighField), dynamicObject.getFloat(mLowField),
118+
dynamicObject.getFloat(mOpenField), dynamicObject.getFloat(mCloseField)));
119+
xIndex++;
120+
}
121+
} else {
122+
123+
for (T object : results) {
81124

82-
DynamicRealmObject dynamicObject = new DynamicRealmObject(object);
83-
mValues.add(new CandleEntry(dynamicObject.getInt(mIndexField), dynamicObject.getFloat(mHighField), dynamicObject.getFloat(mLowField),
84-
dynamicObject.getFloat(mOpenField), dynamicObject.getFloat(mCloseField)));
125+
DynamicRealmObject dynamicObject = new DynamicRealmObject(object);
126+
mValues.add(new CandleEntry(dynamicObject.getInt(mIndexField), dynamicObject.getFloat(mHighField), dynamicObject.getFloat(mLowField),
127+
dynamicObject.getFloat(mOpenField), dynamicObject.getFloat(mCloseField)));
128+
}
85129
}
86130
}
87131

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/**
2323
* Created by Philipp Jahoda on 21/10/15.
2424
*/
25-
public class RealmLineDataSet<T extends RealmObject> extends RealmLineRadarDataSet<T, Entry> implements ILineDataSet {
25+
public class RealmLineDataSet<T extends RealmObject> extends RealmLineRadarDataSet<T> implements ILineDataSet {
2626

2727
/**
2828
* List representing all colors that are used for the circles
@@ -104,26 +104,7 @@ public RealmLineDataSet(RealmResults<T> result, String yValuesField, String xInd
104104

105105
@Override
106106
public void build(RealmResults<T> results) {
107-
108-
if (mIndexField == null) { // x-index not available
109-
110-
int xIndex = 0;
111-
112-
for (T object : results) {
113-
114-
DynamicRealmObject dynamicObject = new DynamicRealmObject(object);
115-
mValues.add(new Entry(dynamicObject.getFloat(mValuesField), xIndex));
116-
xIndex++;
117-
}
118-
119-
} else {
120-
121-
for (T object : results) {
122-
123-
DynamicRealmObject dynamicObject = new DynamicRealmObject(object);
124-
mValues.add(new Entry(dynamicObject.getFloat(mValuesField), dynamicObject.getInt(mIndexField)));
125-
}
126-
}
107+
super.build(results);
127108
}
128109

129110
/**

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

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,37 @@
1414
*/
1515
public class RealmPieDataSet<T extends RealmObject> extends RealmBaseDataSet<T, Entry> implements IPieDataSet {
1616

17-
/** the space in degrees between the chart-slices, default 0f */
17+
/**
18+
* the space in degrees between the chart-slices, default 0f
19+
*/
1820
private float mSliceSpace = 0f;
1921

20-
/** indicates the selection distance of a pie slice */
22+
/**
23+
* indicates the selection distance of a pie slice
24+
*/
2125
private float mShift = 18f;
2226

27+
28+
/**
29+
* Constructor for creating a PieDataSet with realm data.
30+
*
31+
* @param result the queried results from the realm database
32+
* @param yValuesField the name of the field in your data object that represents the y-value
33+
*/
34+
public RealmPieDataSet(RealmResults<T> result, String yValuesField) {
35+
super(result, yValuesField);
36+
37+
build(this.results);
38+
calcMinMax(0, results.size());
39+
}
40+
41+
/**
42+
* Constructor for creating a PieDataSet with realm data.
43+
*
44+
* @param result the queried results from the realm database
45+
* @param yValuesField the name of the field in your data object that represents the y-value
46+
* @param xIndexField the name of the field in your data object that represents the x-index
47+
*/
2348
public RealmPieDataSet(RealmResults<T> result, String yValuesField, String xIndexField) {
2449
super(result, yValuesField, xIndexField);
2550

@@ -30,12 +55,27 @@ public RealmPieDataSet(RealmResults<T> result, String yValuesField, String xInde
3055
@Override
3156
public void build(RealmResults<T> results) {
3257

33-
for (T object : results) {
58+
if (mIndexField == null) { // x-index not available
59+
60+
int xIndex = 0;
3461

35-
DynamicRealmObject dynamicObject = new DynamicRealmObject(object);
36-
mValues.add(new Entry(dynamicObject.getFloat(mValuesField), dynamicObject.getInt(mIndexField)));
62+
for (T object : results) {
63+
64+
DynamicRealmObject dynamicObject = new DynamicRealmObject(object);
65+
mValues.add(new Entry(dynamicObject.getFloat(mValuesField), xIndex));
66+
xIndex++;
67+
}
68+
69+
} else {
70+
71+
for (T object : results) {
72+
73+
DynamicRealmObject dynamicObject = new DynamicRealmObject(object);
74+
mValues.add(new Entry(dynamicObject.getFloat(mValuesField), dynamicObject.getInt(mIndexField)));
75+
}
3776
}
3877
}
78+
3979
/**
4080
* sets the space that is left out between the piechart-slices, default: 0°
4181
* --> no space, maximum 45, minimum 0 (no space)

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,28 @@
1111
/**
1212
* Created by Philipp Jahoda on 07/11/15.
1313
*/
14-
public class RealmRadarDataSet<T extends RealmObject> extends RealmLineRadarDataSet<T, Entry> implements IRadarDataSet {
14+
public class RealmRadarDataSet<T extends RealmObject> extends RealmLineRadarDataSet<T> implements IRadarDataSet {
1515

16+
/**
17+
* Constructor for creating a RadarDataSet with realm data.
18+
*
19+
* @param result the queried results from the realm database
20+
* @param yValuesField the name of the field in your data object that represents the y-value
21+
*/
22+
public RealmRadarDataSet(RealmResults<T> result, String yValuesField) {
23+
super(result, yValuesField);
24+
25+
build(this.results);
26+
calcMinMax(0, results.size());
27+
}
28+
29+
/**
30+
* Constructor for creating a RadarDataSet with realm data.
31+
*
32+
* @param result the queried results from the realm database
33+
* @param yValuesField the name of the field in your data object that represents the y-value
34+
* @param xIndexField the name of the field in your data object that represents the x-index
35+
*/
1636
public RealmRadarDataSet(RealmResults<T> result, String yValuesField, String xIndexField) {
1737
super(result, yValuesField, xIndexField);
1838

@@ -22,11 +42,6 @@ public RealmRadarDataSet(RealmResults<T> result, String yValuesField, String xIn
2242

2343
@Override
2444
public void build(RealmResults<T> results) {
25-
26-
for (T object : results) {
27-
28-
DynamicRealmObject dynamicObject = new DynamicRealmObject(object);
29-
mValues.add(new Entry(dynamicObject.getFloat(mValuesField), dynamicObject.getInt(mIndexField)));
30-
}
45+
super.build(results);
3146
}
3247
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class RealmScatterDataSet<T extends RealmObject> extends RealmLineScatter
2828

2929

3030
/**
31-
* Constructor for creating a LineDataSet with realm data.
31+
* Constructor for creating a ScatterDataSet with realm data.
3232
*
3333
* @param result the queried results from the realm database
3434
* @param yValuesField the name of the field in your data object that represents the y-value
@@ -41,7 +41,7 @@ public RealmScatterDataSet(RealmResults<T> result, String yValuesField) {
4141
}
4242

4343
/**
44-
* Constructor for creating a LineDataSet with realm data.
44+
* Constructor for creating a ScatterDataSet with realm data.
4545
*
4646
* @param result the queried results from the realm database
4747
* @param yValuesField the name of the field in your data object that represents the y-value

0 commit comments

Comments
 (0)