0

I am trying to set data from string arraylist to xaxis but it gives duplicate data and if i add xAxis.setGranularity(1f); it gives java.lang.ArrayIndexOutOfBoundsException: length=12; index=-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();

Below is the way i am populating the array list

   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();
Shafin Raza
  • 21
  • 1
  • 8

1 Answers1

0

I was facing the same issue to resolve it add the following line:

xAxis.setGranularityEnabled(true);

instead of

xAxis.setGranularity(1f);

Check the link for more details about Axis.

Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40