与其创建0 到 36 {min - max},不如创建 Union of Xvalues1
&Xvalues2
及其各自Yvalue
的 s & Vice-Versa。
int[] Xvalues1 = { 0, 10, 15, 20, 25, 30, 35 };
int[] Yvalues1 = { 5, 112, 23, 34, 50, 100, 130 };
int[] Xvalues2 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 10 };
int[] Yvalues2 = { 1, 5, 20, 15, 10, 30, 40, 70, 75, 100 };
// X = X1 ⋃ X2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 15, 20, 25, 30, 35]
// Y1 ⋃ Y2 (w.r.t X) = [5, null, null, null, null, null, null, null, null, 112, 23, 34, 50, 100, 130]
绘制X对 Y1、Y2、Y3 ...,
据我所知MPAndroidChart不会绘制null
值并安全地跳到下一个值。
public void startMerging() throws ArrayIndexOutOfBoundsException {
List<Integer> x1 = new ArrayList<Integer>();
List<Integer> y1 = new ArrayList<Integer>();
List<Integer> x2 = new ArrayList<Integer>();
List<Integer> y2 = new ArrayList<Integer>();
List<XYMerger> mergedData = getMergedData(Xvalues1, Yvalues1, Xvalues2);
for (XYMerger xy : mergedData) {
x1.add(xy.getX());
y1.add(xy.getY());
}
mergedData = getMergedData(Xvalues2, Yvalues2, Xvalues1);
for (XYMerger xy : mergedData) {
x2.add(xy.getX());
y2.add(xy.getY());
}
mergedData.clear();
System.out.println(x1); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 15, 20, 25, 30, 35]
System.out.println(y1); // [5, null, null, null, null, null, null, null, null, 112, 23, 34, 50, 100, 130]
System.out.println("\n");
System.out.println(x2); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 15, 20, 25, 30, 35] // X = X1 = X2
System.out.println(y2); // [1, 5, 20, 15, 10, 30, 40, 70, 75, 100, null, null, null, null, null]
}
/**
* @param orgXvals : Original X Values
* @param orgYvals : Original Y Values
* @param extdXvals : Extended X values to be merged
* @return Sorted mergedData
*/
private List<XYMerger> getMergedData(int[] orgXvals , int[] orgYvals , int[] extdXvals) {
HashSet<Integer> tempSet = new HashSet<Integer>();
List<XYMerger> tempMerger = new ArrayList<XYMerger>();
tempSet.clear();
tempMerger.clear();
for (int i = 0; i < orgXvals.length; i++) {
tempSet.add(orgXvals[i]);
XYMerger xy = new XYMerger();
xy.setX(orgXvals[i]);
xy.setY(orgYvals[i]);
tempMerger.add(xy);
}
for (int i = 0; i < extdXvals.length; i++) {
if (tempSet.add(extdXvals[i])) {
XYMerger xy = new XYMerger();
xy.setX(extdXvals[i]);
xy.setY(null);
tempMerger.add(xy);
}
}
Collections.sort(tempMerger, new IntegerComparator(true));
return tempMerger;
}
XY合并
public class XYMerger {
int x;
Integer y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public Integer getY() {
return y;
}
public void setY(Integer y) {
this.y = y;
}
}
整数比较器
public class IntegerComparator implements java.util.Comparator<XYMerger> {
boolean ascending;
public IntegerComparator(boolean isAscending) {
this.ascending = isAscending;
}
@Override
public int compare(XYMerger obj1 , XYMerger obj2) {
if (ascending) {
return (obj1.getX() < obj2.getX() ? -1 : (obj1.getX() == obj2.getX() ? 0 : 1));
}
return (obj1.getX() > obj2.getX() ? -1 : (obj1.getX() == obj2.getX() ? 0 : 1));
}
}