您可以使用 LineChart 并统一 xAxis 并忽略每个数据集的“空”点来执行此操作。请看下面的代码:
//float[] dataInput = {
// 1f, 2f, 3f, 4f, 5f, 6f, 7f
//};
//values for datainput Dataset1 at your axisone positions
ArrayList<Entry> dataset1 = new ArrayList<Entry>();
dataset1.add(new Entry(1f, 0));
dataset1.add(new Entry(2f, 1));
dataset1.add(new Entry(3f, 2));
dataset1.add(new Entry(4f, 3));
dataset1.add(new Entry(5f, 4));
dataset1.add(new Entry(6f, 5));
dataset1.add(new Entry(7f, 6));
//float[] dataIn = {
// 3f, 4f, 5f, 6f, 7f, 8f, 9f
//};
//values for datainput Dataset2 at your axisone positions
ArrayList<Entry> dataset2 = new ArrayList<Entry>();
dataset2.add(new Entry(3f, 0));
dataset2.add(new Entry(4f, 2));
dataset2.add(new Entry(5f, 4));
dataset2.add(new Entry(6f, 5));
dataset2.add(new Entry(7f, 6));
dataset2.add(new Entry(8f, 7));
dataset2.add(new Entry(9f, 8));
//String[] xAxisOne = new String[] {
// "0", "1", "2", "3", "4", "5", "6"
//};
///String[] xAxisTwo = new String[] {
/// "0", "2", "4", "5", "6", "8", "9"
///};
// Union from xAxisOne and xAxisTwo
String[] xAxis = new String[] {"0", "1", "2", "3", "4", "5", "6", "8", "9"};
ArrayList<LineDataSet> lines = new ArrayList<LineDataSet> ();
LineDataSet lDataSet1 = new LineDataSet(dataset1, "DataSet1");
lDataSet1.setColor(Color.RED);
lDataSet1.setCircleColor(Color.RED);
lines.add(lDataSet1);
lines.add(new LineDataSet(dataset2, "DataSet2"));
LineChart chart = (LineChart) getView().findViewById(R.id.chart);
chart.setData(new LineData(xAxis, lines));
最终结果将是:
看起来 dataset1 的红线比蓝线多 2 个点。如果 dataset1 只有这两个点(在位置 1 和 3),如下所示:
ArrayList<Entry> dataset1 = new ArrayList<Entry>();
dataset1.add(new Entry(2f, 1));
dataset1.add(new Entry(4f, 3));
这将是结果: