[![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);
}
}