在条形图中加载数据后条宽减小(MPAndroidChart)

时间:2018-01-06 05:18:10

标签: android mpandroidchart

我搜索了很多,但找不到符合我要求的合适解决方案。在第一次加载数据时条形宽度正常,但是对于大量数据条形宽度减小,我做了很多更改,但没有任何效果。我已经附上了下面的代码和图片。当条形图上的Axis数据设置有任何帮助时,图例不起作用? 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();

2 个答案:

答案 0 :(得分:0)

您似乎需要修改视口。您可以在here

中找到文档

首先,您必须确保条形图不会在设置的视口内压缩,并且如果yEntrys ArrayList中有更多数据,则可以滚动。

为此,您必须设置最大和最小可见范围。假设你想一次看到5个酒吧。可以根据您希望条形宽度的偏好来设置此值。

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

通过这种方式,您可以确保条宽度几乎保持不变,无论数据是什么。

代码编辑 -

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

当有5个数据时 -

Pic-5

当有超过100个数据时

Pic-100

答案 1 :(得分:0)

在以下行之后:

mBarChart.setData(data);

添加:

mBarChart.setVisibleXRange(1,3);

您可以将1替换为显示的最小值数,将3替换为图表上显示的最大值数。对于您必须滚动图表的下一个值,上面的行将一次显示3个值。默认情况下,mpandroid图表会尝试覆盖视口中的所有值,当您在运行时继续添加条目时,您的条形变得很薄以进行调整。添加上面一行后,您的图表将显示您描述的最大值,以便轻松查看。