-3

I need to get all bars but I am getting single bar (last array).Even the logic is right.I don't know please help me.Thanks in advance.I search in google but I didn't got answer.

Below is main part of full code

Initialize

              String empName[];
     int converted[], notconverted[], notcontacted[];

Here I made Json parsing the data coming from the server

            for (int i = 0; i < employees_array.length(); i++) {
                        empName = new String[employees_array.length()];
                        converted = new int[employees_array.length()];
                        notconverted = new int[employees_array.length()];
                        notcontacted = new int[employees_array.length()];
                        JSONObject empData = employees_array.getJSONObject(i);
                        empName[i] = empData.getString("EmployeeName");
                        empName[i].replace("-?\\d+.\\d+", " ");
                        if (empData.has("ConvertedActionCount")) {
                            converted[i] = employees_array.getJSONObject(i).getInt("ConvertedActionCount");
                        }
                        if (empData.has("NotConvertedActionCount")) {
                            notconverted[i] = employees_array.getJSONObject(i).getInt("NotConvertedActionCount");
                        }
                        if (empData.has("NotContactedActionCount")) {
                            notcontacted[i] = employees_array.getJSONObject(i).getInt("NotContactedActionCount");
                        }


                        Log.d("arraysize", converted[i] + "");
                        function_EMPChart(empArraysize, empName, converted, 
                    notconverted, notcontacted);

I am passing values to function Emp chart method

      private void function_EMPChart(int empArraysize, String[] s, int[] 
         converted, int[] notconverted, int[] notcontacted) {
    XAxis xl = msc_managerDashboard_empStats.getXAxis();
    xl.setPosition(XAxis.XAxisPosition.BOTTOM);

    xl.setDrawAxisLine(true);
    xl.setDrawGridLines(false);
    xl.setGranularity(10f);

    YAxis yl = msc_managerDashboard_empStats.getAxisLeft();

    yl.setDrawAxisLine(true);
    yl.setDrawGridLines(true);
    yl.setAxisMinimum(0f); // this replaces setStartAtZero(true)
    YAxis yr = msc_managerDashboard_empStats.getAxisRight();

    yr.setDrawAxisLine(true);
    yr.setDrawGridLines(false);
    yr.setAxisMinimum(0f);

    ArrayList<BarEntry> yVals = new ArrayList<BarEntry>();
    ArrayList<String> xVals = new ArrayList<>();
    xVals = new ArrayList<String>(Arrays.asList(s));
    msc_managerDashboard_empStats.setFitBars(true);


    for (int j = 0; j < s.length; j++) {
        Log.d("checklength", j + "");

        int value1 = converted[j];
        int value2 = notconverted[j];
        int value3 = notcontacted[j];
        Log.d("getvalue", value1 + " " + value2 + " " + value3);

        yVals.add(new BarEntry(j, new float[]{value1, value2, value3}));
        yVals.add(new BarEntry(4, new float[]{10.0f, 20.0f, 30.0f}));
        yVals.add(new BarEntry(5, new float[]{5.0f, 15.0f, 20.0f}));


        Log.d("checky", yVals.add(new BarEntry(j, new float[]{converted[j], notconverted[j], notcontacted[j]})) + "");

    }
    BarDataSet set1 = new BarDataSet(yVals, "");
    set1.setColors(getColors());
    ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
    dataSets.add(set1);
    BarData data = new BarData(dataSets);
    data.notifyDataChanged();

    msc_managerDashboard_empStats.setData(data);
    msc_managerDashboard_empStats.notifyDataSetChanged();
    msc_managerDashboard_empStats.invalidate();


    Log.d("yvalues", yVals + "");


    msc_managerDashboard_empStats.setFitBars(true);


}
sainadh
  • 11
  • 8
  • No but in MP chart there is multiple charts inside listview .there he is using same data but I want different data.This is the link https://github.com/JeffWangGithub/MPChartSample – sainadh Mar 12 '18 at 16:29
  • then simply download the project and run it on your machine and try to play with it little bit. – Nouman Ch Mar 12 '18 at 16:35
  • I am trying But I don't know where to add Arraylist in adapter or activity? – sainadh Mar 12 '18 at 16:37

1 Answers1

0

I guess you are using this library. I don't really understand your code in what you try to accomplish (I see multiple bars per employee I guess). At least what is wrong, you call setData within this for loop for (int i = 0; i < employees_array.length(); i++), which means you override the data every time. You should create a list of BarDataSets in that for loop and set the data in the end. Your main issue here is that you create a new set of data every time. If you keep the same set your code might also work. Define dataSets once in the beginning and keep adding your sets. I mean this line:

ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);

I guess you want to have a grouped bar chart which you can find here: https://github.com/PhilJay/MPAndroidChart/wiki/Setting-Data#grouped-barchart

Example:

YourData[] group1 = ...;
YourData[] group2 = ...;

List<BarEntry> entriesGroup1 = new ArrayList<>();
List<BarEntry> entriesGroup2 = new ArrayList<>();

// fill the lists
for(int i = 0; i < group1.length; i++) {
    entriesGroup1.add(new BarEntry(i, group1.getValue()));
    entriesGroup2.add(new BarEntry(i, group2.getValue()));
}

BarDataSet set1 = new BarDataSet(entriesGroup1, "Group 1");
BarDataSet set2 = new BarDataSet(entriesGroup2, "Group 2");

BarData data = new BarData(set1, set2);
data.setBarWidth(barWidth); // set the width of each bar
barChart.setData(data);
barChart.groupBars(1980f, groupSpace, barSpace); // perform the "explicit" grouping
barChart.invalidate(); // refresh
Rene
  • 1,027
  • 10
  • 18
  • Its a stacked bar chart.I am trying to store the data in arrays and using that arrays in other method – sainadh Mar 16 '18 at 10:32
  • Yes, but as I said that method is in the for loop and does `new ArrayList()`, that way you override the previous dataset and only use the last item of the for loop. – Rene Mar 16 '18 at 10:45