问题
I am using MPAndroidChart for charting. I am building a line chart which has multiple dataset objects.
From the examples provided by MPAndroidChart I can observe that multiple dataset objects can be drawn in the same line chart but with the same number of points (y-values) and those y-values referenced to the same x-values. What happens if I want to draw multiple dataset objects in the same line chart with different number of points (y-values) referenced to different x-values for each one? How can I get rid of this? I cannot be able to do it.
For example:
Dataset object 1 Y-values: 0, 12, 23, 34, 50, 100, 130
X-values: 0, 10, 15, 20, 25, 30, 35
Dataset object 2 Y-values: 1, 5, 10, 15, 20, 30, 40, 70, 75, 80
X-values: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
回答1:
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.
回答2:
Instead of creating 0 to 36 {min - max}, create Union of Xvalues1
& Xvalues2
with it's respective 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]
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));
}
}
来源:https://stackoverflow.com/questions/28116357/mpandroidchart-how-to-represent-multiple-dataset-object-with-different-number-of