1
votes

[![enter image description here][1]][1]xaxisvalues in mpandroidchart are repeating if there is less values available in chart

I tried doing this but if chart has one value it is repeating

lineChart.getAxisRight().setEnabled(false);
                    XAxis xAxis = lineChart.getXAxis();
                    xAxis.setGranularity(1f);
                    xAxis.setGranularityEnabled(true);

lineChart.getXAxis().setLabelCount(yvalues.size()-1,true);
                    System.out.println("xaxis"+yvalues.size());
                    xAxis.setValueFormatter(new ValueFormatter() {
                        @Override
                        public String getFormattedValue(float value) {
                            System.out.println("xaxis values"+value);
                            return yvalues.get(Math.min(Math.max((int) 
value, 0), yvalues.size()-1)).xAxisValue;
                        }
                    });
                    setData(yvalues);

Setdata values function from where i am setting all entries which i am taking dynamically from server

private void setData(List<Data> dataList) {
    if(lineChart.getData() != null){
        System.out.println("linechart"+lineChart.getData());
        lineChart.clearValues();
    }
    lineChart.getDescription().setEnabled(false);
    ArrayList<Entry> values = new ArrayList<>();
    List<Integer> colors = new ArrayList<>();

    int green = Color.rgb(110, 190, 102);
    int red = Color.rgb(211, 74, 88);

    for (int i = 0; i < dataList.size(); i++) {

        Data d = dataList.get(i);
        Entry entry = new Entry(d.xValue, d.yValue);
        values.add(entry);

        // specific colors
        if (d.yValue >= 0)
            colors.add(red);
        else
            colors.add(green);
    }

    LineDataSet set;

    if (lineChart.getData() != null &&
            lineChart.getData().getDataSetCount() > 0) {
        set = (LineDataSet) lineChart.getData().getDataSetByIndex(0);
        set.setValues(values);
        lineChart.getData().notifyDataChanged();
        lineChart.notifyDataSetChanged();
    } else {
        set = new LineDataSet(values, "Values");
        set.setColors(colors);
        set.setValueTextColors(colors);
        lineChart.getLegend().setEnabled(false);
        LineData data = new LineData(set);
        data.setValueTextSize(13f);
        data.setValueTypeface(tfRegular);
        data.setValueFormatter(new Formatter());
        lineChart.setData(data);
        lineChart.notifyDataSetChanged();
        lineChart.invalidate();
    }
}

it is my formatter class to convert my string value and show it on axis as i want to show date

 private class Formatter extends ValueFormatter
{

    private final DecimalFormat mFormat;

    Formatter() {
        mFormat = new DecimalFormat("######.0");
    }

    @Override
    public String getFormattedValue(float value) {
        return mFormat.format(value);
    }
}
1
If there are xaxis values, then there should be yaxis values( if there are no values pass the value as 0) - sanjeev
sorry actually i am using mpandroidchart for the first time and as of now i have not passed any xaxisvalue - Anushree
can you please share any code if you can - Anushree
Sure I can help you.. but you need to tell me what you need to plot there and where you get values from.. - sanjeev
I am trying to put dynamic values which will be dates and i did it but the problem is now if there is only one value in chart yaxis value keep repeating itself - Anushree

1 Answers

0
votes

Please use

xAxis.setGranularity(1f);
xAxis.setGranularityEnabled(true);

Also refer this link...

https://github.com/PhilJay/MPAndroidChart/wiki/The-Axis