ashish
ashish

Reputation: 307

bar width reduce after loading data in bar chart (MPAndroidChart)

I have searched a lot and could not found any suitable solution for my requirement. While loading data for the first time the bar width is OK , but for large amount of data the bar width gets reduced, I did many changes on that but nothing worked. I have attached the code and pics below.and legend is not working while set Axis data on bar chart any help? first Data

after loading data

RequestQueue requestQueue = Volley.newRequestQueue(getContext());
    JsonObjectRequest jsOnbjRequest = new
            JsonObjectRequest(Request.Method.POST,
                    Constants.GETPICCHART, pieObject,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response)
                        {
                            final ArrayList<BarEntry> yEntrys = new ArrayList<>();
                            Log.d("pieData",response.toString());
                            try {
                                JSONArray jsonarray = (JSONArray) response.get("piechartlist");
                                for (int i = 0; i < jsonarray.length(); i++) {
                                    JSONObject jsonobject = jsonarray.getJSONObject(i);

                                    String messageSent = jsonobject.getString("messageSent").trim();
                                    String failed = jsonobject.getString("failed").trim();
                                    String rejected = jsonobject.getString("rejected").trim();
                                    String expired = jsonobject.getString("expired").trim();
                                    String unDelivered = jsonobject.getString("unDelivered");
                                    String delivered = jsonobject.getString("delivered");
                                    String ndnc = jsonobject.getString("ndnc");
                                    yEntrys.add(new BarEntry(Float.parseFloat(messageSent),Integer.parseInt(messageSent)));
                                    yEntrys.add(new BarEntry(Float.parseFloat(failed),Integer.parseInt(failed)));
                                    yEntrys.add(new BarEntry(Float.parseFloat(rejected),Integer.parseInt(rejected)));
                                    yEntrys.add(new BarEntry(Float.parseFloat(expired),Integer.parseInt(expired)));
                                    yEntrys.add(new BarEntry(Float.parseFloat(unDelivered),Integer.parseInt(unDelivered)));
                                    yEntrys.add(new BarEntry(Float.parseFloat(delivered),Integer.parseInt(delivered)));
                                    yEntrys.add(new BarEntry(Float.parseFloat(ndnc),Integer.parseInt(ndnc)));

                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            BarDataSet set1;

                            set1 = new BarDataSet(yEntrys,"");
                            set1.setBarBorderWidth(1f);
                            set1.setDrawIcons(false);
                            ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
                            dataSets.add(set1);
                            ArrayList<Integer> colors = new ArrayList<Integer>();
                            colors.add(ContextCompat.getColor(getContext(),R.color.massage_Sent));
                            colors.add(ContextCompat.getColor(getContext(),R.color.failed));
                            colors.add(ContextCompat.getColor(getContext(),R.color.rejected));
                            colors.add(ContextCompat.getColor(getContext(),R.color.expiered));
                            colors.add(ContextCompat.getColor(getContext(),R.color.undelivred));
                            colors.add(ContextCompat.getColor(getContext(),R.color.delivered));
                            colors.add(ContextCompat.getColor(getContext(),R.color.ndnc));
                            set1.setColors(colors);
                            mBarChart.setExtraBottomOffset(1);
                            BarData data = new BarData(dataSets);
                            data.setBarWidth(0.9f);
                            data.setValueFormatter(new MyValueFormatter());
                            XAxis xAxis = mBarChart.getXAxis();
                            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
                            xAxis.setLabelRotationAngle(30);
                            xAxis.setSpaceMax(5f);
                            xAxis.setGranularity(1f);
                            xAxis.setDrawGridLines(false);
                            xAxis.setGranularityEnabled(true);
                            Legend l=mBarChart.getLegend();
                            LegendEntry l1=new LegendEntry("messageSent", Legend.LegendForm.DEFAULT,10f,2f,null,ContextCompat.getColor(getContext(),R.color.massage_Sent));
                            LegendEntry l2=new LegendEntry("Failed", Legend.LegendForm.DEFAULT,10f,2f,null, ContextCompat.getColor(getContext(),R.color.failed));
                            LegendEntry l3=new LegendEntry("Rejected", Legend.LegendForm.DEFAULT,10f,1f,null,ContextCompat.getColor(getContext(),R.color.rejected));
                            LegendEntry l4=new LegendEntry("Expired", Legend.LegendForm.DEFAULT,10f,2f,null, ContextCompat.getColor(getContext(),R.color.expiered));
                            LegendEntry l5=new LegendEntry("Undelivred", Legend.LegendForm.DEFAULT,10f,2f,null, ContextCompat.getColor(getContext(),R.color.undelivred));
                            LegendEntry l6=new LegendEntry("Delivered", Legend.LegendForm.DEFAULT,10f,1f,null,ContextCompat.getColor(getContext(),R.color.delivered));
                            LegendEntry l7=new LegendEntry("NNDNC", Legend.LegendForm.DEFAULT,10f,1f,null,ContextCompat.getColor(getContext(),R.color.ndnc));
                            mBarChart.setFitBars(true);
                            l.setEnabled(true);
                            l.setWordWrapEnabled(true);
                            l.setCustom(new LegendEntry[]{l1,l2,l3,l4,l5,l6,l7});
                            mBarChart.setData(data);
                            mBarChart.invalidate();

Upvotes: 2

Views: 3229

Answers (2)

Muhammad Saad Rafique
Muhammad Saad Rafique

Reputation: 3189

After below line:

mBarChart.setData(data);

Add:

mBarChart.setVisibleXRange(1,3);

Where you can replace 1 as minimum number of values shown and 3 as maximum number of values shown on your chart. Above line will show 3 values at a time for next values you have to scroll chart. By default mpandroid charts try to cover all values in viewport and when you keep on adding entries on runtime your bars becomes thin to adjust. Once you add above line your chart will show maximum values as you described so it can b seen easily.

Upvotes: 1

Subrata Mondal
Subrata Mondal

Reputation: 852

It seems that you need to modify your viewport. You can find a documentation in here

First you have to ensure that the Bars doesn't crumple inside the set Viewport and can be scrolled if there are more datas in the yEntrys ArrayList.

To do that you have to set the maximum and minimum visible range. Say you want to see 5 bars at a time. This value can be set according to your preference of how much wide you want the bars to be.

mBarChart.setVisibleXRangeMaximum(5);//no more than 5 values on the x-axis can be viewed at once without scrolling
mBarChart.setVisibleXRangeMinimum(5);//it is not possible to zoom in further than 5 values on the x-axis

This way you can ensure that the Bar width remains almost same, whatever may be the datas.

Code Edited -

final ArrayList<BarEntry> yEntrys = new ArrayList<>();
Log.d("pieData",response.toString());
try {
    JSONArray jsonarray = (JSONArray) response.get("piechartlist");
    for (int i = 0; i < jsonarray.length(); i++) {
        JSONObject jsonobject = jsonarray.getJSONObject(i);
        //...
        yEntrys.add(new BarEntry(Float.parseFloat(messageSent),Integer.parseInt(messageSent)));
        yEntrys.add(new BarEntry(Float.parseFloat(failed),Integer.parseInt(failed)));
        yEntrys.add(new BarEntry(Float.parseFloat(rejected),Integer.parseInt(rejected)));
        yEntrys.add(new BarEntry(Float.parseFloat(expired),Integer.parseInt(expired)));
        yEntrys.add(new BarEntry(Float.parseFloat(unDelivered),Integer.parseInt(unDelivered)));
        yEntrys.add(new BarEntry(Float.parseFloat(delivered),Integer.parseInt(delivered)));
        yEntrys.add(new BarEntry(Float.parseFloat(ndnc),Integer.parseInt(ndnc)));
    }
} catch (JSONException e) {
    e.printStackTrace();
}

BarDataSet set1;

set1 = new BarDataSet(yEntrys,"");
//...
set1.setColors(colors);
mBarChart.setExtraBottomOffset(1);
BarData data = new BarData(dataSets);
//data.setBarWidth(0.9f);//This line is irrelevant now.
//mBarChart.setFitBars(true);//Irrelevant. Also can remove this line
mBarChart.setData(data);
//Add these two lines after setting the data
mBarChart.setVisibleXRangeMaximum(5);
mBarChart.setVisibleXRangeMinimum(5);

When there are 5 datas-

Pic-5

When there are more that 100 datas-

Pic-100

Upvotes: 0

Related Questions