我试图在MpAndroid折线图中将字符串数组中的数据设置为xaxis

时间:2018-04-12 06:39:36

标签: android mpandroidchart linechart

  

我正在尝试将字符串arraylist中的数据设置为xaxis,但它会提供重复数据,如果我添加xAxis.setGranularity(1f);它给出了java.lang.ArrayIndexOutOfBoundsException:length = 12;指数= -1

As you can see the dates at xaxis are getting repeated

      graphxaxis = new ArrayList<String>();
      graphyaxis = new ArrayList<Entry>();
      fromyaxis = new ArrayList<Entry>();
      toyaxis = new ArrayList<Entry>();

    mChart = (LineChart) findViewById(R.id.chart1);
    mChart.setOnChartGestureListener(this);
    mChart.setOnChartValueSelectedListener(this);
    mChart.setDrawGridBackground(true);

    // no description text
    mChart.getDescription().setEnabled(false);

    // enable touch gestures
    mChart.setTouchEnabled(false);

    // enable scaling and dragging
    mChart.setDragEnabled(true);
    mChart.setScaleEnabled(true);
    mChart.setPinchZoom(false);
    MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
    mv.setChartView(mChart); // For bounds control
    mChart.setMarker(mv); // Set the marker to the chart


    XAxis xAxis = mChart.getXAxis();
    xAxis.setGranularity(1f);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setValueFormatter(new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return graphxaxis.get((int)value);
        }
    });

   xAxis.setDrawGridLines(false);
    YAxis leftAxis = mChart.getAxisLeft();     
    leftAxis.setAxisMinimum(0f);      
    leftAxis.setDrawZeroLine(false);
    leftAxis.setDrawGridLines(false);
    mChart.animateX(2500);
    mChart.invalidate();
    Legend l = mChart.getLegend();

    // modify the legend ...
    l.setForm(Legend.LegendForm.LINE);

    // // dont forget to refresh the drawing
   mChart.invalidate();
  

下面是我填充数组列表的方式

   graphxaxis.add(jsonobject.getString("date").substring(0,6));

                  graphyaxis.add(new Entry(Float.valueOf(i),Float.parseFloat(jsonobject.getString("actual"))));

                  fromyaxis.add(new Entry(Float.valueOf(i),Float.parseFloat(jsonobject.getString("from"))));

                  toyaxis.add(new Entry(Float.valueOf(i),Float.parseFloat(jsonobject.getString("to"))));

                  mChart.notifyDataSetChanged();

1 个答案:

答案 0 :(得分:0)

我遇到同样的问题要解决它添加以下行:

C->Haskell Compiler, version 0.13.13 (gtk2hs branch) "Bin IO", 27 May 2012

而不是

xAxis.setGranularityEnabled(true);

检查link以获取有关Axis的更多详细信息。

相关问题