0
votes

I am using MPAdroid to plot a Bar chart with grouped bars and labels in front of them. But the result I am getting is shown in the scrot below enter image description here

The upper chart is chart1 and bottom one is chart2. Now the real order of the labels should have been TEMP-3,TEMP-1,BENTLER-1,BENTLER-2,ANNE-LOSE,ANNE-LOSE,BENTLER-1,BENTLER-2,ANNE-LOSE(from the below). But you can see the order is messed up. With TEMP-3(The first label) being repeated unnecessarily. And also when i print the machines arraylist in my logs, it is same as the desired output. I have posted the logs at the bottom.

This is how I am setting the chart.

       BarChart chart = (BarChart) findViewById(R.id.chart2);
       BarDataSet bds3 = new BarDataSet(percentage,"percentage");            
       BarData ddata = new BarData(bds3);
       float barWidth = 0.45f;
       ddata.setBarWidth(barWidth);
        chart.setScaleEnabled(true);
        chart.setPinchZoom(true);
        chart.setData(ddata);
        XAxis xa = chart.getXAxis();
        String[] mac = new String[machines.size()];
        for(int i=0;i<machines.size();++i)
        {
           mac[i] = machines.get(i);
           Log.i("Message5432",mac[i]);
        }
        xa.setValueFormatter(new MyAxisValueFormatter(mac));
        chart.fitScreen();
       xa.setLabelCount(machines.size()+1,true);
        xa.setCenterAxisLabels(true);

        xa.setGranularity(1f);
        xa.setGranularityEnabled(true);
        chart.setTouchEnabled(true);
        chart.setDragEnabled(true);
        chart.setScaleEnabled(true);
        chart.setVisibility(View.VISIBLE);
        chart.invalidate();


        BarDataSet bds2 = new BarDataSet(barEntries, "Target");

        BarChart chart1 = (BarChart) findViewById(R.id.chart1);
        BarDataSet bds1 = new BarDataSet(barEntries1,"Prepared");
        bds1.setColor(Color.parseColor("#F44336"));
        BarData ddata1 = new BarData(bds1,bds2);
        chart1.setScaleEnabled(true);
        chart1.setPinchZoom(true);
        chart1.setData(ddata1);
        XAxis xa1 = chart1.getXAxis();
        xa1.setValueFormatter(new MyAxisValueFormatter(mac));
        chart1.setTouchEnabled(true);
        chart1.setDragEnabled(true);
        chart1.setScaleEnabled(true);
        float groupSpace = 0.08f;
        float barSpace = 0.02f; 
        chart1.fitScreen();
        ddata1.setBarWidth(barWidth);
        xa1.setLabelCount(machines.size()+1,true);
        chart1.groupBars( -0.5f,groupSpace, barSpace);
        chart1.setFitBars(true);
        chart1.setVisibility(View.VISIBLE);
        xa1.setGranularity(1f);
        xa1.setGranularityEnabled(true);
        xa1.setCenterAxisLabels(true);
        chart1.invalidate();

And I have printed the data in machines ArrayList and I am positive the data in it is not faulty. I am not posting the code of Downloading the data and putting it in the machines arrayList.

Here is my MyAxisValueFormatter class.

  public class MyAxisValueFormatter implements IAxisValueFormatter
{
    String [] machines;
    public MyAxisValueFormatter(String[] arr)
    {
        this.machines = arr;
    }

    @Override
    public String getFormattedValue(float value, AxisBase axis) {
        return machines[(int)value];
    }

    @Override
    public int getDecimalDigits() {
        return 0;
    }
}

And this is my OnCreate method,(the part related to the graph)

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_production_graphs);
        barEntries.clear();
        barEntries1.clear();
        percentage.clear();
        machines.clear();
        dta.clear();
        gblprpd.clear();

        BarChart chart = (BarChart) findViewById(R.id.chart2);
        chart.invalidate();
        chart.setVisibility(View.GONE);
        chart = (BarChart) findViewById(R.id.chart1);
        chart.invalidate();
        chart.setVisibility(View.GONE);
        // This portion calls an Aysnc task for Downloading files.
        try {
            new ProductionGraphs.FTPDownloader().execute();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

These are my logs.

07-04 19:28:48.185 10169-10169/com.example.quickstart I/Message5432: TEMP-3 
    TEMP-1 
    BENTLER-1 
    BENTLER-2 
    ANNE-LOSE 
    TEMP-3 
    BENTLER-1 
    BENTLER-2 
07-04 19:28:48.186 10169-10169/com.example.quickstart I/Message5432: ANNE-LOSE 

This is how set bar values

 int data = in.read();
                   s = "";
                   int commas = 0;
                   int max = 0;
                   int xa = 0, xa1 = 0,xa2=0;
                   while (data != -1) {

                       char ch = (char) data;
                       if (ch != ',')
                       {
                           s = s + ch;
                           data = in.read();
                           continue;
                       }
                       else
                           {
                           ++commas;
                           s = s + " ";

                           if (commas == 8) {
                               barEntries.add(new BarEntry(xa, Float.parseFloat(s)));
                               Log.i("FTP", s);
                               ++xa;
                           }
                           if (commas == 6) {
                               barEntries1.add(new BarEntry(xa1, Float.parseFloat(s)));
                               Log.i("FTP", s);
                               ++xa1;
                           }

                           if (commas == 4) {

                               machines.add(s);
                               Log.i("FTP", s);
                           }
                           gblprpd.add(s);
                           s = "";
                       }
                           data = in.read();

                           if (commas == 8) {
                               s = s + (char) data;
                               for (int i = 0; i < 5; ++i)
                                   s = s + (char) in.read();
                               s = s + " ";
                               percentage.add(new BarEntry(xa2,Float.parseFloat(s)));
                               ++xa2;
                               gblprpd.add(s);
                               s = "";
                               commas = 0;
                               data = in.read();
                           }
                       }

This is the input from the server(The values are different, but the format is the same.)

enter image description here

I hope I have given you sufficient information to work with. Please feel free to ask for more. Thanks in advance for your time.

1

1 Answers

1
votes

This happens because you have to specify exact number of labels to xAxis:

xa1.setLabelCount(barEntries.size());

Also see this: https://stackoverflow.com/a/48116532/3101777