Plot Multiple Charts in one in MPAndroidChart

与世无争的帅哥 提交于 2019-11-29 14:09:08

You can do this with LineChart and unifying the xAxis and ignoring the "null" point for each dataset. See the code below:

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

The final result will be:

Looks that the red line of dataset1 have 2 more points than blue line. If the dataset1 have only these two points(at position 1 and 3) like this:

ArrayList<Entry> dataset1 = new ArrayList<Entry>();
    dataset1.add(new Entry(2f, 1));
    dataset1.add(new Entry(4f, 3));

it will be the result:

Krishna Mohan Singh

you can use this Line Chart passing multiple Data Set

LineChart lineChart1 = (LineChart) findViewById(R.id.chart1);

    ArrayList<Entry> entries = new ArrayList<>();
    entries.add(new Entry(4f, 0));
    entries.add(new Entry(8f, 1));
    entries.add(new Entry(6f, 2));
    entries.add(new Entry(10f, 3));
    entries.add(new Entry(18f, 4));
    entries.add(new Entry(9f, 5));

    ArrayList<Entry> entry = new ArrayList<>();
    entry.add(new Entry(3f, 0));
    entry.add(new Entry(10f, 1));
    entry.add(new Entry(4f, 2));
    entry.add(new Entry(14f, 3));
    entry.add(new Entry(12f, 4));
    entry.add(new Entry(5f, 5));
    ArrayList<LineDataSet> lines = new ArrayList<LineDataSet> ();
    String[] xAxis = new String[] {"1", "2", "3", "4", "5","6"};
    LineDataSet lDataSet1 = new LineDataSet(entries, "DataSet1");
    lDataSet1.setDrawFilled(true);
    lines.add(lDataSet1);

    LineDataSet lDataSet2 = new LineDataSet(entry, "DataSet2");
    lDataSet2.setDrawFilled(true);
    lines.add(lDataSet2);

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