1

I wanna format XAxis values from int to String (actually date) the problem is that formatter is not stopping when ArrayList size is reached and I get out of array range exception. What can I do about this ?

    barEntries=new ArrayList <> (  );
    XAxisLabels=new ArrayList <> (  );
    barEntries.add ( new BarEntry ( 0,0 ) );
    XAxisLabels.add ( "2017-10-29 09:26:07" );
    barEntries.add ( new BarEntry ( 1,400 ) );
    XAxisLabels.add ( "2017-10-28 19:04:22" );
    barEntries.add ( new BarEntry ( 2,200 ) );
    XAxisLabels.add ( "2017-10-28 19:05:12" );
    barEntries.add ( new BarEntry ( 3,300 ) );
    XAxisLabels.add ( "2017-10-29 09:26:07" );

            BarDataSet barDataSet = new BarDataSet ( barEntries, "DataSet" );
            barDataSet.setColors ( ColorTemplate.COLORFUL_COLORS );

            BarData barData = new BarData ( barDataSet );

            XAxis xAxis = barChart.getXAxis ( );
            xAxis.setPosition ( XAxis.XAxisPosition.BOTH_SIDED );
            xAxis.setValueFormatter ( new MyXAxisValueFormatter ( XAxisLabels ) );
            //xAxis.setGranularity ( 1 );
            xAxis.setCenterAxisLabels ( true );
            //xAxis.setAxisMinimum ( 1 );
            barChart.setData ( barData );
            barChart.notifyDataSetChanged ( );
            barChart.invalidate ( );

My Formatter:

public class MyXAxisValueFormatter implements IAxisValueFormatter{
        int counter=0;
        private ArrayList <String> mValues = new ArrayList <> (  );
        public MyXAxisValueFormatter(ArrayList<String> values)
        {
            this.mValues=values;
        }
        @Override
        public String getFormattedValue ( float value, AxisBase axis ) {
            String x;
            value=value+counter*0.3f;
            counter++;
            x= mValues.get ( (int)value);
            return x;
        }
}

Application screenshot

3 Answers 3

3
public class MyXAxisValueFormatter implements IAxisValueFormatter {
private List labels;

public MyXAxisValueFormatter(List<String> labels) {
    this.labels = labels;
}

@Override
public String getFormattedValue(float value, AxisBase axis) {
    try {
        int index = (int) value;
        return labels.get(index);
    } catch (Exception e) {
        return "";
    }
}
}
Sign up to request clarification or add additional context in comments.

19 Comments

It will work ... only check the conversion of float to string. This method will call automatically for each value you dont need to provide your arraylist. If it works please dont forget to mark this post as answer. Happy coding buddy :)
But how it gonna know where are Strings that it needs to take to sign x axis ?
Basically axis value formatter is called upon selected axis. So if you call it on specific axis it get values of all labels from there and convert them all one by one.
Test this you may need to change the condition of string as I dont get in your logic that what you are doing by multiplying value with 3.0f. Give it a try it will covert all of your float values to strings of given axis.Thank you :)
If you need further assistance you can text me anytime buddy :)
|
2

I've got similar problem - my formatting started at -1 - > so i had java.lang.ArrayIndexOutOfBoundsException So I fixed it like this:

@Override
    public String getFormattedValue(float value) {

        if (((int) value) < values.size() && (int) value >= 0) {
            //Log.e(TAG, "getFormattedValue: " + (int) value); // value is -1, exception!
            String strDate = values.get((int) value);
            if (mCurrentRange == 7)
                strDate = getDayOfWeekFromStr(strDate);
            else strDate = removeYearFromDate(strDate);
            return strDate;
        } else {
            return "0";
        }
    }

Comments

1

I am having some issue in adding String to xaxis

{
                    ArrayList<Entry> yvalues = new ArrayList<>();
                    ArrayList<String> labels = new ArrayList<String>();
                    String[] values = new String[]{"Jan","Feb","Mac"};
                    for (int i = 0; i < graphData.getDate().size(); i++) {
                        yvalues.add(new Entry(i,Float.parseFloat(graphData.getMeasurement().get(i))));
                    }
                    lineChart.getAxisRight().setEnabled(false);
                    LineDataSet dataSet = new LineDataSet(yvalues, "Cells");
                    dataSet.setFillAlpha(110);
                    ArrayList<ILineDataSet> dataSets = new ArrayList<>();
                    dataSets.add(dataSet);
                    dataSet.setLineWidth(3f);
                    dataSet.setValueTextColor(Color.WHITE);
                    dataSet.setColor(Color.RED);
                    LineData data = new LineData(dataSets);
                    lineChart.setData(data);
                    XAxis xAxis = lineChart.getXAxis();
                    xAxis.setValueFormatter(new MyXAxisValueFormatter(values));

                }

MyXAxisValueFormatter is this

public class XAxisValueFormatter extends IAxisValueFormatter{
    private String[] mvalues;
    public XAxisValueFormatter(String[] mvalues){
        this.mvalues = mvalues;
    }

    @Override
    public String getFormattedValue(float value, AxisBase axis) {
        System.out.println("value"+mvalues[(int)value]);
        return mvalues[(int)value];
    }
}

When adding value to MyXAxisValueFormatter it is showing error

setValueFormatter(com.github.mikephil.chartinf.formatter.ValueFormatter) in AxisBase cannot be applied to (com.anu.myapp.myFragment.XAxisValueFormatter)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.