Reputation: 247
I use a barchart from MPAndroidChart
version 3.
I for set label used this code:
public class MainActivity extends AppCompatActivity {
BarChart chart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chart = (BarChart) findViewById(R.id.chart);
List<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(0, 2000));
entries.add(new BarEntry(1, 100));
entries.add(new BarEntry(2, 500));
entries.add(new BarEntry(3, 250));
entries.add(new BarEntry(4, 170));
entries.add(new BarEntry(5, 600));
BarDataSet set = new BarDataSet(entries, "Recovery");
BarData data = new BarData(set);
data.setBarWidth(1);
chart.setData(data);
chart.setFitBars(true);
chart.invalidate();
String[] day = new String[]{"day1","day2","day3","day4","day5","day6"};
XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(new LabelFormatter(day));
xAxis.setGranularity(1);
}
public class LabelFormatter implements IAxisValueFormatter {
private final String[] mLabels;
public LabelFormatter(String[] labels) {
mLabels = labels;
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
return mLabels[(int) value];
}
}
but unfortunately i get this error :
java.lang.ArrayIndexOutOfBoundsException: length=6; index=-1
And this section refers to this code:
return mLabels[(int) value];
I do not know what to do.
How to fix this problem?! Thanks.
Upvotes: 1
Views: 2314
Reputation: 414
It seems that it already gets the value before the mLabels
is set. You can set it directly to your xAxis
and you don't need to create a LabelFormatter
like:
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
if (value >= 0) {
if (day.length > (int) value) {
return day[(int) value];
} else {
return "";
}
} else {
return "";
}
}
});
Upvotes: 0
Reputation: 1361
Use this function--
public void create_graph(List<String> graph_label, List<Integer> userScore) {
try {
chart.setDrawBarShadow(false);
chart.setDrawValueAboveBar(true);
chart.getDescription().setEnabled(false);
chart.setPinchZoom(false);
chart.setDrawGridBackground(false);
YAxis yAxis = chart.getAxisLeft();
yAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return String.valueOf((int) value);
}
});
yAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
yAxis.setGranularity(1f);
yAxis.setGranularityEnabled(true);
chart.getAxisRight().setEnabled(false);
XAxis xAxis = chart.getXAxis();
xAxis.setGranularity(1f);
xAxis.setGranularityEnabled(true);
xAxis.setCenterAxisLabels(true);
xAxis.setDrawGridLines(true);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setValueFormatter(new IndexAxisValueFormatter(graph_label));
List<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < userScore.size(); i++) {
yVals1.add(new BarEntry(i, userScore.get(i)));
}
BarDataSet set1;
if (chart.getData() != null && chart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) chart.getData().getDataSetByIndex(0);
set1.setValues(yVals1);
chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
} else {
// create 2 datasets with different types
set1 = new BarDataSet(yVals1, "SCORE");
set1.setColor(Color.rgb(255, 204, 0));
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
chart.setData(data);
}
chart.setFitBars(true);
Legend l = chart.getLegend();
l.setFormSize(12f); // set the size of the legend forms/shapes
l.setForm(Legend.LegendForm.SQUARE); // set what type of form/shape should be used
l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART_INSIDE);
l.setTextSize(10f);
l.setTextColor(Color.BLACK);
l.setXEntrySpace(5f); // set the space between the legend entries on the x-axis
l.setYEntrySpace(5f); // set the space between the legend entries on the y-axis
chart.invalidate();
chart.animateY(2000);
} catch (Exception ignored) {
}
}
And call it on Activity
BarChart chart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chart = (BarChart) findViewById(R.id.barchart);
List<Integer> entries = new ArrayList<>();
entries.add(2000);
entries.add(100);
entries.add(500);
entries.add(250);
entries.add(170);
entries.add(600);
List<String> labels = new ArrayList<>();
labels.add("day1");
labels.add("day2");
labels.add("day3");
labels.add("day4");
labels.add("day5");
labels.add("day6");
create_graph(labels,entries);
}
Upvotes: 1