Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set String value of xAxis in MPAndroidChart?

I want to make Line Chart Graph, but i have problem to show value string in xAxis, im used Library Github from MPAndroidChart to LineChart. Please help me how to add String Value and actually to much question i want ask

private void drawLineChartLine(){

        private float[] yDataL = {40, 60, 70, 80};
        private String[] xDataL = {"Week 1", "Week 1" , "Week 3" , "Week 4"};


        ArrayList<Entry> yEntrys = new ArrayList<>();

        final ArrayList<String> xEntrys = new ArrayList<>();

        for(int i = 0; i < yDataL.length; i++){
            yEntrys.add(new Entry(yDataL[i] ,i));
        }

        for(int i = 1; i < xDataL.length; i++){
            xEntrys.add(xDataL[i]);
        }

        //create the data set
        LineDataSet lineDataset = new LineDataSet(yEntrys, "assa");

        XAxis xAxis = lineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);


        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return xEntrys.get((int) value);
            }
        });

        LineData lineData = new LineData(lineDataset);
        lineChart.setData(lineData);
        lineChart.invalidate();
}

i got error

Invalid index 40, size is 6

in Code

xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return xEntrys.get((int) value);
            }
        });
like image 654
Megi Fernanda Avatar asked Jul 26 '17 07:07

Megi Fernanda


3 Answers

According to the new update v3.1.0 IAxisValueFormatter method is deprecated.

So with the use of com.github.mikephil.charting.formatter.IndexAxisValueFormatter we can show month list in x-Axis

private List<Entry> getIncomeEntries() {
    ArrayList<Entry> incomeEntries = new ArrayList<>();

    incomeEntries.add(new Entry(1, 11300));
    incomeEntries.add(new Entry(2, 1390));
    incomeEntries.add(new Entry(3, 1190));
    incomeEntries.add(new Entry(4, 7200));
    incomeEntries.add(new Entry(5, 4790));
    incomeEntries.add(new Entry(6, 4500));
    incomeEntries.add(new Entry(7, 8000));
    incomeEntries.add(new Entry(8, 7034));
    incomeEntries.add(new Entry(9, 4307));
    incomeEntries.add(new Entry(10, 8762));
    incomeEntries.add(new Entry(11, 4355));
    incomeEntries.add(new Entry(12, 6000));

  return incomeEntries.subList(0, 12);
}

In Oncreate method

ArrayList<ILineDataSet> dataSets = new ArrayList<>();
List<String> xAxisValues = new ArrayList<>(Arrays.asList("Jan", "Feb", "March", "April", "May", "June","July", "August", "September", "October", "November", "Decemeber"));
List<Entry> incomeEntries = getIncomeEntries();
dataSets = new ArrayList<>();
LineDataSet set1;

set1 = new LineDataSet(incomeEntries, "Income");
set1.setColor(Color.rgb(65, 168, 121));
set1.setValueTextColor(Color.rgb(55, 70, 73));
set1.setValueTextSize(10f);
set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
dataSets.add(set1);

//customization
LineChart mLineGraph = findByViewId(R.id.line_chart);
mLineGraph.setTouchEnabled(true);
mLineGraph.setDragEnabled(true);
mLineGraph.setScaleEnabled(false);
mLineGraph.setPinchZoom(false);
mLineGraph.setDrawGridBackground(false);
mLineGraph.setExtraLeftOffset(15);
mLineGraph.setExtraRightOffset(15);
//to hide background lines
mLineGraph.getXAxis().setDrawGridLines(false);
mLineGraph.getAxisLeft().setDrawGridLines(false);
mLineGraph.getAxisRight().setDrawGridLines(false);

//to hide right Y and top X border
YAxis rightYAxis = mLineGraph.getAxisRight();
rightYAxis.setEnabled(false);
YAxis leftYAxis = mLineGraph.getAxisLeft();
leftYAxis.setEnabled(false);
XAxis topXAxis = mLineGraph.getXAxis();
topXAxis.setEnabled(false);


XAxis xAxis = mLineGraph.getXAxis();
xAxis.setGranularity(1f);
xAxis.setCenterAxisLabels(true);
xAxis.setEnabled(true);
xAxis.setDrawGridLines(false);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

set1.setLineWidth(4f);
set1.setCircleRadius(3f);
set1.setDrawValues(false);
set1.setCircleHoleColor(getResources().getColor(R.color.pie_color_4));
set1.setCircleColor(getResources().getColor(R.color.pie_color_4));

//String setter in x-Axis
mLineGraph.getXAxis().setValueFormatter(new com.github.mikephil.charting.formatter.IndexAxisValueFormatter(xAxisValues));

LineData data = new LineData(dataSets);
mLineGraph.setData(data);
mLineGraph.animateX(2000);
mLineGraph.invalidate();
mLineGraph.getLegend().setEnabled(false);
mLineGraph.getDescription().setEnabled(false);
like image 122
Anshul1507 Avatar answered Nov 11 '22 00:11

Anshul1507


Here the easy method to dynamic string label (XAxis),

XAxis xAxis = lineChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setValueFormatter(new IndexAxisValueFormatter(getAreaCount));

public ArrayList<String> getAreaCount() {

            ArrayList<String> label = new ArrayList<>();
            for (int i = 0; i < areaList.size(); i++)
                label.add(areaList.get(i).getTopicName());
            return label;
        }
like image 42
Gayathri Avatar answered Nov 10 '22 23:11

Gayathri


public class IndexAxisValueFormatter extends ValueFormatter
{
    private String[] mValues = new String[] {};
private int mValueCount = 0;

/**
 * An empty constructor.
 * Use `setValues` to set the axis labels.
 */
public IndexAxisValueFormatter() {
}

/**
 * Constructor that specifies axis labels.
 *
 * @param values The values string array
 */
public IndexAxisValueFormatter(String[] values) {
    if (values != null)
        setValues(values);
}

/**
 * Constructor that specifies axis labels.
 *
 * @param values The values string array
 */
public IndexAxisValueFormatter(Collection<String> values) {
    if (values != null)
        setValues(values.toArray(new String[values.size()]));
}

@Override
public String getFormattedValue(float value, AxisBase axisBase) {
    int index = Math.round(value);

    if (index < 0 || index >= mValueCount || index != (int)value)
        return "";

    return mValues[index];
}

public String[] getValues()
{
    return mValues;
}

public void setValues(String[] values)
{
    if (values == null)
        values = new String[] {};

    this.mValues = values;
    this.mValueCount = values.length;
}
}

Add the below code in the method where you are trying to display Bar Chart.

final String[] weekdays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; // Your List / array with String Values For X-axis Labels

// Set the value formatter 
XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(new IndexAxisValueFormatter(weekdays));

It Solved! Happy Coding.

like image 9
Arjun G Avatar answered Nov 11 '22 01:11

Arjun G