MPAndroidChart How to represent multiple dataset object with different number of points in the same chart line

隐身守侯 提交于 2019-11-29 12:12:09

The solution is simple.

Create an x-values array ranging from 0 to 36:

ArrayList<String> xvals = new ArrayList<String>();

for(int i = 0; i <= 35; i++) {
   xvals.add("Label"+i);
}

// create your entries...

// add the data...

In that way you can display entries with x-indices ranging from 0 to 35. Make sure you set the correct x-index for each entry you want to display.

Instead of creating 0 to 36 {min - max}, create Union of Xvalues1 & Xvalues2 with it's respective Yvalues & 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]

Plot X against Y1, Y2, Y3 ...,

As far as I know MPAndroidChart doesn't plot null values & safely skips to next value.

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;
}

XYMerger

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;
    }

}

IntegerComparator

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));
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!