如何在Mpandroidchart折线图内的xAxis上设置标签?

时间:2019-10-07 11:08:48

标签: android mpandroidchart

我试图在折线图中mpandroidchart内的xAxis上给标签,但每次都会出错。

我哪里出错了?

还是应该采用另一种方法?

如果必须的话,那是什么?

 public class OpdIpdAnalysis extends AppCompatActivity{

            ApiService service;
            TokenManager tokenManager;
            Call<ModelResponse> call;

            private ListView mListView;

             //GraphView
            private LineChart mLineChart;

             @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_opd_ipd_analysis);


                //GraphView casting
                mLineChart =(LineChart) findViewById(R.id.lineChart);
                mLineChart.setTouchEnabled(true);
                mLineChart.setPinchZoom(true);

                    //code for retrofit call

                    call = service.getAllOpdIpdData();
                    call.enqueue(new Callback<ModelResponse>() {
                        @Override
                        public void onResponse(Call<ModelResponse> call, Response<ModelResponse> response) {
                            myProgressBar.setVisibility(View.GONE);
                            if (response.isSuccessful() && response.body() != null) {
                                updatUI(response.body().getOpdIpdComparison());

                                List<Entry> entries1 = new ArrayList<>();
                                List<Entry> entries2 = new ArrayList<>();
                                //final List<String> xLabel = new ArrayList<>();

                                for (OpdIpdModel opdIpdModel : response.body().getOpdIpdComparison()){

                                    entries1.add(new Entry(opdIpdModel.getMonth(),opdIpdModel.getoPD()));
                                    entries2.add(new Entry(opdIpdModel.getMonth(), opdIpdModel.getiPD()));
                                    //xLabel.add(opdIpdModel.getMonthString());

                                    String[] xLabel = {opdIpdModel.getMonthString()};
                                    XAxis xAxis = mLineChart.getXAxis();
                                    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
                                    xAxis.setDrawGridLines(false);
                                    xAxis.setGranularity(1);
                                    xAxis.setAxisMinimum(1);
                                    xAxis.setCenterAxisLabels(true);
                                    xAxis.setValueFormatter(new MyXAxisValueFormatter(xLabel));
                                }

                                LineDataSet lineDataSet1 = new LineDataSet(entries1, "OpdLine");
                                lineDataSet1.setDrawCircles(false);
                                lineDataSet1.setLineWidth(4f);
                                lineDataSet1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
                                lineDataSet1.setColor(randomAndroidColor1);

                                LineDataSet lineDataSet2 = new LineDataSet(entries2, "IpdLine");
                                lineDataSet2.setDrawCircles(false);
                                lineDataSet2.setLineWidth(4f);
                                lineDataSet2.setMode(LineDataSet.Mode.CUBIC_BEZIER);
                                lineDataSet2.setColor(randomAndroidColor2);

                                //add the data
                                List<ILineDataSet> iLineDataSets = new ArrayList<>();
                                iLineDataSets.add(lineDataSet1);
                                iLineDataSets.add(lineDataSet2);

                                LineData lineData = new LineData(iLineDataSets);
                                mLineChart.setData(lineData);
                                mLineChart.setVisibility(View.VISIBLE);
                                mLineChart.animateXY(1500,1500);
                                mLineChart.setVisibleXRangeMaximum(10);

                                //mLineChart.setXAxisRenderer(new CustomXAxisRenderer(mLineChart.getViewPortHandler(),mLineChart.getTransformer(YAxis.AxisDependency.LEFT)));

                                Description description = new Description();
                                description.setText("Growth rate per month");
                                description.setTextSize(12);
                                description.setTextColor(Color.BLACK);
                                mLineChart.setDescription(description);
                                mLineChart.invalidate();

                            }
                        }           
        }

我的应用每次都崩溃。

我收到一个错误:

  

java.lang.ArrayIndexOutOfBoundsException:length = 4; index = 4

现在如何解决此问题并按预期设置标签?

1 个答案:

答案 0 :(得分:0)

问题在于,entries1,entries2和xValues的大小不相同。您需要在以下行上设置断点:

xAxis.setCenterAxisLabels(true);

这时检查三个列表的大小是否相等。之后,您需要调试冲突的位置,然后通过添加0值或空字符串(如果您得到的是空值)来处理情况。但是,所有三个arraylist的大小都应该相同。